Skip to content

Commit 3990e9c

Browse files
author
Paulo Gomes
committed
Remove MUSL and enable threadless libgit2 support
Use of MUSL was a temporary solution to mitigate cross-platform issues while building openssl and libssh2. Since Unmanaged transport has been deprecated, openssl and libssh2 dependencies are no longer required and by extension MUSL. Enables libgit2 threadless support and provides a regression assurance for #339. Signed-off-by: Paulo Gomes <[email protected]>
1 parent 11851a0 commit 3990e9c

File tree

8 files changed

+55
-142
lines changed

8 files changed

+55
-142
lines changed

Dockerfile

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
ARG BASE_VARIANT=alpine
22
ARG GO_VERSION=1.18
3-
ARG XX_VERSION=1.1.0
3+
ARG XX_VERSION=1.1.2
44

5-
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2-all
6-
ARG LIBGIT2_TAG=v0.1.2
5+
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2-only
6+
ARG LIBGIT2_TAG=v0.2.0
77

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

@@ -17,7 +17,7 @@ FROM gostable AS go-linux
1717
# These will be used at current arch to yield execute the cross compilations.
1818
FROM go-${TARGETOS} AS build-base
1919

20-
RUN apk add --no-cache clang lld pkgconfig
20+
RUN apk add clang lld pkgconfig
2121

2222
COPY --from=xx / /
2323

@@ -37,23 +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
56-
5740
# Build stage install per target platform
5841
# dependency and effectively cross compile the application.
5942
FROM build-go-mod as build
@@ -64,7 +47,7 @@ COPY --from=libgit2-libs /usr/local/ /usr/local/
6447

6548
# Some dependencies have to installed
6649
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
67-
RUN xx-apk add musl-dev gcc lld
50+
RUN xx-apk add musl-dev gcc clang lld
6851

6952
WORKDIR /workspace
7053

@@ -74,20 +57,14 @@ COPY controllers/ controllers/
7457
COPY pkg/ pkg/
7558
COPY internal/ internal/
7659

77-
COPY --from=musl-tool-chain /workspace/build /workspace/build
78-
7960
ARG TARGETPLATFORM
8061
ARG TARGETARCH
8162
ENV CGO_ENABLED=1
8263

83-
# Performance related changes:
84-
# - Use read-only bind instead of copying go source files.
85-
# - Cache go packages.
86-
RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) && \
87-
export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \
88-
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \
89-
export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static" && \
90-
GOARCH=$TARGETARCH go build \
64+
RUN export LIBRARY_PATH="/usr/local/$(xx-info triple)" && \
65+
export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig" && \
66+
export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libgit2) -static -fuse-ld=lld" && \
67+
xx-go build \
9168
-ldflags "-s -w" \
9269
-tags 'netgo,osusergo,static_build' \
9370
-o /image-automation-controller -trimpath main.go;

Makefile

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ TAG ?= latest
77
CRD_OPTIONS ?= crd:crdVersions=v1
88

99
# Base image used to build the Go binary
10-
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-all
11-
LIBGIT2_TAG ?= v0.1.2
10+
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-only
11+
LIBGIT2_TAG ?= v0.2.0
1212

1313
# Allows for defining additional Docker buildx arguments,
1414
# e.g. '--push'.
@@ -42,37 +42,20 @@ LIBGIT2_PATH := $(BUILD_DIR)/libgit2/$(LIBGIT2_TAG)
4242
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
4343
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
4444
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
45-
MUSL-CC =
4645

4746
export CGO_ENABLED=1
4847
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig
4948
export LIBRARY_PATH=$(LIBGIT2_LIB_PATH)
5049
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include -I$(LIBGIT2_PATH)/include/openssl
51-
50+
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
5251

5352
# The pkg-config command will yield warning messages until libgit2 is downloaded.
5453
ifeq ($(shell uname -s),Darwin)
55-
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2 2>/dev/null)
5654
GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build'
57-
else
58-
export PKG_CONFIG_PATH:=$(PKG_CONFIG_PATH):$(LIBGIT2_LIB64_PATH)/pkgconfig
59-
export LIBRARY_PATH:=$(LIBRARY_PATH):$(LIBGIT2_LIB64_PATH)
60-
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2 2>/dev/null)
6155
endif
6256

