Skip to content

Commit

Permalink
add version flag for operator-controller manager binary
Browse files Browse the repository at this point in the history
Signed-off-by: yashoza19 <yoza@redhat.com>
  • Loading branch information
yashoza19 committed Apr 29, 2024
1 parent 32ed986 commit 76b03b5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export CATALOGD_VERSION ?= $(shell go list -mod=mod -m -f "{{.Version}}" github.
export KAPP_VERSION ?= $(shell go list -mod=mod -m -f "{{.Version}}" github.com/vmware-tanzu/carvel-kapp-controller)
export RUKPAK_VERSION=$(shell go list -mod=mod -m -f "{{.Version}}" github.com/operator-framework/rukpak)
export WAIT_TIMEOUT ?= 60s
VERSION_PATH := $(PKG)/internal/version
GIT_COMMIT ?= $(shell git rev-parse HEAD)
IMG?=$(IMAGE_REPO):$(IMAGE_TAG)
TESTDATA_DIR := testdata

Expand Down Expand Up @@ -200,11 +202,12 @@ export VERSION ?= $(shell git describe --tags --always --dirty)
export CGO_ENABLED ?= 0
export GO_BUILD_ASMFLAGS ?= all=-trimpath=${PWD}
export GO_BUILD_LDFLAGS ?= -s -w -X $(shell go list -m)/version.Version=$(VERSION)
export VERSION_FLAGS ?= -ldflags "-X $(VERSION_PATH).GitCommit=$(GIT_COMMIT)"
export GO_BUILD_GCFLAGS ?= all=-trimpath=${PWD}
export GO_BUILD_TAGS ?= upstream
export GO_BUILD_FLAGS ?=

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
BUILDCMD = go build $(GO_BUILD_FLAGS) $(VERSION_FLAGS) -tags '$(GO_BUILD_TAGS)' -ldflags '$(GO_BUILD_LDFLAGS)' -gcflags '$(GO_BUILD_GCFLAGS)' -asmflags '$(GO_BUILD_ASMFLAGS)' -o $(BUILDBIN)/manager ./cmd/manager

.PHONY: build-deps
build-deps: manifests generate fmt vet
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,8 @@ package main

import (
"flag"
"fmt"
"github.com/operator-framework/operator-controller/internal/version"

Check failure on line 22 in cmd/manager/main.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s dot -s default -s prefix(github.com/operator-framework) -s prefix(github.com/operator-framework/operator-controller) --custom-order (gci)
"net/http"
"os"
"time"
Expand Down Expand Up @@ -45,17 +47,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, "Displays operator-controller version information")
opts := zap.Options{
Development: true,
}
Expand All @@ -65,7 +69,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 provisioner", "Git commit", version.String())

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme.Scheme,
Expand Down
40 changes: 40 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package version

import (
"fmt"
"runtime/debug"
)

var (
gitCommit = "unknown"
commitDate = "unknown"
repoState = "unknown"

stateMap = map[string]string{
"true": "dirty",
"false": "clean",
}
)

func String() string {
return fmt.Sprintf("revision: %q, date: %q, state: %q", 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
}
}
}
}

0 comments on commit 76b03b5

Please sign in to comment.