Skip to content

Commit 6eef398

Browse files
committed
Add a --version tag to controller-gen
It's useful to know the version of a controller-gen binary lying around. With this, we can write checks in the kubebuilder Makefile to upgrade controller-gen when needed, or at least warn about it.
1 parent c99e03f commit 6eef398

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

cmd/controller-gen/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sigs.k8s.io/controller-tools/pkg/markers"
3333
"sigs.k8s.io/controller-tools/pkg/rbac"
3434
"sigs.k8s.io/controller-tools/pkg/schemapatcher"
35+
"sigs.k8s.io/controller-tools/pkg/version"
3536
"sigs.k8s.io/controller-tools/pkg/webhook"
3637
)
3738

@@ -124,6 +125,7 @@ type noUsageError struct{ error }
124125
func main() {
125126
helpLevel := 0
126127
whichLevel := 0
128+
showVersion := false
127129

128130
cmd := &cobra.Command{
129131
Use: "controller-gen",
@@ -146,6 +148,12 @@ func main() {
146148
controller-gen crd -ww
147149
`,
148150
RunE: func(c *cobra.Command, rawOpts []string) error {
151+
// print version if asked for it
152+
if showVersion {
153+
version.Print()
154+
return nil
155+
}
156+
149157
// print the help if we asked for it (since we've got a different help flag :-/), then bail
150158
if helpLevel > 0 {
151159
return c.Usage()
@@ -175,6 +183,7 @@ func main() {
175183
}
176184
cmd.Flags().CountVarP(&whichLevel, "which-markers", "w", "print out all markers available with the requested generators\n(up to -www for the most detailed output, or -wwww for json output)")
177185
cmd.Flags().CountVarP(&helpLevel, "detailed-help", "h", "print out more detailed help\n(up to -hhh for the most detailed output, or -hhhh for json output)")
186+
cmd.Flags().BoolVar(&showVersion, "version", false, "show version")
178187
cmd.Flags().Bool("help", false, "print out usage and a summary of options")
179188
oldUsage := cmd.UsageFunc()
180189
cmd.SetUsageFunc(func(c *cobra.Command) error {

pkg/version/version.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime/debug"
6+
)
7+
8+
// version returns the version of the main module
9+
func version() string {
10+
info, ok := debug.ReadBuildInfo()
11+
if !ok {
12+
// binary has not been built with module support
13+
return "(unknown)"
14+
}
15+
return info.Main.Version
16+
}
17+
18+
// Print prints the main module version on stdout.
19+
//
20+
// Print will display either:
21+
//
22+
// - "Version: v0.2.1" when the program has been compiled with:
23+
//
24+
// $ go get github.com/controller-tools/cmd/[email protected]
25+
//
26+
// Note: go modules requires the usage of semver compatible tags starting with
27+
// 'v' to have nice human-readable versions.
28+
//
29+
// - "Version: (devel)" when the program is compiled from a local git checkout.
30+
//
31+
// - "Version: (unknown)" when not using go modules.
32+
func Print() {
33+
fmt.Printf("Version: %s\n", version())
34+
}

0 commit comments

Comments
 (0)