Skip to content

Commit

Permalink
build: Ensure clean go build environment for goreleaser and ko (#824)
Browse files Browse the repository at this point in the history
Previous builds were overriding the `GOOS` and `GOARCH` enviroment
variables, which is fine to do when using `ko` directly, but appears to
be
broken when building via ko using `goreleaser`.

This commit ensures that `GOOS` and `GOARCH` are always unset when
running builds by undefining them via `make` `undefine`. The `override`
keyword ensures that the env vars are always undefined regardless of the
source (e.g. enviroment, make variables, etc) to ensure a clean build
environment.

Instead of using `GOARCH` env var for building for the local build
architecure, this commit uses `goreleaser`'s `{{ .Runtime.Goarch }}`
variable available via templating which contains the architecture of the
build machine.

This commit also removes the `ldflags` config from the `ko`
configuration in `goreleaser` config as this is automatically inherited
from the `build` config referenced in the `ko` config.
  • Loading branch information
jimmidyson authored Jul 26, 2024
1 parent 036d8f5 commit 3ac7bf0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
23 changes: 8 additions & 15 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ before:
name: caren-system
$(helm template {{ .ProjectName }} ./charts/{{ .ProjectName }} \
--namespace caren-system \
--set-string image.tag=v{{ trimprefix .Version "v" }} \
--set-string helmRepositoryImage.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Env.GOARCH }} \
--set-string image.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Runtime.Goarch }}{{ end }} \
--set-string helmRepositoryImage.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Runtime.Goarch }} \
--set-string image.repository=ko.local/{{ .ProjectName }}{{ end }} \
)
EOF'
Expand Down Expand Up @@ -80,12 +80,13 @@ builds:
post:
- |
sh -ec 'if [ {{ .IsSnapshot }} == true ] && [ {{ .Runtime.Goarch }} == {{ .Arch }} ]; then
env GOOS=linux GOARCH={{ .Arch }} \
SOURCE_DATE_EPOCH=$(date +%s) \
env SOURCE_DATE_EPOCH=$(date +%s) \
KO_DATA_DATE_EPOCH=$(date +%s) \
KO_DOCKER_REPO=ko.local/{{ .ProjectName }} \
ko build \
--bare \
-t v{{ trimprefix .Version "v" }} \
--platform linux/{{ .Arch }} \
-t v{{ trimprefix .Version "v" }}-{{ .Arch }} \
./cmd
fi'
Expand Down Expand Up @@ -135,16 +136,6 @@ docker_manifests:
kos:
- id: cluster-api-runtime-extensions-nutanix
build: cluster-api-runtime-extensions-nutanix
ldflags:
- -s
- -w
- -X 'k8s.io/component-base/version.buildDate={{ .CommitDate }}'
- -X 'k8s.io/component-base/version.gitCommit={{ .FullCommit }}'
- -X 'k8s.io/component-base/version.gitTreeState={{ .Env.GIT_TREE_STATE }}'
- -X 'k8s.io/component-base/version.gitVersion=v{{ trimprefix .Version "v" }}'
- -X 'k8s.io/component-base/version.major={{ .Major }}'
- -X 'k8s.io/component-base/version.minor={{ .Minor }}'
- -X 'k8s.io/component-base/version/verflag.programName={{ .ProjectName }}'
labels:
org.opencontainers.image.created: "{{ .CommitDate }}"
org.opencontainers.image.title: "{{ .ProjectName }}"
Expand All @@ -156,6 +147,8 @@ kos:
- linux/arm64
repository: ghcr.io/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix
bare: true
creation_time: "{{.CommitTimestamp}}"
ko_data_creation_time: "{{.CommitTimestamp}}"
tags:
- 'v{{ trimprefix .Version "v" }}'

