Skip to content

Commit 3943c45

Browse files
authored
Add build info metric (valkey-io#106)
Add a `valkey_operator_build_info` metric. This allows tracking of rollouts via metrics. This uses the standard Prometheus version collector helper package and injects the build context via Go build ldflags. Signed-off-by: SuperQ <superq@gmail.com>
1 parent e1183a0 commit 3943c45

8 files changed

Lines changed: 39 additions & 21 deletions

File tree

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ make install # Install CRDs
193193
make run # Run controller locally
194194

195195
# 5. Build and test container
196-
make docker-build IMG=valkey-operator:dev
197-
make deploy IMG=valkey-operator:dev
196+
make docker-build IMG=valkey-operator VERSION=dev
197+
make deploy IMG=valkey-operator VERSION=dev
198198
```
199199

200200
## Code Review Process

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
FROM golang:1.25 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
5+
ARG LDFLAGS
56

67
WORKDIR /workspace
78
# Copy the Go Modules manifests
@@ -19,7 +20,7 @@ COPY . .
1920
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2021
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2122
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
22-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
23+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -ldflags "${LDFLAGS}" -o manager cmd/main.go
2324

2425
# Use distroless as minimal base image to package the manager binary
2526
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Image URL to use all building/pushing image targets
2-
IMG ?= controller:latest
2+
VERSION ?= latest
3+
IMG ?= controller
4+
CONTAINER_IMAGE_NAME ?= $(IMG):$(VERSION)
35

46
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
57
ifeq (,$(shell go env GOBIN))
@@ -19,6 +21,16 @@ CONTAINER_TOOL ?= docker
1921
SHELL = /usr/bin/env bash -o pipefail
2022
.SHELLFLAGS = -ec
2123

24+
BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
25+
BUILDDATE ?= $(shell date -Iseconds)
26+
REVISION ?= $(shell git rev-parse HEAD)
27+
28+
VERSION_LDFLAGS := \
29+
-X github.com/prometheus/common/version.Version=$(VERSION) \
30+
-X github.com/prometheus/common/version.Revision=$(REVISION) \
31+
-X github.com/prometheus/common/version.Branch=$(BRANCH) \
32+
-X github.com/prometheus/common/version.BuildDate=$(BUILDDATE)
33+
2234
.PHONY: all
2335
all: build
2436

@@ -106,7 +118,7 @@ lint-config: golangci-lint ## Verify golangci-lint linter configuration
106118

107119
.PHONY: build
108120
build: manifests generate fmt vet lint ## Build manager binary.
109-
go build -o bin/manager cmd/main.go
121+
go build -ldflags "$(VERSION_LDFLAGS)" -o bin/manager cmd/main.go
110122

111123
.PHONY: run
112124
run: manifests generate fmt vet ## Run a controller from your host.
@@ -117,17 +129,17 @@ run: manifests generate fmt vet ## Run a controller from your host.
117129
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
118130
.PHONY: docker-build
119131
docker-build: ## Build docker image with the manager.
120-
$(CONTAINER_TOOL) build -t ${IMG} .
132+
$(CONTAINER_TOOL) build --build-arg "LDFLAGS=$(VERSION_LDFLAGS)" -t ${CONTAINER_IMAGE_NAME} .
121133

122134
.PHONY: docker-push
123135
docker-push: ## Push docker image with the manager.
124-
$(CONTAINER_TOOL) push ${IMG}
136+
$(CONTAINER_TOOL) push ${CONTAINER_IMAGE_NAME}
125137

126138
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
127-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
139+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator VERSION=0.0.1). To use this option you need to:
128140
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
129141
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
130-
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
142+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image VERSION:<tag>> then the export will fail)
131143
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
132144
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
133145
.PHONY: docker-buildx
@@ -136,14 +148,14 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
136148
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
137149
- $(CONTAINER_TOOL) buildx create --name valkey-operator-builder
138150
$(CONTAINER_TOOL) buildx use valkey-operator-builder
139-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
151+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg "LDFLAGS=$(VERSION_LDFLAGS)" --tag ${CONTAINER_IMAGE_NAME} -f Dockerfile.cross .
140152
- $(CONTAINER_TOOL) buildx rm valkey-operator-builder
141153
rm Dockerfile.cross
142154

143155
.PHONY: build-installer
144156
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
145157
mkdir -p dist
146-
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${IMG}
158+
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${CONTAINER_IMAGE_NAME}
147159
"$(KUSTOMIZE)" build config/default > dist/install.yaml
148160

149161
##@ Deployment
@@ -164,7 +176,7 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified
164176

165177
.PHONY: deploy
166178
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
167-
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${IMG}
179+
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${CONTAINER_IMAGE_NAME}
168180
"$(KUSTOMIZE)" build config/default | "$(KUBECTL)" apply -f -
169181

170182
.PHONY: undeploy

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Valkey Operator is a Kubernetes operator that automates the deployment and manag
4141
**Build and push your image to the location specified by `IMG`:**
4242

4343
```sh
44-
make docker-build docker-push IMG=<some-registry>/valkey-operator:tag
44+
make docker-build docker-push IMG=<some-registry>/valkey-operator VERSION=tag
4545
```
4646

4747
**NOTE:** This image ought to be published in the personal registry you specified.
@@ -57,7 +57,7 @@ make install
5757
**Deploy the Manager to the cluster with the image specified by `IMG`:**
5858

5959
```sh
60-
make deploy IMG=<some-registry>/valkey-operator:tag
60+
make deploy IMG=<some-registry>/valkey-operator VERSION=tag
6161
```
6262

6363
> **NOTE**: If you encounter RBAC errors, you may need to grant yourself cluster-admin privileges or be logged in as admin.
@@ -100,7 +100,7 @@ Following the options to release and provide this solution to the users.
100100
1. Build the installer for the image built and published in the registry:
101101

102102
```sh
103-
make build-installer IMG=<some-registry>/valkey-operator:tag
103+
make build-installer IMG=<some-registry>/valkey-operator VERSION=tag
104104
```
105105

106106
**NOTE:** The makefile target mentioned above generates an 'install.yaml'

cmd/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ import (
2626
"go.uber.org/zap/zapcore"
2727
_ "k8s.io/client-go/plugin/pkg/client/auth"
2828

29+
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3132
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3233
ctrl "sigs.k8s.io/controller-runtime"
3334
"sigs.k8s.io/controller-runtime/pkg/healthz"
3435
"sigs.k8s.io/controller-runtime/pkg/log/zap"
36+
"sigs.k8s.io/controller-runtime/pkg/metrics"
3537
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3638
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3739
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -89,6 +91,8 @@ func main() {
8991

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

94+
metrics.Registry.MustRegister(versioncollector.NewCollector("valkey_operator"))
95+
9296
// if the enable-http2 flag is false (the default), http/2 should be disabled
9397
// due to its vulnerabilities. More specifically, disabling http/2 will
9498
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.25.3
55
require (
66
github.com/onsi/ginkgo/v2 v2.27.2
77
github.com/onsi/gomega v1.38.2
8+
github.com/prometheus/client_golang v1.23.2
89
github.com/stretchr/testify v1.11.1
910
github.com/valkey-io/valkey-go v1.0.68
1011
go.uber.org/zap v1.27.0
@@ -50,7 +51,6 @@ require (
5051
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
5152
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5253
github.com/pmezard/go-difflib v1.0.0 // indirect
53-
github.com/prometheus/client_golang v1.23.2 // indirect
5454
github.com/prometheus/client_model v0.6.2 // indirect
5555
github.com/prometheus/common v0.66.1 // indirect
5656
github.com/prometheus/procfs v0.16.1 // indirect

test/e2e/e2e_suite_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const valkeyClientImage = "valkey/valkey:9.0.0"
4949

5050
var (
5151
// managerImage is the manager image to be built and loaded for testing.
52-
managerImage = "valkey/valkey-operator:v0.0.1"
52+
managerImage = "valkey/valkey-operator"
53+
managerVersion = "v0.0.1"
5354
// shouldCleanupCertManager tracks whether CertManager was installed by this suite.
5455
shouldCleanupCertManager = false
5556
)
@@ -71,12 +72,12 @@ var _ = BeforeSuite(func() {
7172
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to purge old events")
7273

7374
By("building the manager image")
74-
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", managerImage))
75+
cmd = exec.Command("make", "docker-build", "IMG="+managerImage, "VERSION="+managerVersion)
7576
_, err = utils.Run(cmd)
7677
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager image")
7778

7879
By("loading the manager image on Kind")
79-
err = utils.LoadImageToKindClusterWithName(managerImage)
80+
err = utils.LoadImageToKindClusterWithName(managerImage + ":" + managerVersion)
8081
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager image into Kind")
8182

8283
setupCertManager()
@@ -104,7 +105,7 @@ var _ = BeforeSuite(func() {
104105
Expect(err).NotTo(HaveOccurred(), "Failed to install CRDs")
105106

106107
By("deploying the controller-manager")
107-
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", managerImage))
108+
cmd = exec.Command("make", "deploy", "IMG="+managerImage, "VERSION="+managerVersion)
108109
_, err = utils.Run(cmd)
109110
Expect(err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
110111
})

test/e2e/valkeycluster_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929

3030
. "github.com/onsi/ginkgo/v2"
3131
. "github.com/onsi/gomega"
32-
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
3434
valkeyiov1alpha1 "valkey.io/valkey-operator/api/v1alpha1"
3535
"valkey.io/valkey-operator/test/utils"
3636
)

0 commit comments

Comments
 (0)