diff --git a/.github/workflows/build-images-base.yaml b/.github/workflows/build-images-base.yaml index afeeae5f3..5a917eb9d 100644 --- a/.github/workflows/build-images-base.yaml +++ b/.github/workflows/build-images-base.yaml @@ -116,7 +116,10 @@ jobs: provenance: false tags: | ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}:${{ env.IMG_TAGS }} - build-args: QUAY_IMAGE_EXPIRY=${{ inputs.quayImageExpiry }} + build-args: | + QUAY_IMAGE_EXPIRY=${{ inputs.quayImageExpiry }} + GIT_SHA=${{ github.sha }} + DIRTY=false - name: Print Image URL run: echo "Image pushed to ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}:${{ env.IMG_TAGS }}" @@ -215,4 +218,4 @@ jobs: ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}-catalog:${{ env.IMG_TAGS }} # The Quay image expiry label for the generated catalog Dockerfile is set via opm, using the value set in the QUAY_IMAGE_EXPIRY environment variable - name: Print Catalog Image URL - run: echo "Catalog image pushed to ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}-catalog:${{ env.IMG_TAGS }}" \ No newline at end of file + run: echo "Catalog image pushed to ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}-catalog:${{ env.IMG_TAGS }}" diff --git a/Dockerfile b/Dockerfile index 75958ebc2..40bfd09d4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,12 +14,19 @@ COPY main.go main.go COPY api/ api/ COPY controllers/ controllers/ COPY pkg/ pkg/ +COPY version/ version/ # Set environment variables for cross-compilation ARG TARGETARCH # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} 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=${TARGETARCH} 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 d379ea20b..9022a8504 100644 --- a/Makefile +++ b/Makefile @@ -332,17 +332,28 @@ test-unit: clean-cov generate fmt vet ## Run Unit tests. ##@ 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: export OPERATOR_NAMESPACE = kuadrant-system +run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +run: DIRTY=$(shell $(PROJECT_PATH)/utils/check-git-dirty.sh || echo "unknown") run: generate fmt vet ## Run a controller from your host. - go run ./main.go + 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. - $(CONTAINER_ENGINE) build --build-arg QUAY_IMAGE_EXPIRY=$(QUAY_IMAGE_EXPIRY) -t $(IMG) . + $(CONTAINER_ENGINE) build \ + --build-arg QUAY_IMAGE_EXPIRY=$(QUAY_IMAGE_EXPIRY) \ + --build-arg GIT_SHA=$(GIT_SHA) \ + --build-arg DIRTY=$(DIRTY) \ + --build-arg QUAY_IMAGE_EXPIRY=$(QUAY_IMAGE_EXPIRY) \ + -t $(IMG) . docker-push: ## Push docker image with the manager. $(CONTAINER_ENGINE) push $(IMG) @@ -425,6 +436,7 @@ prepare-release: ## Prepare the manifests for OLM and Helm Chart for a release. LIMITADOR_OPERATOR_VERSION=$(LIMITADOR_OPERATOR_VERSION) \ DNS_OPERATOR_VERSION=$(DNS_OPERATOR_VERSION) \ WASM_SHIM_VERSION=$(WASM_SHIM_VERSION) + sed -i -e 's/Version = ".*"/Version = "$(VERSION)"/' $(PROJECT_PATH)/version/version.go ##@ Code Style diff --git a/main.go b/main.go index 212cc2991..1b8b68fcb 100644 --- a/main.go +++ b/main.go @@ -55,6 +55,7 @@ import ( "github.com/kuadrant/kuadrant-operator/pkg/library/kuadrant" "github.com/kuadrant/kuadrant-operator/pkg/library/reconcilers" "github.com/kuadrant/kuadrant-operator/pkg/log" + "github.com/kuadrant/kuadrant-operator/version" //+kubebuilder:scaffold:imports ) @@ -62,6 +63,8 @@ var ( scheme = k8sruntime.NewScheme() logLevel = env.GetString("LOG_LEVEL", "info") logMode = env.GetString("LOG_MODE", "production") + gitSHA string // value injected in compilation-time + dirty string // value injected in compilation-time ) func init() { @@ -100,6 +103,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("build information", "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 000000000..8f0687f15 --- /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 000000000..b8879c816 --- /dev/null +++ b/version/version.go @@ -0,0 +1,20 @@ +/* +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" +)