Skip to content

Commit b59fffa

Browse files
author
Paulo Gomes
authored
Merge pull request #452 from pjbgf/feature-nolibgit2
gogit: Add new ForceGoGitImplementation FeatureGate
2 parents 4e1d121 + 0191d6b commit b59fffa

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

controllers/imageupdateautomation_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ type ImageUpdateAutomationReconciler struct {
8686
helper.Metrics
8787

8888
NoCrossNamespaceRef bool
89+
90+
features map[string]bool
8991
}
9092

9193
type ImageUpdateAutomationReconcilerOptions struct {
@@ -255,8 +257,13 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
255257
return failWithError(err)
256258
}
257259

260+
gitImplementation := origin.Spec.GitImplementation
261+
if goGitOnly, _ := r.features[features.ForceGoGitImplementation]; goGitOnly {
262+
gitImplementation = sourcev1.GoGitImplementation
263+
}
264+
258265
var gitClient git.RepositoryClient
259-
switch origin.Spec.GitImplementation {
266+
switch gitImplementation {
260267
case sourcev1.LibGit2Implementation:
261268
gitClient, err = libgit2.NewClient(tmp, authOpts)
262269
case sourcev1.GoGitImplementation, "":
@@ -268,7 +275,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
268275

269276
gitClient, err = gogit.NewClient(tmp, authOpts, opts...)
270277
default:
271-
err = fmt.Errorf("failed to create git client; referred GitRepository has invalid implementation: %s", origin.Spec.GitImplementation)
278+
err = fmt.Errorf("failed to create git client; referred GitRepository has invalid implementation: %s", gitImplementation)
272279
}
273280
if err != nil {
274281
return failWithError(err)
@@ -423,6 +430,10 @@ func (r *ImageUpdateAutomationReconciler) SetupWithManager(mgr ctrl.Manager, opt
423430
return err
424431
}
425432

433+
if r.features == nil {
434+
r.features = features.FeatureGates()
435+
}
436+
426437
return ctrl.NewControllerManagedBy(mgr).
427438
For(&imagev1.ImageUpdateAutomation{}, builder.WithPredicates(
428439
predicate.Or(predicate.GenerationChangedPredicate{}, predicates.ReconcileRequestedPredicate{}))).

controllers/suite_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
3636

3737
imagev1 "github.com/fluxcd/image-automation-controller/api/v1beta1"
38+
"github.com/fluxcd/image-automation-controller/internal/features"
3839
// +kubebuilder:scaffold:imports
3940
)
4041

@@ -60,19 +61,34 @@ func TestMain(m *testing.M) {
6061
utilruntime.Must(sourcev1.AddToScheme(scheme.Scheme))
6162
utilruntime.Must(imagev1.AddToScheme(scheme.Scheme))
6263

64+
if err := transport.InitManagedTransport(); err != nil {
65+
panic(fmt.Sprintf("failed to initialize libgit2 managed transport: %v", err))
66+
}
67+
68+
code := runTestsWithFeatures(m, nil)
69+
if code != 0 {
70+
fmt.Println("failed with default feature values")
71+
os.Exit(code)
72+
}
73+
74+
code = runTestsWithFeatures(m, map[string]bool{
75+
features.ForceGoGitImplementation: false,
76+
})
77+
78+
os.Exit(code)
79+
}
80+
81+
func runTestsWithFeatures(m *testing.M, feats map[string]bool) int {
6382
testEnv = testenv.New(testenv.WithCRDPath(
6483
filepath.Join("..", "config", "crd", "bases"),
6584
filepath.Join("testdata", "crds"),
6685
))
6786

68-
if err := transport.InitManagedTransport(); err != nil {
69-
panic(fmt.Sprintf("failed to initialize libgit2 managed transport: %v", err))
70-
}
71-
7287
controllerName := "image-automation-controller"
7388
if err := (&ImageUpdateAutomationReconciler{
7489
Client: testEnv,
7590
EventRecorder: testEnv.GetEventRecorderFor(controllerName),
91+
features: feats,
7692
}).SetupWithManager(testEnv, ImageUpdateAutomationReconcilerOptions{}); err != nil {
7793
panic(fmt.Sprintf("failed to start ImageUpdateAutomationReconciler: %v", err))
7894
}
@@ -92,7 +108,7 @@ func TestMain(m *testing.M) {
92108
panic(fmt.Sprintf("failed to stop the test environment: %v", err))
93109
}
94110

95-
os.Exit(code)
111+
return code
96112
}
97113

98114
// This provides a regression assurance for image-automation-controller/#339.

internal/features/features.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ const (
2525
// GitForcePushBranch enables the use of "force push" when push branches
2626
// are configured.
2727
GitForcePushBranch = "GitForcePushBranch"
28+
29+
// ForceGoGitImplementation ignores the value set for gitImplementation
30+
// of a GitRepository object and ensures that go-git is used for all git operations.
31+
//
32+
// When enabled, libgit2 won't be initialized, nor will any git2go cgo
33+
// code be called.
34+
ForceGoGitImplementation = "ForceGoGitImplementation"
2835
)
2936

3037
var features = map[string]bool{
3138
// GitForcePushBranch
3239
// opt-out from v0.27
3340
GitForcePushBranch: true,
41+
42+
// ForceGoGitImplementation
43+
// opt-out from v0.27
44+
ForceGoGitImplementation: true,
3445
}
3546

3647
// DefaultFeatureGates contains a list of all supported feature gates and

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,11 @@ func main() {
163163
}
164164
// +kubebuilder:scaffold:builder
165165

166-
if err = transport.InitManagedTransport(); err != nil {
167-
setupLog.Error(err, "unable to initialize libgit2 managed transport")
168-
os.Exit(1)
166+
if gogitOnly, _ := features.Enabled(features.ForceGoGitImplementation); !gogitOnly {
167+
if err = transport.InitManagedTransport(); err != nil {
168+
setupLog.Error(err, "unable to initialize libgit2 managed transport")
169+
os.Exit(1)
170+
}
169171
}
170172

171173
setupLog.Info("starting manager")

0 commit comments

Comments
 (0)