Skip to content

Commit 574bb4c

Browse files
authored
Merge pull request #853 from pjbgf/remove-musl
Remove MUSL and enable threadless libgit2 support
2 parents d9dfb9d + f51f9d3 commit 574bb4c

File tree

8 files changed

+44
-118
lines changed

8 files changed

+44
-118
lines changed

Dockerfile

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ARG GO_VERSION=1.18
33
ARG XX_VERSION=1.1.2
44

55
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2-only
6-
ARG LIBGIT2_TAG=v0.1.4
6+
ARG LIBGIT2_TAG=v0.2.0
77

88
FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS libgit2-libs
99

@@ -37,22 +37,6 @@ COPY go.sum go.sum
3737
# Cache modules
3838
RUN go mod download
3939

40-
# The musl-tool-chain layer is an adhoc solution
41-
# for the problem in which xx gets confused during compilation
42-
# and a) looks for gold linker and then b) cannot find musl's dynamic linker.
43-
FROM --platform=$BUILDPLATFORM alpine as musl-tool-chain
44-
45-
COPY --from=xx / /
46-
47-
RUN apk add bash curl tar
48-
49-
WORKDIR /workspace
50-
COPY hack/download-musl.sh .
51-
52-
ARG TARGETPLATFORM
53-
ARG TARGETARCH
54-
RUN ROOT_DIR="$(pwd)" TARGET_ARCH="$(xx-info alpine-arch)" ENV_FILE=true \
55-
./download-musl.sh
5640

5741
# Build stage install per target platform
5842
# dependency and effectively cross compile the application.
@@ -64,7 +48,7 @@ COPY --from=libgit2-libs /usr/local/ /usr/local/
6448

6549
# Some dependencies have to installed
6650
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
67-
RUN xx-apk add musl-dev gcc lld
51+
RUN xx-apk add musl-dev gcc clang lld
6852

6953
WORKDIR /workspace
7054

@@ -74,17 +58,14 @@ COPY controllers/ controllers/
7458
COPY pkg/ pkg/
7559
COPY internal/ internal/
7660

77-
COPY --from=musl-tool-chain /workspace/build /workspace/build
78-
7961
ARG TARGETPLATFORM
8062
ARG TARGETARCH
8163
ENV CGO_ENABLED=1
8264

8365
# Instead of using xx-go, (cross) compile with vanilla go leveraging musl tool chain.
84-
RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) && \
85-
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig" && \
86-
export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libgit2) -static" && \
87-
GOARCH=$TARGETARCH go build \
66+
RUN export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig" && \
67+
export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libgit2) -static -fuse-ld=lld" && \
68+
xx-go build \
8869
-ldflags "-s -w" \
8970
-tags 'netgo,osusergo,static_build' \
9071
-o /source-controller -trimpath main.go;

Makefile

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TAG ?= latest
44

55
# Base image used to build the Go binary
66
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-only
7-
LIBGIT2_TAG ?= v0.1.4
7+
LIBGIT2_TAG ?= v0.2.0
88

99
# Allows for defining additional Go test args, e.g. '-tags integration'.
1010
GO_TEST_ARGS ?= -race
@@ -33,24 +33,12 @@ ENVTEST_BIN_VERSION ?= 1.19.2
3333
LIBGIT2_PATH := $(BUILD_DIR)/libgit2/$(LIBGIT2_TAG)
3434
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
3535
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
36-
MUSL-CC =
3736

3837
export CGO_ENABLED=1
3938
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig
4039
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
4140
GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build$(addprefix ,,$(GO_TAGS))'
4241

43-
ifeq ($(shell uname -s),Linux)
44-
ifneq ($(shell uname -m),x86_64)
45-
MUSL-PREFIX=$(BUILD_DIR)/musl/$(shell uname -m)-linux-musl-native/bin/$(shell uname -m)-linux-musl
46-
MUSL-CC=$(MUSL-PREFIX)-gcc
47-
export CC=$(MUSL-PREFIX)-gcc
48-
export CXX=$(MUSL-PREFIX)-g++
49-
export AR=$(MUSL-PREFIX)-ar
50-
GO_STATIC_FLAGS=-ldflags "-s -w -extldflags \"-static\"" -tags 'netgo,osusergo,static_build$(addprefix ,,$(GO_TAGS))'
51-
endif
52-
endif
53-
5442
# API (doc) generation utilities
5543
CONTROLLER_GEN_VERSION ?= v0.7.0
5644
GEN_API_REF_DOCS_VERSION ?= v0.3.0
@@ -179,15 +167,11 @@ install-envtest: setup-envtest ## Download envtest binaries locally.
179167
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
180168

181169
COSIGN = $(GOBIN)/cosign
182-
$(LIBGIT2): $(MUSL-CC)
170+
$(LIBGIT2):
183171
$(call go-install-tool,$(COSIGN),github.com/sigstore/cosign/cmd/cosign@latest)
184172

185173
IMG=$(LIBGIT2_IMG) TAG=$(LIBGIT2_TAG) PATH=$(PATH):$(GOBIN) ./hack/install-libraries.sh
186174

187-
$(MUSL-CC):
188-
ifneq ($(shell uname -s),Darwin)
189-
./hack/download-musl.sh
190-
endif
191175

