Skip to content

Commit 3ac7bf0

Browse files
authored
build: Ensure clean go build environment for goreleaser and ko (#824)
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.
1 parent 036d8f5 commit 3ac7bf0

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

.goreleaser.yml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ before:
4141
name: caren-system
4242
$(helm template {{ .ProjectName }} ./charts/{{ .ProjectName }} \
4343
--namespace caren-system \
44-
--set-string image.tag=v{{ trimprefix .Version "v" }} \
45-
--set-string helmRepositoryImage.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Env.GOARCH }} \
44+
--set-string image.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Runtime.Goarch }}{{ end }} \
45+
--set-string helmRepositoryImage.tag=v{{ trimprefix .Version "v" }}{{ if .IsSnapshot }}-{{ .Runtime.Goarch }} \
4646
--set-string image.repository=ko.local/{{ .ProjectName }}{{ end }} \
4747
)
4848
EOF'
@@ -80,12 +80,13 @@ builds:
8080
post:
8181
- |
8282
sh -ec 'if [ {{ .IsSnapshot }} == true ] && [ {{ .Runtime.Goarch }} == {{ .Arch }} ]; then
83-
env GOOS=linux GOARCH={{ .Arch }} \
84-
SOURCE_DATE_EPOCH=$(date +%s) \
83+
env SOURCE_DATE_EPOCH=$(date +%s) \
84+
KO_DATA_DATE_EPOCH=$(date +%s) \
8585
KO_DOCKER_REPO=ko.local/{{ .ProjectName }} \
8686
ko build \
8787
--bare \
88-
-t v{{ trimprefix .Version "v" }} \
88+
--platform linux/{{ .Arch }} \
89+
-t v{{ trimprefix .Version "v" }}-{{ .Arch }} \
8990
./cmd
9091
fi'
9192
@@ -135,16 +136,6 @@ docker_manifests:
135136
kos:
136137
- id: cluster-api-runtime-extensions-nutanix
137138
build: cluster-api-runtime-extensions-nutanix
138-
ldflags:
139-
- -s
140-
- -w
141-
- -X 'k8s.io/component-base/version.buildDate={{ .CommitDate }}'
142-
- -X 'k8s.io/component-base/version.gitCommit={{ .FullCommit }}'
143-
- -X 'k8s.io/component-base/version.gitTreeState={{ .Env.GIT_TREE_STATE }}'
144-
- -X 'k8s.io/component-base/version.gitVersion=v{{ trimprefix .Version "v" }}'
145-
- -X 'k8s.io/component-base/version.major={{ .Major }}'
146-
- -X 'k8s.io/component-base/version.minor={{ .Minor }}'
147-
- -X 'k8s.io/component-base/version/verflag.programName={{ .ProjectName }}'
148139
labels:
149140
org.opencontainers.image.created: "{{ .CommitDate }}"
150141
org.opencontainers.image.title: "{{ .ProjectName }}"
@@ -156,6 +147,8 @@ kos:
156147
- linux/arm64
157148
repository: ghcr.io/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix
158149
bare: true
150+
creation_time: "{{.CommitTimestamp}}"
151+
ko_data_creation_time: "{{.CommitTimestamp}}"
159152
tags:
160153
- 'v{{ trimprefix .Version "v" }}'
161154

