Skip to content

Commit 3157db1

Browse files
authored
Merge pull request #39 from kubernetes-sigs/setup-auto-build-publish-images
setup auto-build/publish setup with cloudbuild
2 parents 1331fa0 + 3562682 commit 3157db1

File tree

7 files changed

+59
-19
lines changed

7 files changed

+59
-19
lines changed

Makefile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ CONTROLLER_GEN := $(abspath $(TOOLS_BIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_
5959
CONTROLLER_GEN_PKG := sigs.k8s.io/controller-tools/cmd/controller-gen
6060

6161
# Image URL to use all building/pushing image targets
62-
IMG ?= controller:latest
62+
IMG_PREFIX ?= controller
63+
IMG_TAG ?= latest
64+
6365

6466
# Default value for ignore-not-found flag in undeploy target
6567
ignore-not-found ?= true
@@ -170,17 +172,17 @@ run: manifests generate fmt vet ## Run a controller from your host.
170172
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
171173
.PHONY: docker-build
172174
docker-build: ## Build docker image with the manager.
173-
$(CONTAINER_TOOL) build -t ${IMG} .
175+
$(CONTAINER_TOOL) build -t ${IMG_PREFIX}:${IMG_TAG} .
174176

175177
.PHONY: docker-push
176178
docker-push: ## Push docker image with the manager.
177-
$(CONTAINER_TOOL) push ${IMG}
179+
$(CONTAINER_TOOL) push ${IMG_PREFIX}:${IMG_TAG}
178180

179181
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
180-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
182+
# architectures. (i.e. make docker-buildx IMG_PREFIX=myregistry/mypoperator IMG_TAG=0.0.1). To use this option you need to:
181183
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
182184
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
183-
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
185+
# - be able to push the image to your registry (i.e. if you do not set a valid value via ${IMG_PREFIX}:${IMG_TAG} then the export will fail)
184186
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
185187
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
186188
.PHONY: docker-buildx
@@ -189,14 +191,14 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
189191
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
190192
- $(CONTAINER_TOOL) buildx create --name nrrcontroller-builder
191193
$(CONTAINER_TOOL) buildx use nrrcontroller-builder
192-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
194+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG_PREFIX}:${IMG_TAG} -f Dockerfile.cross .
193195
- $(CONTAINER_TOOL) buildx rm nrrcontroller-builder
194196
rm Dockerfile.cross
195197

196198
.PHONY: build-installer
197199
build-installer: manifests generate $(KUSTOMIZE) ## Generate a consolidated YAML with CRDs and deployment.
198200
mkdir -p dist
199-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
201+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG_PREFIX}:${IMG_TAG}
200202
$(KUSTOMIZE) build config/default > dist/install.yaml
201203

202204
## --------------------------------------
@@ -221,7 +223,7 @@ uninstall: manifests $(KUSTOMIZE) ## Uninstall CRDs from the K8s cluster specifi
221223

222224
.PHONY: deploy
223225
deploy: manifests $(KUSTOMIZE) ## Deploy controller to the K8s cluster specified in ~/.kube/config.
224-
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
226+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG_PREFIX}:${IMG_TAG}
225227
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
226228

227229
.PHONY: undeploy

cloudbuild.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://cloud.google.com/cloud-build/docs/build-config
2+
3+
options:
4+
substitution_option: ALLOW_LOOSE
5+
steps:
6+
- name: gcr.io/k8s-staging-test-infra/gcb-docker-gcloud:v20251211-3eba3d0954
7+
entrypoint: ./scripts/build-and-publish-image.sh
8+
env:
9+
- IMG_PREFIX=us-central1-docker.pkg.dev/k8s-staging-images/node-readiness-controller

docs/TEST_README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Build the controller image and load it into the Kind cluster nodes.
4040

4141
```bash
4242
# Build the image
43-
make docker-build IMG=controller:latest
43+
make docker-build IMG_PREFIX=controller IMG_TAG=latest
4444

4545
# Load the image into the kind cluster
4646
kind load docker-image controller:latest --name nrr-test
@@ -50,7 +50,7 @@ kind load docker-image controller:latest --name nrr-test
5050

5151
Deploy the controller image to nrr-test-worker
5252
```bash
53-
make deploy IMG=controller:latest
53+
make deploy IMG_PREFIX=controller IMG_TAG=latest
5454
```
5555

5656
Verify the controller is running on the platform node (`nrr-test-worker`):
@@ -128,4 +128,4 @@ This section tests how the controller handles new nodes being added to the clust
128128

129129
```bash
130130
kind delete cluster --name nrr-test
131-
```
131+
```

