Skip to content

Commit 5ffa6a5

Browse files
author
Paulo Gomes
committed
libgit2: Remove references to libgit2 from code
Signed-off-by: Paulo Gomes <[email protected]>
1 parent aad4060 commit 5ffa6a5

File tree

13 files changed

+106
-1609
lines changed

13 files changed

+106
-1609
lines changed

.dockerignore

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

ATTRIBUTIONS.md

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

Dockerfile

Lines changed: 13 additions & 27 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 --no-cache clang lld pkgconfig
15+
RUN apk add --no-cache clang lld
2116

2217
COPY --from=xx / /
2318

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

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

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

6147
ARG TARGETPLATFORM
6248
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
6353
ENV CGO_ENABLED=1
6454

65-
# Instead of using xx-go, (cross) compile with vanilla go leveraging musl tool chain.
66-
RUN export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig" && \
67-
export CGO_LDFLAGS="$(pkg-config --static --libs --cflags libgit2) -static -fuse-ld=lld" && \
55+
RUN export CGO_LDFLAGS="-static -fuse-ld=lld" && \
6856
xx-go build \
6957
-ldflags "-s -w" \
7058
-tags 'netgo,osusergo,static_build' \
@@ -73,7 +61,6 @@ RUN export PKG_CONFIG_PATH="/usr/local/$(xx-info triple)/lib/pkgconfig" && \
7361
# Ensure that the binary was cross-compiled correctly to the target platform.
7462
RUN xx-verify --static /source-controller
7563

76-
7764
FROM alpine:3.16
7865

7966
ARG TARGETPLATFORM
@@ -82,7 +69,6 @@ RUN apk --no-cache add ca-certificates \
8269

8370
# Copy over binary from build
8471
COPY --from=build /source-controller /usr/local/bin/
85-
COPY ATTRIBUTIONS.md /
8672

8773
USER 65534:65534
8874
ENTRYPOINT [ "source-controller" ]

Makefile

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
IMG ?= fluxcd/source-controller
33
TAG ?= latest
44

5-
# Base image used to build the Go binary
6-
LIBGIT2_IMG ?= ghcr.io/fluxcd/golang-with-libgit2-only
7-
LIBGIT2_TAG ?= v0.4.0
8-
95
# Allows for defining additional Go test args, e.g. '-tags integration'.
106
GO_TEST_ARGS ?= -race
117

@@ -39,14 +35,6 @@ ENVTEST_BIN_VERSION ?= 1.24.0
3935
# each fuzzer should run for.
4036
FUZZ_TIME ?= 1m
4137

42-
# Caches libgit2 versions per tag, "forcing" rebuild only when needed.
43-
LIBGIT2_PATH := $(BUILD_DIR)/libgit2/$(LIBGIT2_TAG)
44-
LIBGIT2_LIB_PATH := $(LIBGIT2_PATH)/lib
45-
LIBGIT2 := $(LIBGIT2_LIB_PATH)/libgit2.a
46-
47-
export CGO_ENABLED=1
48-
export PKG_CONFIG_PATH=$(LIBGIT2_LIB_PATH)/pkgconfig
49-
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
5038
GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build$(addprefix ,,$(GO_TAGS))'
5139

5240
# API (doc) generation utilities
@@ -75,11 +63,11 @@ endif
7563

7664
all: build
7765

78-
build: check-deps $(LIBGIT2) ## Build manager binary
66+
build: check-deps ## Build manager binary
7967
go build $(GO_STATIC_FLAGS) -o $(BUILD_DIR)/bin/manager main.go
8068

8169
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
82-
test: $(LIBGIT2) install-envtest test-api check-deps ## Run all tests
70+
test: install-envtest test-api check-deps ## Run all tests
8371
HTTPS_PROXY="" HTTP_PROXY="" \
8472
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
8573
GIT_CONFIG_GLOBAL=/dev/null \
@@ -88,7 +76,7 @@ test: $(LIBGIT2) install-envtest test-api check-deps ## Run all tests
8876
$(GO_TEST_ARGS) \
8977
-coverprofile cover.out
9078

91-
test-ctrl: $(LIBGIT2) install-envtest test-api check-deps ## Run controller tests
79+
test-ctrl: install-envtest test-api check-deps ## Run controller tests
9280
HTTPS_PROXY="" HTTP_PROXY="" \
9381
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
9482
GIT_CONFIG_GLOBAL=/dev/null \
@@ -105,7 +93,7 @@ endif
10593
test-api: ## Run api tests
10694
cd api; go test $(GO_TEST_ARGS) ./... -coverprofile cover.out
10795

108-
run: $(LIBGIT2) generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
96+
run: generate fmt vet manifests ## Run against the configured Kubernetes cluster in ~/.kube/config
10997
go run $(GO_STATIC_FLAGS) ./main.go
11098

11199
install: manifests ## Install CRDs into a cluster
@@ -139,7 +127,7 @@ fmt: ## Run go fmt against code
139127
go fmt ./...
140128
cd api; go fmt ./...
141129

