diff --git a/Dockerfile b/Dockerfile index 2fbf5a44d..ca9289fef 100644 --- a/Dockerfile +++ b/Dockerfile @@ -84,8 +84,7 @@ ENV CGO_ENABLED=1 RUN export $(cat build/musl/$(xx-info alpine-arch).env | xargs) && \ export LIBRARY_PATH="/usr/local/$(xx-info triple):/usr/local/$(xx-info triple)/lib64" && \ export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig:/usr/local/$(xx-info triple)/lib64/pkgconfig" && \ - export FLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2)" && \ - export CGO_LDFLAGS="${FLAGS} -static" && \ + export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libssh2 openssl libgit2) -static" && \ GOARCH=$TARGETARCH go build \ -ldflags "-s -w" \ -tags 'netgo,osusergo,static_build' \ diff --git a/Makefile b/Makefile index d0fd80a7f..a96f87673 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ IMG ?= fluxcd/source-controller TAG ?= latest # Base image used to build the Go binary -LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2 -LIBGIT2_TAG ?= libgit2-1.1.1-4 +LIBGIT2_IMG ?= quay.io/paulinhu/golang-with-libgit2 +LIBGIT2_TAG ?= libgit2-1.1.1-5 # Allows for defining additional Docker buildx arguments, # e.g. '--push'. @@ -30,7 +30,7 @@ MUSL-CC = export CGO_ENABLED=1 export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig:$(LIBGIT2_LIB64_PATH)/pkgconfig -export LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH):$(LIBGIT2_LIB64_PATH) +export LIBRARY_PATH=$(LIBGIT2_LIB_PATH):$(LIBGIT2_LIB64_PATH) export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include -I$(LIBGIT2_PATH)/include/openssl ifeq ($(shell uname -s),Darwin) @@ -41,11 +41,18 @@ endif ifeq ($(shell uname -s),Linux) +ifeq ($(shell uname -m),x86_64) +# Linux x86_64 seem to be able to cope with the static libraries +# by having only musl-dev installed, without the need of using musl toolchain. + GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build' +else MUSL-PREFIX=$(REPOSITORY_ROOT)/build/musl/$(shell uname -m)-linux-musl-native/bin/$(shell uname -m)-linux-musl MUSL-CC=$(MUSL-PREFIX)-gcc export CC=$(MUSL-PREFIX)-gcc export CXX=$(MUSL-PREFIX)-g++ export AR=$(MUSL-PREFIX)-ar + GO_STATIC_FLAGS=-ldflags "-s -w -extldflags \"-static\"" -tags 'netgo,osusergo,static_build' +endif endif # API (doc) generation utilities @@ -59,7 +66,6 @@ else GOBIN=$(shell go env GOBIN) endif - # Architecture to use envtest with ifeq ($(shell uname -m),x86_64) ENVTEST_ARCH ?= amd64 @@ -70,21 +76,18 @@ endif all: build build: $(LIBGIT2) ## Build manager binary - go build -o bin/manager main.go + go build $(GO_STATIC_FLAGS) -o bin/manager main.go KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)" test: $(LIBGIT2) install-envtest test-api ## Run tests KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \ - go test ./... \ - -ldflags "-s -w" \ - -coverprofile cover.out \ - -tags 'netgo,osusergo,static_build' + go test $(GO_STATIC_FLAGS) ./... -coverprofile cover.out test-api: ## Run api tests cd api; go test ./... -coverprofile cover.out run: $(LIBGIT2) generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config - go run ./main.go + go run $(GO_STATIC_FLAGS) ./main.go install: manifests ## Install CRDs into a cluster kustomize build config/crd | kubectl apply -f - @@ -192,7 +195,9 @@ PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) define go-install-tool @[ -f $(1) ] || { \ set -e ;\ -TMP_DIR=$$(mktemp -d) ;\ +set -x ;\ +TMP_DIR=$(REPOSITORY_ROOT)/build/tmp ;\ +mkdir -p $$TMP_DIR ;\ cd $$TMP_DIR ;\ go mod init tmp ;\ echo "Downloading $(2)" ;\ diff --git a/hack/download-musl.sh b/hack/download-musl.sh index e1e518566..3f5b527d2 100755 --- a/hack/download-musl.sh +++ b/hack/download-musl.sh @@ -26,7 +26,7 @@ MUSL_DIR="${ROOT_DIR}/build/musl" if [ "${TARGET_ARCH}" = "$(uname -m)" ]; then MUSL_FILENAME="${MUSL_X86_64_FILENAME}" MUSL_SHA512="${MUSL_X86_64_SHA512}" - MUSL_PREFIX=$(xx-info alpine-arch)-linux-musl-native/bin/$(xx-info alpine-arch)-linux-musl + MUSL_PREFIX="${TARGET_ARCH}-linux-musl-native/bin/${TARGET_ARCH}-linux-musl" if [ "${TARGET_ARCH}" = "arm64" ] || [ "${TARGET_ARCH}" = "aarch64" ]; then MUSL_FILENAME="${MUSL_AARCH64_FILENAME}" MUSL_SHA512="${MUSL_AARCH64_SHA512}" @@ -34,7 +34,7 @@ if [ "${TARGET_ARCH}" = "$(uname -m)" ]; then else MUSL_FILENAME="${MUSL_XX86_64_FILENAME}" MUSL_SHA512="${MUSL_XX86_64_SHA512}" - MUSL_PREFIX=$(xx-info alpine-arch)-linux-musl-cross/bin/$(xx-info alpine-arch)-linux-musl + MUSL_PREFIX="${TARGET_ARCH}-linux-musl-cross/bin/${TARGET_ARCH}-linux-musl" if [ "${TARGET_ARCH}" = "arm64" ] || [ "${TARGET_ARCH}" = "aarch64" ]; then MUSL_FILENAME="${MUSL_XAARCH64_FILENAME}" MUSL_SHA512="${MUSL_XAARCH64_SHA512}"