docs/getting-started.draft.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ spec:
4747

4848
### Deployment
4949

50-
**Build and push your image to the location specified by `IMG`:**
50+
**Build and push your image to the location specified by `IMG_PREFIX`:`IMG_TAG` :**
5151

5252
```sh
53-
make docker-build docker-push IMG=<some-registry>/nrr-controller:tag
53+
make docker-build docker-push IMG_PREFIX=<some-registry>/nrr-controller IMG_TAG=tag
5454
```
5555

5656
#### Option 1: Deploy Using Make Commands
@@ -60,7 +60,7 @@ make docker-build docker-push IMG=<some-registry>/nrr-controller:tag
6060
make install
6161
6262
# Deploy the controller
63-
make deploy IMG=<some-registry>/nrr-controller:tag
63+
make deploy IMG_PREFIX=<some-registry>/nrr-controller IMG_TAG=tag
6464
6565
# Create sample rules
6666
kubectl apply -k examples/network-readiness-rule.yaml

scripts/build-and-publish-image.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
5+
REPO_ROOT=$(git rev-parse --show-toplevel)
6+
cd "$REPO_ROOT"
7+
8+
if [[ -z ${IMG_PREFIX:-} ]]; then
9+
echo "IMG_PREFIX is not set"
10+
exit 1
11+
fi
12+
13+
if [[ -z ${IMG_TAG:-} ]]; then
14+
# Use a tag if the current commit is a tag, otherwise use a date+git-hash tag
15+
if git describe --exact-match --tags HEAD >/dev/null 2>&1; then
16+
IMG_TAG=$(git describe --exact-match --tags HEAD)
17+
else
18+
IMG_TAG="$(date +v%Y%m%d)-$(git rev-parse --short HEAD)"
19+
fi
20+
fi
21+
echo "Using IMG_TAG=${IMG_TAG}"
22+
23+
IMG_TAG=${IMG_TAG} IMG_PREFIX=${IMG_PREFIX%/} make docker-build
24+
25+
IMG_TAG=${IMG_TAG} IMG_PREFIX=${IMG_PREFIX%/} make docker-push

test/e2e/e2e_suite_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ var (
4040
// isCertManagerAlreadyInstalled will be set true when CertManager CRDs be found on the cluster
4141
isCertManagerAlreadyInstalled = false
4242

43-
// projectImage is the name of the image which will be build and loaded
43+
// imagePrefix is the name of the image which will be build and loaded
4444
// with the code source changes to be tested.
45-
projectImage = "controller:latest"
45+
imagePrefix = "controller"
46+
// imageTag is the tag of the image which will be build and loaded
47+
// with the code source changes to be tested.
48+
imageTag = "latest"
4649
)
4750

4851
// TestE2E runs the end-to-end (e2e) test suite for the project. These tests execute in an isolated,
@@ -57,14 +60,15 @@ func TestE2E(t *testing.T) {
5760

5861
var _ = BeforeSuite(func() {
5962
By("building the manager(Operator) image")
60-
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
63+
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG_PREFIX=%s", imagePrefix), fmt.Sprintf("IMG_TAG=%s", imageTag))
6164
_, err := utils.Run(cmd)
6265
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")
6366

6467
// TODO(user): If you want to change the e2e test vendor from Kind, ensure the image is
6568
// built and available before running the tests. Also, remove the following block.
6669
By("loading the manager(Operator) image on Kind")
67-
err = utils.LoadImageToKindClusterWithName(projectImage)
70+
//err = utils.LoadImageToKindClusterWithName(projectImage)
71+
err = utils.LoadImageToKindClusterWithName(fmt.Sprintf("%s:%s", imagePrefix, imageTag))
6872
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager(Operator) image into Kind")
6973

7074
// The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing.

test/e2e/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var _ = Describe("Manager", Ordered, func() {
7070
Expect(err).NotTo(HaveOccurred(), "Failed to install CRDs")
7171

7272
By("deploying the controller-manager")
73-
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
73+
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG_PREFIX=%s", imagePrefix), fmt.Sprintf("IMG_TAG=%s", imageTag))
7474
_, err = utils.Run(cmd)
7575
Expect(err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
7676

0 commit comments

Comments
 (0)