From 858b873255c46aaa5edb49cef8be567e8c039b1d Mon Sep 17 00:00:00 2001 From: "jose.vazquez" Date: Wed, 4 Sep 2024 09:41:06 +0200 Subject: [PATCH 1/4] Allow CLI binaries to set a version Signed-off-by: jose.vazquez --- pkg/version/version.go | 8 +++++ pkg/version/version_suite_test.go | 29 +++++++++++++++++ pkg/version/version_test.go | 53 +++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 pkg/version/version_suite_test.go create mode 100644 pkg/version/version_test.go diff --git a/pkg/version/version.go b/pkg/version/version.go index f2709df4a..35cb3ed97 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -22,8 +22,16 @@ import ( "runtime/debug" ) +// version to be set using ldflags: +// -ldflags "-X sigs.k8s.io/controller-tools/pkg/version.version=1.0.0" +// falls back to module information is unset +var version = "" + // Version returns the version of the main module func Version() string { + if version != "" { + return version + } info, ok := debug.ReadBuildInfo() if !ok || info == nil || info.Main.Version == "" { // binary has not been built with module support or doesn't contain a version. diff --git a/pkg/version/version_suite_test.go b/pkg/version/version_suite_test.go new file mode 100644 index 000000000..dea3230b4 --- /dev/null +++ b/pkg/version/version_suite_test.go @@ -0,0 +1,29 @@ +/* +Copyright 2024 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. +*/ + +package version + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestVersioning(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Test Version Suite") +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go new file mode 100644 index 000000000..3ec9b414d --- /dev/null +++ b/pkg/version/version_test.go @@ -0,0 +1,53 @@ +/* +Copyright 2024 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. +*/ + +package version + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("TestVersion", func() { + tests := []struct { + name string + version string + expected string + }{ + { + name: "empty returns unknown", + version: "", + expected: "(unknown)", + }, + { + name: "set to a value returns it", + version: "1.2.3", + expected: "1.2.3", + }, + } + for _, tc := range tests { + It("Version set to "+tc.name, func() { + versionBackup := version + defer func() { + version = versionBackup + }() + version = tc.version + result := Version() + Expect(result).To(Equal(tc.expected)) + }) + } +}) + From 141df7f73040211a326873c1ba1aaa20c3c14697 Mon Sep 17 00:00:00 2001 From: "jose.vazquez" Date: Tue, 10 Sep 2024 09:58:50 +0200 Subject: [PATCH 2/4] Release binaries with RELEASE_TAG as version --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 961ad9c41..c00d60909 100644 --- a/Makefile +++ b/Makefile @@ -178,7 +178,8 @@ release-binary: $(RELEASE_DIR) -v "$$(pwd):/workspace$(DOCKER_VOL_OPTS)" \ -w /workspace \ golang:$(GO_VERSION) \ - go build -a -trimpath -ldflags "-extldflags '-static'" \ + go build -a -trimpath \ + -ldflags "-extldflags '-static' -X sigs.k8s.io/controller-tools/pkg/version.version=$(RELEASE_TAG)" \ -o ./out/$(RELEASE_BINARY) ./cmd/controller-gen ## -------------------------------------- From 53b8fdb4ea8093efacc00609b754e1d84fed4f3d Mon Sep 17 00:00:00 2001 From: "jose.vazquez" Date: Tue, 10 Sep 2024 14:51:20 +0200 Subject: [PATCH 3/4] Make workflow pass the RELEASE_TAG env var --- .github/workflows/release.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 9adc4ebf6..607dcebce 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -14,6 +14,8 @@ jobs: name: Upload binaries to release runs-on: ubuntu-latest steps: + - name: Set env + run: echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV - name: Check out code uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # tag=v4.1.7 - name: Calculate go version From 90baf8831f689add05a55a5181d18d525d544f65 Mon Sep 17 00:00:00 2001 From: josvaz Date: Tue, 10 Sep 2024 19:11:47 +0200 Subject: [PATCH 4/4] Update version help comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stefan Büringer <4662360+sbueringer@users.noreply.github.com> --- pkg/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/version/version.go b/pkg/version/version.go index 35cb3ed97..8d55cb597 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -23,7 +23,7 @@ import ( ) // version to be set using ldflags: -// -ldflags "-X sigs.k8s.io/controller-tools/pkg/version.version=1.0.0" +// -ldflags "-X sigs.k8s.io/controller-tools/pkg/version.version=v1.0.0" // falls back to module information is unset var version = ""