142-
vet: $(LIBGIT2) ## Run go vet against code
130+
vet: ## Run go vet against code
143131
go vet ./...
144132
cd api; go vet ./...
145133

@@ -148,8 +136,6 @@ generate: controller-gen ## Generate API code
148136

149137
docker-build: ## Build the Docker image
150138
docker buildx build \
151-
--build-arg LIBGIT2_IMG=$(LIBGIT2_IMG) \
152-
--build-arg LIBGIT2_TAG=$(LIBGIT2_TAG) \
153139
--platform=$(BUILD_PLATFORMS) \
154140
-t $(IMG):$(TAG) \
155141
$(BUILD_ARGS) .
@@ -182,15 +168,6 @@ install-envtest: setup-envtest ## Download envtest binaries locally.
182168
# setup-envtest sets anything below k8s to 0555
183169
chmod -R u+w $(BUILD_DIR)/testbin
184170

185-
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
186-
187-
COSIGN = $(GOBIN)/cosign
188-
$(LIBGIT2):
189-
$(call go-install-tool,$(COSIGN),github.com/sigstore/cosign/cmd/cosign@latest)
190-
191-
IMG=$(LIBGIT2_IMG) TAG=$(LIBGIT2_TAG) PATH=$(PATH):$(GOBIN) ./hack/install-libraries.sh
192-
193-
194171
.PHONY: help
195172
help: ## Display this help menu
196173
@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)
@@ -202,20 +179,6 @@ e2e:
202179
./hack/ci/e2e.sh
203180

204181
verify: update-attributions fmt vet manifests api-docs tidy
205-
ifneq ($(shell grep -o 'LIBGIT2_IMG ?= \w.*' Makefile | cut -d ' ' -f 3):$(shell grep -o 'LIBGIT2_TAG ?= \w.*' Makefile | cut -d ' ' -f 3), \
206-
$(shell grep -o "LIBGIT2_IMG=\w.*" Dockerfile | cut -d'=' -f2):$(shell grep -o "LIBGIT2_TAG=\w.*" Dockerfile | cut -d'=' -f2))
207-
@{ \
208-
echo "LIBGIT2_IMG and LIBGIT2_TAG must match in both Makefile and Dockerfile"; \
209-
exit 1; \
210-
}
211-
endif
212-
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'))
213-
@{ \
214-
echo "LIBGIT2_TAG must match in both Makefile and tests/fuzz/oss_fuzz_prebuild.sh"; \
215-
exit 1; \
216-
}
217-
endif
218-
219182
@if [ ! "$$(git status --porcelain --untracked-files=no)" = "" ]; then \
220183
echo "working directory is dirty:"; \
221184
git --no-pager diff; \
@@ -236,7 +199,7 @@ rm -rf $$TMP_DIR ;\
236199
endef
237200

238201
# Build fuzzers used by oss-fuzz.
239-
fuzz-build: $(LIBGIT2)
202+
fuzz-build:
240203
rm -rf $(shell pwd)/build/fuzz/
241204
mkdir -p $(shell pwd)/build/fuzz/out/
242205

@@ -260,15 +223,3 @@ fuzz-native:
260223
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
261224
FUZZ_TIME=$(FUZZ_TIME) \
262225
./tests/fuzz/native_go_run.sh
263-
264-
# Creates an env file that can be used to load all source-controller's dependencies
265-
# this is handy when you want to run adhoc debug sessions on tests or start the
266-
# controller in a new debug session.
267-
env: $(LIBGIT2)
268-
echo 'GO_ENABLED="1"' > $(BUILD_DIR)/.env
269-
echo 'PKG_CONFIG_PATH="$(PKG_CONFIG_PATH)"' >> $(BUILD_DIR)/.env
270-
echo 'LIBRARY_PATH="$(LIBRARY_PATH)"' >> $(BUILD_DIR)/.env
271-
echo 'CGO_CFLAGS="$(CGO_CFLAGS)"' >> $(BUILD_DIR)/.env
272-
echo 'CGO_LDFLAGS="$(CGO_LDFLAGS)"' >> $(BUILD_DIR)/.env
273-
echo 'KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS)' >> $(BUILD_DIR)/.env
274-
echo 'GIT_CONFIG_GLOBAL=/dev/null' >> $(BUILD_DIR)/.env

