From 85c93a7b209e5519d901f83a2610ce98c5604af7 Mon Sep 17 00:00:00 2001 From: saranyareddy24 Date: Sun, 25 Jun 2023 21:55:51 +0530 Subject: [PATCH] Adding version details to sidecar Signed-off-by: saranyareddy24 --- Makefile | 16 ++++++++++++++-- cmd/manager/main.go | 8 ++++++++ internal/version/version.go | 37 +++++++++++++++++++++++++++++++++++++ sidecar/main.go | 12 ++++++++++-- 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 internal/version/version.go diff --git a/Makefile b/Makefile index 6e52a0e54..d2776e11a 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,18 @@ ifneq ($(TAG),latest) BUNDLE_VERSION ?= --version=$(shell sed s/^v// <<< $(TAG)) endif +# To get the GIT commit id +GIT_COMMIT ?= $(shell git rev-list -1 HEAD) + +# Fetch the git tag +GIT_TAG ?= $(shell git describe --tags HEAD 2>/dev/null || echo $(GIT_COMMIT)) + +GO_PROJECT=github.com/csi-addons/kubernetes-csi-addons + +LDFLAGS ?= +LDFLAGS += -X $(GO_PROJECT)/internal/version.GitCommit=$(GIT_COMMIT) +LDFLAGS += -X $(GO_PROJECT)/internal/version.Version=$(GIT_TAG) + # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. ENVTEST_K8S_VERSION = 1.23 @@ -158,8 +170,8 @@ bundle-validate: container-cmd operator-sdk .PHONY: build build: generate fmt vet ## Build manager binary. - go build -o bin/manager cmd/manager/main.go - go build -o bin/sidecar sidecar/main.go + go build -ldflags '$(LDFLAGS)' -o bin/manager cmd/manager/main.go + go build -ldflags '$(LDFLAGS)' -o bin/sidecar sidecar/main.go go build -o bin/csi-addons ./cmd/csi-addons .PHONY: run diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 74bed2498..44050924f 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -28,6 +28,7 @@ import ( replicationController "github.com/csi-addons/kubernetes-csi-addons/controllers/replication.storage" "github.com/csi-addons/kubernetes-csi-addons/internal/connection" "github.com/csi-addons/kubernetes-csi-addons/internal/util" + "github.com/csi-addons/kubernetes-csi-addons/internal/version" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -68,6 +69,7 @@ func main() { probeAddr string enableLeaderElection bool enableAdmissionWebhooks bool + showVersion bool ctx = context.Background() cfg = util.NewConfig() ) @@ -80,6 +82,7 @@ func main() { flag.IntVar(&cfg.MaxConcurrentReconciles, "max-concurrent-reconciles", cfg.MaxConcurrentReconciles, "Maximum number of concurrent reconciles") flag.StringVar(&cfg.Namespace, "namespace", cfg.Namespace, "Namespace where the CSIAddons pod is deployed") flag.BoolVar(&enableAdmissionWebhooks, "enable-admission-webhooks", true, "Enable the admission webhooks") + flag.BoolVar(&showVersion, "version", false, "Print Version details") opts := zap.Options{ Development: true, TimeEncoder: zapcore.ISO8601TimeEncoder, @@ -87,6 +90,11 @@ func main() { opts.BindFlags(flag.CommandLine) flag.Parse() + if showVersion { + version.PrintVersion() + return + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) kubeConfig := ctrl.GetConfigOrDie() diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 000000000..2bb028232 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,37 @@ +/* +Copyright 2023 The Kubernetes-CSI-Addons Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package version + +import ( + "fmt" + "runtime" +) + +var ( + // GitCommit tell the latest git commit image is built from. + GitCommit string + // SidecarVersion which will be sidecar release version. + Version string +) + +func PrintVersion() { + fmt.Println("Version:", Version) + fmt.Println("Git Commit:", GitCommit) + fmt.Println("Go Version:", runtime.Version()) + fmt.Println("Compiler:", runtime.Compiler) + fmt.Printf("Platform: %s/%s\n", runtime.GOOS, runtime.GOARCH) +} diff --git a/sidecar/main.go b/sidecar/main.go index f1bd48341..d4a516929 100644 --- a/sidecar/main.go +++ b/sidecar/main.go @@ -18,9 +18,9 @@ package main import ( "flag" - "time" - + "time" "github.com/csi-addons/kubernetes-csi-addons/internal/sidecar/service" + "github.com/csi-addons/kubernetes-csi-addons/internal/version" "github.com/csi-addons/kubernetes-csi-addons/sidecar/internal/client" "github.com/csi-addons/kubernetes-csi-addons/sidecar/internal/csiaddonsnode" "github.com/csi-addons/kubernetes-csi-addons/sidecar/internal/server" @@ -46,13 +46,21 @@ func main() { podName = flag.String("pod", "", "name of the Pod that contains this sidecar") podNamespace = flag.String("namespace", "", "namespace of the Pod that contains this sidecar") podUID = flag.String("pod-uid", "", "UID of the Pod that contains this sidecar") + showVersion = flag.Bool("version", false, "Print Version details") ) klog.InitFlags(nil) + if err := flag.Set("logtostderr", "true"); err != nil { klog.Exitf("failed to set logtostderr flag: %v", err) } + flag.Parse() + if *showVersion { + version.PrintVersion() + return + } + controllerEndpoint, err := util.BuildEndpointURL(*controllerIP, *controllerPort, *podName, *podNamespace) if err != nil { klog.Fatalf("Failed to validate controller endpoint: %w", err)