6357
ifeq ($(shell uname -s),Linux)
64-
ifeq ($(shell uname -m),x86_64)
65-
# Linux x86_64 seem to be able to cope with the static libraries
66-
# by having only musl-dev installed, without the need of using musl toolchain.
6758
GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build'
68-
else
69-
MUSL-PREFIX=$(BUILD_DIR)/musl/$(shell uname -m)-linux-musl-native/bin/$(shell uname -m)-linux-musl
70-
MUSL-CC=$(MUSL-PREFIX)-gcc
71-
export CC=$(MUSL-PREFIX)-gcc
72-
export CXX=$(MUSL-PREFIX)-g++
73-
export AR=$(MUSL-PREFIX)-ar
74-
GO_STATIC_FLAGS=-ldflags "-s -w -extldflags \"-static\"" -tags 'netgo,osusergo,static_build'
75-
endif
7659
endif
7760

7861
# API (doc) generation utilities
@@ -213,16 +196,11 @@ controller-gen: ## Download controller-gen locally if necessary.
213196
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
214197

215198
COSIGN = $(GOBIN)/cosign
216-
$(LIBGIT2): $(MUSL-CC)
199+
$(LIBGIT2):
217200
$(call go-install-tool,$(COSIGN),github.com/sigstore/cosign/cmd/cosign@latest)
218201

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

221-
$(MUSL-CC):
222-
ifneq ($(shell uname -s),Darwin)
223-
./hack/download-musl.sh
224-
endif
225-
226204
# Find or download gen-crd-api-reference-docs
227205
GEN_CRD_API_REFERENCE_DOCS = $(GOBIN)/gen-crd-api-reference-docs
228206
.PHONY: gen-crd-api-reference-docs

controllers/suite_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
git2go "github.com/libgit2/git2go/v33"
2728
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2829
"k8s.io/client-go/kubernetes/scheme"
2930
ctrl "sigs.k8s.io/controller-runtime"
@@ -53,6 +54,8 @@ func init() {
5354
}
5455

