Skip to content

Commit ad90ec9

Browse files
committed
add version flag for operator-controller manager binary
Signed-off-by: yashoza19 <[email protected]>
1 parent 32ed986 commit ad90ec9

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,18 @@ kind-load-test-artifacts: $(KIND) #EXHELP Load the e2e testdata container images
196196

197197
#SECTION Build
198198

199-
export VERSION ?= $(shell git describe --tags --always --dirty)
199+
export GIT_REPO ?= github.com/operator-framework/operator-controller
200+
export VERSION ?= $(shell git describe --tags --always --abbrev=0)
201+
export VERSION_PATH ?= ${GIT_REPO}/internal/version
202+
export GIT_COMMIT ?= $(shell git rev-parse HEAD)
200203
export CGO_ENABLED ?= 0
201204
export GO_BUILD_ASMFLAGS ?= all=-trimpath=${PWD}
202-
export GO_BUILD_LDFLAGS ?= -s -w -X $(shell go list -m)/version.Version=$(VERSION)
205+
export GO_BUILD_LDFLAGS ?= -s -w -X '$(VERSION_PATH).version=$(VERSION)' -X '$(VERSION_PATH).gitCommit=$(GIT_COMMIT)'
203206
export GO_BUILD_GCFLAGS ?= all=-trimpath=${PWD}
204207
export GO_BUILD_TAGS ?= upstream
205208
export GO_BUILD_FLAGS ?=
206209

207-
BUILDCMD = go build $(GO_BUILD_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager
210+
BUILDCMD = go build $(GO_BUILD_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags "$(GO_BUILD_LDFLAGS)" -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager
208211

209212
.PHONY: build-deps
210213
build-deps: manifests generate fmt vet

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"
@@ -35,6 +36,7 @@ import (
3536
"github.com/operator-framework/operator-controller/internal/catalogmetadata/cache"
3637
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
3738
"github.com/operator-framework/operator-controller/internal/controllers"
39+
"github.com/operator-framework/operator-controller/internal/version"
3840
"github.com/operator-framework/operator-controller/pkg/features"
3941
"github.com/operator-framework/operator-controller/pkg/scheme"
4042
)
@@ -45,17 +47,19 @@ var (
4547

4648
func main() {
4749
var (
48-
metricsAddr string
49-
enableLeaderElection bool
50-
probeAddr string
51-
cachePath string
50+
metricsAddr string
51+
enableLeaderElection bool
52+
probeAddr string
53+
cachePath string
54+
operatorControllerVersion bool
5255
)
5356
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
5457
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
5558
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
5659
"Enable leader election for controller manager. "+
5760
"Enabling this will ensure there is only one active controller manager.")
5861
flag.StringVar(&cachePath, "cache-path", "/var/cache", "The local directory path used for filesystem based caching")
62+
flag.BoolVar(&operatorControllerVersion, "version", false, "Displays operator-controller version information")
5963
opts := zap.Options{
6064
Development: true,
6165
}
@@ -65,7 +69,13 @@ func main() {
6569
features.OperatorControllerFeatureGate.AddFlag(pflag.CommandLine)
6670
pflag.Parse()
6771

72+
if operatorControllerVersion {
73+
fmt.Println(version.String())
74+
os.Exit(0)
75+
}
76+
6877
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts), zap.StacktraceLevel(zapcore.DPanicLevel)))
78+
setupLog.Info("starting up the provisioner", "Git commit", version.String())
6979

7080
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7181
Scheme: scheme.Scheme,

internal/version/version.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.time":
32+
commitDate = setting.Value
33+
case "vcs.modified":
34+
if v, ok := stateMap[setting.Value]; ok {
35+
repoState = v
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)