Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on: # yamllint disable-line rule:truthy
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
env:
IMAGE_NAME: hetzner-cloud-controller-manager-staging
REGISTRY: ghcr.io/syself
Expand Down Expand Up @@ -47,13 +49,6 @@ jobs:
- name: Install Cosign
uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0

- name: Setup Env
run: |
DOCKER_BUILD_LDFLAGS="$(hack/version.sh)"
echo 'DOCKER_BUILD_LDFLAGS<<EOF' >> $GITHUB_ENV
echo $DOCKER_BUILD_LDFLAGS >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV

- name: Build and push manager image
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
id: docker_build_release
Expand All @@ -62,8 +57,6 @@ jobs:
context: .
file: ./images/hetzner-cloud-controller-manager/Dockerfile
push: true
build-args: |
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Go Test

on:
pull_request:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5

- name: Run tests
run: go test ./...
9 changes: 0 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ jobs:
sudo mv ./bom /usr/local/bin/bom
sudo chmod +x /usr/local/bin/bom

- name: Setup Env
run: |
DOCKER_BUILD_LDFLAGS="$(hack/version.sh)"
echo 'DOCKER_BUILD_LDFLAGS<<EOF' >> $GITHUB_ENV
echo $DOCKER_BUILD_LDFLAGS >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV

- name: Build and push manager image
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
id: docker_build_release
Expand All @@ -72,8 +65,6 @@ jobs:
context: .
file: ./images/hetzner-cloud-controller-manager/Dockerfile
push: true
build-args: |
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
Expand Down
90 changes: 0 additions & 90 deletions hack/version.sh

This file was deleted.

15 changes: 11 additions & 4 deletions hcloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ const (

var errMissingRobotCredentials = errors.New("missing robot credentials - cannot connect to robot API")

// providerVersion is set by the build process using -ldflags -X.
var providerVersion = "unknown"
// ProviderVersion shows the provider version via debug.ReadBuildInfo(). If this returns the empty
// string, read https://github.com/golang/go/issues/51279.
func ProviderVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "failed-to-get-version-info"
}
return info.Main.Version
}

type cloud struct {
hcloudClient *hcloud.Client
Expand Down Expand Up @@ -120,7 +127,7 @@ func newHcloudClient(rootDir string) (*hcloud.Client, error) {
}
opts := []hcloud.ClientOption{
hcloud.WithToken(token),
hcloud.WithApplication("hetzner-cloud-controller", providerVersion),
hcloud.WithApplication("hetzner-cloud-controller", ProviderVersion()),
}

// start metrics server if enabled (enabled by default)
Expand Down Expand Up @@ -214,7 +221,7 @@ func newCloud(_ io.Reader) (cloudprovider.Interface, error) {
return nil, fmt.Errorf("%s: %w", op, err)
}

klog.Infof("Hetzner Cloud k8s cloud controller %s started\n", providerVersion)
klog.Infof("Hetzner Cloud k8s cloud controller %s started\n", ProviderVersion())

lbOpsDefaults.DisableIPv6 = lbDisableIPv6

Expand Down
8 changes: 6 additions & 2 deletions hcloud/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/syself/hrobot-go/models"
corev1 "k8s.io/api/core/v1"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/klog/v2"
)

type addressFamily int
Expand Down Expand Up @@ -130,10 +131,13 @@ func (i *instances) InstanceShutdown(ctx context.Context, node *corev1.Node) (bo
return false, nil
}