5556
func TestMain(m *testing.M) {
57+
mustHaveNoThreadSupport()
58+
5659
utilruntime.Must(imagev1_reflect.AddToScheme(scheme.Scheme))
5760
utilruntime.Must(sourcev1.AddToScheme(scheme.Scheme))
5861
utilruntime.Must(imagev1.AddToScheme(scheme.Scheme))
@@ -90,3 +93,22 @@ func TestMain(m *testing.M) {
9093

9194
os.Exit(code)
9295
}
96+
97+
// This provides a regression assurance for image-automation-controller/#339.
98+
// Validates that:
99+
// - libgit2 was built with no support for threads.
100+
// - git2go accepts libgit2 built with no support for threads.
101+
//
102+
// The logic below does the validation of the former, whilst
103+
// referring to git2go forces its init() execution, which is
104+
// where any validation to that effect resides.
105+
//
106+
// git2go does not support threadless libgit2 by default,
107+
// hence a fork is being used which disables such validation.
108+
//
109+
// TODO: extract logic into pkg.
110+
func mustHaveNoThreadSupport() {
111+
if git2go.Features()&git2go.FeatureThreads != 0 {
112+
panic("libgit2 must not be build with thread support")
113+
}
114+
}

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/image-automation-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
github.com/Masterminds/sprig/v3 v3.2.2
919
github.com/ProtonMail/go-crypto v0.0.0-20220714114130-e85cedf506cd

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
449449
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
450450
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
451451
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
452-
github.com/libgit2/git2go/v33 v33.0.9 h1:4ch2DJed6IhJO28BEohkUoGvxLsRzUjxljoNFJ6/O78=
453-
github.com/libgit2/git2go/v33 v33.0.9/go.mod h1:KdpqkU+6+++4oHna/MIOgx4GCQ92IPCdpVRMRI80J+4=
454452
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
455453
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
456454
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
@@ -536,6 +534,8 @@ github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FI
536534
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
537535
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
538536
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
537+
github.com/pjbgf/git2go/v33 v33.0.9-nothread-check h1:gSK7FaLECIM3VSuBOAsVZQtWd+51iTB5lv9RyxhOYMk=
538+
github.com/pjbgf/git2go/v33 v33.0.9-nothread-check/go.mod h1:KdpqkU+6+++4oHna/MIOgx4GCQ92IPCdpVRMRI80J+4=
539539
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
540540
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
541541
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

hack/download-musl.sh

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

hack/install-libraries.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ install_libraries(){
136136
fi
137137
fi
138138

139-
FILE_NAME="linux-$(uname -m)-all-libs.tar.gz"
140-
DIR="libgit2-linux-all-libs"
139+
FILE_NAME="linux-$(uname -m)-libgit2-only.tar.gz"
140+
DIR="linux-libgit2-only"
141141
if [[ $OSTYPE == 'darwin'* ]]; then
142-
FILE_NAME="darwin-all-libs.tar.gz"
143-
DIR="darwin-all-libs"
142+
FILE_NAME="darwin-libgit2-only.tar.gz"
143+
DIR="darwin-libgit2-only"
144144
fi
145145

146146
download_files "${FILE_NAME}"

tests/fuzz/oss_fuzz_build.sh

Lines changed: 4 additions & 7 deletions
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.2}"
19+
LIBGIT2_TAG="${LIBGIT2_TAG:-v0.2.0}"
2020
GOPATH="${GOPATH:-/root/go}"
2121
GO_SRC="${GOPATH}/src"
2222
PROJECT_PATH="github.com/fluxcd/image-automation-controller"
@@ -28,9 +28,9 @@ export TARGET_DIR="$(/bin/pwd)/build/libgit2/${LIBGIT2_TAG}"
2828
# For most cases, libgit2 will already be present.
2929
# The exception being at the oss-fuzz integration.
3030
if [ ! -d "${TARGET_DIR}" ]; then
31-
curl -o output.tar.gz -LO "https://github.com/fluxcd/golang-with-libgit2/releases/download/${LIBGIT2_TAG}/linux-$(uname -m)-all-libs.tar.gz"
31+
curl -o output.tar.gz -LO "https://github.com/fluxcd/golang-with-libgit2/releases/download/${LIBGIT2_TAG}/linux-$(uname -m)-libgit2-only.tar.gz"
3232

33-
DIR=libgit2-linux-all-libs
33+
DIR=linux-libgit2-only
3434
NEW_DIR="$(/bin/pwd)/build/libgit2/${LIBGIT2_TAG}"
3535
INSTALLED_DIR="/home/runner/work/golang-with-libgit2/golang-with-libgit2/build/${DIR}"
3636

@@ -77,7 +77,6 @@ SOURCE_VER=$(go list -m github.com/fluxcd/source-controller/api | awk '{print $2
7777
REFLECTOR_VER=$(go list -m github.com/fluxcd/image-reflector-controller/api | awk '{print $2}')
7878

7979
go mod download
80-
go mod tidy -go=1.18
8180
go get -d github.com/fluxcd/image-automation-controller
8281
go get -d github.com/AdaLogics/go-fuzz-headers
8382

@@ -103,9 +102,7 @@ function go_compile(){
103102
go-fuzz -tags gofuzz -func="${function}" -o "${fuzzer}.a" .
104103
${CXX} ${CXXFLAGS} ${LIB_FUZZING_ENGINE} -o "${OUT}/${fuzzer}" \
105104
"${fuzzer}.a" \
106-
"${TARGET_DIR}/lib/libgit2.a" "${TARGET_DIR}/lib/libssh2.a" \
107-
"${TARGET_DIR}/lib/libz.a" "${TARGET_DIR}/lib64/libssl.a" \
108-
"${TARGET_DIR}/lib64/libcrypto.a" \
105+
"${TARGET_DIR}/lib/libgit2.a" \
109106
-fsanitize="${SANITIZER}"
110107
fi
111108
}

0 commit comments

Comments
 (0)