config/testdata/git/large-repo.yaml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
apiVersion: source.toolkit.fluxcd.io/v1beta1
22
kind: GitRepository
33
metadata:
4-
name: large-repo-go-git
4+
name: large-repo
55
spec:
6-
gitImplementation: go-git
7-
interval: 10m
8-
timeout: 2m
9-
url: https://github.com/hashgraph/hedera-mirror-node.git
10-
ref:
11-
branch: main
12-
ignore: |
13-
/*
14-
!/charts
15-
---
16-
apiVersion: source.toolkit.fluxcd.io/v1beta1
17-
kind: GitRepository
18-
metadata:
19-
name: large-repo-libgit2
20-
spec:
21-
gitImplementation: libgit2
226
interval: 10m
237
timeout: 2m
248
url: https://github.com/hashgraph/hedera-mirror-node.git

controllers/gitrepository_controller.go

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545
"github.com/fluxcd/pkg/apis/meta"
4646
"github.com/fluxcd/pkg/git"
4747
"github.com/fluxcd/pkg/git/gogit"
48-
"github.com/fluxcd/pkg/git/libgit2"
4948
"github.com/fluxcd/pkg/git/repository"
5049
"github.com/fluxcd/pkg/runtime/conditions"
5150
helper "github.com/fluxcd/pkg/runtime/controller"
@@ -116,9 +115,6 @@ type GitRepositoryReconciler struct {
116115

117116
Storage *Storage
118117
ControllerName string
119-
// Libgit2TransportInitialized lets the reconciler know whether
120-
// libgit2 transport was intialized successfully.
121-
Libgit2TransportInitialized func() bool
122118

123119
requeueDependency time.Duration
124120
features map[string]bool
@@ -423,18 +419,6 @@ func (r *GitRepositoryReconciler) reconcileStorage(ctx context.Context,
423419
// change, it short-circuits the whole reconciliation with an early return.
424420
func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
425421
obj *sourcev1.GitRepository, commit *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error) {
426-
gitImplementation := obj.Spec.GitImplementation
427-
if goGitOnly, _ := r.features[features.ForceGoGitImplementation]; goGitOnly {
428-
gitImplementation = sourcev1.GoGitImplementation
429-
}
430-
431-
// Exit early, if we need to use libgit2 AND managed transport hasn't been intialized.
432-
if !r.Libgit2TransportInitialized() && gitImplementation == sourcev1.LibGit2Implementation {
433-
return sreconcile.ResultEmpty, serror.NewStalling(
434-
errors.New("libgit2 managed transport not initialized"), "Libgit2TransportNotEnabled",
435-
)
436-
}
437-
438422
// Remove previously failed source verification status conditions. The
439423
// failing verification should be recalculated. But an existing successful
440424
// verification need not be removed as it indicates verification of previous
@@ -505,7 +489,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
505489
optimizedClone = true
506490
}
507491

508-
c, err := r.gitCheckout(ctx, obj, authOpts, dir, optimizedClone, gitImplementation)
492+
c, err := r.gitCheckout(ctx, obj, authOpts, dir, optimizedClone)
509493
if err != nil {
510494
return sreconcile.ResultEmpty, err
511495
}
@@ -539,7 +523,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
539523

540524
// If we can't skip the reconciliation, checkout again without any
541525
// optimization.
542-
c, err := r.gitCheckout(ctx, obj, authOpts, dir, false, gitImplementation)
526+
c, err := r.gitCheckout(ctx, obj, authOpts, dir, false)
543527
if err != nil {
544528
return sreconcile.ResultEmpty, err
545529
}
@@ -732,7 +716,7 @@ func (r *GitRepositoryReconciler) reconcileInclude(ctx context.Context,
732716
// performs a git checkout.
733717
func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
734718
obj *sourcev1.GitRepository, authOpts *git.AuthOptions, dir string,
735-
optimized bool, gitImplementation string) (*git.Commit, error) {
719+
optimized bool) (*git.Commit, error) {
736720
// Configure checkout strategy.
737721
cloneOpts := repository.CloneOptions{
738722
RecurseSubmodules: obj.Spec.RecurseSubmodules,
@@ -757,28 +741,15 @@ func (r *GitRepositoryReconciler) gitCheckout(ctx context.Context,
757741
gitCtx, cancel := context.WithTimeout(ctx, obj.Spec.Timeout.Duration)
758742
defer cancel()
759743

760-
var gitReader repository.Reader
761-
var err error
762-
763-
switch gitImplementation {
764-
case sourcev1.LibGit2Implementation:
765-
clientOpts := []libgit2.ClientOption{libgit2.WithDiskStorage()}
766-
if authOpts.Transport == git.HTTP {
767-
clientOpts = append(clientOpts, libgit2.WithInsecureCredentialsOverHTTP())
768-
}
769-
gitReader, err = libgit2.NewClient(dir, authOpts, clientOpts...)
770-
case sourcev1.GoGitImplementation:
771-
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage()}
772-
if authOpts.Transport == git.HTTP {
773-
clientOpts = append(clientOpts, gogit.WithInsecureCredentialsOverHTTP())
774-
}
775-
gitReader, err = gogit.NewClient(dir, authOpts, clientOpts...)
776-
default:
777-
err = fmt.Errorf("invalid Git implementation: %s", gitImplementation)
744+
clientOpts := []gogit.ClientOption{gogit.WithDiskStorage()}
745+
if authOpts.Transport == git.HTTP {
746+
clientOpts = append(clientOpts, gogit.WithInsecureCredentialsOverHTTP())
778747
}
748+
749+
gitReader, err := gogit.NewClient(dir, authOpts, clientOpts...)
779750
if err != nil {
780751
e := serror.NewGeneric(
781-
fmt.Errorf("failed to create Git client for implementation '%s': %w", gitImplementation, err),
752+
fmt.Errorf("failed to create Git client: %w", err),
782753
sourcev1.GitOperationFailedReason,
783754
)
784755
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())

0 commit comments

Comments
 (0)