From 119e3c3854ee4339cc02d7ce9b9ab9adaec7c5da Mon Sep 17 00:00:00 2001 From: Dinar Valeev Date: Mon, 25 Jan 2021 17:48:13 +0100 Subject: [PATCH] Enable docker multiarch build Build docker image for aarch64 and combine them into a single manifest Signed-off-by: Dinar Valeev --- .github/workflows/release.yaml | 5 +++-- Dockerfile | 4 +++- Makefile | 35 ++++++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 7ffcfa8de8..e1bdc28c63 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -19,8 +19,9 @@ jobs: run: | export PATH=~/go/bin:$PATH docker login -u "${DOCKER_USER}" -p "${DOCKER_PASSWORD}" - make docker-build - make docker-push + make docker-build-multi + make docker-push-multi + make docker-manifest - uses: dave-mcconnell/helm-gh-pages-microservices@master with: access-token: ${{ secrets.CR_TOKEN }} diff --git a/Dockerfile b/Dockerfile index f6903ee493..fb6f0ea999 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,8 @@ FROM golang:1.15 as builder WORKDIR /workspace +ARG GOARCH=amd64 + # Copy the Go Modules manifests COPY go.mod go.mod COPY go.sum go.sum @@ -15,7 +17,7 @@ COPY api/ api/ COPY controllers/ controllers/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go +RUN CGO_ENABLED=0 GOARCH=${GOARCH} GOOS=linux GO111MODULE=on go build -a -o manager main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details diff --git a/Makefile b/Makefile index 1d408d26c8..869d3694b2 100644 --- a/Makefile +++ b/Makefile @@ -165,15 +165,38 @@ dns-tools: ## Run temporary dnstools pod for debugging DNS issues dns-smoke-test: kubectl -n k8gb run -it --rm --restart=Never --image=infoblox/dnstools:latest dnstools --command -- /usr/bin/dig @k8gb-coredns roundrobin.cloud.example.com +# build docker images for multiple architectures +# useful for CI +.PHONY: docker-build-multi +docker-build-multi: test + $(call docker-build-arch,amd64) + $(call docker-build-arch,arm64) + +# push docker for multiple architectures +.PHONY: docker-push-multi +docker-push-multi: + $(call docker-push-arch,amd64) + $(call docker-push-arch,arm64) + +# create and push docker manifest +.PHONY: docker-manifest +docker-manifest: docker-push-multi + docker manifest create ${IMG} \ + ${IMG}-amd64 \ + ${IMG}-arm64 + docker manifest annotate ${IMG} ${IMG}-arm64 \ + --os linux --arch arm64 + docker manifest push ${IMG} + # build the docker image .PHONY: docker-build docker-build: test - docker build . -t $(IMG) + $(call docker-build-arch,amd64) # push the docker image .PHONY: docker-push docker-push: - docker push $(IMG) + $(call docker-push-arch,amd64) # build and push the docker image exclusively for testing using commit hash .PHONY: docker-test-build-push @@ -412,6 +435,14 @@ define install-kustomize $(eval KUSTOMIZE_PATH = $(GOBIN)/kustomize) endef +define docker-build-arch + docker build --build-arg GOARCH=${1} . -t ${IMG}-${1} +endef + +define docker-push-arch + docker push ${IMG}-${1} +endef + define docker-test-build-push docker build . -t k8gb:$(COMMIT_HASH) docker tag k8gb:$(COMMIT_HASH) $(REPO):v$(COMMIT_HASH)