Skip to content

Commit

Permalink
Add last git commit hash to build flags
Browse files Browse the repository at this point in the history
This ensures we have the git hash of the changes in our CI logs
so we can correlate the code changes being executed.
  • Loading branch information
thunderboltsid committed Nov 22, 2023
1 parent c18f70a commit dc3e7c4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ kind-delete: ## Delete the kind cluster

.PHONY: build
build: generate fmt ## Build manager binary.
go build -o bin/manager main.go
go build -ldflags "-X main.gitCommit=$(git rev-parse HEAD)" -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand Down Expand Up @@ -330,7 +330,7 @@ cluster-templates: $(KUSTOMIZE) ## Generate cluster templates for all flavors

.PHONY: docker-build-e2e
docker-build-e2e: $(KO) ## Build docker image with the manager with e2e tag.
KO_DOCKER_REPO=ko.local $(KO) build -B --platform=${PLATFORMS_E2E} -t e2e -L .
KO_DOCKER_REPO=ko.local $(KO) build -B --platform=${PLATFORMS_E2E} --build-arg GIT_COMMIT_HASH=$(git rev-parse HEAD) -t e2e -L .
docker tag ko.local/cluster-api-provider-nutanix:e2e ${IMG_REPO}:e2e

.PHONY: prepare-local-clusterctl
Expand Down
2 changes: 1 addition & 1 deletion hack/ensure-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ EOF
local go_version
IFS=" " read -ra go_version <<< "$(go version)"
local minimum_go_version
minimum_go_version=go1.18.3
minimum_go_version=go1.21.4
if [[ "${minimum_go_version}" != $(echo -e "${minimum_go_version}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then
cat <<EOF
Detected go version: ${go_version[*]}.
Expand Down
4 changes: 2 additions & 2 deletions hack/install-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -o errexit
set -o nounset
set -o pipefail

wget https://go.dev/dl/go1.18.4.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.18.4.linux-amd64.tar.gz
wget https://go.dev/dl/go1.21.4.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin
go version

17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ var (
setupLog = ctrl.Log.WithName("setup")
)

// gitCommit is the git commit hash of the code that is running.
var gitCommit string

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

Expand All @@ -71,6 +74,9 @@ func init() {
const (
// DefaultMaxConcurrentReconciles is the default maximum number of concurrent reconciles
defaultMaxConcurrentReconciles = 10

// GIT_COMMIT_HASH_ENVVAR overrides the git commit hash provided by build flags
GIT_COMMIT_HASH_ENVVAR = "GIT_COMMIT_HASH"
)

func main() {
Expand All @@ -80,6 +86,7 @@ func main() {
probeAddr string
maxConcurrentReconciles int
)

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand All @@ -90,6 +97,7 @@ func main() {
"max-concurrent-reconciles",
defaultMaxConcurrentReconciles,
"The maximum number of allowed, concurrent reconciles.")

opts := zap.Options{
TimeEncoder: zapcore.RFC3339TimeEncoder,
}
Expand All @@ -98,6 +106,11 @@ func main() {

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

commitHash := gitCommit
if envCommitHash := os.Getenv(GIT_COMMIT_HASH_ENVVAR); envCommitHash != "" {
commitHash = envCommitHash
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Expand All @@ -111,7 +124,7 @@ func main() {
os.Exit(1)
}

// Setup the context that's going to be used in controllers and for the manager.
// Set up the context that's going to be used in controllers and for the manager.
ctx := ctrl.SetupSignalHandler()

// Create a secret informer for the Nutanix client
Expand Down Expand Up @@ -173,7 +186,7 @@ func main() {
os.Exit(1)
}

setupLog.Info("starting manager")
setupLog.Info("starting CAPX Controller", "gitCommitHash", commitHash)
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
Expand Down
6 changes: 4 additions & 2 deletions package/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build the manager binary
FROM golang:1.16 as builder
FROM golang:1.21 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand All @@ -15,8 +15,10 @@ COPY api/ api/
COPY controllers/ controllers/
COPY pkg/ pkg/

ARG GIT_COMMIT_HASH
ENV GIT_COMMIT_HASH=${GIT_COMMIT_HASH}
# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.gitCommit=$(git rev-parse HEAD)" -a -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

0 comments on commit dc3e7c4

Please sign in to comment.