Skip to content

Commit

Permalink
Version commit hash (#191)
Browse files Browse the repository at this point in the history
log build information on startup

Signed-off-by: ehearneredhat <ehearne@redhat.com>
  • Loading branch information
ehearneRedHat authored Aug 26, 2024
1 parent df3d1e9 commit 6e5fced
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
image: ${{ env.OPERATOR_NAME }}
tags: ${{ env.IMG_TAGS }}
platforms: linux/amd64,linux/arm64
build-args: |
GIT_SHA=${{ github.sha }}
DIRTY=false
dockerfiles: |
./Dockerfile
Expand Down Expand Up @@ -92,6 +96,9 @@ jobs:
image: ${{ env.OPERATOR_NAME }}-bundle
tags: ${{ needs.build.outputs.build-tags }}
platforms: linux/amd64,linux/arm64
build-args: |
GIT_SHA=${{ github.sha }}
DIRTY=false
dockerfiles: |
./bundle.Dockerfile
Expand Down
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Build the manager binary
FROM golang:1.21 as builder
FROM golang:1.21 AS builder
ARG TARGETOS
ARG TARGETARCH

ARG GIT_SHA
ARG DIRTY

ENV GIT_SHA=${GIT_SHA:-unknown}
ENV DIRTY=${DIRTY:-unknown}


WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
Expand All @@ -15,13 +22,12 @@ RUN go mod download
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/ internal/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -a -o manager cmd/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
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,25 @@ local-deploy-namespaced: docker-build kind-load-image ## Deploy the dns operator
##@ Build

.PHONY: build
build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
build: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
build: manifests generate fmt vet ## Build manager binary.
go build -o bin/manager cmd/main.go
go build -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" -o bin/manager cmd/main.go

.PHONY: run
run: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
run: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
run: manifests generate fmt vet ## Run a controller from your host.
go run ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure
go run -ldflags "-X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure

# If you wish built the manager image targeting other platforms you can use the --platform flag.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
docker-build: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .
$(CONTAINER_TOOL) build -t ${IMG} . --build-arg GIT_SHA=$(GIT_SHA) --build-arg DIRTY=$(DIRTY)

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
Expand Down
9 changes: 9 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ import (
_ "github.com/kuadrant/dns-operator/internal/provider/azure"
_ "github.com/kuadrant/dns-operator/internal/provider/google"
_ "github.com/kuadrant/dns-operator/internal/provider/inmemory"
"github.com/kuadrant/dns-operator/internal/version"
//+kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
gitSHA string // pass ldflag here to display gitSHA hash
dirty string // must be string as passed in by ldflag to determine display .
)

const (
Expand All @@ -62,6 +65,10 @@ func init() {
//+kubebuilder:scaffold:scheme
}

func printControllerMetaInfo() {
setupLog.Info("build information", "version", version.Version, "commit", gitSHA, "dirty", dirty)
}

func main() {
var metricsAddr string
var enableLeaderElection bool
Expand Down Expand Up @@ -92,6 +99,8 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

printControllerMetaInfo()

var watchNamespaces = "WATCH_NAMESPACES"
defaultOptions := ctrl.Options{
Scheme: scheme,
Expand Down
15 changes: 15 additions & 0 deletions hack/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 internal/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.4.0-dev" // Change as versions are released
)

0 comments on commit 6e5fced

Please sign in to comment.