diff --git a/Makefile b/Makefile index 304bcaf5e..a3f8f6910 100644 --- a/Makefile +++ b/Makefile @@ -207,10 +207,13 @@ CGO_ENABLED := 0 endif export CGO_ENABLED +export GIT_REPO := $(shell go list -m) +export VERSION_PATH := ${GIT_REPO}/internal/version export GO_BUILD_ASMFLAGS := all=-trimpath=$(PWD) -export GO_BUILD_LDFLAGS := -s -w -X $(shell go list -m)/version.Version=$(VERSION) export GO_BUILD_GCFLAGS := all=-trimpath=$(PWD) export GO_BUILD_FLAGS := +export GO_BUILD_LDFLAGS := -s -w \ + -X '$(VERSION_PATH).version=$(VERSION)' \ BUILDCMD = go build $(GO_BUILD_FLAGS) -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 8bdb04e17..56b9e6bfe 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -18,6 +18,7 @@ package main import ( "flag" + "fmt" "net/http" "os" "time" @@ -33,6 +34,7 @@ import ( "github.com/operator-framework/operator-controller/internal/catalogmetadata/cache" catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client" "github.com/operator-framework/operator-controller/internal/controllers" + "github.com/operator-framework/operator-controller/internal/version" "github.com/operator-framework/operator-controller/pkg/features" "github.com/operator-framework/operator-controller/pkg/scheme" ) @@ -43,10 +45,11 @@ var ( func main() { var ( - metricsAddr string - enableLeaderElection bool - probeAddr string - cachePath string + metricsAddr string + enableLeaderElection bool + probeAddr string + cachePath string + operatorControllerVersion bool ) flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") @@ -54,6 +57,7 @@ func main() { "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") flag.StringVar(&cachePath, "cache-path", "/var/cache", "The local directory path used for filesystem based caching") + flag.BoolVar(&operatorControllerVersion, "version", false, "Prints operator-controller version information") opts := zap.Options{ Development: true, } @@ -63,7 +67,13 @@ func main() { features.OperatorControllerFeatureGate.AddFlag(pflag.CommandLine) pflag.Parse() + if operatorControllerVersion { + fmt.Println(version.String()) + os.Exit(0) + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts), zap.StacktraceLevel(zapcore.DPanicLevel))) + setupLog.Info("starting up the controller", "version info", version.String()) mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme.Scheme, diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 000000000..ef90dffa0 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,44 @@ +package version + +import ( + "fmt" + "runtime/debug" +) + +var ( + gitCommit = "unknown" + commitDate = "unknown" + repoState = "unknown" + version = "unknown" + + stateMap = map[string]string{ + "true": "dirty", + "false": "clean", + } +) + +func String() string { + return fmt.Sprintf("version: %q, commit: %q, date: %q, state: %q", version, gitCommit, commitDate, repoState) +} + +func init() { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + for _, setting := range info.Settings { + switch setting.Key { + case "vcs.revision": + gitCommit = setting.Value + case "vcs.time": + commitDate = setting.Value + case "vcs.modified": + if v, ok := stateMap[setting.Value]; ok { + repoState = v + } + } + } + if version == "unknown" { + version = info.Main.Version + } +}