File tree Expand file tree Collapse file tree 5 files changed +94
-1
lines changed
Expand file tree Collapse file tree 5 files changed +94
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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# # --------------------------------------
Original file line number Diff line number Diff 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
2631func 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.
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments