diff --git a/.github/workflows/build-images.yaml b/.github/workflows/build-images.yaml index 3d96fbaa..6400acd3 100644 --- a/.github/workflows/build-images.yaml +++ b/.github/workflows/build-images.yaml @@ -66,6 +66,8 @@ jobs: platforms: linux/amd64,linux/arm64,linux/s390x,linux/ppc64le build-args: | VERSION=${{ env.VERSION }} + GIT_SHA=${{ github.sha }} + DIRTY=false DEFAULT_AUTHORINO_IMAGE=${{ env.DEFAULT_AUTHORINO_IMAGE }} containerfiles: | ./Dockerfile diff --git a/.gitignore b/.gitignore index fa24facb..2f0e21b8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.dylib bin testbin/* +/authorino-operator # Test binary, build with `go test -c` *.test diff --git a/Dockerfile b/Dockerfile index 21c2b5d7..265428af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,10 @@ COPY pkg/ pkg/ ARG VERSION=latest ARG DEFAULT_AUTHORINO_IMAGE=quay.io/kuadrant/authorino:latest -RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${VERSION} -X github.com/kuadrant/authorino-operator/controllers.DefaultAuthorinoImage=${DEFAULT_AUTHORINO_IMAGE}" -o manager main.go +ARG GIT_SHA=unknown +ARG DIRTY=unknown + +RUN CGO_ENABLED=0 GO111MODULE=on go build -a -ldflags "-X main.version=${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY} -X github.com/kuadrant/authorino-operator/controllers.DefaultAuthorinoImage=${DEFAULT_AUTHORINO_IMAGE}" -o manager main.go # Use Red Hat minimal base image to package the binary # https://catalog.redhat.com/software/containers/ubi9-minimal diff --git a/Makefile b/Makefile index 89b29dbb..816cb0ac 100644 --- a/Makefile +++ b/Makefile @@ -212,14 +212,20 @@ test: manifests generate fmt vet setup-envtest ## Run the tests. ##@ Build +build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +build: DIRTY=$(shell $(PROJECT_DIR)/utils/check-git-dirty.sh || echo "unknown") build: generate fmt vet ## Build manager binary. - go build -ldflags "-X main.version=$(VERSION) -X github.com/kuadrant/authorino-operator/controllers.DefaultAuthorinoImage=$(ACTUAL_DEFAULT_AUTHORINO_IMAGE)" -o bin/manager main.go + go build -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY} -X github.com/kuadrant/authorino-operator/controllers.DefaultAuthorinoImage=$(ACTUAL_DEFAULT_AUTHORINO_IMAGE)" -o bin/manager main.go +run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +run: DIRTY=$(shell $(PROJECT_DIR)/utils/check-git-dirty.sh || echo "unknown") run: manifests generate fmt vet ## Run a controller from your host. - go run -ldflags "-X main.version=$(VERSION) -X github.com/kuadrant/authorino-operator/controllers.DefaultAuthorinoImage=$(ACTUAL_DEFAULT_AUTHORINO_IMAGE)" ./main.go + go run -ldflags "-X main.version=$(VERSION) -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY} -X github.com/kuadrant/authorino-operator/controllers.DefaultAuthorinoImage=$(ACTUAL_DEFAULT_AUTHORINO_IMAGE)" ./main.go +docker-build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown") +docker-build: DIRTY=$(shell $(PROJECT_DIR)/utils/check-git-dirty.sh || echo "unknown") docker-build: ## Build docker image with the manager. - docker build --build-arg VERSION=$(VERSION) --build-arg ACTUAL_DEFAULT_AUTHORINO_IMAGE=$(ACTUAL_DEFAULT_AUTHORINO_IMAGE) -t $(OPERATOR_IMAGE) . + docker build --build-arg VERSION=$(VERSION) --build-arg GIT_SHA=$(GIT_SHA) --build-arg DIRTY=$(DIRTY) --build-arg ACTUAL_DEFAULT_AUTHORINO_IMAGE=$(ACTUAL_DEFAULT_AUTHORINO_IMAGE) -t $(OPERATOR_IMAGE) . docker-push: ## Push docker image with the manager. docker push ${OPERATOR_IMAGE} @@ -245,10 +251,10 @@ install-operator: manifests kustomize ## Install CRDs into the K8s cluster speci uninstall-operator: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. kubectl delete -f $(OPERATOR_MANIFESTS) --ignore-not-found -install-authorino: create-namespace install-cert-manager ## install RBAC and CRD for authorino +install-authorino: $(KUSTOMIZE) create-namespace install-cert-manager ## install RBAC and CRD for authorino $(KUSTOMIZE) build config/authorino | kubectl apply -f - -uninstall-authorino: ## uninstall RBAC and CRD for authorino +uninstall-authorino: $(KUSTOMIZE) ## uninstall RBAC and CRD for authorino $(KUSTOMIZE) build config/authorino | kubectl delete -f - --ignore-not-found install-cert-manager: ## install the cert manager need for the web hooks @@ -267,7 +273,7 @@ delete-namespace: DEPLOYMENT_DIR = $(PROJECT_DIR)/config/deploy DEPLOYMENT_FILE = $(DEPLOYMENT_DIR)/manifests.yaml .PHONY: deploy-manifest -deploy-manifest: +deploy-manifest: $(KUSTOMIZE) mkdir -p $(DEPLOYMENT_DIR) cd $(PROJECT_DIR)/config/manager && $(KUSTOMIZE) edit set image controller=$(OPERATOR_IMAGE) ;\ cd $(PROJECT_DIR) && $(KUSTOMIZE) build config/deploy > $(DEPLOYMENT_FILE) diff --git a/main.go b/main.go index 54946103..31245573 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,8 @@ var ( setupLog = ctrl.Log.WithName("setup") logger log.Logger version string // value injected in compilation-time + gitSHA string // value injected in compilation-time + dirty string // value injected in compilation-time ) func init() { @@ -89,7 +91,11 @@ func main() { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) - setupLog.Info("booting up authorino operator", "version", version, "default authorino image", controllers.DefaultAuthorinoImage) + setupLog.Info("booting up authorino operator", + "version", version, + "commit", gitSHA, + "dirty", dirty, + "default authorino image", controllers.DefaultAuthorinoImage) mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, diff --git a/utils/check-git-dirty.sh b/utils/check-git-dirty.sh new file mode 100755 index 00000000..43760316 --- /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