Skip to content

Commit 9651950

Browse files
committed
gitrepo: Enable default feature gates in tests
Introduce a new field in the GitRepositoryReconciler to set the enabled features. This makes it test friendly compared to using global flags for setting and checking flags in the tests. Enable default feature gates in all the GitRepo reconciler tests. Signed-off-by: Sunny <[email protected]>
1 parent e180b3c commit 9651950

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

controllers/gitrepository_controller.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type GitRepositoryReconciler struct {
115115
ControllerName string
116116

117117
requeueDependency time.Duration
118+
features map[string]bool
118119
}
119120

120121
type GitRepositoryReconcilerOptions struct {
@@ -134,6 +135,15 @@ func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
134135
func (r *GitRepositoryReconciler) SetupWithManagerAndOptions(mgr ctrl.Manager, opts GitRepositoryReconcilerOptions) error {
135136
r.requeueDependency = opts.DependencyRequeueInterval
136137

138+
if r.features == nil {
139+
r.features = map[string]bool{}
140+
}
141+
142+
// Check and enable gated features.
143+
if oc, _ := features.Enabled(features.OptimizedGitClones); oc {
144+
r.features[features.OptimizedGitClones] = true
145+
}
146+
137147
return ctrl.NewControllerManagedBy(mgr).
138148
For(&sourcev1.GitRepository{}, builder.WithPredicates(
139149
predicate.Or(predicate.GenerationChangedPredicate{}, predicates.ReconcileRequestedPredicate{}),
@@ -414,7 +424,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context,
414424
checkoutOpts.SemVer = ref.SemVer
415425
}
416426

417-
if oc, _ := features.Enabled(features.OptimizedGitClones); oc {
427+
if val, ok := r.features[features.OptimizedGitClones]; ok && val {
418428
if artifact := obj.GetArtifact(); artifact != nil {
419429
checkoutOpts.LastRevision = artifact.Revision
420430
}

controllers/gitrepository_controller_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
5858

5959
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
60+
"github.com/fluxcd/source-controller/internal/features"
6061
sreconcile "github.com/fluxcd/source-controller/internal/reconcile"
6162
"github.com/fluxcd/source-controller/internal/reconcile/summarize"
6263
"github.com/fluxcd/source-controller/pkg/git"
@@ -499,6 +500,7 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
499500
Client: builder.Build(),
500501
EventRecorder: record.NewFakeRecorder(32),
501502
Storage: testStorage,
503+
features: features.FeatureGates(),
502504
}
503505

504506
for _, i := range testGitImplementations {
@@ -641,6 +643,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
641643
Client: fakeclient.NewClientBuilder().WithScheme(runtime.NewScheme()).Build(),
642644
EventRecorder: record.NewFakeRecorder(32),
643645
Storage: testStorage,
646+
features: features.FeatureGates(),
644647
}
645648

646649
for _, tt := range tests {
@@ -857,6 +860,7 @@ func TestGitRepositoryReconciler_reconcileArtifact(t *testing.T) {
857860
r := &GitRepositoryReconciler{
858861
EventRecorder: record.NewFakeRecorder(32),
859862
Storage: testStorage,
863+
features: features.FeatureGates(),
860864
}
861865

862866
obj := &sourcev1.GitRepository{
@@ -1042,6 +1046,7 @@ func TestGitRepositoryReconciler_reconcileInclude(t *testing.T) {
10421046
EventRecorder: record.NewFakeRecorder(32),
10431047
Storage: storage,
10441048
requeueDependency: dependencyInterval,
1049+
features: features.FeatureGates(),
10451050
}
10461051

10471052
obj := &sourcev1.GitRepository{
@@ -1206,6 +1211,7 @@ func TestGitRepositoryReconciler_reconcileStorage(t *testing.T) {
12061211
r := &GitRepositoryReconciler{
12071212
EventRecorder: record.NewFakeRecorder(32),
12081213
Storage: testStorage,
1214+
features: features.FeatureGates(),
12091215
}
12101216

12111217
obj := &sourcev1.GitRepository{
@@ -1247,6 +1253,7 @@ func TestGitRepositoryReconciler_reconcileDelete(t *testing.T) {
12471253
r := &GitRepositoryReconciler{
12481254
EventRecorder: record.NewFakeRecorder(32),
12491255
Storage: testStorage,
1256+
features: features.FeatureGates(),
12501257
}
12511258

12521259
obj := &sourcev1.GitRepository{
@@ -1384,6 +1391,7 @@ func TestGitRepositoryReconciler_verifyCommitSignature(t *testing.T) {
13841391
r := &GitRepositoryReconciler{
13851392
EventRecorder: record.NewFakeRecorder(32),
13861393
Client: builder.Build(),
1394+
features: features.FeatureGates(),
13871395
}
13881396

13891397
obj := &sourcev1.GitRepository{
@@ -1525,6 +1533,7 @@ func TestGitRepositoryReconciler_ConditionsUpdate(t *testing.T) {
15251533
Client: builder.Build(),
15261534
EventRecorder: record.NewFakeRecorder(32),
15271535
Storage: testStorage,
1536+
features: features.FeatureGates(),
15281537
}
15291538

15301539
key := client.ObjectKeyFromObject(obj)
@@ -1857,6 +1866,7 @@ func TestGitRepositoryReconciler_notify(t *testing.T) {
18571866

18581867
reconciler := &GitRepositoryReconciler{
18591868
EventRecorder: recorder,
1869+
features: features.FeatureGates(),
18601870
}
18611871
commit := &git.Commit{
18621872
Message: "test commit",

controllers/suite_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636

3737
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
3838
"github.com/fluxcd/source-controller/internal/cache"
39+
"github.com/fluxcd/source-controller/internal/features"
3940
// +kubebuilder:scaffold:imports
4041
)
4142

@@ -106,6 +107,7 @@ func TestMain(m *testing.M) {
106107
EventRecorder: record.NewFakeRecorder(32),
107108
Metrics: testMetricsH,
108109
Storage: testStorage,
110+
features: features.FeatureGates(),
109111
}).SetupWithManager(testEnv); err != nil {
110112
panic(fmt.Sprintf("Failed to start GitRepositoryReconciler: %v", err))
111113
}

0 commit comments

Comments
 (0)