Skip to content

Commit 75dee8f

Browse files
author
Paulo Gomes
committed
Use static libraries from built image
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 93c636e commit 75dee8f

File tree

5 files changed

+86
-121
lines changed

5 files changed

+86
-121
lines changed

.github/actions/run-tests/Dockerfile

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

.github/actions/run-tests/action.yml

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

.github/workflows/e2e.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
2828
restore-keys: |
2929
${{ runner.os }}-go-
30+
- name: Run tests
31+
run: make test
3032
- name: Setup Kubernetes
3133
uses: engineerd/[email protected]
3234
with:
@@ -36,11 +38,6 @@ jobs:
3638
uses: fluxcd/pkg/actions/kustomize@main
3739
- name: Setup Helm
3840
uses: fluxcd/pkg/actions/helm@main
39-
- name: Run tests
40-
uses: ./.github/actions/run-tests
41-
env:
42-
GOROOT:
43-
GOPATH: /github/home/go
4441
- name: Verify
4542
run: make verify
4643
- name: Run E2E tests

Makefile

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,24 @@ CRD_OPTIONS ?= crd:crdVersions=v1
1818
# Repository root based on Git metadata
1919
REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
2020

21-
# Libgit2 version
22-
LIBGIT2_VERSION ?= 1.1.1
23-
2421
# Other dependency versions
2522
ENVTEST_BIN_VERSION ?= 1.19.2
2623

27-
# libgit2 related magical paths
28-
# These are used to determine if the target libgit2 version is already available on
29-
# the system, or where they should be installed to
30-
SYSTEM_LIBGIT2_VERSION := $(shell pkg-config --modversion libgit2 2>/dev/null)
31-
LIBGIT2_PATH := $(REPOSITORY_ROOT)/hack/libgit2
24+
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
25+
26+
LIBGIT2_PATH := $(REPOSITORY_ROOT)/build/libgit2
3227
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
33-
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.so.$(LIBGIT2_VERSION)
28+
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
29+
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
3430

35-
ifneq ($(LIBGIT2_VERSION),$(SYSTEM_LIBGIT2_VERSION))
36-
LIBGIT2_FORCE ?= 1
37-
endif
31+
export CGO_ENABLED=1
32+
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig:$(LIBGIT2_LIB64_PATH)/pkgconfig
33+
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include
3834

3935
ifeq ($(shell uname -s),Darwin)
40-
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.$(LIBGIT2_VERSION).dylib
41-
HAS_BREW := $(shell brew --version 2>/dev/null)
42-
ifdef HAS_BREW
43-
HAS_OPENSSL := $(shell brew --prefix [email protected])
44-
endif
36+
export CGO_LDFLAGS=-L$(LIBGIT2_LIB_PATH) -lssh2 -lssl -lcrypto -lgit2
37+
else
38+
export CGO_LDFLAGS=-L$(LIBGIT2_LIB_PATH) -L$(LIBGIT2_LIB64_PATH) -lssh2 -lz -lgit2 -lrt -lssl -lcrypto -lz -ldl -pthread
4539
endif
4640

4741

@@ -56,59 +50,28 @@ else
5650
GOBIN=$(shell go env GOBIN)
5751
endif
5852

59-
ifeq ($(strip ${PKG_CONFIG_PATH}),)
60-
MAKE_PKG_CONFIG_PATH = $(LIBGIT2_LIB_PATH)/pkgconfig
61-
else
62-
MAKE_PKG_CONFIG_PATH = ${PKG_CONFIG_PATH}:$(LIBGIT2_LIB_PATH)/pkgconfig
63-
endif
64-
65-
ifdef HAS_OPENSSL
66-
MAKE_PKG_CONFIG_PATH := $(MAKE_PKG_CONFIG_PATH):$(HAS_OPENSSL)/lib/pkgconfig
67-
endif
6853

6954
# Architecture to use envtest with
7055
ENVTEST_ARCH ?= amd64
7156

7257
all: build
7358

7459
build: $(LIBGIT2) ## Build manager binary
75-
ifeq ($(shell uname -s),Darwin)
76-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
77-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
78-
go build -o bin/manager main.go
79-
else
80-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
8160
go build -o bin/manager main.go
82-
endif
8361