Expand Down
14 changes: 8 additions & 6 deletions make/dev.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ dev.run-on-kind: kind.create clusterctl.init
ifndef SKIP_BUILD
dev.run-on-kind: release-snapshot
endif
dev.run-on-kind: SNAPSHOT_VERSION := $(shell gojq -r '.version+"-"+.runtime.goarch' dist/metadata.json)
dev.run-on-kind:
kind load docker-image --name $(KIND_CLUSTER_NAME) \
ko.local/cluster-api-runtime-extensions-nutanix:$$(gojq -r .version dist/metadata.json) \
ghcr.io/nutanix-cloud-native/caren-helm-reg:$$(gojq -r .version dist/metadata.json)-$(GOARCH)
ko.local/cluster-api-runtime-extensions-nutanix:$(SNAPSHOT_VERSION) \
ghcr.io/nutanix-cloud-native/caren-helm-reg:$(SNAPSHOT_VERSION)
helm upgrade --install cluster-api-runtime-extensions-nutanix ./charts/cluster-api-runtime-extensions-nutanix \
--set-string image.repository=ko.local/cluster-api-runtime-extensions-nutanix \
--set-string image.tag=$$(gojq -r .version dist/metadata.json) \
--set-string helmRepositoryImage.tag=$$(gojq -r .version dist/metadata.json)-$(GOARCH) \
--set-string image.tag=$(SNAPSHOT_VERSION) \
--set-string helmRepositoryImage.tag=$(SNAPSHOT_VERSION) \
--wait --wait-for-jobs
kubectl rollout restart deployment cluster-api-runtime-extensions-nutanix
kubectl rollout restart deployment helm-repository
Expand All @@ -26,11 +27,12 @@ dev.update-webhook-image-on-kind: export KUBECONFIG := $(KIND_KUBECONFIG)
ifndef SKIP_BUILD
dev.update-webhook-image-on-kind: release-snapshot
endif
dev.update-webhook-image-on-kind: SNAPSHOT_VERSION := $(shell gojq -r '.version+"-"+.runtime.goarch' dist/metadata.json)
dev.update-webhook-image-on-kind:
kind load docker-image --name $(KIND_CLUSTER_NAME) \
ko.local/cluster-api-runtime-extensions-nutanix:$$(gojq -r .version dist/metadata.json)
ko.local/cluster-api-runtime-extensions-nutanix:$(SNAPSHOT_VERSION)
kubectl set image deployment \
cluster-api-runtime-extensions-nutanix webhook=ko.local/cluster-api-runtime-extensions-nutanix:$$(gojq -r .version dist/metadata.json)
cluster-api-runtime-extensions-nutanix webhook=ko.local/cluster-api-runtime-extensions-nutanix:$(SNAPSHOT_VERSION)
kubectl rollout restart deployment cluster-api-runtime-extensions-nutanix
kubectl rollout status deployment cluster-api-runtime-extensions-nutanix

Expand Down
14 changes: 6 additions & 8 deletions make/go.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
# to be private (not available publicly) and should therefore not use the proxy or checksum database
export GOPRIVATE ?=

# Always ensure that GOOS and GOARCH are unset in the evironment, otherwise this can cause issues
# with goreleaser and ko building images for the wrong platform.
override undefine GOOS
override undefine GOARCH

ALL_GO_SUBMODULES := $(shell find -mindepth 2 -maxdepth 2 -name go.mod -printf '%P\n' | sort)
GO_SUBMODULES_NO_DOCS := $(filter-out $(addsuffix /go.mod,docs),$(ALL_GO_SUBMODULES))
THIRD_PARTY_GO_SUBMODULES := $(shell find hack/third-party -mindepth 2 -name go.mod -printf 'hack/third-party/%P\n' | sort)

ifndef GOOS
export GOOS := $(OS)
endif
ifndef GOARCH
export GOARCH := $(shell go env GOARCH)
endif

define go_test
source <(setup-envtest use -p env $(ENVTEST_VERSION)) && \
gotestsum \
Expand Down Expand Up @@ -91,7 +89,7 @@ ifneq ($(SKIP_BUILD),true)
$(MAKE) GORELEASER_FLAGS=$$'--config=<(env GOOS=$(shell go env GOOS) GOARCH=$(shell go env GOARCH) gojq --yaml-input --yaml-output \'del(.builds[0].goarch) | del(.builds[0].goos) | .builds[0].targets|=(["linux_amd64","linux_arm64",env.GOOS+"_"+env.GOARCH] | unique | map(. | sub("_amd64";"_amd64_v1")))\' .goreleaser.yml)' release-snapshot
endif
$(info $(M) $(if $(filter $(E2E_DRYRUN), true),dry-,)running e2e tests$(if $(E2E_LABEL), labelled "$(E2E_LABEL)")$(if $(E2E_FOCUS), matching "$(E2E_FOCUS)"))
env E2E_IMAGE_TAG="$$(gojq --raw-output '.version' $(REPO_ROOT)/dist/metadata.json)" \
env E2E_IMAGE_TAG="$$(gojq --raw-output '.version+"-"+.runtime.goarch' $(REPO_ROOT)/dist/metadata.json)" \
envsubst -no-unset -no-empty -i '$(E2E_CONF_FILE)' -o '$(E2E_CONF_FILE_ENVSUBST)'
env AWS_B64ENCODED_CREDENTIALS="$$(clusterawsadm bootstrap credentials encode-as-profile 2>/dev/null)" \
ginkgo run \
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/config/caren.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ managementClusterName: caren-e2e
images:
- name: ko.local/cluster-api-runtime-extensions-nutanix:${E2E_IMAGE_TAG}
loadBehavior: mustLoad
- name: ghcr.io/nutanix-cloud-native/caren-helm-reg:${E2E_IMAGE_TAG}-${GOARCH}
- name: ghcr.io/nutanix-cloud-native/caren-helm-reg:${E2E_IMAGE_TAG}
loadBehavior: mustLoad

providers:
Expand Down

0 comments on commit 3ac7bf0

Please sign in to comment.