Skip to content

Commit b343008

Browse files
author
Paulo Gomes
committed
Update test to run against static libraries
Signed-off-by: Paulo Gomes <[email protected]>
1 parent 01a2eaa commit b343008

File tree

6 files changed

+89
-119
lines changed

6 files changed

+89
-119
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/build.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
name: build
2-
32
on:
43
pull_request:
54
push:
@@ -10,11 +9,15 @@ permissions:
109
contents: read # for actions/checkout to fetch code
1110

1211
jobs:
13-
test:
12+
test-linux-amd64:
1413
runs-on: ubuntu-latest
1514
steps:
1615
- name: checkout
1716
uses: actions/checkout@v2
17+
- name: Setup Go
18+
uses: actions/setup-go@v2
19+
with:
20+
go-version: 1.17.x
1821
- name: Restore go cache
1922
uses: actions/cache@v1
2023
with:
@@ -24,8 +27,6 @@ jobs:
2427
restore-keys: |
2528
${{ runner.os }}-go-
2629
- name: Run tests
27-
uses: ./.github/actions/run-tests
28-
env:
29-
GOPATH: /github/home/go
30+
run: make test
3031
- name: Verify
3132
run: make verify

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ testbin
3131

3232
# Exclude all libgit2 related files
3333
hack/libgit2/
34+
build/

Makefile

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,23 @@ SOURCE_VER ?= v0.21.0
2727
# Change this if you bump the image-reflector-controller/api version in go.mod.
2828
REFLECTOR_VER ?= v0.15.0
2929

30-
# Version of libgit2 the controller should depend on.
31-
LIBGIT2_VERSION ?= 1.1.1
32-
3330
# Repository root based on Git metadata.
3431
REPOSITORY_ROOT := $(shell git rev-parse --show-toplevel)
3532

36-
# libgit2 related magical paths
37-
# These are used to determine if the target libgit2 version is already available on
38-
# the system, or where they should be installed to
39-
SYSTEM_LIBGIT2_VERSION := $(shell pkg-config --modversion libgit2 2>/dev/null)
40-
LIBGIT2_PATH := $(REPOSITORY_ROOT)/hack/libgit2
33+
LIBGIT2_PATH := $(REPOSITORY_ROOT)/build/libgit2
4134
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
42-
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.so.$(LIBGIT2_VERSION)
35+
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
36+
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
4337

44-
ifneq ($(LIBGIT2_VERSION),$(SYSTEM_LIBGIT2_VERSION))
45-
LIBGIT2_FORCE ?= 1
46-
endif
38+
export CGO_ENABLED=1
39+
export LIBRARY_PATH=$(LIBGIT2_LIB_PATH):$(LIBGIT2_LIB64_PATH)
40+
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig:$(LIBGIT2_LIB64_PATH)/pkgconfig
41+
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include
4742

4843
ifeq ($(shell uname -s),Darwin)
49-
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.$(LIBGIT2_VERSION).dylib
50-
HAS_BREW := $(shell brew --version 2>/dev/null)
51-
ifdef HAS_BREW
52-
HAS_OPENSSL := $(shell brew --prefix [email protected])
53-
endif
44+
export CGO_LDFLAGS=-L$(LIBGIT2_LIB_PATH) -lssh2 -lssl -lcrypto -lgit2
45+
else
46+
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2)
5447
endif
5548

5649
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
@@ -60,15 +53,6 @@ else
6053
GOBIN=$(shell go env GOBIN)
6154
endif
6255

63-
ifeq ($(strip ${PKG_CONFIG_PATH}),)
64-
MAKE_PKG_CONFIG_PATH = $(LIBGIT2_LIB_PATH)/pkgconfig
65-
else
66-
MAKE_PKG_CONFIG_PATH = ${PKG_CONFIG_PATH}:$(LIBGIT2_LIB_PATH)/pkgconfig
67-
endif
68-
69-
ifdef HAS_OPENSSL
70-
MAKE_PKG_CONFIG_PATH := $(MAKE_PKG_CONFIG_PATH):$(HAS_OPENSSL)/lib/pkgconfig
71-
endif
7256

7357
TEST_CRDS := controllers/testdata/crds
7458

@@ -106,41 +90,21 @@ ${CACHE}/imagepolicies_${REFLECTOR_VER}.yaml:
10690

10791
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
10892
test: $(LIBGIT2) test-api test_deps generate fmt vet manifests api-docs install-envtest ## Run tests
109-
ifeq ($(shell uname -s),Darwin)
110-
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
111-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
112-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
113-
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
114-
go test ./... -coverprofile cover.out
115-
else
116-
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
117-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
11893
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
119-
go test ./... -coverprofile cover.out
120-
endif
94+
go test ./... \
95+
-ldflags "-s -w" \
96+
-coverprofile cover.out \
97+
-tags 'netgo,osusergo,static_build'
12198

12299
test-api: ## Run api tests
123100
cd api; go test ./... -coverprofile cover.out
124101

125102
manager: $(LIBGIT2) generate fmt vet ## Build manager binary
126-
ifeq ($(shell uname -s),Darwin)
127-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
128-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
129-
go build -o bin/manager main.go
130-
else
131-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
132-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
133-
go build -o bin/manager main.go
134-
endif
103+
go run ./main.go
135104

136105

137106
run: $(LIBGIT2) generate fmt vet manifests # Run against the configured Kubernetes cluster in ~/.kube/config
138-
ifeq ($(shell uname -s),Darwin)
139-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
140-
go run ./main.go --log-level=${LOG_LEVEL} --log-encoding=console
141-
else
142107
go run ./main.go --log-level=${LOG_LEVEL} --log-encoding=console
143-
endif
144108

145109
install: manifests ## Install CRDs into a cluster
146110
kustomize build config/crd | kubectl apply -f -
@@ -173,16 +137,8 @@ fmt: ## Run go fmt against code
173137
cd api; go fmt ./...
174138

175139
vet: $(LIBGIT2) ## Run go vet against code
176-
ifeq ($(shell uname -s),Darwin)
177-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
178-
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
179140
go vet ./...
180141
cd api; go vet ./...
181-
else
182-
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
183-
go vet ./...
184-
cd api; go vet ./...
185-
endif
186142

187143

188144
generate: controller-gen ## Generate code
@@ -211,14 +167,7 @@ controller-gen: ## Download controller-gen locally if necessary.
211167
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
212168

213169
$(LIBGIT2):
214-
ifeq (1, $(LIBGIT2_FORCE))
215-
@{ \
216-
set -e; \
217-
mkdir -p $(LIBGIT2_PATH); \
218-
curl -sL https://raw.githubusercontent.com/fluxcd/golang-with-libgit2/$(LIBGIT2_TAG)/hack/Makefile -o $(LIBGIT2_PATH)/Makefile; \
219-
INSTALL_PREFIX=$(LIBGIT2_PATH) make -C $(LIBGIT2_PATH) libgit2; \
220-
}
221-
endif
170+
IMG_TAG=$(LIBGIT2_IMG):$(LIBGIT2_TAG) ./hack/extract-libraries.sh
222171

223172
# Find or download gen-crd-api-reference-docs
224173
GEN_CRD_API_REFERENCE_DOCS = $(shell pwd)/bin/gen-crd-api-reference-docs

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)