Skip to content

Commit

Permalink
Merge pull request #34 from Xelon-AG/fix/version-info
Browse files Browse the repository at this point in the history
fix: inject version info via ldflags
  • Loading branch information
pavel-github authored Jun 15, 2024
2 parents 30afc70 + 4ffc3bc commit 9fd707e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 19 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ build/
deploy/
test/
tools/
Makefile
README.md
.golangci.yaml
13 changes: 12 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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



Expand Down
46 changes: 33 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <command>"
@echo ""
Expand Down
4 changes: 2 additions & 2 deletions internal/driver/cloud/xelon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (d *Driver) GetPluginInfo(_ context.Context, req *csi.GetPluginInfoRequest)

return &csi.GetPluginInfoResponse{
Name: DefaultDriverName,
VendorVersion: "wip",
VendorVersion: GetVersion(),
}, nil
}

Expand Down
40 changes: 40 additions & 0 deletions internal/driver/version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9fd707e

Please sign in to comment.