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

Adding version details to sidecar #385

Merged
merged 1 commit into from
Jun 27, 2023
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
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -68,6 +69,7 @@ func main() {
probeAddr string
enableLeaderElection bool
enableAdmissionWebhooks bool
showVersion bool
ctx = context.Background()
cfg = util.NewConfig()
)
Expand All @@ -80,13 +82,19 @@ 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,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

if showVersion {
version.PrintVersion()
return
}

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

kubeConfig := ctrl.GetConfigOrDie()
Expand Down
37 changes: 37 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -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
// Version tells the 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)
}
9 changes: 9 additions & 0 deletions sidecar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"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"
Expand All @@ -46,13 +47,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)
Expand Down