Skip to content

Commit 5e3df9e

Browse files
committed
Fix cloudbuild to include an extra tag
This commit improves the Makefile to allow more configurable build options. Also includes an EXTRA_TAG variable so automatic builds push images with branch name tags Signed-off-by: Max Cao <[email protected]>
1 parent 5356bd8 commit 5e3df9e

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

Makefile

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,28 @@ CONTROLLER_GEN = go run ${PROJECT_DIR}/vendor/sigs.k8s.io/controller-tools/cmd/c
2626
ARCH ?= $(shell go env GOARCH)
2727

2828
CONTAINER_RUNTIME ?= docker
29+
USE_DOCKER ?=
30+
ifeq ($(CONTAINER_RUNTIME), docker)
31+
USE_DOCKER = -docker
32+
endif
33+
2934
BUILDER_IMAGE ?=
30-
REGISTRY ?= gcr.io/k8s-staging-karpenter-cluster-api
31-
TAG ?= latest
32-
IMG ?= $(REGISTRY)/karpenter-clusterapi-controller:$(TAG)
35+
36+
BUILDX_CMD ?= $(CONTAINER_RUNTIME) buildx
37+
IMG_BUILD_CMD ?= $(BUILDX_CMD) build
38+
39+
IMG_REGISTRY ?= gcr.io/k8s-staging-karpenter-cluster-api
40+
IMG_NAME ?= karpenter-clusterapi-controller
41+
IMG_REPO ?= $(IMG_REGISTRY)/$(IMG_NAME)
42+
IMG_TAG ?= $(shell git describe --tags --dirty --always)
43+
IMG ?= $(IMG_REPO):$(IMG_TAG)
44+
45+
ifdef EXTRA_TAG
46+
IMG_EXTRA_TAG ?= $(IMG_REPO):$(EXTRA_TAG)
47+
endif
48+
ifdef IMG_EXTRA_TAG
49+
IMG_BUILD_EXTRA_OPTS += -t $(IMG_EXTRA_TAG)
50+
endif
3351

3452
all: help
3553

@@ -50,16 +68,28 @@ generate: gen-objects manifests ## generate all controller-gen files
5068
karpenter-clusterapi-controller: ## build the main karpenter controller
5169
go build -o bin/karpenter-clusterapi-controller cmd/controller/main.go
5270

53-
.PHONY: image
54-
image: ## Build manager container image
55-
$(CONTAINER_RUNTIME) build --build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) --build-arg ARCH=$(ARCH) . -t $(IMG)
56-
57-
.PHONY: image-push
58-
image-push: ## Push the manager container image to the container registry
59-
$(CONTAINER_RUNTIME) push $(IMG)
60-
61-
.PHONY: release-staging ## Build and push the staging image to the registry.
62-
release-staging: image image-push
71+
# https://github.com/containers/buildah/issues/4671
72+
# podman/buildah does not support "buildx build --push"
73+
# Remove all this extra logic when it does
74+
.PHONY: image-build
75+
image-build:
76+
$(IMG_BUILD_CMD) -t $(IMG) \
77+
--build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) \
78+
--build-arg ARCH=$(ARCH) \
79+
$(IMG_BUILD_EXTRA_OPTS) .
80+
$(CONTAINER_RUNTIME) push $(IMG) $(IMG_EXTRA_TAG)
81+
82+
.PHONY: image-build-docker
83+
image-build-docker:
84+
$(IMG_BUILD_CMD) -t $(IMG) \
85+
--build-arg BUILDER_IMAGE=$(BUILDER_IMAGE) \
86+
--build-arg ARCH=$(ARCH) \
87+
$(PUSH) \
88+
$(IMG_BUILD_EXTRA_OPTS) .
89+
90+
.PHONY: image-push # Push the manager container image to the container IMG_REGISTRY
91+
image-push: PUSH=--push
92+
image-push: image-build$(USE_DOCKER)
6393

6494
.PHONY: manifests
6595
manifests: ## generate the controller-gen kubernetes manifests

cloudbuild.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# this prevents errors if you don't use both _GIT_TAG and _PULL_BASE_REF,
22
# or any new substitutions added in the future.
33
options:
4-
substitution_option: ALLOW_LOOSE
4+
substitutionOption: ALLOW_LOOSE
55
steps:
66
- name: 'gcr.io/k8s-staging-test-infra/gcb-docker-gcloud:latest'
77
entrypoint: make
88
env:
9-
- TAG=$_GIT_TAG
9+
- IMG_REPO=gcr.io/k8s-staging-karpenter-cluster-api/karpenter-clusterapi-controller
10+
- IMG_TAG=$_GIT_TAG
11+
- EXTRA_TAG=$_PULL_BASE_REF
12+
- DOCKER_BUILDX_CMD=/buildx-entrypoint
1013
args:
11-
- release-staging
14+
- image-push
1215
substitutions:
1316
# _GIT_TAG will be filled with a git-based tag for the image, of the form vYYYYMMDD-hash, and
1417
# can be used as a substitution
1518
_GIT_TAG: '0.0.0'
1619
# _PULL_BASE_REF will contain the ref that was pushed to to trigger this build -
17-
# a branch like 'master' or 'release-0.2', or a tag like 'v0.2'.
20+
# a branch like 'main' or 'release-0.2', or a tag like 'v0.2'.
1821
_PULL_BASE_REF: 'main'
19-
images:
20-
- 'gcr.io/k8s-staging-karpenter-cluster-api/karpenter-clusterapi-controller:$_GIT_TAG'
22+
timeout: 1200s # 20 minutes

0 commit comments

Comments
 (0)