Skip to content

Commit 5945880

Browse files
committed
separate managed and unmanaged code
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 3837d7b commit 5945880

16 files changed

+288
-564
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export CGO_CFLAGS=-I$(LIBGIT2_PATH)/include -I$(LIBGIT2_PATH)/include/openssl
4343

4444
# The pkg-config command will yield warning messages until libgit2 is downloaded.
4545
ifeq ($(shell uname -s),Darwin)
46-
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libssh2 openssl libgit2 2>/dev/null)
46+
export CGO_LDFLAGS=$(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) pkg-config --libs --static --cflags libgit2 2>/dev/null)
4747
GO_STATIC_FLAGS=-ldflags "-s -w" -tags 'netgo,osusergo,static_build$(addprefix ,,$(GO_TAGS))'
4848
else
4949
export PKG_CONFIG_PATH:=$(PKG_CONFIG_PATH):$(LIBGIT2_LIB64_PATH)/pkgconfig
@@ -98,9 +98,9 @@ build: check-deps $(LIBGIT2) ## Build manager binary
9898

9999
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
100100
test: $(LIBGIT2) install-envtest test-api check-deps ## Run tests
101-
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
101+
cd pkg/git/libgit2 && KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
102102
GIT_CONFIG_GLOBAL=/dev/null \
103-
go test $(GO_STATIC_FLAGS) \
103+
go test $(GO_STATIC_FLAGS) -v \
104104
./... \
105105
$(GO_TEST_ARGS) \
106106
-coverprofile cover.out

controllers/gitrepository_controller.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,26 +455,8 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
455455
return sreconcile.ResultEmpty, e
456456
}
457457

458-
repositoryURL := obj.Spec.URL
459-
// managed GIT transport only affects the libgit2 implementation
460-
if managed.Enabled() && obj.Spec.GitImplementation == sourcev1.LibGit2Implementation {
461-
// We set the TransportAuthID of this set of authentication options here by constructing
462-
// a unique ID that won't clash in a multi tenant environment. This unique ID is used by
463-
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
464-
// libgit2, which is inflexible and unstable.
465-
if strings.HasPrefix(repositoryURL, "http") {
466-
authOpts.TransportAuthID = fmt.Sprintf("http://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
467-
}
468-
if strings.HasPrefix(repositoryURL, "ssh") {
469-
authOpts.TransportAuthID = fmt.Sprintf("ssh://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
470-
}
471-
}
472-
473458
// Fetch the included artifact metadata.
474459
artifacts, err := r.fetchIncludes(ctx, obj)
475-
if err != nil {
476-
return sreconcile.ResultEmpty, err
477-
}
478460

479461
// Observe if the artifacts still match the previous included ones
480462
if artifacts.Diff(obj.Status.IncludedArtifacts) {
@@ -491,7 +473,27 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
491473
optimizedClone = true
492474
}
493475

494-
c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, optimizedClone)
476+
// managed GIT transport only affects the libgit2 implementation
477+
if managed.Enabled() && obj.Spec.GitImplementation == sourcev1.LibGit2Implementation {
478+
// We set the TransportAuthID of this set of authentication options here by constructing
479+
// a unique ID that won't clash in a multi tenant environment. This unique ID is used by
480+
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
481+
// libgit2, which is inflexible and unstable.
482+
if strings.HasPrefix(obj.Spec.URL, "http") {
483+
authOpts.TransportAuthID = fmt.Sprintf("http://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
484+
} else if strings.HasPrefix(obj.Spec.URL, "ssh") {
485+
authOpts.TransportAuthID = fmt.Sprintf("ssh://%s/%s/%d", obj.Name, obj.UID, obj.Generation)
486+
} else {
487+
e := &serror.Stalling{
488+
Err: fmt.Errorf("git repository URL has invalid transport type: '%s'", obj.Spec.URL),
489+
Reason: sourcev1.GitOperationFailedReason,
490+
}
491+
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Err.Error())
492+
return sreconcile.ResultEmpty, e
493+
}
494+
}
495+
496+
c, err := r.gitCheckout(ctx, obj, obj.Spec.URL, authOpts, dir, optimizedClone)
495497
if err != nil {
496498
e := serror.NewGeneric(
497499
fmt.Errorf("failed to checkout and determine revision: %w", err),
@@ -530,7 +532,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
530532

531533
// If we can't skip the reconciliation, checkout again without any
532534
// optimization.
533-
c, err := r.gitCheckout(ctx, obj, repositoryURL, authOpts, dir, false)
535+
c, err := r.gitCheckout(ctx, obj, obj.Spec.URL, authOpts, dir, false)
534536
if err != nil {
535537
e := serror.NewGeneric(
536538
fmt.Errorf("failed to checkout and determine revision: %w", err),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/fluxcd/pkg/helmtestserver v0.7.2
2929
github.com/fluxcd/pkg/lockedfile v0.1.0
3030
github.com/fluxcd/pkg/runtime v0.15.1
31-
github.com/fluxcd/pkg/ssh v0.3.4
31+
github.com/fluxcd/pkg/ssh v0.4.0
3232
github.com/fluxcd/pkg/testserver v0.2.0
3333
github.com/fluxcd/pkg/untar v0.1.0
3434
github.com/fluxcd/pkg/version v0.1.0

0 commit comments

Comments
 (0)