From 63b337f3fb3b6b0be0c89b249ae01d1d25a5702e Mon Sep 17 00:00:00 2001 From: Shachar Sharon Date: Mon, 17 Apr 2023 14:05:17 +0300 Subject: [PATCH] makefile: build multi-arch image Build multi-architecture image using buildah and qemu static utilities. Creates an image with manifest which refs amd64 (x86_64) and arm64 (aarch64) architecture-specific sub-images. Using the same Dockerfile regardless of the actual CPU architecture, and let buildah+qemu do all the low-level logic. Note: typically, qemu would emulate arm64 on x86_64 which yields longer build time. Signed-off-by: Shachar Sharon --- Makefile | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Makefile b/Makefile index b157b1f9..2beec538 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,9 @@ BUNDLE_METADATA_OPTS?=$(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL) COMMIT_ID=$(shell git describe --abbrev=40 --always --exclude='*' --dirty=+ 2>/dev/null) GIT_VERSION=$(shell git describe --match='v[0-9]*.[0-9]' --match='v[0-9]*.[0-9].[0-9]' 2>/dev/null || echo "(unset)") +# List of supported CPU architectures +ARCH_LIST:=amd64 arm64 + CONFIG_KUST_DIR:=config/default CRD_KUST_DIR:=config/crd MGR_KUST_DIR:=config/manager @@ -187,6 +190,28 @@ image-build-buildah: build $(BUILDAH_CMD) config --entrypoint='["/manager"]' $$cn && \ $(BUILDAH_CMD) commit $$cn $(IMG) +.PHONY: image-build-multiarch +image-build-multiarch: qemu-utils + $(BUILDAH_CMD) manifest create $(IMG) + for arch in $(ARCH_LIST); do \ + $(BUILDAH_CMD) bud \ + --manifest $(IMG) \ + --arch $${arch} \ + --tag "$(IMG)-$${arch}" \ + --build-arg=GIT_VERSION="$(GIT_VERSION)" \ + --build-arg=COMMIT_ID="$(COMMIT_ID)" \ + --build-arg=ARCH="$${arch}" . ; \ + done + $(BUILDAH_CMD) manifest inspect $(IMG) + +.PHONY: image-push-multiarch +image-push-multiarch: + $(BUILDAH_CMD) manifest push --all $(IMG) "docker://$(IMG)" + +.PHONY: image-multiarch +image-multiarch: image-build-multiarch image-push-multiarch + + # Push the container image docker-push: container-push container-push: @@ -322,3 +347,12 @@ GITLINT=$(GOBIN_ALT)/gitlint else GITLINT=$(shell command -v gitlint ;) endif + +.PHONY: qemu-utils +qemu-utils: +ifeq (, $(shell command -v qemu-x86_64-static ;)) + $(error "qemu-x86_64-static not found in PATH") +endif +ifeq (, $(shell command -v qemu-aarch64-static ;)) + $(error "qemu-aarch64-static not found in PATH") +endif