func (i *instances) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
func (i *instances) InstanceMetadata(ctx context.Context, node *corev1.Node) (metadata *cloudprovider.InstanceMetadata, reterr error) {
const op = "hcloud/instancesv2.InstanceMetadata"
metrics.OperationCalled.WithLabelValues(op).Inc()

defer func() {
klog.InfoS("InstanceMetadata", "node", node,
"InstanceMetadata", metadata, "err", reterr)
}()
hcloudServer, bmServer, isHCloudServer, err := i.lookupServer(ctx, node)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions images/hetzner-cloud-controller-manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ COPY . /src/hetzner-cloud-controller-manager
WORKDIR /src/hetzner-cloud-controller-manager
RUN --mount=type=cache,target=/root/.cache --mount=type=cache,target=/go/pkg \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \
go build -mod=readonly -ldflags "${LDFLAGS} -extldflags '-static'" \
-o manager main.go
go build -mod=readonly -ldflags "-extldflags '-static'" \
-o manager .

FROM --platform=${BUILDPLATFORM} gcr.io/distroless/static:nonroot
WORKDIR /
Expand Down
27 changes: 18 additions & 9 deletions internal/credentials/hotreload.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ func handleEvent(credentialsDir, baseName string, hcloudClient *hcloud.Client, r

case "hcloud":
// This case is executed, when the process is running on a local machine.
return loadHcloudCredentials(credentialsDir, hcloudClient)
err := loadHcloudCredentials(credentialsDir, hcloudClient)
if err != nil {
return fmt.Errorf("handleEvent hcloud: %w", err)
}
return nil

case "..data":
// This case is executed, when the secrets are mounted in a Kubernetes pod.
Expand All @@ -102,14 +106,19 @@ func handleEvent(credentialsDir, baseName string, hcloudClient *hcloud.Client, r
// This means the files/symlinks don't change. When the secrets get changed, then
// a new ..data directory gets created. This is done by Kubernetes to make the
// update of all files atomic.
err = loadHcloudCredentials(credentialsDir, hcloudClient)
if err != nil {
return err
if hcloudClient != nil {
err = loadHcloudCredentials(credentialsDir, hcloudClient)
if err != nil {
return fmt.Errorf("handleEvent ..data loadHcloudCredentials: %w", err)
}
}
if robotClient == nil {
return nil
if robotClient != nil {
err := loadRobotCredentials(credentialsDir, robotClient)
if err != nil {
return fmt.Errorf("handleEvent ..data loadRobotCredentials: %w", err)
}
}
return loadRobotCredentials(credentialsDir, robotClient)
return nil

default:
klog.Infof("Ignoring fsnotify event for file %q: %s", baseName, event.String())
Expand Down Expand Up @@ -192,7 +201,7 @@ func loadHcloudCredentials(credentialsDir string, hcloudClient *hcloud.Client) e

token, err := readHcloudCredentials(credentialsDir)
if err != nil {
return err
return fmt.Errorf("loadHcloudCredentials: %w", err)
}

if len(token) != 64 {
Expand All @@ -218,7 +227,7 @@ func loadHcloudCredentials(credentialsDir string, hcloudClient *hcloud.Client) e
func GetInitialHcloudCredentialsFromDirectory(credentialsDir string) (string, error) {
token, err := readHcloudCredentials(credentialsDir)
if err != nil {
return "", fmt.Errorf("readHcloudCredentials: %w", err)
return "", fmt.Errorf("Getting initial credentials: readHcloudCredentials: %w", err)
}

// Update global variable
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ limitations under the License.
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/syself/hetzner-cloud-controller-manager/hcloud"
_ "github.com/syself/hetzner-cloud-controller-manager/hcloud"
"k8s.io/apimachinery/pkg/util/wait"
cloudprovider "k8s.io/cloud-provider"
Expand All @@ -45,8 +48,21 @@ func main() {
}

fss := cliflag.NamedFlagSets{}

// Add custom option, so that we can see which ccm version gets used.
var printVersion bool
fss.FlagSet("hcloud").BoolVar(&printVersion, "ccm-version", false, "Print CCM version and exit.")

command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors, names.CCMControllerAliases(), fss, wait.NeverStop)

command.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if printVersion {
fmt.Println(hcloud.ProviderVersion())
os.Exit(0)
}
return nil
}

pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
logs.InitLogs()
defer logs.FlushLogs()
Expand Down