make/dev.mk

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ dev.run-on-kind: kind.create clusterctl.init
77
ifndef SKIP_BUILD
88
dev.run-on-kind: release-snapshot
99
endif
10+
dev.run-on-kind: SNAPSHOT_VERSION := $(shell gojq -r '.version+"-"+.runtime.goarch' dist/metadata.json)
1011
dev.run-on-kind:
1112
kind load docker-image --name $(KIND_CLUSTER_NAME) \
12-
ko.local/cluster-api-runtime-extensions-nutanix:$$(gojq -r .version dist/metadata.json) \
13-
ghcr.io/nutanix-cloud-native/caren-helm-reg:$$(gojq -r .version dist/metadata.json)-$(GOARCH)
13+
ko.local/cluster-api-runtime-extensions-nutanix:$(SNAPSHOT_VERSION) \
14+
ghcr.io/nutanix-cloud-native/caren-helm-reg:$(SNAPSHOT_VERSION)
1415
helm upgrade --install cluster-api-runtime-extensions-nutanix ./charts/cluster-api-runtime-extensions-nutanix \
1516
--set-string image.repository=ko.local/cluster-api-runtime-extensions-nutanix \
16-
--set-string image.tag=$$(gojq -r .version dist/metadata.json) \
17-
--set-string helmRepositoryImage.tag=$$(gojq -r .version dist/metadata.json)-$(GOARCH) \
17+
--set-string image.tag=$(SNAPSHOT_VERSION) \
18+
--set-string helmRepositoryImage.tag=$(SNAPSHOT_VERSION) \
1819
--wait --wait-for-jobs
1920
kubectl rollout restart deployment cluster-api-runtime-extensions-nutanix
2021
kubectl rollout restart deployment helm-repository
@@ -26,11 +27,12 @@ dev.update-webhook-image-on-kind: export KUBECONFIG := $(KIND_KUBECONFIG)
2627
ifndef SKIP_BUILD
2728
dev.update-webhook-image-on-kind: release-snapshot
2829
endif
30+
dev.update-webhook-image-on-kind: SNAPSHOT_VERSION := $(shell gojq -r '.version+"-"+.runtime.goarch' dist/metadata.json)
2931
dev.update-webhook-image-on-kind:
3032
kind load docker-image --name $(KIND_CLUSTER_NAME) \
31-
ko.local/cluster-api-runtime-extensions-nutanix:$$(gojq -r .version dist/metadata.json)
33+
ko.local/cluster-api-runtime-extensions-nutanix:$(SNAPSHOT_VERSION)
3234
kubectl set image deployment \
33-
cluster-api-runtime-extensions-nutanix webhook=ko.local/cluster-api-runtime-extensions-nutanix:$$(gojq -r .version dist/metadata.json)
35+
cluster-api-runtime-extensions-nutanix webhook=ko.local/cluster-api-runtime-extensions-nutanix:$(SNAPSHOT_VERSION)
3436
kubectl rollout restart deployment cluster-api-runtime-extensions-nutanix
3537
kubectl rollout status deployment cluster-api-runtime-extensions-nutanix
3638

make/go.mk

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
# to be private (not available publicly) and should therefore not use the proxy or checksum database
66
export GOPRIVATE ?=
77

8+
# Always ensure that GOOS and GOARCH are unset in the evironment, otherwise this can cause issues
9+
# with goreleaser and ko building images for the wrong platform.
10+
override undefine GOOS
11+
override undefine GOARCH
12+
813
ALL_GO_SUBMODULES := $(shell find -mindepth 2 -maxdepth 2 -name go.mod -printf '%P\n' | sort)
914
GO_SUBMODULES_NO_DOCS := $(filter-out $(addsuffix /go.mod,docs),$(ALL_GO_SUBMODULES))
1015
THIRD_PARTY_GO_SUBMODULES := $(shell find hack/third-party -mindepth 2 -name go.mod -printf 'hack/third-party/%P\n' | sort)
1116

12-
ifndef GOOS
13-
export GOOS := $(OS)
14-
endif
15-
ifndef GOARCH
16-
export GOARCH := $(shell go env GOARCH)
17-
endif
18-
1917
define go_test
2018
source <(setup-envtest use -p env $(ENVTEST_VERSION)) && \
2119
gotestsum \
@@ -91,7 +89,7 @@ ifneq ($(SKIP_BUILD),true)
9189
$(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
9290
endif
9391
$(info $(M) $(if $(filter $(E2E_DRYRUN), true),dry-,)running e2e tests$(if $(E2E_LABEL), labelled "$(E2E_LABEL)")$(if $(E2E_FOCUS), matching "$(E2E_FOCUS)"))
94-
env E2E_IMAGE_TAG="$$(gojq --raw-output '.version' $(REPO_ROOT)/dist/metadata.json)" \
92+
env E2E_IMAGE_TAG="$$(gojq --raw-output '.version+"-"+.runtime.goarch' $(REPO_ROOT)/dist/metadata.json)" \
9593
envsubst -no-unset -no-empty -i '$(E2E_CONF_FILE)' -o '$(E2E_CONF_FILE_ENVSUBST)'
9694
env AWS_B64ENCODED_CREDENTIALS="$$(clusterawsadm bootstrap credentials encode-as-profile 2>/dev/null)" \
9795
ginkgo run \

test/e2e/config/caren.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ managementClusterName: caren-e2e
66
images:
77
- name: ko.local/cluster-api-runtime-extensions-nutanix:${E2E_IMAGE_TAG}
88
loadBehavior: mustLoad
9-
- name: ghcr.io/nutanix-cloud-native/caren-helm-reg:${E2E_IMAGE_TAG}-${GOARCH}
9+
- name: ghcr.io/nutanix-cloud-native/caren-helm-reg:${E2E_IMAGE_TAG}
1010
loadBehavior: mustLoad
1111

1212
providers:

0 commit comments

Comments
 (0)