Skip to content

Commit 9fd16d8

Browse files
committed
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 <[email protected]>
1 parent b874488 commit 9fd16d8

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

.github/workflows/e2e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
exit 1
4545
fi
4646
- name: Build container image
47-
run: make docker-build IMG=test/source-controller TAG=latest
47+
run: make docker-build IMG=test/source-controller TAG=latest BUILD_PLATFORMS=linux/amd64 BUILD_ARGS=--load
4848
- name: Load test image
4949
run: kind load docker-image test/source-controller:latest
5050
- name: Deploy controller

Dockerfile

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
ARG BASE_IMG=ghcr.io/fluxcd/golang-with-libgit2
2-
ARG BASE_TAG=1.16.8-bullseye-libgit2-1.1.1-1
3-
FROM ${BASE_IMG}:${BASE_TAG} AS build
1+
ARG BASE_VARIANT=bullseye
2+
ARG GO_VERSION=1.16.8
3+
ARG XX_VERSION=1.0.0-rc.2
4+
5+
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2
6+
ARG LIBGIT2_TAG=libgit2-1.1.1
7+
8+
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
9+
FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} as libgit2
10+
11+
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
12+
FROM --platform=$BUILDPLATFORM golang:1.17rc1-${BASE_VARIANT} AS golatest
13+
14+
FROM gostable AS go-linux
15+
16+
FROM go-${TARGETOS} AS build-base-bullseye
17+
18+
# Copy the build utiltiies
19+
COPY --from=xx / /
20+
COPY --from=libgit2 /Makefile /libgit2/
21+
22+
# Install the libgit2 build dependencies
23+
RUN make -C /libgit2 cmake
24+
25+
ARG TARGETPLATFORM
26+
RUN make -C /libgit2 dependencies
27+
28+
FROM build-base-${BASE_VARIANT} as libgit2-bullseye
29+
30+
# Compile and install libgit2
31+
ARG TARGETPLATFORM
32+
RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2
33+
34+
FROM libgit2-${BASE_VARIANT} as build-bullseye
435

536
# Configure workspace
637
WORKDIR /workspace
@@ -27,7 +58,16 @@ ARG TARGETPLATFORM
2758
RUN xx-go build -o source-controller -trimpath \
2859
main.go
2960

30-
FROM debian:bullseye-slim as controller
61+
FROM build-${BASE_VARIANT} as prepare-bullseye
62+
63+
# Move libgit2 lib to generic and predictable location
64+
ARG TARGETPLATFORM
65+
RUN mkdir -p /libgit2/lib/ \
66+
&& cp -d /usr/lib/$(xx-info triple)/libgit2.so* /libgit2/lib/
67+
68+
FROM prepare-${BASE_VARIANT} as build
69+
70+
FROM debian:${BASE_VARIANT}-slim as controller
3171

3272
# Link repo to the GitHub Container Registry image
3373
LABEL org.opencontainers.image.source="https://github.com/fluxcd/source-controller"

Makefile

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ IMG ?= fluxcd/source-controller
33
TAG ?= latest
44

55
# Base image used to build the Go binary
6-
BASE_IMG ?= ghcr.io/fluxcd/golang-with-libgit2
7-
BASE_TAG ?= 1.16.8-bullseye-libgit2-1.1.1-1
6+
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2
7+
LIBGIT2_TAG ?= libgit2-1.1.1
88

99
# Allows for defining additional Docker buildx arguments,
1010
# e.g. '--push'.
11-
BUILDX_ARGS ?=
11+
BUILD_ARGS ?=
1212
# Architectures to build images for
13-
BUILDX_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7
13+
BUILD_PLATFORMS ?= linux/amd64,linux/arm64,linux/arm/v7
1414

1515
# Produce CRDs that work back to Kubernetes 1.16
1616
CRD_OPTIONS ?= crd:crdVersions=v1
@@ -110,18 +110,12 @@ generate: controller-gen ## Generate API code
110110
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
111111

112112
docker-build: ## Build the Docker image
113-
docker build \
114-
--build-arg BASE_IMG=$(BASE_IMG) \
115-
--build-arg BASE_TAG=$(BASE_TAG) \
116-
-t $(IMG):$(TAG) .
117-
118-
docker-buildx: ## Build the cross-platform Docker image
119113
docker buildx build \
120-
--build-arg BASE_IMG=$(BASE_IMG) \
121-
--build-arg BASE_TAG=$(BASE_TAG) \
122-
--platform=$(BUILDX_PLATFORMS) \
114+
--build-arg LIBGIT2_IMG=$(LIBGIT2_IMG) \
115+
--build-arg LIBGIT2_TAG=$(LIBGIT2_TAG) \
116+
--platform=$(BUILD_PLATFORMS) \
123117
-t $(IMG):$(TAG) \
124-
$(BUILDX_ARGS) .
118+
$(BUILD_ARGS) .
125119

126120
docker-push: ## Push Docker image
127121
docker push $(IMG):$(TAG)
@@ -178,8 +172,8 @@ ifeq (1, $(LIBGIT2_FORCE))
178172
@{ \
179173
set -e; \
180174
mkdir -p $(LIBGIT2_PATH); \
181-
docker cp $(shell docker create --rm $(BASE_IMG):$(BASE_TAG)):/libgit2/Makefile $(LIBGIT2_PATH); \
182-
INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH); \
175+
curl -sL https://raw.githubusercontent.com/fluxcd/golang-with-libgit2/$(LIBGIT2_TAG)/hack/Makefile -o $(LIBGIT2_PATH)/Makefile; \
176+
INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH) libgit2; \
183177
}
184178
endif
185179

0 commit comments

Comments
 (0)