84-
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
62+
8563
test: $(LIBGIT2) install-envtest test-api ## Run tests
86-
ifeq ($(shell uname -s),Darwin)
87-
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
88-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
89-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
9064
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
91-
go test ./... -coverprofile cover.out
92-
else
93-
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
94-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
95-
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
96-
go test ./... -coverprofile cover.out
97-
endif
65+
go test ./... \
66+
-ldflags "-s -w" \
67+
-coverprofile cover.out \
68+
-tags 'netgo,osusergo,static_build'
9869

9970
test-api: ## Run api tests
10071
cd api; go test ./... -coverprofile cover.out
10172

10273
run: $(LIBGIT2) generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
103-
ifeq ($(shell uname -s),Darwin)
104-
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
105-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
10674
go run ./main.go
107-
else
108-
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
109-
go run ./main.go
110-
endif
111-
11275

11376
install: manifests ## Install CRDs into a cluster
11477
kustomize build config/crd | kubectl apply -f -
@@ -142,16 +105,8 @@ fmt: ## Run go fmt against code
142105
cd api; go fmt ./...
143106

144107
vet: $(LIBGIT2) ## Run go vet against code
145-
ifeq ($(shell uname -s),Darwin)
146-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
147-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
148108
go vet ./...
149109
cd api; go vet ./...
150-
else
151-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
152-
go vet ./...
153-
cd api; go vet ./...
154-
endif
155110

156111
generate: controller-gen ## Generate API code
157112
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
@@ -193,14 +148,8 @@ install-envtest: setup-envtest ## Download envtest binaries locally.
193148
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
194149

195150
$(LIBGIT2):
196-
ifeq (1, $(LIBGIT2_FORCE))
197-
@{ \
198-
set -e; \
199-
mkdir -p $(LIBGIT2_PATH); \
200-
curl -sL https://raw.githubusercontent.com/fluxcd/golang-with-libgit2/$(LIBGIT2_TAG)/hack/Makefile -o $(LIBGIT2_PATH)/Makefile; \
201-
INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH) libgit2; \
202-
}
203-
endif
151+
IMG_TAG=$(LIBGIT2_IMG):$(LIBGIT2_TAG) ./hack/extract-libraries.sh
152+
204153

205154
.PHONY: help
206155
help: ## Display this help menu

hack/extract-libraries.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
3+
set -euxo pipefail
4+
5+
IMG_TAG="${IMG_TAG:-.}"
6+
7+
function extract(){
8+
PLATFORM=$1
9+
DIR=$2
10+
11+
id=$(docker create --platform="${PLATFORM}" "${IMG_TAG}")
12+
docker cp "${id}":/usr/local - > output.tar.gz
13+
docker rm -v "${id}"
14+
15+
tar -xf output.tar.gz "local/${DIR}"
16+
rm output.tar.gz
17+
}
18+
19+
function setup() {
20+
PLATFORM=$1
21+
DIR=$2
22+
23+
extract "${PLATFORM}" "${DIR}"
24+
25+
NEW_DIR="$(/bin/pwd)/build/libgit2"
26+
INSTALLED_DIR="/usr/local/${DIR}"
27+
28+
mkdir -p "./build"
29+
30+
# Make a few movements to account for the change in
31+
# behaviour in tar between MacOS and Linux
32+
mv "local/${DIR}" "libgit2"
33+
rm -rf "local"
34+
mv "libgit2/" "./build"
35+
36+
# Update the prefix paths included in the .pc files.
37+
# This will make it easier to update to the location in which they will be used.
38+
if [[ $OSTYPE == 'darwin'* ]]; then
39+
# sed has a sight different behaviour in MacOS
40+
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "" "s;${INSTALLED_DIR};${NEW_DIR};g" {}
41+
else
42+
find "${NEW_DIR}" -type f -name "*.pc" | xargs -I {} sed -i "s;${INSTALLED_DIR};${NEW_DIR};g" {}
43+
fi
44+
}
45+
46+
function setup_current() {
47+
if [ -d "./build/libgit2" ]; then
48+
echo "Skipping libgit2 setup as it already exists"
49+
exit 0
50+
fi
51+
52+
DIR="x86_64-alpine-linux-musl"
53+
PLATFORM="linux/amd64"
54+
55+
if [[ "$(uname -m)" == armv7* ]]; then
56+
DIR="armv7-alpine-linux-musleabihf"
57+
PLATFORM="linux/arm/v7"
58+
elif [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
59+
DIR="aarch64-alpine-linux-musl"
60+
PLATFORM="linux/arm64"
61+
fi
62+
63+
setup "${PLATFORM}" "${DIR}"
64+
}
65+
66+
setup_current

0 commit comments

Comments
 (0)