192176
.PHONY: help
193177
help: ## Display this help menu

controllers/suite_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
dockerRegistry "github.com/distribution/distribution/v3/registry"
4646
_ "github.com/distribution/distribution/v3/registry/auth/htpasswd"
4747
_ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory"
48+
git2go "github.com/libgit2/git2go/v33"
4849

4950
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
5051
"github.com/fluxcd/source-controller/internal/cache"
@@ -179,6 +180,8 @@ func setupRegistryServer(ctx context.Context) (*registryClientTestServer, error)
179180
}
180181

181182
func TestMain(m *testing.M) {
183+
mustHaveNoThreadSupport()
184+
182185
initTestTLS()
183186

184187
utilruntime.Must(sourcev1.AddToScheme(scheme.Scheme))
@@ -333,3 +336,22 @@ func randStringRunes(n int) string {
333336
func int64p(i int64) *int64 {
334337
return &i
335338
}
339+
340+
// This provides a regression assurance for image-automation-controller/#339.
341+
// Validates that:
342+
// - libgit2 was built with no support for threads.
343+
// - git2go accepts libgit2 built with no support for threads.
344+
//
345+
// The logic below does the validation of the former, whilst
346+
// referring to git2go forces its init() execution, which is
347+
// where any validation to that effect resides.
348+
//
349+
// git2go does not support threadless libgit2 by default,
350+
// hence a fork is being used which disables such validation.
351+
//
352+
// TODO: extract logic into pkg.
353+
func mustHaveNoThreadSupport() {
354+
if git2go.Features()&git2go.FeatureThreads != 0 {
355+
panic("libgit2 must not be build with thread support")
356+
}
357+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ go 1.18
44

55
replace github.com/fluxcd/source-controller/api => ./api
66

7+
// A temporary fork of git2go was created to enable use
8+
// of libgit2 without thread support to fix:
9+
// fluxcd/image-automation-controller/#339.
10+
//
11+
// This can be removed once libgit2/git2go#918 is merged.
12+
//
13+
// The fork automatically releases new patches based on upstream:
14+
// https://github.com/pjbgf/git2go/commit/d72e39cdc20f7fe014ba73072b01ba7b569e9253
15+
replace github.com/libgit2/git2go/v33 => github.com/pjbgf/git2go/v33 v33.0.9-nothread-check
16+
717
require (
818
cloud.google.com/go/storage v1.23.0
919
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.22.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,6 @@ github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
593593
github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
594594
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
595595
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
596-
github.com/libgit2/git2go/v33 v33.0.9 h1:4ch2DJed6IhJO28BEohkUoGvxLsRzUjxljoNFJ6/O78=
597-
github.com/libgit2/git2go/v33 v33.0.9/go.mod h1:KdpqkU+6+++4oHna/MIOgx4GCQ92IPCdpVRMRI80J+4=
598596
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
599597
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
600598
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
@@ -719,6 +717,8 @@ github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+v
719717
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
720718
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
721719
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
720+
github.com/pjbgf/git2go/v33 v33.0.9-nothread-check h1:gSK7FaLECIM3VSuBOAsVZQtWd+51iTB5lv9RyxhOYMk=
721+
github.com/pjbgf/git2go/v33 v33.0.9-nothread-check/go.mod h1:KdpqkU+6+++4oHna/MIOgx4GCQ92IPCdpVRMRI80J+4=
722722
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI=
723723
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ=
724724
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=

hack/download-musl.sh

Lines changed: 0 additions & 71 deletions
This file was deleted.

hack/install-libraries.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fix_pkgconfigs(){
7979
# Update the prefix paths included in the .pc files.
8080
if [[ $OSTYPE == 'darwin'* ]]; then
8181
# https://github.com/fluxcd/golang-with-libgit2/blob/v0.1.4/.github/workflows/release.yaml#L158
82-
INSTALLED_DIR="/Users/runner/work/golang-with-libgit2/golang-with-libgit2/build/libgit2-darwin-amd64"
82+
INSTALLED_DIR="/Users/runner/work/golang-with-libgit2/golang-with-libgit2/build/darwin-libgit2-only"
8383

8484
# This will make it easier to update to the location in which they will be used.
8585
# sed has a sight different behaviour in MacOS
@@ -137,7 +137,7 @@ install_libraries(){
137137
fi
138138
fi
139139

140-
FILE_NAME="linux-x86_64-libgit2-only.tar.gz"
140+
FILE_NAME="linux-$(uname -m)-libgit2-only.tar.gz"
141141
DIR="linux-libgit2-only"
142142
if [[ $OSTYPE == 'darwin'* ]]; then
143143
FILE_NAME="darwin-libgit2-only.tar.gz"

tests/fuzz/oss_fuzz_build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
set -euxo pipefail
1818

19-
LIBGIT2_TAG="${LIBGIT2_TAG:-v0.1.4}"
19+
LIBGIT2_TAG="${LIBGIT2_TAG:-v0.2.0}"
2020
GOPATH="${GOPATH:-/root/go}"
2121
GO_SRC="${GOPATH}/src"
2222
PROJECT_PATH="github.com/fluxcd/source-controller"

0 commit comments

Comments
 (0)