Skip to content

Commit 715d27e

Browse files
josvazgsbueringer
andauthored
🐛 Allow CLI binaries to set a version (#1049)
* Allow CLI binaries to set a version Signed-off-by: jose.vazquez <[email protected]> * Release binaries with RELEASE_TAG as version * Make workflow pass the RELEASE_TAG env var * Update version help comment Co-authored-by: Stefan Büringer <[email protected]> --------- Signed-off-by: jose.vazquez <[email protected]> Co-authored-by: Stefan Büringer <[email protected]>
1 parent 6c6bad6 commit 715d27e

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

.github/workflows/release.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
name: Upload binaries to release
1515
runs-on: ubuntu-latest
1616
steps:
17+
- name: Set env
18+
run: echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
1719
- name: Check out code
1820
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # tag=v4.1.7
1921
- name: Calculate go version

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ release-binary: $(RELEASE_DIR)
178178
-v "$$(pwd):/workspace$(DOCKER_VOL_OPTS)" \
179179
-w /workspace \
180180
golang:$(GO_VERSION) \
181-
go build -a -trimpath -ldflags "-extldflags '-static'" \
181+
go build -a -trimpath \
182+
-ldflags "-extldflags '-static' -X sigs.k8s.io/controller-tools/pkg/version.version=$(RELEASE_TAG)" \
182183
-o ./out/$(RELEASE_BINARY) ./cmd/controller-gen
183184

184185
## --------------------------------------

pkg/version/version.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ import (
2222
"runtime/debug"
2323
)
2424

25+
// version to be set using ldflags:
26+
// -ldflags "-X sigs.k8s.io/controller-tools/pkg/version.version=v1.0.0"
27+
// falls back to module information is unset
28+
var version = ""
29+
2530
// Version returns the version of the main module
2631
func Version() string {
32+
if version != "" {
33+
return version
34+
}
2735
info, ok := debug.ReadBuildInfo()
2836
if !ok || info == nil || info.Main.Version == "" {
2937
// binary has not been built with module support or doesn't contain a version.

pkg/version/version_suite_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestVersioning(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Test Version Suite")
29+
}

pkg/version/version_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("TestVersion", func() {
25+
tests := []struct {
26+
name string
27+
version string
28+
expected string
29+
}{
30+
{
31+
name: "empty returns unknown",
32+
version: "",
33+
expected: "(unknown)",
34+
},
35+
{
36+
name: "set to a value returns it",
37+
version: "1.2.3",
38+
expected: "1.2.3",
39+
},
40+
}
41+
for _, tc := range tests {
42+
It("Version set to "+tc.name, func() {
43+
versionBackup := version
44+
defer func() {
45+
version = versionBackup
46+
}()
47+
version = tc.version
48+
result := Version()
49+
Expect(result).To(Equal(tc.expected))
50+
})
51+
}
52+
})
53+

0 commit comments

Comments
 (0)