Skip to content

Commit 6d72160

Browse files
committed
Add version subcommand
1 parent e446ef6 commit 6d72160

File tree

9 files changed

+55
-35
lines changed

9 files changed

+55
-35
lines changed

.github/workflows/publish-docker.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ jobs:
5353
flavor: |
5454
latest=${{ github.ref == 'refs/heads/main' }}
5555
56+
- name: Set short git commit SHA
57+
id: vars
58+
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
59+
5660
- name: Set up QEMU
5761
uses: docker/setup-qemu-action@v3
5862
with:
@@ -82,3 +86,7 @@ jobs:
8286
tags: ${{ steps.meta.outputs.tags }}
8387
labels: ${{ steps.meta.outputs.labels }}
8488
target: ${{ matrix.image.target }}
89+
build-args: |
90+
VERSION=${{ steps.meta.outputs.version }}
91+
GIT_COMMIT=${{ steps.vars.sha_short }}
92+
BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ linters:
3939
- usetesting
4040
- whitespace
4141
settings:
42+
gomoddirectives:
43+
toolchain-forbidden: true
44+
go-version-pattern: 1\.\d+(\.0)?$
4245
revive:
4346
rules:
4447
- name: comment-spacings

.license-scan-overrides.jsonl

Lines changed: 0 additions & 11 deletions
This file was deleted.

.license-scan-rules.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

Dockerfile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ FROM --platform=$BUILDPLATFORM golang:1.25.0 AS builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

6+
ARG BUILD_DATE
7+
ARG GIT_COMMIT
8+
ARG VERSION
9+
610
WORKDIR /workspace
711
# Copy the Go Modules manifests
812
COPY go.mod go.mod
@@ -23,12 +27,23 @@ COPY internal/ internal/
2327
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
2428
RUN --mount=type=cache,target=/root/.cache/go-build \
2529
--mount=type=cache,target=/go/pkg \
26-
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags="-s -w" -a -o manager cmd/main.go
30+
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags="-s -w -X 'main.version=${VERSION}' -X 'main.gitCommit=${GIT_COMMIT}' -X 'main.buildDate=${BUILD_DATE}'" -a -o manager cmd/main.go
2731

2832
# Use distroless as minimal base image to package the manager binary
2933
# Refer to https://github.com/GoogleContainerTools/distroless for more details
3034
FROM gcr.io/distroless/static:nonroot AS manager
31-
LABEL source_repository="https://github.com/ironcore-dev/metal-operator"
35+
36+
ARG BUILD_DATE
37+
ARG GIT_COMMIT
38+
ARG VERSION
39+
40+
LABEL source_repository="https://github.com/ironcore-dev/metal-operator" \
41+
org.opencontainers.image.url="https://github.com/ironcore-dev/network-operator" \
42+
org.opencontainers.image.created=${BUILD_DATE} \
43+
org.opencontainers.image.revision=${GIT_COMMIT} \
44+
org.opencontainers.image.version=${VERSION} \
45+
org.opencontainers.image.licenses="Apache-2.0"
46+
3247
WORKDIR /
3348
COPY --from=builder /workspace/manager .
3449
USER 65532:65532

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ endif
1212
GOARCH := $(shell go env GOARCH)
1313
GOOS := $(shell go env GOOS)
1414

15+
VERSION ?= dev
16+
GIT_COMMIT ?= $(shell git rev-parse --short HEAD)
17+
BUILD_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
18+
19+
LDFLAGS = -ldflags="-X 'main.version=$(VERSION)' -X 'main.gitCommit=$(GIT_COMMIT)' -X 'main.buildDate=$(BUILD_DATE)'"
20+
1521
# CONTAINER_TOOL defines the container tool to be used for building images.
1622
# Be aware that the target commands are only tested with Docker which is
1723
# scaffolded by default. However, you might want to replace it to use other
@@ -127,7 +133,7 @@ docs: gen-crd-api-reference-docs ## Run go generate to generate API reference do
127133

128134
.PHONY: build
129135
build: manifests generate fmt vet ## Build manager binary.
130-
go build -o bin/manager cmd/main.go
136+
go build $(LDFLAGS) -o bin/manager cmd/main.go
131137

132138
.PHONY: run
133139
run: manifests generate fmt vet ## Run a controller from your host.
@@ -143,7 +149,11 @@ helm: manifests kubebuilder
143149
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
144150
.PHONY: docker-build
145151
docker-build: ## Build docker image with the manager.
146-
$(CONTAINER_TOOL) build -t ${IMG} .
152+
$(CONTAINER_TOOL) build \
153+
--build-arg=BUILD_DATE=$(BUILD_DATE) \
154+
--build-arg=GIT_COMMIT=$(GIT_COMMIT) \
155+
--build-arg=VERSION=$(VERSION) \
156+
-t ${IMG} .
147157

148158
.PHONY: docker-push
149159
docker-push: ## Push docker image with the manager.

api/v1alpha1/doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
// Package v1alpha1 contains API Schema definitions for the networking.cloud.sap API group
5-
// +groupName=networking.cloud.sap
5+
// +kubebuilder:validation:Required
66
// +kubebuilder:object:generate=true
7+
// +groupName=networking.cloud.sap
78
package v1alpha1

api/v1alpha1/groupversion_info.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
// Package v1alpha1 contains API Schema definitions for the networking.cloud.sap v1alpha1 API group.
5-
// +kubebuilder:validation:Required
6-
// +kubebuilder:object:generate=true
7-
// +groupName=networking.cloud.sap
85
package v1alpha1
96

107
import (

cmd/main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/tls"
88
"flag"
99
"fmt"
10+
"log"
1011
"os"
1112
"path/filepath"
1213
"strings"
@@ -20,8 +21,6 @@ import (
2021
// Set runtime concurrency to match CPU limit imposed by Kubernetes
2122
_ "go.uber.org/automaxprocs"
2223

23-
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
24-
// to ensure that exec-entrypoint and run can make use of them.
2524
_ "k8s.io/client-go/plugin/pkg/client/auth"
2625
ctrl "sigs.k8s.io/controller-runtime"
2726
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
@@ -44,6 +43,10 @@ import (
4443
var (
4544
scheme = runtime.NewScheme()
4645
setupLog = ctrl.Log.WithName("setup")
46+
47+
version = "dev"
48+
gitCommit = "none"
49+
buildDate = "unknown"
4750
)
4851

4952
func init() {
@@ -53,6 +56,14 @@ func init() {
5356
}
5457

5558
func main() {
59+
if len(os.Args) > 1 && os.Args[1] == "version" {
60+
log.SetFlags(0)
61+
log.Printf("Version: %s", version)
62+
log.Printf("Git Commit: %s", gitCommit)
63+
log.Printf("Build Date: %s", buildDate)
64+
os.Exit(0)
65+
}
66+
5667
var metricsAddr string
5768
var metricsCertPath, metricsCertName, metricsCertKey string
5869
var webhookCertPath, webhookCertName, webhookCertKey string

0 commit comments

Comments
 (0)