Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
FROM golang:1.25 AS builder
ARG TARGETOS
ARG TARGETARCH
ARG BUILD_FLAGS

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

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,18 @@ push-helmchart: package-helmchart ## Push helm image. It will be pushed to the $

##@ Build

GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
BUILD_TIME=$(shell date "+%FT%T")
VERSION_PKG = github.com/ClickHouse/clickhouse-operator/internal/version
GO_LDFLAGS = -X $(VERSION_PKG).Version=v$(VERSION) -X $(VERSION_PKG).GitCommitHash=$(GIT_COMMIT) -X $(VERSION_PKG).BuildTime=$(BUILD_TIME)

.PHONY: build
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go
go build -ldflags "$(GO_LDFLAGS)" -o bin/clickhouse-manager cmd/main.go

.PHONY: build-linux-manager
build-linux-manager:
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on go build -gcflags="all=-N -l" -o bin/manager_linux cmd/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} GO111MODULE=on go build -gcflags="all=-N -l" -ldflags "$(GO_LDFLAGS)" -o bin/manager_linux cmd/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand All @@ -202,7 +207,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .
$(CONTAINER_TOOL) build --build-arg BUILD_FLAGS="$(GO_LDFLAGS)" -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand All @@ -221,7 +226,7 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- $(CONTAINER_TOOL) buildx create --name clickhouse-operator-builder
$(CONTAINER_TOOL) buildx use clickhouse-operator-builder
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --build-arg BUILD_FLAGS="$(GO_LDFLAGS)" --tag ${IMG} -f Dockerfile.cross .
- $(CONTAINER_TOOL) buildx rm clickhouse-operator-builder
rm Dockerfile.cross

Expand Down
16 changes: 14 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ClickHouse/clickhouse-operator/internal/controller/clickhouse"
"github.com/ClickHouse/clickhouse-operator/internal/controller/keeper"
"github.com/ClickHouse/clickhouse-operator/internal/controllerutil"
"github.com/ClickHouse/clickhouse-operator/internal/version"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand Down Expand Up @@ -133,7 +134,14 @@ func run() error {
metricsServerOptions.KeyName = metricsCertKey
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
config, err := ctrl.GetConfig()
if err != nil {
return fmt.Errorf("get kubeconfig: %w", err)
}

config.UserAgent = version.BuildUserAgent()

mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
Metrics: metricsServerOptions,
WebhookServer: webhookServer,
Expand Down Expand Up @@ -176,7 +184,11 @@ func run() error {
return fmt.Errorf("unable to setup readyz checker: %w", err)
}

setupLog.Info("starting manager")
setupLog.Info("starting manager",
"version", version.Version,
"gitCommitHash", version.GitCommitHash,
"buildTime", version.BuildTime,
)

if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
return fmt.Errorf("unable to start manager: %w", err)
Expand Down
31 changes: 31 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package version

import (
"fmt"
"runtime"
)

// Information about the current application build.
// These variables are set at build time using -ldflags "-X ...", and should not be changed in code.
var (
// Version is the application version, which should be set to a valid semver string at build time.
Version = "unknown"
// GitCommitHash is the git commit hash of the current build, which should be set at build time.
GitCommitHash = "unknown"
// BuildTime is the time when the application was built, which should be set at build time.
BuildTime = "unknown"
)

const (
hashLength = 7
)

// BuildUserAgent builds a user agent string that includes the application version and build info.
func BuildUserAgent() string {
hash := GitCommitHash
if len(hash) > hashLength {
hash = hash[:hashLength]
}

return fmt.Sprintf("clickhouse-operator/%s (%s/%s; %s)", Version, runtime.GOOS, runtime.GOARCH, hash)
}
Loading