diff --git a/Makefile b/Makefile index 6e52a0e54..511254405 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,24 @@ 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 --exact-match --abbrev=0 2>/dev/null) + +# In the above command, if it fails to fetch the git tag, then this +# command will fetch the short commit hash +ifeq ($(GIT_TAG),) +GIT_TAG=$(shell git rev-parse --short HEAD) +endif + +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 +176,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..308515279 100644 --- a/sidecar/main.go +++ b/sidecar/main.go @@ -18,13 +18,13 @@ package main import ( "flag" - "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" "github.com/csi-addons/kubernetes-csi-addons/sidecar/internal/util" + "time" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -47,12 +47,21 @@ func main() { 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") ) + var showVersion bool klog.InitFlags(nil) + if err := flag.Set("logtostderr", "true"); err != nil { klog.Exitf("failed to set logtostderr flag: %v", err) } + + flag.BoolVar(&showVersion, "version", false, "Print Version details") 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)