Skip to content

Commit

Permalink
Build information log with version, hash and dirty attributes (#861)
Browse files Browse the repository at this point in the history
* build information log with version, hash and dirty attributes

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

* Makefile: prepare-release updates version.go

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>

---------

Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki authored Sep 17, 2024
1 parent 2cd87da commit c4335c2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build-images-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

Expand Down Expand Up @@ -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 }}"
run: echo "Catalog image pushed to ${{ env.IMG_REGISTRY_HOST }}/${{ env.IMG_REGISTRY_ORG }}/${{ env.OPERATOR_NAME }}-catalog:${{ env.IMG_TAGS }}"
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ 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
)

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() {
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions utils/check-git-dirty.sh
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit c4335c2

Please sign in to comment.