Skip to content

Commit 9b038f7

Browse files
committed
Embed operator version into binary
Add version printing into cli and log. Signed-off-by: Konstantin Khlebnikov <[email protected]>
1 parent 81bd394 commit 9b038f7

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

Dockerfile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
ARG VERSION
2+
ARG REVISION
3+
ARG BUILD_DATE
4+
15
# Build the manager binary
26
FROM golang:1.24 AS builder
37

8+
ARG VERSION
9+
410
WORKDIR /workspace
511

612
# Copy the Go Modules manifests
@@ -18,8 +24,10 @@ COPY api/ api/
1824
COPY pkg/ pkg/
1925
COPY controllers/ controllers/
2026

27+
ARG GO_LDFLAGS="-X github.com/ytsaurus/ytsaurus-k8s-operator/pkg/version.version=${VERSION}"
28+
2129
# Build
22-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
30+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "${GO_LDFLAGS}" -a -o manager main.go
2331

2432
# Use distroless as minimal base image to package the manager binary
2533
# Refer to https://github.com/GoogleContainerTools/distroless for more details
@@ -28,9 +36,9 @@ WORKDIR /
2836
COPY --from=builder /workspace/manager .
2937
USER 65532:65532
3038

31-
ARG VERSION=UNSET
32-
ARG REVISION=UNSET
33-
ARG BUILD_DATE=UNSET
39+
ARG VERSION
40+
ARG REVISION
41+
ARG BUILD_DATE
3442

3543
LABEL org.opencontainers.image.title="YTsaurus Operator for Kubernetes"
3644
LABEL org.opencontainers.image.url="https://ytsaurus.tech"

Makefile

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,24 @@ OPERATOR_CHART_NAME = ytop-chart
3535
OPERATOR_CHART_CRDS = $(OPERATOR_CHART)/templates/crds
3636
OPERATOR_INSTANCE = ytsaurus-dev
3737

38+
ifdef RELEASE_VERSION
39+
OPERATOR_VERSION = $(RELEASE_VERSION)
40+
else
41+
OPERATOR_VERSION = $(OPERATOR_TAG)
42+
endif
43+
3844
ifdef RELEASE_SUFFIX
3945
OPERATOR_IMAGE_RELEASE=$(OPERATOR_IMAGE)$(RELEASE_SUFFIX)
4046
OPERATOR_CHART_NAME_RELEASE=$(OPERATOR_CHART_NAME)$(RELEASE_SUFFIX)
4147
else
4248
OPERATOR_IMAGE_RELEASE=$(OPERATOR_IMAGE)
4349
OPERATOR_CHART_NAME_RELEASE=$(OPERATOR_CHART_NAME)
4450
endif
51+
4552
## K8s namespace for YTsaurus operator.
4653
OPERATOR_NAMESPACE = ytsaurus-operator
4754

48-
ifdef RELEASE_VERSION
49-
DOCKER_BUILD_ARGS += --build-arg VERSION="$(RELEASE_VERSION)"
50-
else
51-
DOCKER_BUILD_ARGS += --build-arg VERSION="$(OPERATOR_TAG)"
52-
endif
55+
DOCKER_BUILD_ARGS += --build-arg VERSION="$(OPERATOR_VERSION)"
5356
DOCKER_BUILD_ARGS += --build-arg REVISION="$(shell git rev-parse HEAD)"
5457
DOCKER_BUILD_ARGS += --build-arg BUILD_DATE="$(shell date -Iseconds -u)"
5558

@@ -60,6 +63,8 @@ else
6063
GOBIN=$(shell go env GOBIN)
6164
endif
6265

66+
GO_LDFLAGS = -X github.com/ytsaurus/ytsaurus-k8s-operator/pkg/version.version=$(OPERATOR_VERSION)
67+
6368
# Setting SHELL to bash allows bash commands to be executed by recipes.
6469
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
6570
SHELL = /usr/bin/env bash -o pipefail
@@ -347,7 +352,7 @@ kind-yt-info:
347352

348353
.PHONY: build
349354
build: generate-code ## Build manager binary.
350-
go build -o bin/manager main.go
355+
go build -ldflags "$(GO_LDFLAGS)" -o bin/manager main.go
351356

352357
.PHONY: run
353358
run: generate-code manifests ## Run a controller from your host.

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import (
4141
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4242

4343
ytv1 "github.com/ytsaurus/ytsaurus-k8s-operator/api/v1"
44+
45+
"github.com/ytsaurus/ytsaurus-k8s-operator/pkg/version"
4446
//+kubebuilder:scaffold:imports
4547
)
4648

@@ -61,6 +63,11 @@ func main() {
6163
var enableLeaderElection bool
6264
var probeAddr string
6365
var maxConcurrentReconciles int
66+
flag.BoolFunc("version", "print the version", func(s string) error {
67+
version.PrintVersion()
68+
os.Exit(0)
69+
return nil
70+
})
6471
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6572
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6673
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
@@ -79,6 +86,8 @@ func main() {
7986

8087
ctx := ctrl.SetupSignalHandler()
8188

89+
setupLog.Info("Starting ytsaurus operator", "version", version.GetVersion())
90+
8291
var clusterDomain string
8392
if domain := os.Getenv("K8S_CLUSTER_DOMAIN"); domain != "" {
8493
clusterDomain = domain

pkg/version/version.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
6+
"runtime/debug"
7+
)
8+
9+
// version provided by ldflags at compile-time
10+
var version string
11+
12+
func GetVersion() string {
13+
if version == "" {
14+
if i, ok := debug.ReadBuildInfo(); ok {
15+
version = i.Main.Version
16+
}
17+
}
18+
return version
19+
}
20+
21+
func PrintVersion() {
22+
fmt.Println(GetVersion())
23+
}

0 commit comments

Comments
 (0)