From d04c532461c894b556433a47a5c9f6de8ccf934f Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Thu, 7 Oct 2021 17:46:54 +0200 Subject: [PATCH] Switch to scratch based libgit2 container image This moves the `libgit2` compilation to the image, to ensure it can be build on builders that aren't backed by AMD64. The image is structured in such a way that e.g. running nightly builds targeting a different Go version, or targeting a different OS vendor would be possible in the future via build arguments. Signed-off-by: Hidde Beydals --- .github/workflows/e2e.yaml | 2 +- Dockerfile | 48 ++++++++++++++++++++++++++++++++++---- Makefile | 26 ++++++++------------- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index b75e7059..12472092 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -44,7 +44,7 @@ jobs: exit 1 fi - name: Build container image - run: make docker-build IMG=test/source-controller TAG=latest + run: make docker-build IMG=test/source-controller TAG=latest BUILD_PLATFORMS=linux/amd64 BUILD_ARGS=--load - name: Load test image run: kind load docker-image test/source-controller:latest - name: Deploy controller diff --git a/Dockerfile b/Dockerfile index 68d65124..2f03fd84 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,37 @@ -ARG BASE_IMG=ghcr.io/fluxcd/golang-with-libgit2 -ARG BASE_TAG=1.16.8-bullseye-libgit2-1.1.1-1 -FROM ${BASE_IMG}:${BASE_TAG} AS build +ARG BASE_VARIANT=bullseye +ARG GO_VERSION=1.16.8 +ARG XX_VERSION=1.0.0-rc.2 + +ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2 +ARG LIBGIT2_TAG=libgit2-1.1.1 + +FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx +FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} as libgit2 + +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable +FROM --platform=$BUILDPLATFORM golang:1.17rc1-${BASE_VARIANT} AS golatest + +FROM gostable AS go-linux + +FROM go-${TARGETOS} AS build-base-bullseye + +# Copy the build utiltiies +COPY --from=xx / / +COPY --from=libgit2 /Makefile /libgit2/ + +# Install the libgit2 build dependencies +RUN make -C /libgit2 cmake + +ARG TARGETPLATFORM +RUN make -C /libgit2 dependencies + +FROM build-base-${BASE_VARIANT} as libgit2-bullseye + +# Compile and install libgit2 +ARG TARGETPLATFORM +RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2 + +FROM libgit2-${BASE_VARIANT} as build-bullseye # Configure workspace WORKDIR /workspace @@ -27,7 +58,16 @@ ARG TARGETPLATFORM RUN xx-go build -o source-controller -trimpath \ main.go -FROM debian:bullseye-slim as controller +FROM build-${BASE_VARIANT} as prepare-bullseye + +# Move libgit2 lib to generic and predictable location +ARG TARGETPLATFORM +RUN mkdir -p /libgit2/lib/ \ + && cp -d /usr/lib/$(xx-info triple)/libgit2.so* /libgit2/lib/ + +FROM prepare-${BASE_VARIANT} as build + +FROM debian:${BASE_VARIANT}-slim as controller # Link repo to the GitHub Container Registry image LABEL org.opencontainers.image.source="https://github.com/fluxcd/source-controller" diff --git a/Makefile b/Makefile index 63e65106..47243bd2 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,14 @@ IMG ?= fluxcd/source-controller TAG ?= latest # Base image used to build the Go binary -BASE_IMG ?= ghcr.io/fluxcd/golang-with-libgit2 -BASE_TAG ?= 1.16.8-bullseye-libgit2-1.1.1-1 +LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2 +LIBGIT2_TAG ?= libgit2-1.1.1 # Allows for defining additional Docker buildx arguments, # e.g. '--push'. -BUILDX_ARGS ?= +BUILD_ARGS ?= # Architectures to build images for -BUILDX_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7 +BUILD_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7 # Produce CRDs that work back to Kubernetes 1.16 CRD_OPTIONS ?= crd:crdVersions=v1 @@ -110,18 +110,12 @@ generate: controller-gen ## Generate API code cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..." docker-build: ## Build the Docker image - docker build \ - --build-arg BASE_IMG=$(BASE_IMG) \ - --build-arg BASE_TAG=$(BASE_TAG) \ - -t $(IMG):$(TAG) . - -docker-buildx: ## Build the cross-platform Docker image docker buildx build \ - --build-arg BASE_IMG=$(BASE_IMG) \ - --build-arg BASE_TAG=$(BASE_TAG) \ - --platform=$(BUILDX_PLATFORMS) \ + --build-arg LIBGIT2_IMG=$(LIBGIT2_IMG) \ + --build-arg LIBGIT2_TAG=$(LIBGIT2_TAG) \ + --platform=$(BUILD_PLATFORMS) \ -t $(IMG):$(TAG) \ - $(BUILDX_ARGS) . + $(BUILD_ARGS) . docker-push: ## Push Docker image docker push $(IMG):$(TAG) @@ -178,8 +172,8 @@ ifeq (1, $(LIBGIT2_FORCE)) @{ \ set -e; \ mkdir -p $(LIBGIT2_PATH); \ - docker cp $(shell docker create --rm $(BASE_IMG):$(BASE_TAG)):/libgit2/Makefile $(LIBGIT2_PATH); \ - INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH); \ + curl -sL https://raw.githubusercontent.com/fluxcd/golang-with-libgit2/$(LIBGIT2_TAG)/hack/Makefile -o $(LIBGIT2_PATH)/Makefile; \ + INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH) libgit2; \ } endif