diff --git a/.github/workflows/build-images-base.yaml b/.github/workflows/build-images-base.yaml index 8e2d9909..75eab485 100644 --- a/.github/workflows/build-images-base.yaml +++ b/.github/workflows/build-images-base.yaml @@ -74,6 +74,9 @@ jobs: platforms: linux/amd64,linux/arm64 dockerfiles: | ./Dockerfile + build-args: | + GIT_SHA=${{ github.sha }} + DIRTY=false - name: Push Image if: ${{ !env.ACT }} id: push-to-quay diff --git a/Dockerfile b/Dockerfile index 541c670a..e03b52fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,9 +14,15 @@ COPY main.go main.go COPY api/ api/ COPY controllers/ controllers/ COPY pkg/ pkg/ +COPY version/ version/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go +ARG GIT_SHA +ARG DIRTY + +ENV GIT_SHA=${GIT_SHA:-unknown} +ENV DIRTY=${DIRTY:-unknown} +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -o manager main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index f40030c7..c9d4efd7 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,6 @@ SHELL = /usr/bin/env bash -o pipefail MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH))) -# VERSION defines the project version for the bundle. -# Update this value when you upgrade the version of your project. -# To re-generate a bundle for another specific version without changing the standard setup, you can: -# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2) -# - use environment variables to overwrite this value (e.g export VERSION=0.0.2) VERSION ?= 0.0.0 # CHANNELS define the bundle channels used in the bundle. @@ -267,17 +262,22 @@ test-unit: clean-cov generate fmt vet ## Run Unit tests. go test $(UNIT_DIRS) -coverprofile $(PROJECT_PATH)/coverage/unit/cover.out -v -timeout 0 $(TEST_PATTERN) ##@ Build - +build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +build: DIRTY=$(shell $(PROJECT_PATH)/utils/check-git-dirty.sh || echo "unknown") build: generate fmt vet ## Build manager binary. - go build -o bin/manager main.go + go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -o bin/manager main.go run: export LOG_LEVEL = debug run: export LOG_MODE = development -run: manifests generate fmt vet ## Run a controller from your host. - go run ./main.go +run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +run: DIRTY=$(shell $(PROJECT_PATH)/utils/check-git-dirty.sh || echo "unknown") +run: manifests generate fmt vet ## Run a controller from your host.) + go run -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" ./main.go +docker-build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +docker-build: DIRTY=$(shell $(PROJECT_PATH)/utils/check-git-dirty.sh || echo "unknown") docker-build: ## Build docker image with the manager. - docker build -t $(IMG) . + docker build --build-arg GIT_SHA=$(GIT_SHA) --build-arg DIRTY=$(DIRTY) -t $(IMG) . docker-push: ## Push docker image with the manager. docker push $(IMG) diff --git a/main.go b/main.go index 7c8a9c66..fabb1f94 100644 --- a/main.go +++ b/main.go @@ -38,13 +38,18 @@ import ( "github.com/kuadrant/limitador-operator/controllers" "github.com/kuadrant/limitador-operator/pkg/log" "github.com/kuadrant/limitador-operator/pkg/reconcilers" + //+kubebuilder:scaffold:imports + // import version + "github.com/kuadrant/limitador-operator/version" ) var ( scheme = k8sruntime.NewScheme() logLevel = env.GetString("LOG_LEVEL", "info") logMode = env.GetString("LOG_MODE", "production") + gitSHA string + dirty string ) func init() { @@ -67,6 +72,7 @@ func printControllerMetaInfo() { setupLog.Info(fmt.Sprintf("go version: %s", runtime.Version())) setupLog.Info(fmt.Sprintf("go os/arch: %s/%s", runtime.GOOS, runtime.GOARCH)) setupLog.Info("base logger", "log level", logLevel, "log mode", logMode) + setupLog.Info("", "version", version.Version, "commit", gitSHA, "dirty", dirty) } func main() { diff --git a/utils/check-git-dirty.sh b/utils/check-git-dirty.sh new file mode 100755 index 00000000..8f0687f1 --- /dev/null +++ b/utils/check-git-dirty.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +if ! command -v git &>/dev/null +then + echo "git not found..." >&2 + exit 1 +fi + +if output=$(git diff --stat 2>/dev/null) +then +[ -n "$output" ] && echo "true" || echo "false" +else + # Not a git repository + exit 1 +fi \ No newline at end of file diff --git a/version/version.go b/version/version.go new file mode 100644 index 00000000..9c9d4f0b --- /dev/null +++ b/version/version.go @@ -0,0 +1,21 @@ +/* +Copyright 2021 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package version + +var ( + // This variable is dependent on what the current release is e.g. if it is v0.10.0 then this variable, outside of releases, will be v0.10.1-dev . + Version = "0.10.0-dev" +)