Skip to content

Commit beb9c75

Browse files
authored
build(makefile): add support for customizing image (#52)
- Replace static IMG variable with IMAGE_REGISTRY, IMAGE_REPOSITORY, and IMAGE_TAG for flexible image handling - Update docker-build, docker-push, and docker-buildx targets to use the new IMAGE variable - Modify build-installer target to set image using IMAGE variable instead of IMG Signed-off-by: Yi Chen <github@chenyicn.net>
1 parent 34331b8 commit beb9c75

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

Makefile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
# Image URL to use all building/pushing image targets
2-
IMG ?= controller:latest
3-
41
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
52
ifeq (,$(shell go env GOBIN))
63
GOBIN=$(shell go env GOPATH)/bin
74
else
85
GOBIN=$(shell go env GOBIN)
96
endif
107

8+
# Image registry (e.g. `docker.io`)
9+
IMAGE_REGISTRY ?= docker.io
10+
# Image repository (e.g. `my-repo/my-image`).
11+
IMAGE_REPOSITORY ?= cron-operator
12+
# Image tag (e.g. `latest`).
13+
IMAGE_TAG ?= latest
14+
# Image URL to use all building/pushing image targets
15+
IMAGE ?= $(IMAGE_REPOSITORY):$(IMAGE_TAG)
16+
1117
# CONTAINER_TOOL defines the container tool to be used for building images.
1218
# Be aware that the target commands are only tested with Docker which is
1319
# scaffolded by default. However, you might want to replace it to use other
@@ -117,11 +123,11 @@ run: manifests generate fmt vet ## Run a controller from your host.
117123
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
118124
.PHONY: docker-build
119125
docker-build: ## Build docker image with the manager.
120-
$(CONTAINER_TOOL) build -t ${IMG} .
126+
$(CONTAINER_TOOL) build -t ${IMAGE} .
121127

122128
.PHONY: docker-push
123129
docker-push: ## Push docker image with the manager.
124-
$(CONTAINER_TOOL) push ${IMG}
130+
$(CONTAINER_TOOL) push ${IMAGE}
125131

126132
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
127133
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
@@ -136,14 +142,14 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform
136142
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
137143
- $(CONTAINER_TOOL) buildx create --name cron-operator-builder
138144
$(CONTAINER_TOOL) buildx use cron-operator-builder
139-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
145+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMAGE} -f Dockerfile.cross .
140146
- $(CONTAINER_TOOL) buildx rm cron-operator-builder
141147
rm Dockerfile.cross
142148

143149
.PHONY: build-installer
144150
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
145151
mkdir -p dist
146-
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${IMG}
152+
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${IMAGE}
147153
"$(KUSTOMIZE)" build config/default > dist/install.yaml
148154

149155
##@ Helm
@@ -174,7 +180,7 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified
174180

175181
.PHONY: deploy
176182
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
177-
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${IMG}
183+
cd config/manager && "$(KUSTOMIZE)" edit set image controller=${IMAGE}
178184
"$(KUSTOMIZE)" build config/default | "$(KUBECTL)" apply -f -
179185

180186
.PHONY: undeploy

test/e2e/e2e_suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestE2E(t *testing.T) {
5757

5858
var _ = BeforeSuite(func() {
5959
By("building the manager(Operator) image")
60-
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectImage))
60+
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMAGE=%s", projectImage))
6161
_, err := utils.Run(cmd)
6262
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to build the manager(Operator) image")
6363

test/e2e/e2e_test.go

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

7171
By("deploying the controller-manager")
72-
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectImage))
72+
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMAGE=%s", projectImage))
7373
_, err = utils.Run(cmd)
7474
Expect(err).NotTo(HaveOccurred(), "Failed to deploy the controller-manager")
7575
})

0 commit comments

Comments
 (0)