Skip to content
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

✨ Add version flag for operator-controller manager binary #802

Merged
merged 1 commit into from
May 7, 2024
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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 14 additions & 4 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"net/http"
"os"
"time"
Expand All @@ -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"
)
Expand All @@ -43,17 +45,19 @@ 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.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"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,
}
Expand All @@ -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,
Expand Down
44 changes: 44 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package version

import (
"fmt"
"runtime/debug"
)

var (
gitCommit = "unknown"
commitDate = "unknown"
repoState = "unknown"
version = "unknown"
everettraven marked this conversation as resolved.
Show resolved Hide resolved

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
}
}
Loading