diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 62e8936c5..1f61fc3e3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 @@ -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<> $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 @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a93c001c2..d817c4976 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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<> $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 @@ -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 diff --git a/hack/version.sh b/hack/version.sh deleted file mode 100755 index 7571ec596..000000000 --- a/hack/version.sh +++ /dev/null @@ -1,90 +0,0 @@ -#!/bin/bash -# Copyright 2020 The Kubernetes Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -o errexit -set -o nounset -set -o pipefail - -version::get_version_vars() { - GIT_COMMIT="$(git rev-parse HEAD^{commit})" - - if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then - GIT_TREE_STATE="clean" - else - GIT_TREE_STATE="dirty" - fi - - # borrowed from k8s.io/hack/lib/version.sh - # Use git describe to find the version based on tags. - if GIT_VERSION=$(git describe --tags --abbrev=14 2>/dev/null); then - # This translates the "git describe" to an actual semver.org - # compatible semantic version that looks something like this: - # v1.1.0-alpha.0.6+84c76d1142ea4d - DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g") - if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then - # We have distance to subversion (v1.1.0-subversion-1-gCommitHash) - GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\-\2/") - elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then - # We have distance to base tag (v1.1.0-1-gCommitHash) - GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/-\1/") - fi - if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then - # git describe --dirty only considers changes to existing files, but - # that is problematic since new untracked .go files affect the build, - # so use our idea of "dirty" from git status instead. - GIT_VERSION+="-dirty" - fi - - - # Try to match the "git describe" output to a regex to try to extract - # the "major" and "minor" versions and whether this is the exact tagged - # version or whether the tree is between two tagged versions. - if [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then - GIT_MAJOR=${BASH_REMATCH[1]} - GIT_MINOR=${BASH_REMATCH[2]} - fi - - # If GIT_VERSION is not a valid Semantic Version, then refuse to build. - if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then - echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}" - echo "Please see more details here: https://semver.org" - exit 1 - fi - fi - - GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags) -} - -# borrowed from k8s.io/hack/lib/version.sh and modified -# Prints the value that needs to be passed to the -ldflags parameter of go build -version::ldflags() { - version::get_version_vars - - local -a ldflags - function add_ldflag() { - local key=${1} - local val=${2} - ldflags+=( - "-X 'github.com/syself/hetzner-cloud-controller-manager/hcloud.${key}=${val}'" - ) - } - - add_ldflag "providerVersion" "${GIT_VERSION}" - - # The -ldflags parameter takes a single string, so join the output. - echo "${ldflags[*]-}" -} - -version::ldflags diff --git a/hcloud/cloud.go b/hcloud/cloud.go index 4b7b76bb9..809406597 100644 --- a/hcloud/cloud.go +++ b/hcloud/cloud.go @@ -72,8 +72,13 @@ 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" +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 @@ -120,7 +125,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) @@ -214,7 +219,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 diff --git a/hcloud/instances.go b/hcloud/instances.go index d4066ce06..fe67c11a1 100644 --- a/hcloud/instances.go +++ b/hcloud/instances.go @@ -26,6 +26,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 @@ -129,10 +130,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 diff --git a/images/hetzner-cloud-controller-manager/Dockerfile b/images/hetzner-cloud-controller-manager/Dockerfile index a617a07d4..89d084f2d 100644 --- a/images/hetzner-cloud-controller-manager/Dockerfile +++ b/images/hetzner-cloud-controller-manager/Dockerfile @@ -20,7 +20,7 @@ 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'" \ + go build -mod=readonly -ldflags "-extldflags '-static'" \ -o manager main.go FROM --platform=${BUILDPLATFORM} gcr.io/distroless/static:nonroot