Skip to content

Commit b81bc88

Browse files
committed
add version directory with logic being moved to mimic controller-tools
Signed-off-by: Troy Connor <[email protected]>
1 parent 51c6d75 commit b81bc88

File tree

7 files changed

+107
-30
lines changed

7 files changed

+107
-30
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ release-binary: $(RELEASE_DIR)
174174
-v "$$(pwd):/workspace$(DOCKER_VOL_OPTS)" \
175175
-w /workspace/tools/setup-envtest \
176176
golang:$(GO_VERSION) \
177-
go build -a -trimpath -ldflags "-X 'main.BranchVersion=$(RELEASE_TAG)' -extldflags '-static'" \
177+
go build -a -trimpath -ldflags "-X 'sigs.k8s.io/controller-runtime/tools/setup-envtest/version.version=$(RELEASE_TAG)' -extldflags '-static'" \
178178
-o ./out/$(RELEASE_BINARY) ./
179179

180180
## --------------------------------------

tools/setup-envtest/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ var (
5151
"directory to store binary assets (default: $OS_SPECIFIC_DATA_DIR/envtest-binaries)")
5252

5353
index = flag.String("index", remote.DefaultIndexURL, "index to discover envtest binaries")
54-
55-
// BranchVersion is the current setup-envtest branch that is used for the binary version of setup-envtest.
56-
BranchVersion = ""
5754
)
5855

5956
// TODO(directxman12): handle interrupts?
@@ -277,9 +274,7 @@ Environment Variables:
277274
PrintFormat: printFormat,
278275
}.Do(env)
279276
case "version":
280-
workflows.Version{
281-
BinaryVersion: BranchVersion,
282-
}.Do(env)
277+
workflows.Version{}.Do(env)
283278
default:
284279
flag.Usage()
285280
envp.Exit(2, "unknown action %q", action)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package version
2+
3+
import "runtime/debug"
4+
5+
// Version to be set using ldflags:
6+
// -ldflags "-X sigs.k8s.io/controller-tools/pkg/version.version=v1.0.0"
7+
// falls back to module information is unse
8+
var version = ""
9+
10+
// Version returns the version of the main module
11+
func Version() string {
12+
if version != "" {
13+
return version
14+
}
15+
info, ok := debug.ReadBuildInfo()
16+
if !ok || info == nil || info.Main.Version == "" {
17+
// binary has not been built with module support or doesn't contain a version.
18+
return "(unknown)"
19+
}
20+
return info.Main.Version
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
package version
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestVersioning(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Test Version Suite")
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
12+
See the License for the specific language governing permissions and
13+
14+
limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import (
20+
"runtime/debug"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
var _ = Describe("TestVersion", func() {
27+
28+
info, ok := debug.ReadBuildInfo()
29+
Expect(ok).To(BeTrue())
30+
tests := map[string]struct {
31+
version string
32+
expected string
33+
}{
34+
"empty returns build info": {
35+
version: "",
36+
expected: info.Main.Version,
37+
},
38+
"set to a value returns it": {
39+
version: "1.2.3",
40+
expected: "1.2.3",
41+
},
42+
}
43+
for name, tc := range tests {
44+
It("Version set to "+name, func() {
45+
versionBackup := version
46+
defer func() {
47+
version = versionBackup
48+
}()
49+
version = tc.version
50+
result := Version()
51+
Expect(result).To(Equal(tc.expected))
52+
})
53+
}
54+
})

tools/setup-envtest/workflows/workflows.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"context"
88
"fmt"
99
"io"
10-
"runtime/debug"
1110

1211
"github.com/go-logr/logr"
1312

1413
envp "sigs.k8s.io/controller-runtime/tools/setup-envtest/env"
14+
"sigs.k8s.io/controller-runtime/tools/setup-envtest/version"
1515
)
1616

1717
// Use is a workflow that prints out information about stored
@@ -90,20 +90,9 @@ func (f Sideload) Do(env *envp.Env) {
9090

9191
// Version is the workflow that shows the current binary version
9292
// of setup-envtest.
93-
type Version struct {
94-
BinaryVersion string
95-
}
93+
type Version struct{}
9694

9795
// Do executes the workflow.
9896
func (v Version) Do(env *envp.Env) {
99-
version := "(unknown)"
100-
if v.BinaryVersion == "" {
101-
info, ok := debug.ReadBuildInfo()
102-
if ok && info != nil && info.Main.Version != "" {
103-
// binary has been built with module support.
104-
version = info.Main.Version
105-
}
106-
v.BinaryVersion = version
107-
}
108-
fmt.Fprintf(env.Out, "setup-envtest version: %s\n", v.BinaryVersion)
97+
fmt.Fprintf(env.Out, "setup-envtest version: %s\n", version.Version())
10998
}

tools/setup-envtest/workflows/workflows_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,6 @@ var _ = Describe("Workflows", func() {
446446
})
447447

448448
Describe("version", func() {
449-
It("should print out the version based on the Binary Version", func() {
450-
v := wf.Version{
451-
BinaryVersion: "v0.18.2",
452-
}
453-
v.Do(env)
454-
Expect(out.String()).ToNot(BeEmpty())
455-
Expect(out.String()).To(Equal("setup-envtest version: v0.18.2\n"))
456-
})
457-
458449
It("should print out the version if the RELEASE_TAG is empty", func() {
459450
v := wf.Version{}
460451
v.Do(env)

0 commit comments

Comments
 (0)