From 4ffc3bcd40eaa76794abfdd80ffcac4a71a7bb36 Mon Sep 17 00:00:00 2001 From: pavel <36902936+pavel-github@users.noreply.github.com> Date: Sat, 15 Jun 2024 17:15:43 +0200 Subject: [PATCH] fix: inject version info via ldflags --- .dockerignore | 1 + Dockerfile | 13 +++++++- Makefile | 46 +++++++++++++++++++-------- internal/driver/cloud/xelon_client.go | 4 +-- internal/driver/controller.go | 2 +- internal/driver/driver.go | 2 +- internal/driver/identity.go | 2 +- internal/driver/version.go | 40 +++++++++++++++++++++++ 8 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 internal/driver/version.go diff --git a/.dockerignore b/.dockerignore index 0f2181b..12a41e3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,5 +3,6 @@ build/ deploy/ test/ tools/ +Makefile README.md .golangci.yaml diff --git a/Dockerfile b/Dockerfile index f869b70..f0562da 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,11 @@ # syntax=docker/dockerfile:1 FROM golang:1.22 AS builder +ARG GIT_COMMIT +ARG GIT_TREE_STATE +ARG SOURCE_DATE_EPOCH +ARG VERSION + ENV CGO_ENABLED=0 # copy manifest files only to cache layer with dependencies @@ -12,7 +17,13 @@ COPY cmd/ cmd/ COPY internal/ internal/ # build -RUN go build -o xelon-csi -ldflags="-s -w" -trimpath cmd/xelon-csi/main.go +RUN go build -trimpath \ + -ldflags="-s -w \ + -X github.com/Xelon-AG/xelon-csi/internal/driver.gitCommit=${GIT_COMMIT:-none} \ + -X github.com/Xelon-AG/xelon-csi/internal/driver.gitTreeState=${GIT_TREE_STATE:-none} \ + -X github.com/Xelon-AG/xelon-csi/internal/driver.sourceDateEpoch=${SOURCE_DATE_EPOCH:-0} \ + -X github.com/Xelon-AG/xelon-csi/internal/driver.version=${VERSION:-local}" \ + -o xelon-csi cmd/xelon-csi/main.go diff --git a/Makefile b/Makefile index 1c00849..74d4719 100644 --- a/Makefile +++ b/Makefile @@ -7,17 +7,17 @@ IMAGE_NAME ?= xelonag/xelon-csi BUILD_DIR := build VERSION ?= $(shell git describe --always) -COMMIT ?= $(shell git rev-parse HEAD) +GIT_COMMIT ?= $(shell git rev-parse HEAD) ifeq ($(strip $(shell git status --porcelain 2>/dev/null)),) GIT_TREE_STATE=clean else GIT_TREE_STATE=dirty endif -BUILD_DATE ?= $(shell date -Is) -LDFLAGS ?= -X github.com/Xelon-AG/xelon-csi/driver.driverVersion=${VERSION} -LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-csi/driver.gitCommit=${COMMIT} -LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-csi/driver.gitTreeState=${GIT_TREE_STATE} -LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-csi/driver.buildDate=${BUILD_DATE} +SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct) +LDFLAGS ?= -X github.com/Xelon-AG/xelon-csi/internal/driver.gitCommit=${GIT_COMMIT} +LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-csi/internal/driver.gitTreeState=${GIT_TREE_STATE} +LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-csi/internal/driver.sourceDateEpoch=${SOURCE_DATE_EPOCH} +LDFLAGS := $(LDFLAGS) -X github.com/Xelon-AG/xelon-csi/internal/driver.version=${VERSION} ## tools: Install required tooling. @@ -53,24 +53,44 @@ test: .PHONE: build build: @echo "==> Building binary..." - @echo " running go build for GOOS=linux GOARCH=amd64" - @GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(PROJECT_NAME) cmd/xelon-csi/main.go + @echo " running go build" + @CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o $(BUILD_DIR)/$(PROJECT_NAME) cmd/xelon-csi/main.go -## build-docker-dev: Build docker image with included binary. -.PHONE: build-docker-dev -build-docker-dev: build +## build-docker: Build docker image with included binary. +.PHONE: build-docker +build-docker: @echo "==> Building docker image $(IMAGE_NAME)..." - @docker build --build-arg VERSION=$(VERSION) --tag $(IMAGE_NAME):dev --file Dockerfile . + @docker build \ + --build-arg GIT_COMMIT=$(GIT_COMMIT) \ + --build-arg GIT_TREE_STATE=$(GIT_TREE_STATE) \ + --build-arg SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) \ + --build-arg VERSION=$(VERSION) \ + --tag $(IMAGE_NAME) --file Dockerfile . ## release-docker-dev: Release development docker image. .PHONE: release-docker-dev -release-docker-dev: build-docker-dev +release-docker-dev: build-docker + @echo "==> Tagging docker image $(IMAGE_NAME):dev..." + @docker tag $(IMAGE_NAME) $(IMAGE_NAME):dev @echo "==> Releasing development docker image $(IMAGE_NAME):dev..." @docker push $(IMAGE_NAME):dev +## release-docker: Release docker image. +.PHONE: release-docker +release-docker: build-docker + @echo "==> Tagging docker image $(IMAGE_NAME):latest..." + @docker tag $(IMAGE_NAME) $(IMAGE_NAME):latest + @echo "==> Releasing docker image $(IMAGE_NAME):latest..." + @docker push $(IMAGE_NAME):latest + @echo "==> Tagging docker image $(IMAGE_NAME):$(VERSION)..." + @docker tag $(IMAGE_NAME) $(IMAGE_NAME):$(VERSION) + @echo "==> Releasing docker image $(IMAGE_NAME):$(VERSION)..." + @docker push $(IMAGE_NAME):$(VERSION) + + help: Makefile @echo "Usage: make " @echo "" diff --git a/internal/driver/cloud/xelon_client.go b/internal/driver/cloud/xelon_client.go index 8896884..f165ea2 100644 --- a/internal/driver/cloud/xelon_client.go +++ b/internal/driver/cloud/xelon_client.go @@ -8,7 +8,7 @@ import ( type ClientOptions xelon.ClientOption -func NewXelonClient(token, clientID, baseURL string) (*xelon.Client, error) { +func NewXelonClient(token, clientID, baseURL, userAgent string) (*xelon.Client, error) { if token == "" { return nil, errors.New("token must not be empty") } @@ -22,7 +22,7 @@ func NewXelonClient(token, clientID, baseURL string) (*xelon.Client, error) { var opts []xelon.ClientOption opts = append(opts, xelon.WithBaseURL(baseURL)) opts = append(opts, xelon.WithClientID(clientID)) - opts = append(opts, xelon.WithUserAgent("xelon-csi")) + opts = append(opts, xelon.WithUserAgent(userAgent)) client := xelon.NewClient(token, opts...) return client, nil diff --git a/internal/driver/controller.go b/internal/driver/controller.go index e17b77c..e3ffc3b 100644 --- a/internal/driver/controller.go +++ b/internal/driver/controller.go @@ -61,7 +61,7 @@ type controllerService struct { func newControllerService(ctx context.Context, opts *Options) (*controllerService, error) { klog.V(2).InfoS("Initialize controller service") - xelonClient, err := cloud.NewXelonClient(opts.XelonToken, opts.XelonClientID, opts.XelonBaseURL) + xelonClient, err := cloud.NewXelonClient(opts.XelonToken, opts.XelonClientID, opts.XelonBaseURL, UserAgent()) if err != nil { return nil, err } diff --git a/internal/driver/driver.go b/internal/driver/driver.go index a788f97..633566f 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -49,7 +49,7 @@ type Driver struct { } func NewDriver(ctx context.Context, opts *Options) (*Driver, error) { - klog.InfoS("Driver information", "driver", DefaultDriverName, "version", "dev") + klog.InfoS("Driver information", "driver", DefaultDriverName, "version_info", GetVersionInfo()) d := &Driver{ endpoint: opts.Endpoint, diff --git a/internal/driver/identity.go b/internal/driver/identity.go index e073ef7..bea361b 100644 --- a/internal/driver/identity.go +++ b/internal/driver/identity.go @@ -12,7 +12,7 @@ func (d *Driver) GetPluginInfo(_ context.Context, req *csi.GetPluginInfoRequest) return &csi.GetPluginInfoResponse{ Name: DefaultDriverName, - VendorVersion: "wip", + VendorVersion: GetVersion(), }, nil } diff --git a/internal/driver/version.go b/internal/driver/version.go new file mode 100644 index 0000000..590adaa --- /dev/null +++ b/internal/driver/version.go @@ -0,0 +1,40 @@ +package driver + +import ( + "fmt" + "runtime" +) + +// These are set during build time via -ldflags +var ( + gitCommit = "none" + gitTreeState = "none" + sourceDateEpoch = "0" + version = "dev" +) + +type VersionInfo struct { + GitCommit string `json:"git_commit,omitempty"` + GitTreeState string `json:"git_tree_state,omitempty"` + GoVersion string `json:"go_version,omitempty"` + SourceDateEpoch string `json:"source_data_epoch,omitempty"` + Version string `json:"version,omitempty"` +} + +func GetVersion() string { + return version +} + +func GetVersionInfo() VersionInfo { + return VersionInfo{ + GitCommit: gitCommit, + GitTreeState: gitTreeState, + GoVersion: runtime.Version(), + SourceDateEpoch: sourceDateEpoch, + Version: version, + } +} + +func UserAgent() string { + return fmt.Sprintf("xelon-csi/%s", version) +}