Skip to content

Commit 5b38dbb

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 5b38dbb

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
func Version() string {
11+
if version != "" {
12+
return version
13+
}
14+
info, ok := debug.ReadBuildInfo()
15+
if !ok || info == nil || info.Main.Version == "" {
16+
// binary has not been built with module support or doesn't contain a version.
17+
return "(unknown)"
18+
}
19+
return info.Main.Version
20+
}

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
}

0 commit comments

Comments
 (0)