Skip to content

Commit cacb877

Browse files
authored
Use ko, build amd64 + arm64 controller images (#95)
* Use ko, build amd64 + arm64 controller images * make ko-build always builds for the local platform --------- Signed-off-by: Angelos Kolaitis <neoaggelos@gmail.com>
1 parent aacd098 commit cacb877

File tree

4 files changed

+39
-76
lines changed

4 files changed

+39
-76
lines changed

.github/workflows/release.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ jobs:
1717
- name: Check out code
1818
uses: actions/checkout@v4
1919

20-
- name: Log in to the Container registry
21-
uses: docker/login-action@v3.2.0
22-
with:
23-
registry: https://ghcr.io
24-
username: ${{ github.actor }}
25-
password: ${{ secrets.GITHUB_TOKEN }}
20+
- name: Login
21+
run: |
22+
make ko-login USERNAME=${{ github.actor }} PASSWORD=${{ secrets.GITHUB_TOKEN }}
2623
27-
- name: Build and push image
24+
- name: Build image
2825
run: |
29-
make docker-build TAG=${{ github.ref_name }}
30-
make docker-push TAG=${{ github.ref_name }}
26+
make ko-push TAG=${{ github.ref_name }}
3127
3228
- name: Build release assets
3329
run: make dist TAG=${{ github.ref_name }}

Dockerfile

Lines changed: 0 additions & 33 deletions
This file was deleted.

Makefile

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
# Image URL to use all building/pushing image targets
22
TAG ?= latest
3-
IMG ?= ghcr.io/lxc/cluster-api-provider-incus:$(TAG)
3+
REGISTRY ?= ghcr.io
4+
REPO ?= $(REGISTRY)/lxc/cluster-api-provider-incus
5+
IMG ?= $(REPO):$(TAG)
6+
7+
# Used by ko build
8+
export KO_DOCKER_REPO = $(REPO)
9+
10+
# PLATFORMS defines the target platforms for the manager image be built.
11+
PLATFORMS ?= linux/arm64,linux/amd64
12+
LOCAL_PLATFORM ?= linux/$(shell go env GOARCH)
413

514
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
615
ENVTEST_K8S_VERSION = 1.31.0
@@ -94,8 +103,8 @@ _SKIP_ARGS := $(foreach arg,$(strip $(E2E_GINKGO_SKIP)),-skip="$(arg)")
94103
endif
95104

96105
.PHONY: e2e-image
97-
e2e-image: IMG = ghcr.io/lxc/cluster-api-provider-incus:e2e ## Build controller image for e2e tests
98-
e2e-image: docker-build
106+
e2e-image: TAG = e2e ## Build controller image for e2e tests
107+
e2e-image: ko-build
99108

100109
E2E_DATA_DIR ?= $(REPO_ROOT)/test/e2e/data
101110
ARTIFACTS ?= $(REPO_ROOT)/_artifacts
@@ -154,33 +163,17 @@ V ?= 0
154163
run: manifests generate fmt vet ## Run a controller from your host.
155164
go run ./cmd/main.go --diagnostics-address=":" --v=${V}
156165

157-
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
158-
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
159-
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
160-
.PHONY: docker-build
161-
docker-build: ## Build docker image with the manager.
162-
$(CONTAINER_TOOL) build -t ${IMG} .
163-
164-
.PHONY: docker-push
165-
docker-push: ## Push docker image with the manager.
166-
$(CONTAINER_TOOL) push ${IMG}
167-
168-
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
169-
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
170-
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
171-
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
172-
# - 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)
173-
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
174-
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
175-
.PHONY: docker-buildx
176-
docker-buildx: ## Build and push docker image for the manager for cross-platform support
177-
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
178-
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
179-
- $(CONTAINER_TOOL) buildx create --name test-builder
180-
$(CONTAINER_TOOL) buildx use test-builder
181-
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
182-
- $(CONTAINER_TOOL) buildx rm test-builder
183-
rm Dockerfile.cross
166+
.PHONY: ko-build
167+
ko-build: ko ## Build manager image and load to local docker instance.
168+
$(KO) build ./cmd --bare --tags $(TAG) --sbom=none --platform=$(LOCAL_PLATFORM) --local
169+
170+
.PHONY: ko-login
171+
ko-login: ko ## Configure credentials for pushing images (needs USERNAME and PASSWORD).
172+
echo $(PASSWORD) | $(KO) login $(REGISTRY) --username $(USERNAME) --password-stdin
173+
174+
.PHONY: ko-push
175+
ko-push: ko ## Build and push manager image.
176+
$(KO) build ./cmd --bare --tags $(TAG) --sbom=none --platform=$(PLATFORMS)
184177

185178
##@ Release
186179

@@ -231,15 +224,17 @@ KUBECTL ?= kubectl
231224
KUSTOMIZE ?= $(LOCALBIN)/kustomize
232225
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
233226
ENVTEST ?= $(LOCALBIN)/setup-envtest
234-
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
235-
GINKGO = $(LOCALBIN)/ginkgo
236-
IMAGE_BUILDER = $(LOCALBIN)/image-builder
227+
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
228+
GINKGO ?= $(LOCALBIN)/ginkgo
229+
KO ?= $(LOCALBIN)/ko
230+
IMAGE_BUILDER ?= $(LOCALBIN)/image-builder
237231

238232
## Tool Versions
239233
KUSTOMIZE_VERSION ?= v5.5.0
240234
CONTROLLER_TOOLS_VERSION ?= v0.16.4
241235
ENVTEST_VERSION ?= release-0.19
242236
GOLANGCI_LINT_VERSION ?= v1.61.0
237+
KO_VERSION ?= v0.18.0
243238

244239
.PHONY: image-builder
245240
image-builder: $(LOCALBIN)
@@ -270,6 +265,11 @@ ginkgo: $(GINKGO) ## Download ginkgo locally if necessary.
270265
$(GINKGO): $(LOCALBIN)
271266
go build -o $@ github.com/onsi/ginkgo/v2/ginkgo
272267

268+
.PHONY: ko
269+
ko: $(KO) ## Download ko locally if necessary.
270+
$(KO): $(LOCALBIN)
271+
$(call go-install-tool,$(KO),github.com/google/ko,$(KO_VERSION))
272+
273273
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
274274
# $1 - target path with name of binary
275275
# $2 - package url which can be installed

config/manager/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ spec:
5959
# type: RuntimeDefault
6060
containers:
6161
- command:
62-
- /manager
62+
- /ko-app/cmd
6363
args:
6464
- --leader-elect
6565
- --health-addr=:9440

0 commit comments

Comments
 (0)