From 2193b6350b83bfa4c741c9e8b8ff4888593d8936 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 13 Oct 2021 00:52:00 +0530 Subject: [PATCH 1/2] Use tonistiigi/xx for cross-platform builds Update the Dockerfile to use tonistiigi/xx to cross-compile the manager binaries natively and speed up the multi-arch image build time. Signed-off-by: Sunny --- Dockerfile | 14 ++++++++++++-- Makefile | 10 +++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6bd20ea3..9db5450d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,13 @@ -FROM golang:1.16-alpine as builder +ARG XX_VERSION=1.0.0-rc.2 + +FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx + +FROM --platform=$BUILDPLATFORM golang:1.16-alpine as builder + +# Copy the build utilities. +COPY --from=xx / / + +ARG TARGETPLATFORM WORKDIR /workspace @@ -18,7 +27,8 @@ COPY controllers/ controllers/ COPY internal/ internal/ # build -RUN CGO_ENABLED=0 go build -a -o kustomize-controller main.go +ENV CGO_ENABLED=0 +RUN xx-go build -a -o kustomize-controller main.go FROM alpine:3.14 diff --git a/Makefile b/Makefile index 09107ce2..5dd33007 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,11 @@ else GOBIN=$(shell go env GOBIN) endif +# Allows for defining additional Docker buildx arguments, e.g. '--push'. +BUILD_ARGS ?= +# Architectures to build images for. +BUILD_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7 + all: manager # Download the envtest binaries to testbin @@ -94,7 +99,10 @@ generate: controller-gen # Build the docker image docker-build: - docker build . -t ${IMG} + docker buildx build \ + --platform=$(BUILD_PLATFORMS) \ + -t ${IMG} \ + ${BUILD_ARGS} . # Push the docker image docker-push: From 8d89ab20be55d1be70683e910f55da317bf4cc39 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 13 Oct 2021 00:53:15 +0530 Subject: [PATCH 2/2] Update github actions to work with new builds e2e: - Enable buildx. - Build only for linux/amd64 and export the built image. Signed-off-by: Sunny --- .github/workflows/e2e.yaml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 0defe586..1e412814 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -12,6 +12,15 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + - name: Setup QEMU + uses: docker/setup-qemu-action@v1 + with: + platforms: all + - name: Setup Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v1 + with: + buildkitd-flags: "--debug" - name: Restore Go cache uses: actions/cache@v1 with: @@ -44,7 +53,7 @@ jobs: exit 1 fi - name: Build container image - run: make docker-build IMG=test/kustomize-controller:latest + run: make docker-build IMG=test/kustomize-controller:latest BUILD_PLATFORMS=linux/amd64 BUILD_ARGS=--load - name: Load test image run: kind load docker-image test/kustomize-controller:latest - name: Install CRDs