Skip to content

Commit 6d73b73

Browse files
authored
add version flag for operator-controller manager binary (#802)
Signed-off-by: yashoza19 <[email protected]>
1 parent bd7f6ab commit 6d73b73

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,13 @@ CGO_ENABLED := 0
203203
endif
204204
export CGO_ENABLED
205205

206+
export GIT_REPO := $(shell go list -m)
207+
export VERSION_PATH := ${GIT_REPO}/internal/version
206208
export GO_BUILD_ASMFLAGS := all=-trimpath=$(PWD)
207-
export GO_BUILD_LDFLAGS := -s -w -X $(shell go list -m)/version.Version=$(VERSION)
208209
export GO_BUILD_GCFLAGS := all=-trimpath=$(PWD)
209210
export GO_BUILD_FLAGS :=
211+
export GO_BUILD_LDFLAGS := -s -w \
212+
-X '$(VERSION_PATH).version=$(VERSION)' \
210213

211214
BUILDCMD = go build $(GO_BUILD_FLAGS) -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager
212215

cmd/manager/main.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"flag"
21+
"fmt"
2122
"net/http"
2223
"os"
2324
"time"
@@ -33,6 +34,7 @@ import (
3334
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
3435
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
3536
"github.com/operator-framework/operator-controller/internal/controllers"
37+
"github.com/operator-framework/operator-controller/internal/version"
3638
"github.com/operator-framework/operator-controller/pkg/features"
3739
"github.com/operator-framework/operator-controller/pkg/scheme"
3840
)
@@ -43,17 +45,19 @@ var (
4345

4446
func main() {
4547
var (
46-
metricsAddr string
47-
enableLeaderElection bool
48-
probeAddr string
49-
cachePath string
48+
metricsAddr string
49+
enableLeaderElection bool
50+
probeAddr string
51+
cachePath string
52+
operatorControllerVersion bool
5053
)
5154
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
5255
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
5356
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
5457
"Enable leader election for controller manager. "+
5558
"Enabling this will ensure there is only one active controller manager.")
5659
flag.StringVar(&cachePath, "cache-path", "/var/cache", "The local directory path used for filesystem based caching")
60+
flag.BoolVar(&operatorControllerVersion, "version", false, "Prints operator-controller version information")
5761
opts := zap.Options{
5862
Development: true,
5963
}
@@ -63,7 +67,13 @@ func main() {
6367
features.OperatorControllerFeatureGate.AddFlag(pflag.CommandLine)
6468
pflag.Parse()
6569

70+
if operatorControllerVersion {
71+
fmt.Println(version.String())
72+
os.Exit(0)
73+
}
74+
6675
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts), zap.StacktraceLevel(zapcore.DPanicLevel)))
76+
setupLog.Info("starting up the controller", "version info", version.String())
6777

6878
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
6979
Scheme: scheme.Scheme,

internal/version/version.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime/debug"
6+
)
7+
8+
var (
9+
gitCommit = "unknown"
10+
commitDate = "unknown"
11+
repoState = "unknown"
12+
version = "unknown"
13+
14+
stateMap = map[string]string{
15+
"true": "dirty",
16+
"false": "clean",
17+
}
18+
)
19+
20+
func String() string {
21+
return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", version, gitCommit, commitDate, repoState)
22+
}
23+
24+
func init() {
25+
info, ok := debug.ReadBuildInfo()
26+
if !ok {
27+
return
28+
}
29+
for _, setting := range info.Settings {
30+
switch setting.Key {
31+
case "vcs.revision":
32+
gitCommit = setting.Value
33+
case "vcs.time":
34+
commitDate = setting.Value
35+
case "vcs.modified":
36+
if v, ok := stateMap[setting.Value]; ok {
37+
repoState = v
38+
}
39+
}
40+
}
41+
if version == "unknown" {
42+
version = info.Main.Version
43+
}
44+
}

0 commit comments

Comments
 (0)