Skip to content

Commit 4a8ddfa

Browse files
author
Paulo Gomes
committed
git: Decommission libgit2 Unmanaged Transport
Signed-off-by: Paulo Gomes <[email protected]>
1 parent e3c7aef commit 4a8ddfa

File tree

3 files changed

+32
-52
lines changed

3 files changed

+32
-52
lines changed

controllers/imageupdateautomation_controller.go

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import (
6262
"github.com/fluxcd/pkg/runtime/predicates"
6363
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
6464
"github.com/fluxcd/source-controller/pkg/git"
65-
gitlibgit2 "github.com/fluxcd/source-controller/pkg/git/libgit2"
6665
"github.com/fluxcd/source-controller/pkg/git/libgit2/managed"
6766
gitstrat "github.com/fluxcd/source-controller/pkg/git/strategy"
6867

@@ -270,21 +269,18 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
270269
return failWithError(err)
271270
}
272271

273-
// managed GIT transport only affects the libgit2 implementation
274-
if managed.Enabled() {
275-
// We set the TransportOptionsURL of this set of authentication options here by constructing
276-
// a unique URL that won't clash in a multi tenant environment. This unique URL is used by
277-
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
278-
// libgit2, which is inflexible and unstable.
279-
// NB: The Transport Options URL must be unique, therefore it must use the object under
280-
// reconciliation details, instead of the repository it depends on.
281-
if strings.HasPrefix(origin.Spec.URL, "http") {
282-
access.auth.TransportOptionsURL = fmt.Sprintf("http://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
283-
} else if strings.HasPrefix(origin.Spec.URL, "ssh") {
284-
access.auth.TransportOptionsURL = fmt.Sprintf("ssh://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
285-
} else {
286-
return failWithError(fmt.Errorf("git repository URL '%s' has invalid transport type, supported types are: http, https, ssh", origin.Spec.URL))
287-
}
272+
// We set the TransportOptionsURL of this set of authentication options here by constructing
273+
// a unique URL that won't clash in a multi tenant environment. This unique URL is used by
274+
// libgit2 managed transports. This enables us to bypass the inbuilt credentials callback in
275+
// libgit2, which is inflexible and unstable.
276+
// NB: The Transport Options URL must be unique, therefore it must use the object under
277+
// reconciliation details, instead of the repository it depends on.
278+
if strings.HasPrefix(origin.Spec.URL, "http") {
279+
access.auth.TransportOptionsURL = fmt.Sprintf("http://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
280+
} else if strings.HasPrefix(origin.Spec.URL, "ssh") {
281+
access.auth.TransportOptionsURL = fmt.Sprintf("ssh://%s/%s/%d", auto.Name, auto.UID, auto.Generation)
282+
} else {
283+
return failWithError(fmt.Errorf("git repository URL '%s' has invalid transport type, supported types are: http, https, ssh", origin.Spec.URL))
288284
}
289285

290286
// Use the git operations timeout for the repo.
@@ -296,19 +292,17 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
296292
}
297293
defer repo.Free()
298294

299-
if managed.Enabled() {
300-
// Checkout removes TransportOptions before returning, therefore this
301-
// must happen after cloneInto.
302-
// TODO(pjbgf): Git consolidation should improve the API workflow.
303-
managed.AddTransportOptions(access.auth.TransportOptionsURL, managed.TransportOptions{
304-
TargetURL: origin.Spec.URL,
305-
AuthOpts: access.auth,
306-
ProxyOptions: &libgit2.ProxyOptions{Type: libgit2.ProxyTypeAuto},
307-
Context: cloneCtx,
308-
})
295+
// Checkout removes TransportOptions before returning, therefore this
296+
// must happen after cloneInto.
297+
// TODO(pjbgf): Git consolidation should improve the API workflow.
298+
managed.AddTransportOptions(access.auth.TransportOptionsURL, managed.TransportOptions{
299+
TargetURL: origin.Spec.URL,
300+
AuthOpts: access.auth,
301+
ProxyOptions: &libgit2.ProxyOptions{Type: libgit2.ProxyTypeAuto},
302+
Context: cloneCtx,
303+
})
309304

310-
defer managed.RemoveTransportOptions(access.auth.TransportOptionsURL)
311-
}
305+
defer managed.RemoveTransportOptions(access.auth.TransportOptionsURL)
312306

313307
// When there's a push spec, the pushed-to branch is where commits
314308
// shall be made
@@ -554,10 +548,6 @@ func (r *ImageUpdateAutomationReconciler) getRepoAccess(ctx context.Context, rep
554548
return access, nil
555549
}
556550

557-
func (r repoAccess) remoteCallbacks(ctx context.Context) libgit2.RemoteCallbacks {
558-
return gitlibgit2.RemoteCallbacks(ctx, r.auth)
559-
}
560-
561551
// cloneInto clones the upstream repository at the `ref` given (which
562552
// can be `nil`). It returns a `*libgit2.Repository` since that is used
563553
// for committing changes.
@@ -763,12 +753,9 @@ func switchToBranch(repo *libgit2.Repository, ctx context.Context, branch string
763753
}
764754
defer origin.Free()
765755

766-
callbacks := access.remoteCallbacks(ctx)
767-
if managed.Enabled() {
768-
// Override callbacks with dummy ones as they are not needed within Managed Transport.
769-
// However, not setting them may lead to git2go panicing.
770-
callbacks = managed.RemoteCallbacks()
771-
}
756+
// Override callbacks with dummy ones as they are not needed within Managed Transport.
757+
// However, not setting them may lead to git2go panicing.
758+
callbacks := managed.RemoteCallbacks()
772759

773760
// Force the fetching of the remote branch.
774761
err = origin.Fetch([]string{branch}, &libgit2.FetchOptions{
@@ -866,12 +853,9 @@ func push(ctx context.Context, path, branch string, access repoAccess) error {
866853
}
867854
defer origin.Free()
868855

869-
callbacks := access.remoteCallbacks(ctx)
870-
if managed.Enabled() {
871-
// Override callbacks with dummy ones as they are not needed within Managed Transport.
872-
// However, not setting them may lead to git2go panicing.
873-
callbacks = managed.RemoteCallbacks()
874-
}
856+
// Override callbacks with dummy ones as they are not needed within Managed Transport.
857+
// However, not setting them may lead to git2go panicing.
858+
callbacks := managed.RemoteCallbacks()
875859

876860
// calling repo.Push will succeed even if a reference update is
877861
// rejected; to detect this case, this callback is supplied.

internal/features/features.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@ const (
3131
GitManagedTransport = "GitManagedTransport"
3232
)
3333

34-
var features = map[string]bool{
35-
// GitManagedTransport
36-
// opt-in from v0.21 (via environment variable)
37-
// opt-out from v0.23
38-
GitManagedTransport: true,
39-
}
34+
var features = map[string]bool{}
4035

4136
// DefaultFeatureGates contains a list of all supported feature gates and
4237
// their default values.

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ func main() {
161161
}
162162
// +kubebuilder:scaffold:builder
163163

164-
if enabled, _ := features.Enabled(features.GitManagedTransport); enabled {
165-
managed.InitManagedTransport()
164+
if err = managed.InitManagedTransport(); err != nil {
165+
setupLog.Error(err, "unable to initialize libgit2 managed transport")
166+
os.Exit(1)
166167
}
167168

168169
setupLog.Info("starting manager")

0 commit comments

Comments
 (0)