Skip to content

Commit

Permalink
Merge pull request #149 from Kuadrant/version-commit-dirty
Browse files Browse the repository at this point in the history
added display version, hash and dirty in logs
  • Loading branch information
ehearneRedHat authored Jul 8, 2024
2 parents 181e4be + 3eb7709 commit 5fde4c9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-images-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
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
21 changes: 21 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 5fde4c9

Please sign in to comment.