Skip to content

Commit 4bd6a39

Browse files
authored
Merge pull request #222 from fluxcd/update-libgit2
Update github.com/libgit2/git2go to v31.6.1
2 parents 263ff7c + f1220d1 commit 4bd6a39

File tree

12 files changed

+240
-103
lines changed

12 files changed

+240
-103
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hack/libgit2/
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
FROM golang:1.16-buster as builder
2-
3-
# Up-to-date libgit2 dependencies are only available in sid (unstable).
4-
RUN echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list \
5-
&& echo "deb-src http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list
6-
RUN set -eux; \
7-
apt-get update \
8-
&& apt-get install -y libgit2-dev/unstable zlib1g-dev/unstable libssh2-1-dev/unstable libpcre3-dev/unstable \
9-
&& apt-get clean \
10-
&& apt-get autoremove --purge -y \
11-
&& rm -rf /var/lib/apt/lists/*
1+
ARG BASE_VARIANT=bullseye
2+
ARG GO_VERSION=1.16.8
3+
ARG XX_VERSION=1.0.0-rc.2
124

5+
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2
6+
ARG LIBGIT2_TAG=libgit2-1.1.1-1
7+
8+
FROM tonistiigi/xx:${XX_VERSION} AS xx
9+
FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} as libgit2
10+
11+
FROM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
12+
13+
# Copy the build utiltiies
14+
COPY --from=xx / /
15+
COPY --from=libgit2 /Makefile /libgit2/
16+
17+
# Install the libgit2 build dependencies
18+
RUN make -C /libgit2 cmake
19+
20+
RUN make -C /libgit2 dependencies
21+
22+
# Compile and install libgit2
23+
RUN FLAGS=$(xx-clang --print-cmake-defines) make -C /libgit2 libgit2
24+
25+
# Use the GitHub Actions uid:gid combination for proper fs permissions
1326
RUN groupadd -g 116 test && \
1427
useradd -u 1001 --gid test --shell /bin/sh --create-home test
1528

1629
# Run as test user
1730
USER test
1831

32+
# Set path to envtest binaries.
33+
ENV PATH="/github/workspace/envtest:${PATH}"
34+
1935
ENTRYPOINT ["/bin/sh", "-c"]

.github/workflows/build.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ jobs:
2222
${{ runner.os }}-go-
2323
- name: Set up kubebuilder
2424
uses: fluxcd/pkg/actions/kubebuilder@main
25+
- name: Setup envtest
26+
uses: fluxcd/pkg/actions/envtest@main
27+
with:
28+
version: "1.19.2"
2529
- name: Run tests
2630
uses: ./.github/actions/run-tests
2731
env:
2832
GOPATH: /github/home/go
29-
KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ bin
2727
*.swp
2828
*.swo
2929
*~
30+
31+
# Exclude all libgit2 related files
32+
hack/libgit2/

CONTRIBUTING.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ to join the conversation (this will also add an invitation to your
2727
Google calendar for our [Flux
2828
meeting](https://docs.google.com/document/d/1l_M0om0qUEN_NNiGgpqJ2tvsF2iioHkaARDeh6b70B0/edit#)).
2929

30+
### Installing required dependencies
31+
32+
The dependency [libgit2](https://libgit2.org/) needs to be installed to be able
33+
to run source-controller or its test-suite locally (not in a container).
34+
35+
In case this dependency is not present on your system (at the expected
36+
version), the first invocation of a `make` target that requires the
37+
dependency will attempt to compile it locally to `hack/libgit2`. For this build
38+
to succeed; CMake, OpenSSL 1.1 and LibSSH2 must be present on the system.
39+
40+
Triggering a manual build of the dependency is possible as well by running
41+
`make libgit2`. To enforce the build, for example if your system dependencies
42+
match but are not linked in a compatible way, append `LIBGIT2_FORCE=1` to the
43+
`make` command.
44+
45+
#### macOS
46+
47+
```console
48+
$ # Ensure libgit2 dependencies are available
49+
$ brew install cmake [email protected] libssh2 pkg-config
50+
$ LIBGIT2_FORCE=1 make libgit2
51+
```
52+
53+
#### Linux
54+
55+
```console
56+
$ # Ensure libgit2 dependencies are available
57+
$ pacman -S cmake openssl libssh2
58+
$ LIBGIT2_FORCE=1 make libgit2
59+
```
60+
61+
**Note:** Example shown is for Arch Linux, but likewise procedure can be
62+
followed using any other package manager, e.g. `apt`.
63+
3064
### How to run the test suite
3165

3266
Prerequisites:

Dockerfile

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
1-
FROM golang:1.16-buster as builder
2-
3-
# Up-to-date libgit2 dependencies are only available in sid (unstable).
4-
# The libgit2 dependencies must be listed here to be able to build on ARM64.
5-
RUN echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list \
6-
&& echo "deb-src http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list
7-
RUN set -eux; \
8-
apt-get update \
9-
&& apt-get install -y libgit2-dev/unstable zlib1g-dev/unstable libssh2-1-dev/unstable libpcre3-dev/unstable \
10-
&& apt-get clean \
11-
&& apt-get autoremove --purge -y \
12-
&& rm -rf /var/lib/apt/lists/*
1+
ARG BASE_VARIANT=bullseye
2+
ARG GO_VERSION=1.16.8
3+
ARG XX_VERSION=1.0.0-rc.2
134

14-
WORKDIR /workspace
5+
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2
6+
ARG LIBGIT2_TAG=libgit2-1.1.1-1
157

16-
# Copy the Go Modules manifests
17-
COPY go.mod go.mod
18-
COPY go.sum go.sum
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
35+
36+
# Configure workspace
37+
WORKDIR /workspace
1938

2039
# This has its own go.mod, which needs to be present so go mod
2140
# download works.
2241
COPY api/ api/
2342

43+
# Copy modules manifests
44+
COPY go.mod go.mod
45+
COPY go.sum go.sum
46+
2447
# cache deps before building and copying source so that we don't need to re-download as much
2548
# and so that source changes don't invalidate our downloaded layer
2649
RUN go mod download
@@ -30,30 +53,43 @@ COPY main.go main.go
3053
COPY pkg/ pkg/
3154
COPY controllers/ controllers/
3255

33-
# Build
34-
RUN CGO_ENABLED=1 go build -o image-automation-controller main.go
56+
# Build the binary
57+
ENV CGO_ENABLED=1
58+
ARG TARGETPLATFORM
59+
RUN xx-go build -o image-automation-controller -trimpath \
60+
main.go
3561

36-
FROM debian:buster-slim as controller
62+
FROM build-${BASE_VARIANT} as prepare-bullseye
3763

38-
LABEL org.opencontainers.image.source="https://github.com/fluxcd/image-automation-controller"
64+
# Move libgit2 lib to generic and predictable location
65+
ARG TARGETPLATFORM
66+
RUN mkdir -p /libgit2/lib/ \
67+
&& cp -d /usr/lib/$(xx-info triple)/libgit2.so* /libgit2/lib/
3968

40-
# Up-to-date libgit2 dependencies are only available in
41-
# unstable, as libssh2 in testing/bullseye has been linked
42-
# against gcrypt which causes issues with PKCS* formats.
43-
RUN echo "deb http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list \
44-
&& echo "deb-src http://deb.debian.org/debian unstable main" >> /etc/apt/sources.list
45-
RUN set -eux; \
46-
apt-get update \
47-
&& apt-get install -y ca-certificates libgit2-1.1 \
48-
&& apt-get clean \
49-
&& apt-get autoremove --purge -y \
50-
&& rm -rf /var/lib/apt/lists/*
69+
FROM prepare-${BASE_VARIANT} as build
5170

52-
COPY --from=builder /workspace/image-automation-controller /usr/local/bin/
71+
FROM debian:${BASE_VARIANT}-slim as controller
5372

73+
# Configure user
5474
RUN groupadd controller && \
5575
useradd --gid controller --shell /bin/sh --create-home controller
5676

57-
USER controller
77+
# Copy libgit2
78+
COPY --from=build /libgit2/lib/ /usr/local/lib/
79+
RUN ldconfig
80+
81+
# Upgrade packages and install runtime dependencies
82+
RUN echo "deb http://deb.debian.org/debian sid main" >> /etc/apt/sources.list \
83+
&& echo "deb-src http://deb.debian.org/debian sid main" >> /etc/apt/sources.list \
84+
&& apt update \
85+
&& apt install --no-install-recommends -y zlib1g/sid libssl1.1/sid libssh2-1/sid \
86+
&& apt install --no-install-recommends -y ca-certificates \
87+
&& apt clean \
88+
&& apt autoremove --purge -y \
89+
&& rm -rf /var/lib/apt/lists/*
90+
91+
# Copy over binary from build
92+
COPY --from=build /workspace/image-automation-controller /usr/local/bin/
5893

94+
USER controller
5995
ENTRYPOINT [ "image-automation-controller" ]

0 commit comments

Comments
 (0)