From f6847b829f24337a85640fa7133e5c4453746497 Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 12 Oct 2021 04:18:08 +0530 Subject: [PATCH 1/3] 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. Also, update github action for build to enable docker buildx. Signed-off-by: Sunny --- .github/workflows/build.yaml | 7 +++++++ Dockerfile | 15 +++++++++++++-- Makefile | 11 ++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b950895c..301f9984 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -12,6 +12,13 @@ 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 - name: Restore go cache uses: actions/cache@v1 with: diff --git a/Dockerfile b/Dockerfile index 8f260127..da59d14d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,16 @@ +ARG XX_VERSION=1.0.0-rc.2 + +FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx + # Build the manager binary -FROM golang:1.16-alpine as builder +FROM --platform=$BUILDPLATFORM golang:1.16-alpine AS builder + +# Copy the build utilities. +COPY --from=xx / / + +ARG TARGETPLATFORM +# Configure workspace. WORKDIR /workspace # copy modules manifests @@ -20,7 +30,8 @@ COPY controllers/ controllers/ COPY internal/ internal/ # build without giving the arch, so that it gets it from the machine -RUN CGO_ENABLED=0 go build -a -o image-reflector-controller main.go +ENV CGO_ENABLED=0 +RUN xx-go build -a -o image-reflector-controller main.go FROM alpine:3.13 diff --git a/Makefile b/Makefile index 2fb806ea..7abe1688 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,11 @@ CRD_OPTIONS ?= crd:crdVersions=v1 ENVTEST_BIN_VERSION?=1.22.0 KUBEBUILDER_ASSETS?=$(shell $(SETUP_ENVTEST) use -i $(ENVTEST_BIN_VERSION) -p path) +# Allows for defining additional Docker buildx arguments, e.g. '--push'. +BUILD_ARGS ?= +# Architectures to build images for. +BUILD_PLATFORMS ?= linux/amd64 + # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) GOBIN=$(shell go env GOPATH)/bin @@ -77,7 +82,11 @@ generate: controller-gen # Build the docker image docker-build: test - docker build . -t ${IMG} + docker buildx build \ + --platform=$(BUILD_PLATFORMS) \ + -t ${IMG} \ + --load \ + ${BUILD_ARGS} . # Push the docker image docker-push: From 00f05688e4100761e9ce4275f4d209df959b3a47 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 13 Oct 2021 21:06:27 +0530 Subject: [PATCH 2/3] workflows/build: Use buildkit local cache Signed-off-by: Sunny --- .github/workflows/build.yaml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 301f9984..63c02015 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -26,6 +26,14 @@ jobs: key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }} restore-keys: | ${{ runner.os }}-go- + - name: Cache Docker layers + uses: actions/cache@v2 + id: cache + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-ghcache-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-ghcache- - name: Set up Go uses: actions/setup-go@v2 with: @@ -35,4 +43,15 @@ jobs: - name: Run tests run: make test - name: Build container image - run: make docker-build IMG=ghcr.io/fluxcd/${{ github.event.repository.name }}:latest + run: | + make docker-build IMG=ghcr.io/fluxcd/${{ github.event.repository.name }}:latest \ + BUILD_PLATFORMS=linux/amd64 \ + BUILD_ARGS="--cache-from=type=local,src=/tmp/.buildx-cache \ + --cache-to=type=local,dest=/tmp/.buildx-cache-new,mode=max" + - # Temp fix + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + name: Move cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache From 331c404dee61672800bb902aeb1249425b9a5fdf Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 13 Oct 2021 21:07:42 +0530 Subject: [PATCH 3/3] Makefile: Remove test dependency on docker-build Prevent running the test twice in build workflow by removing the dependency on docker-build on test. Signed-off-by: Sunny --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7abe1688..3952478b 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,7 @@ generate: controller-gen cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..." # Build the docker image -docker-build: test +docker-build: docker buildx build \ --platform=$(BUILD_PLATFORMS) \ -t ${IMG} \