Skip to content

Commit

Permalink
Switch to scratch based libgit2 container image
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hiddeco committed Oct 7, 2021
1 parent b874488 commit 1b6c353
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 44 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand Down
24 changes: 9 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -178,7 +172,7 @@ ifeq (1, $(LIBGIT2_FORCE))
@{ \
set -e; \
mkdir -p $(LIBGIT2_PATH); \
docker cp $(shell docker create --rm $(BASE_IMG):$(BASE_TAG)):/libgit2/Makefile $(LIBGIT2_PATH); \
docker cp $(shell docker create --rm $(LIBGIT2_IMG):$(LIBGIT2_TAG)):/Makefile $(LIBGIT2_PATH); \
INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH); \
}
endif
Expand Down

0 comments on commit 1b6c353

Please sign in to comment.