Skip to content

Commit f48e3bd

Browse files
author
Paulo Gomes
authored
Merge pull request #468 from fluxcd/remove-libgit2
Remove libgit2 and git2go from codebase
2 parents 5453088 + 5048e38 commit f48e3bd

24 files changed

+483
-2682
lines changed

.dockerignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
build/libgit2/
1+
build/

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ testbin
2929
*.swo
3030
*~
3131

32-
# Exclude all libgit2 related files
32+
# Exclude all build related files
3333
build/

ATTRIBUTIONS.md

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

DEVELOPMENT.md

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,80 +13,7 @@ There are a number of dependencies required to be able to run the controller and
1313
- [Install Docker](https://docs.docker.com/engine/install/)
1414
- (Optional) [Install Kubebuilder](https://book.kubebuilder.io/quick-start.html#installation)
1515

16-
The dependency [libgit2](https://libgit2.org/) also needs to be installed to be able
17-
to run `image-automation-controller` or its test-suite locally (not in a container).
18-
19-
In case this dependency is not present on your system (at the expected
20-
version), the first invocation of a `make` target that requires the
21-
dependency will attempt to compile it locally to `hack/libgit2`. For this build
22-
to succeed ensure the following dependencies are present on your system:
23-
- [CMake](https://cmake.org/download/)
24-
- [OpenSSL 1.1](https://www.openssl.org/source/)
25-
- [LibSSH2](https://www.libssh2.org/)
26-
- [pkg-config](https://freedesktop.org/wiki/Software/pkg-config/)
27-
28-
29-
Triggering a manual build of the dependency is possible as well by running
30-
`make libgit2`. To enforce the build, for example if your system dependencies
31-
match but are not linked in a compatible way, append `LIBGIT2_FORCE=1` to the
32-
`make` command.
33-
34-
Follow the instructions below to install these dependencies to your system.
35-
36-
### macOS
37-
38-
```console
39-
$ # Ensure libgit2 dependencies are available
40-
$ brew install cmake [email protected] libssh2 pkg-config
41-
$ LIBGIT2_FORCE=1 make libgit2
42-
```
43-
44-
#### openssl and pkg-config
45-
46-
You may see this message when trying to run a build:
47-
48-
```
49-
# pkg-config --cflags libgit2
50-
Package openssl was not found in the pkg-config search path.
51-
Perhaps you should add the directory containing `openssl.pc'
52-
to the PKG_CONFIG_PATH environment variable
53-
Package 'openssl', required by 'libgit2', not found
54-
pkg-config: exit status 1
55-
```
56-
57-
On some macOS systems `brew` will not link the pkg-config file for
58-
openssl into expected directory, because it clashes with a
59-
system-provided openssl. When installing it, or if you do
60-
61-
```console
62-
63-
Warning: Refusing to link macOS provided/shadowed software: [email protected]
64-
If you need to have [email protected] first in your PATH, run:
65-
echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.profile
66-
67-
For compilers to find [email protected] you may need to set:
68-
export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
69-
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"
70-
71-
For pkg-config to find [email protected] you may need to set:
72-
export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"
73-
```
74-
75-
.. it tells you (in the last part of the message) how to add `openssl`
76-
to your `PKG_CONFIG_PATH` so it can be found.
77-
78-
### Linux
79-
80-
```console
81-
$ # Ensure libgit2 dependencies are available
82-
$ pacman -S cmake openssl libssh2
83-
$ LIBGIT2_FORCE=1 make libgit2
84-
```
85-
86-
**Note:** Example shown is for Arch Linux, but likewise procedure can be
87-
followed using any other package manager, e.g. `apt`.
88-
89-
In addition to the above, the following dependencies are also used by some of the `make` targets:
16+
The following dependencies are also used by some of the `make` targets:
9017

9118
- `controller-gen` (v0.7.0)
9219
- `gen-crd-api-reference-docs` (v0.3.0)
@@ -169,18 +96,11 @@ Create a `.vscode/launch.json` file:
16996
"type": "go",
17097
"request": "launch",
17198
"mode": "auto",
172-
"envFile": "${workspaceFolder}/build/.env",
17399
"program": "${workspaceFolder}/main.go"
174100
}
175101
]
176102
}
177103
```
178104

179-
Create the environment file containing details on how to load
180-
`libgit2` dependencies:
181-
```bash
182-
make env
183-
```
184-
185105
Start debugging by either clicking `Run` > `Start Debugging` or using
186106
the relevant shortcut.

Dockerfile

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ ARG BASE_VARIANT=alpine
22
ARG GO_VERSION=1.19
33
ARG XX_VERSION=1.1.2
44

5-
ARG LIBGIT2_IMG=ghcr.io/fluxcd/golang-with-libgit2-only
6-
ARG LIBGIT2_TAG=v0.4.0
7-
8-
FROM ${LIBGIT2_IMG}:${LIBGIT2_TAG} AS libgit2-libs
9-
105
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
116

127
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_VARIANT} as gostable
@@ -17,12 +12,18 @@ FROM gostable AS go-linux
1712
# These will be used at current arch to yield execute the cross compilations.
1813
FROM go-${TARGETOS} AS build-base
1914

20-
RUN apk add clang lld pkgconfig
15+
RUN apk add clang lld
2116

2217
COPY --from=xx / /
2318

24-
# build-go-mod can still be cached at build platform architecture.
25-
FROM build-base as build-go-mod
19+
# build can still be cached at build platform architecture.
20+
FROM build-base as build
21+
22+
ARG TARGETPLATFORM
23+
24+
# Some dependencies have to installed for the target platform:
25+
# https://github.com/tonistiigi/xx#go--cgo
26+
RUN xx-apk add musl-dev gcc
2627

2728
# Configure workspace
2829
WORKDIR /workspace
@@ -37,20 +38,6 @@ COPY go.sum go.sum
3738
# Cache modules
3839
RUN go mod download
3940

40-
# Build stage install per target platform
41-
# dependency and effectively cross compile the application.
42-
FROM build-go-mod as build
43-
44-
ARG TARGETPLATFORM
45-
46-
COPY --from=libgit2-libs /usr/local/ /usr/local/
47-
48-
# Some dependencies have to installed
49-
# for the target platform: https://github.com/tonistiigi/xx#go--cgo
50-
RUN xx-apk add musl-dev gcc clang lld
51-
52-
WORKDIR /workspace
53-
5441
# Copy source code
5542
COPY main.go main.go
5643
COPY controllers/ controllers/
@@ -59,11 +46,13 @@ COPY internal/ internal/
5946

6047
ARG TARGETPLATFORM
6148
ARG TARGETARCH
49+
50+
# Reasons why CGO is in use:
51+
# - The SHA1 implementation (sha1cd) used by go-git depends on CGO for
52+
# performance reasons. See: https://github.com/pjbgf/sha1cd/issues/15
6253
ENV CGO_ENABLED=1
6354

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" && \
55+
RUN export CGO_LDFLAGS="-static -fuse-ld=lld" && \
6756
xx-go build \
6857
-ldflags "-s -w" \
6958
-tags 'netgo,osusergo,static_build' \
@@ -80,7 +69,6 @@ RUN apk --no-cache add ca-certificates \
8069

8170
# Copy over binary from build
8271
COPY --from=build /image-automation-controller /usr/local/bin/
83-
COPY ATTRIBUTIONS.md /
8472

8573
USER 65534:65534
8674
ENTRYPOINT [ "image-automation-controller" ]

Makefile

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ TAG ?= latest
66
# Produce CRDs that work back to Kubernetes 1.16
77
CRD_OPTIONS ?= crd:crdVersions=v1
88

9-
# Base image used to build the Go binary
10-
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-only
11-
LIBGIT2_TAG ?= v0.4.0
12-
139
# Allows for defining additional Docker buildx arguments,
1410
# e.g. '--push'.
1511
BUILD_ARGS ?=
@@ -44,19 +40,6 @@ ENVTEST_BIN_VERSION ?= 1.19.2
4440
# each fuzzer should run for.
4541
FUZZ_TIME ?= 1m
4642

47-
# Caches libgit2 versions per tag, "forcing" rebuild only when needed.
48-
LIBGIT2_PATH := $(BUILD_DIR)/libgit2/$(LIBGIT2_TAG)
49-
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
50-
LIBGIT2_LIB64_PATH := $(LIBGIT2_PATH)/lib64
51-
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
52-
53-
export CGO_ENABLED=1
54-
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig
55-
export LIBRARY_PATH=$(LIBGIT2_LIB_PATH)
56-
export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include -I$(LIBGIT2_PATH)/include/openssl
57-
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
58-
59-
# The pkg-config command will yield warning messages until libgit2 is downloaded.
6043
ifeq ($(shell uname -s),Darwin)
6144
GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build'
6245
endif
@@ -129,17 +112,17 @@ ifeq ($(shell uname -s),Darwin)
129112
endif
130113

131114
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
132-
test: $(LIBGIT2) tidy test-api test_deps generate fmt vet manifests api-docs install-envtest ## Run tests
115+
test: tidy test-api test_deps generate fmt vet manifests api-docs install-envtest ## Run tests
133116
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
134117
go test $(GO_STATIC_FLAGS) $(GO_TEST_ARGS) ./... -coverprofile cover.out
135118

136119
test-api: ## Run api tests
137120
cd api; go test $(GO_TEST_ARGS) ./... -coverprofile cover.out
138121

139-
manager: $(LIBGIT2) generate fmt vet ## Build manager binary
122+
manager: generate fmt vet ## Build manager binary
140123
go build -o $(BUILD_DIR)/bin/manager ./main.go
141124

142-
run: $(LIBGIT2) generate fmt vet manifests # Run against the configured Kubernetes cluster in ~/.kube/config
125+
run: generate fmt vet manifests # Run against the configured Kubernetes cluster in ~/.kube/config
143126
go run $(GO_STATIC_FLAGS) ./main.go --log-level=${LOG_LEVEL} --log-encoding=console
144127

145128
install: manifests ## Install CRDs into a cluster
@@ -172,7 +155,7 @@ fmt: ## Run go fmt against code
172155
go fmt ./...
173156
cd api; go fmt ./...
174157

175-
vet: $(LIBGIT2) ## Run go vet against code
158+
vet: ## Run go vet against code
176159
go vet ./...
177160
cd api; go vet ./...
178161

@@ -182,8 +165,6 @@ generate: controller-gen ## Generate code
182165

183166
docker-build: ## Build the Docker image
184167
docker buildx build \
185-
--build-arg LIBGIT2_IMG=$(LIBGIT2_IMG) \
186-
--build-arg LIBGIT2_TAG=$(LIBGIT2_TAG) \
187168
--platform=$(BUILD_PLATFORMS) \
188169
-t $(IMG):$(TAG) \
189170
$(BUILD_ARGS) .
@@ -200,14 +181,6 @@ CONTROLLER_GEN = $(GOBIN)/controller-gen
200181
controller-gen: ## Download controller-gen locally if necessary.
201182
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
202183

203-
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
204-
205-
COSIGN = $(GOBIN)/cosign
206-
$(LIBGIT2):
207-
$(call go-install-tool,$(COSIGN),github.com/sigstore/cosign/cmd/cosign@latest)
208-
209-
IMG=$(LIBGIT2_IMG) TAG=$(LIBGIT2_TAG) PATH=$(PATH):$(GOBIN) ./hack/install-libraries.sh
210-
211184
# Find or download gen-crd-api-reference-docs
212185
GEN_CRD_API_REFERENCE_DOCS = $(GOBIN)/gen-crd-api-reference-docs
213186
.PHONY: gen-crd-api-reference-docs
@@ -227,7 +200,7 @@ setup-envtest: ## Download envtest-setup locally if necessary.
227200
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
228201

229202
# Build fuzzers used by oss-fuzz.
230-
fuzz-build: $(LIBGIT2)
203+
fuzz-build:
231204
rm -rf $(shell pwd)/build/fuzz/
232205
mkdir -p $(shell pwd)/build/fuzz/out/
233206

@@ -268,20 +241,7 @@ endef
268241
update-attributions:
269242
./hack/update-attributions.sh
270243

271-
verify: update-attributions fmt
272-
ifneq ($(shell grep -o 'LIBGIT2_IMG ?= \w.*' Makefile | cut -d ' ' -f 3):$(shell grep -o 'LIBGIT2_TAG ?= \w.*' Makefile | cut -d ' ' -f 3), \
273-
$(shell grep -o "LIBGIT2_IMG=\w.*" Dockerfile | cut -d'=' -f2):$(shell grep -o "LIBGIT2_TAG=\w.*" Dockerfile | cut -d'=' -f2))
274-
@{ \
275-
echo "LIBGIT2_IMG and LIBGIT2_TAG must match in both Makefile and Dockerfile"; \
276-
exit 1; \
277-
}
278-
endif
279-
ifneq ($(shell grep -o 'LIBGIT2_TAG ?= \w.*' Makefile | cut -d ' ' -f 3), $(shell grep -o "LIBGIT2_TAG=.*" tests/fuzz/oss_fuzz_prebuild.sh | sed 's;LIBGIT2_TAG="$${LIBGIT2_TAG:-;;g' | sed 's;}";;g'))
280-
@{ \
281-
echo "LIBGIT2_TAG must match in both Makefile and tests/fuzz/oss_fuzz_prebuild.sh"; \
282-
exit 1; \
283-
}
284-
endif
244+
verify:
285245
ifneq (, $(shell git status --porcelain --untracked-files=no))
286246
@{ \
287247
echo "working directory is dirty:"; \
@@ -293,15 +253,3 @@ endif
293253
.PHONY: help
294254
help: ## Display this help menu
295255
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
296-
297-
# Creates an env file that can be used to load all image-automation-controller's
298-
# dependencies this is handy when you want to run adhoc debug sessions on tests or
299-
# start the controller in a new debug session.
300-
env: $(LIBGIT2)
301-
echo 'GO_ENABLED="1"' > $(BUILD_DIR)/.env
302-
echo 'PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)"' >> $(BUILD_DIR)/.env
303-
echo 'LIBRARY_PATH="$(LIBRARY_PATH)"' >> $(BUILD_DIR)/.env
304-
echo 'CGO_CFLAGS="$(CGO_CFLAGS)"' >> $(BUILD_DIR)/.env
305-
echo 'CGO_LDFLAGS="$(CGO_LDFLAGS)"' >> $(BUILD_DIR)/.env
306-
echo 'KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS)' >> $(BUILD_DIR)/.env
307-
echo 'GIT_CONFIG_GLOBAL=/dev/null' >> $(BUILD_DIR)/.env

README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,5 @@ guide](https://fluxcd.io/flux/guides/image-update/).
2121

2222
## How to work on it
2323

24-
The shared library `libgit2` needs to be installed to test or build
25-
locally. The version required corresponds to the version of git2go
26-
(which are Go bindings for libgit2), according to [this
27-
table](https://github.com/libgit2/git2go#which-go-version-to-use).
28-
29-
See
30-
https://github.com/fluxcd/source-controller/blob/main/DEVELOPMENT.md#installing-required-dependencies
31-
for instructions on how to install `libgit2`.
24+
For additional information on dependecies and how to contribute
25+
please refer to [DEVELOPMENT.md](DEVELOPMENT.md).

controllers/imageupdateautomation_controller.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import (
5252
"github.com/fluxcd/pkg/apis/meta"
5353
"github.com/fluxcd/pkg/git"
5454
"github.com/fluxcd/pkg/git/gogit"
55-
"github.com/fluxcd/pkg/git/libgit2"
5655
"github.com/fluxcd/pkg/git/repository"
5756
"github.com/fluxcd/pkg/runtime/acl"
5857
helper "github.com/fluxcd/pkg/runtime/controller"
@@ -258,33 +257,16 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
258257
return failWithError(err)
259258
}
260259

261-
gitImplementation := origin.Spec.GitImplementation
262-
if goGitOnly, _ := r.features[features.ForceGoGitImplementation]; goGitOnly {
263-
gitImplementation = sourcev1.GoGitImplementation
260+
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage()}
261+
forcePush, _ := features.Enabled(features.GitForcePushBranch)
262+
if forcePush && pushBranch != ref.Branch {
263+
clientOpts = append(clientOpts, gogit.WithForcePush())
264264
}
265-
266-
var gitClient repository.Client
267-
switch gitImplementation {
268-
case sourcev1.LibGit2Implementation:
269-
clientOpts := []libgit2.ClientOption{libgit2.WithDiskStorage()}
270-
if authOpts.Transport == git.HTTP {
271-
clientOpts = append(clientOpts, libgit2.WithInsecureCredentialsOverHTTP())
272-
}
273-
gitClient, err = libgit2.NewClient(tmp, authOpts, clientOpts...)
274-
case sourcev1.GoGitImplementation, "":
275-
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage()}
276-
forcePush, _ := features.Enabled(features.GitForcePushBranch)
277-
if forcePush && pushBranch != ref.Branch {
278-
clientOpts = append(clientOpts, gogit.WithForcePush())
279-
}
280-
if authOpts.Transport == git.HTTP {
281-
clientOpts = append(clientOpts, gogit.WithInsecureCredentialsOverHTTP())
282-
}
283-
284-
gitClient, err = gogit.NewClient(tmp, authOpts, clientOpts...)
285-
default:
286-
err = fmt.Errorf("failed to create git client; referred GitRepository has invalid implementation: %s", gitImplementation)
265+
if authOpts.Transport == git.HTTP {
266+
clientOpts = append(clientOpts, gogit.WithInsecureCredentialsOverHTTP())
287267
}
268+
269+
gitClient, err := gogit.NewClient(tmp, authOpts, clientOpts...)
288270
if err != nil {
289271
return failWithError(err)
290272
}

0 commit comments

Comments
 (0)