Skip to content

[release-0.20] ✨Add RELEASE_TAG to tools/setup-envtest to show binary version with setup-envtest version #3175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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@11bd71901bbe5b1630ceea73d27597364c9af683 # tag=v4.2.2
- name: Calculate go version
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ release-binary: $(RELEASE_DIR)
-v "$$(pwd):/workspace$(DOCKER_VOL_OPTS)" \
-w /workspace/tools/setup-envtest \
golang:$(GO_VERSION) \
go build -a -trimpath -ldflags "-extldflags '-static'" \
go build -a -trimpath -ldflags "-X 'sigs.k8s.io/controller-runtime/tools/setup-envtest/version.version=$(RELEASE_TAG)' -extldflags '-static'" \
-o ./out/$(RELEASE_BINARY) ./

## --------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion tools/setup-envtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ Commands:
reads a .tar.gz file from stdin and expand it into the store.
must have a concrete version and platform.

version:
list the installed version of setup-envtest.

Versions:

Versions take the form of a small subset of semver selectors.
Expand Down Expand Up @@ -256,7 +259,6 @@ Environment Variables:
version = flag.Arg(1)
}
env := setupEnv(globalLog, version)

// perform our main set of actions
switch action := flag.Arg(0); action {
case "use":
Expand All @@ -274,6 +276,8 @@ Environment Variables:
Input: os.Stdin,
PrintFormat: printFormat,
}.Do(env)
case "version":
workflows.Version{}.Do(env)
default:
flag.Usage()
envp.Exit(2, "unknown action %q", action)
Expand Down
21 changes: 21 additions & 0 deletions tools/setup-envtest/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package version

import "runtime/debug"

// Version to be set using ldflags:
// -ldflags "-X sigs.k8s.io/controller-tools/pkg/version.version=v1.0.0"
// falls back to module information is unse
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.
return "(unknown)"
}
return info.Main.Version
}
27 changes: 27 additions & 0 deletions tools/setup-envtest/version/version_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
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/v2"
. "github.com/onsi/gomega"
)

func TestVersioning(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Test Version Suite")
}
54 changes: 54 additions & 0 deletions tools/setup-envtest/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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 (
"runtime/debug"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("TestVersion", func() {

info, ok := debug.ReadBuildInfo()
Expect(ok).To(BeTrue())
tests := map[string]struct {
version string
expected string
}{
"empty returns build info": {
version: "",
expected: info.Main.Version,
},
"set to a value returns it": {
version: "1.2.3",
expected: "1.2.3",
},
}
for name, tc := range tests {
It("Version set to "+name, func() {
versionBackup := version
defer func() {
version = versionBackup
}()
version = tc.version
result := Version()
Expect(result).To(Equal(tc.expected))
})
}
})
11 changes: 11 additions & 0 deletions tools/setup-envtest/workflows/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package workflows

import (
"context"
"fmt"
"io"

"github.com/go-logr/logr"

envp "sigs.k8s.io/controller-runtime/tools/setup-envtest/env"
"sigs.k8s.io/controller-runtime/tools/setup-envtest/version"
)

// Use is a workflow that prints out information about stored
Expand Down Expand Up @@ -85,3 +87,12 @@ func (f Sideload) Do(env *envp.Env) {
env.Sideload(ctx, f.Input)
env.PrintInfo(f.PrintFormat)
}

// Version is the workflow that shows the current binary version
// of setup-envtest.
type Version struct{}

// Do executes the workflow.
func (v Version) Do(env *envp.Env) {
fmt.Fprintf(env.Out, "setup-envtest version: %s\n", version.Version())
}
13 changes: 13 additions & 0 deletions tools/setup-envtest/workflows/workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io/fs"
"path/filepath"
"runtime/debug"
"sort"
"strings"

Expand Down Expand Up @@ -443,4 +444,16 @@ var _ = Describe("Workflows", func() {
Expect(string(outContents)).To(HavePrefix(expectedPrefix), "should have the debugging prefix")
})
})

Describe("version", func() {
It("should print out the version if the RELEASE_TAG is empty", func() {
v := wf.Version{}
v.Do(env)
info, ok := debug.ReadBuildInfo()
Expect(ok).To(BeTrue())
Expect(out.String()).ToNot(BeEmpty())
Expect(out.String()).To(Equal(fmt.Sprintf("setup-envtest version: %s\n", info.Main.Version)))
})
})

})