Skip to content

Commit d18988e

Browse files
authored
Merge pull request #1016 from fluxcd/condn-checker-with-t
Improve HelmRepository type switching from default to oci
2 parents 5a01112 + 42bc3e8 commit d18988e

10 files changed

+83
-29
lines changed

controllers/bucket_controller_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestBucketReconciler_Reconcile(t *testing.T) {
120120
// Check if the object status is valid.
121121
condns := &conditionscheck.Conditions{NegativePolarity: bucketReadyCondition.NegativePolarity}
122122
checker := conditionscheck.NewChecker(testEnv.Client, condns)
123-
checker.CheckErr(ctx, obj)
123+
checker.WithT(g).CheckErr(ctx, obj)
124124

125125
// kstatus client conformance check.
126126
uo, err := patch.ToUnstructured(obj)
@@ -321,7 +321,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
321321

322322
// In-progress status condition validity.
323323
checker := conditionscheck.NewInProgressChecker(r.Client)
324-
checker.CheckErr(ctx, obj)
324+
checker.WithT(g).CheckErr(ctx, obj)
325325
})
326326
}
327327
}
@@ -662,7 +662,7 @@ func TestBucketReconciler_reconcileSource_generic(t *testing.T) {
662662

663663
// In-progress status condition validity.
664664
checker := conditionscheck.NewInProgressChecker(r.Client)
665-
checker.CheckErr(ctx, obj)
665+
checker.WithT(g).CheckErr(ctx, obj)
666666
})
667667
}
668668
}
@@ -1022,7 +1022,7 @@ func TestBucketReconciler_reconcileSource_gcs(t *testing.T) {
10221022

10231023
// In-progress status condition validity.
10241024
checker := conditionscheck.NewInProgressChecker(r.Client)
1025-
checker.CheckErr(ctx, obj)
1025+
checker.WithT(g).CheckErr(ctx, obj)
10261026
})
10271027
}
10281028
}
@@ -1201,7 +1201,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
12011201

12021202
// In-progress status condition validity.
12031203
checker := conditionscheck.NewInProgressChecker(r.Client)
1204-
checker.CheckErr(ctx, obj)
1204+
checker.WithT(g).CheckErr(ctx, obj)
12051205
})
12061206
}
12071207
}

controllers/gitrepository_controller_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func TestGitRepositoryReconciler_Reconcile(t *testing.T) {
186186
// Check if the object status is valid.
187187
condns := &conditionscheck.Conditions{NegativePolarity: gitRepositoryReadyCondition.NegativePolarity}
188188
checker := conditionscheck.NewChecker(testEnv.Client, condns)
189-
checker.CheckErr(ctx, obj)
189+
checker.WithT(g).CheckErr(ctx, obj)
190190

191191
// kstatus client conformance check.
192192
u, err := patch.ToUnstructured(obj)
@@ -590,7 +590,7 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
590590

591591
// In-progress status condition validity.
592592
checker := conditionscheck.NewInProgressChecker(r.Client)
593-
checker.CheckErr(ctx, obj)
593+
checker.WithT(g).CheckErr(ctx, obj)
594594
})
595595
}
596596
}
@@ -815,7 +815,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
815815
}
816816
// In-progress status condition validity.
817817
checker := conditionscheck.NewInProgressChecker(r.Client)
818-
checker.CheckErr(ctx, obj)
818+
checker.WithT(g).CheckErr(ctx, obj)
819819
})
820820
}
821821
}
@@ -1374,7 +1374,7 @@ func TestGitRepositoryReconciler_reconcileStorage(t *testing.T) {
13741374

13751375
// In-progress status condition validity.
13761376
checker := conditionscheck.NewInProgressChecker(r.Client)
1377-
checker.CheckErr(ctx, obj)
1377+
checker.WithT(g).CheckErr(ctx, obj)
13781378
})
13791379
}
13801380
}

controllers/helmchart_controller_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestHelmChartReconciler_Reconcile(t *testing.T) {
109109
// Check if the object status is valid.
110110
condns := &conditionscheck.Conditions{NegativePolarity: helmChartReadyCondition.NegativePolarity}
111111
checker := conditionscheck.NewChecker(testEnv.Client, condns)
112-
checker.CheckErr(ctx, obj)
112+
checker.WithT(g).CheckErr(ctx, obj)
113113

114114
// kstatus client conformance check.
115115
u, err := patch.ToUnstructured(obj)
@@ -177,7 +177,7 @@ func TestHelmChartReconciler_Reconcile(t *testing.T) {
177177
// Check if the object status is valid.
178178
condns := &conditionscheck.Conditions{NegativePolarity: helmChartReadyCondition.NegativePolarity}
179179
checker := conditionscheck.NewChecker(testEnv.Client, condns)
180-
checker.CheckErr(ctx, obj)
180+
checker.WithT(g).CheckErr(ctx, obj)
181181

182182
g.Expect(testEnv.Delete(ctx, obj)).To(Succeed())
183183

@@ -212,7 +212,7 @@ func TestHelmChartReconciler_Reconcile(t *testing.T) {
212212
// Check if the object status is valid.
213213
condns := &conditionscheck.Conditions{NegativePolarity: helmChartReadyCondition.NegativePolarity}
214214
checker := conditionscheck.NewChecker(testEnv.Client, condns)
215-
checker.CheckErr(ctx, obj)
215+
checker.WithT(g).CheckErr(ctx, obj)
216216

217217
g.Expect(testEnv.Delete(ctx, obj)).To(Succeed())
218218

@@ -444,7 +444,7 @@ func TestHelmChartReconciler_reconcileStorage(t *testing.T) {
444444

445445
// In-progress status condition validity.
446446
checker := conditionscheck.NewInProgressChecker(r.Client)
447-
checker.CheckErr(ctx, obj)
447+
checker.WithT(g).CheckErr(ctx, obj)
448448
})
449449
}
450450
}
@@ -708,7 +708,7 @@ func TestHelmChartReconciler_reconcileSource(t *testing.T) {
708708

709709
// In-progress status condition validity.
710710
checker := conditionscheck.NewInProgressChecker(r.Client)
711-
checker.CheckErr(ctx, &obj)
711+
checker.WithT(g).CheckErr(ctx, &obj)
712712
})
713713
}
714714
}

controllers/helmrepository_controller_oci.go

+32
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4141
"sigs.k8s.io/controller-runtime/pkg/predicate"
4242

43+
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
4344
"github.com/fluxcd/pkg/apis/meta"
4445
"github.com/fluxcd/pkg/oci"
4546
"github.com/fluxcd/pkg/runtime/conditions"
@@ -82,6 +83,11 @@ type HelmRepositoryOCIReconciler struct {
8283
RegistryClientGenerator RegistryClientGeneratorFunc
8384

8485
patchOptions []patch.Option
86+
87+
// unmanagedConditions are the conditions that are not managed by this
88+
// reconciler and need to be removed from the object before taking ownership
89+
// of the object being reconciled.
90+
unmanagedConditions []string
8591
}
8692

8793
// RegistryClientGeneratorFunc is a function that returns a registry client
@@ -95,6 +101,7 @@ func (r *HelmRepositoryOCIReconciler) SetupWithManager(mgr ctrl.Manager) error {
95101
}
96102

97103
func (r *HelmRepositoryOCIReconciler) SetupWithManagerAndOptions(mgr ctrl.Manager, opts HelmRepositoryReconcilerOptions) error {
104+
r.unmanagedConditions = conditionsDiff(helmRepositoryReadyCondition.Owned, helmRepositoryOCIOwnedConditions)
98105
r.patchOptions = getPatchOptions(helmRepositoryOCIOwnedConditions, r.ControllerName)
99106

100107
recoverPanic := true
@@ -124,6 +131,16 @@ func (r *HelmRepositoryOCIReconciler) Reconcile(ctx context.Context, req ctrl.Re
124131
return ctrl.Result{}, client.IgnoreNotFound(err)
125132
}
126133

134+
// If the object contains any of the unmanaged conditions, requeue and wait
135+
// for those conditions to be removed first before processing the object.
136+
// NOTE: This will happen only when a HelmRepository's spec.type is switched
137+
// from "default" to "oci".
138+
if conditions.HasAny(obj, r.unmanagedConditions) {
139+
r.eventLogf(ctx, obj, eventv1.EventTypeTrace, "IncompleteTransition",
140+
"object contains conditions managed by other reconciler")
141+
return ctrl.Result{RequeueAfter: time.Second}, nil
142+
}
143+
127144
// Record suspended status metric
128145
r.RecordSuspend(ctx, obj, obj.Spec.Suspend)
129146

@@ -428,3 +445,18 @@ func makeLoginOption(auth authn.Authenticator, keychain authn.Keychain, registry
428445

429446
return nil, nil
430447
}
448+
449+
func conditionsDiff(a, b []string) []string {
450+
bMap := make(map[string]struct{}, len(b))
451+
for _, j := range b {
452+
bMap[j] = struct{}{}
453+
}
454+
455+
r := []string{}
456+
for _, i := range a {
457+
if _, exists := bMap[i]; !exists {
458+
r = append(r, i)
459+
}
460+
}
461+
return r
462+
}

controllers/helmrepository_controller_oci_test.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"encoding/base64"
2121
"fmt"
22+
"strconv"
2223
"testing"
2324

2425
. "github.com/onsi/gomega"
@@ -122,7 +123,7 @@ func TestHelmRepositoryOCIReconciler_Reconcile(t *testing.T) {
122123
// Check if the object status is valid.
123124
condns := &conditionscheck.Conditions{NegativePolarity: helmRepositoryReadyCondition.NegativePolarity}
124125
checker := conditionscheck.NewChecker(testEnv.Client, condns)
125-
checker.CheckErr(ctx, obj)
126+
checker.WithT(g).CheckErr(ctx, obj)
126127

127128
// kstatus client conformance check.
128129
u, err := patch.ToUnstructured(obj)
@@ -316,7 +317,28 @@ func TestHelmRepositoryOCIReconciler_authStrategy(t *testing.T) {
316317
// NOTE: Check the object directly as reconcile() doesn't apply the
317318
// final patch, the object has unapplied changes.
318319
checker.DisableFetch = true
319-
checker.CheckErr(ctx, obj)
320+
checker.WithT(g).CheckErr(ctx, obj)
321+
})
322+
}
323+
}
324+
325+
func TestConditionsDiff(t *testing.T) {
326+
tests := []struct {
327+
a, b, want []string
328+
}{
329+
{[]string{"a", "b", "c"}, []string{"b", "d"}, []string{"a", "c"}},
330+
{[]string{"a", "b", "c"}, []string{}, []string{"a", "b", "c"}},
331+
{[]string{}, []string{"b", "d"}, []string{}},
332+
{[]string{}, []string{}, []string{}},
333+
{[]string{"a", "b"}, nil, []string{"a", "b"}},
334+
{nil, []string{"a", "b"}, []string{}},
335+
{nil, nil, []string{}},
336+
}
337+
338+
for i, tt := range tests {
339+
t.Run(strconv.Itoa(i), func(t *testing.T) {
340+
g := NewWithT(t)
341+
g.Expect(conditionsDiff(tt.a, tt.b)).To(Equal(tt.want))
320342
})
321343
}
322344
}

controllers/helmrepository_controller_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestHelmRepositoryReconciler_Reconcile(t *testing.T) {
9494
// Check if the object status is valid.
9595
condns := &conditionscheck.Conditions{NegativePolarity: helmRepositoryReadyCondition.NegativePolarity}
9696
checker := conditionscheck.NewChecker(testEnv.Client, condns)
97-
checker.CheckErr(ctx, obj)
97+
checker.WithT(g).CheckErr(ctx, obj)
9898

9999
// kstatus client conformance check.
100100
u, err := patch.ToUnstructured(obj)
@@ -292,7 +292,7 @@ func TestHelmRepositoryReconciler_reconcileStorage(t *testing.T) {
292292

293293
// In-progress status condition validity.
294294
checker := conditionscheck.NewInProgressChecker(r.Client)
295-
checker.CheckErr(ctx, obj)
295+
checker.WithT(g).CheckErr(ctx, obj)
296296
})
297297
}
298298
}
@@ -746,7 +746,7 @@ func TestHelmRepositoryReconciler_reconcileSource(t *testing.T) {
746746

747747
// In-progress status condition validity.
748748
checker := conditionscheck.NewInProgressChecker(r.Client)
749-
checker.CheckErr(ctx, obj)
749+
checker.WithT(g).CheckErr(ctx, obj)
750750
})
751751
}
752752
}
@@ -1278,7 +1278,7 @@ func TestHelmRepositoryReconciler_ReconcileTypeUpdatePredicateFilter(t *testing.
12781278
// Check if the object status is valid.
12791279
condns := &conditionscheck.Conditions{NegativePolarity: helmRepositoryReadyCondition.NegativePolarity}
12801280
checker := conditionscheck.NewChecker(testEnv.Client, condns)
1281-
checker.CheckErr(ctx, obj)
1281+
checker.WithT(g).CheckErr(ctx, obj)
12821282

12831283
// kstatus client conformance check.
12841284
u, err := patch.ToUnstructured(obj)
@@ -1330,7 +1330,7 @@ func TestHelmRepositoryReconciler_ReconcileTypeUpdatePredicateFilter(t *testing.
13301330
// Check if the object status is valid.
13311331
condns = &conditionscheck.Conditions{NegativePolarity: helmRepositoryOCINegativeConditions}
13321332
checker = conditionscheck.NewChecker(testEnv.Client, condns)
1333-
checker.CheckErr(ctx, obj)
1333+
checker.WithT(g).CheckErr(ctx, obj)
13341334

13351335
g.Expect(testEnv.Delete(ctx, obj)).To(Succeed())
13361336

@@ -1395,7 +1395,7 @@ func TestHelmRepositoryReconciler_ReconcileSpecUpdatePredicateFilter(t *testing.
13951395
// Check if the object status is valid.
13961396
condns := &conditionscheck.Conditions{NegativePolarity: helmRepositoryReadyCondition.NegativePolarity}
13971397
checker := conditionscheck.NewChecker(testEnv.Client, condns)
1398-
checker.CheckErr(ctx, obj)
1398+
checker.WithT(g).CheckErr(ctx, obj)
13991399

14001400
// kstatus client conformance check.
14011401
u, err := patch.ToUnstructured(obj)
@@ -1427,7 +1427,7 @@ func TestHelmRepositoryReconciler_ReconcileSpecUpdatePredicateFilter(t *testing.
14271427
// Check if the object status is valid.
14281428
condns = &conditionscheck.Conditions{NegativePolarity: helmRepositoryReadyCondition.NegativePolarity}
14291429
checker = conditionscheck.NewChecker(testEnv.Client, condns)
1430-
checker.CheckErr(ctx, obj)
1430+
checker.WithT(g).CheckErr(ctx, obj)
14311431

14321432
g.Expect(testEnv.Delete(ctx, obj)).To(Succeed())
14331433

controllers/ocirepository_controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestOCIRepository_Reconcile(t *testing.T) {
217217
// Check if the object status is valid
218218
condns := &conditionscheck.Conditions{NegativePolarity: ociRepositoryReadyCondition.NegativePolarity}
219219
checker := conditionscheck.NewChecker(testEnv.Client, condns)
220-
checker.CheckErr(ctx, obj)
220+
checker.WithT(g).CheckErr(ctx, obj)
221221

222222
// kstatus client conformance check
223223
u, err := patch.ToUnstructured(obj)
@@ -1998,7 +1998,7 @@ func TestOCIRepository_reconcileStorage(t *testing.T) {
19981998

19991999
// In-progress status condition validity.
20002000
checker := conditionscheck.NewInProgressChecker(r.Client)
2001-
checker.CheckErr(ctx, obj)
2001+
checker.WithT(g).CheckErr(ctx, obj)
20022002
})
20032003
}
20042004
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
github.com/fluxcd/pkg/lockedfile v0.1.0
3232
github.com/fluxcd/pkg/masktoken v0.2.0
3333
github.com/fluxcd/pkg/oci v0.18.0
34-
github.com/fluxcd/pkg/runtime v0.27.0
34+
github.com/fluxcd/pkg/runtime v0.28.0
3535
github.com/fluxcd/pkg/sourceignore v0.3.0
3636
github.com/fluxcd/pkg/ssh v0.7.0
3737
github.com/fluxcd/pkg/testserver v0.4.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,8 @@ github.com/fluxcd/pkg/masktoken v0.2.0 h1:HoSPTk4l1fz5Fevs2vVRvZGru33blfMwWSZKsH
541541
github.com/fluxcd/pkg/masktoken v0.2.0/go.mod h1:EA7GleAHL33kN6kTW06m5R3/Q26IyuGO7Ef/0CtpDI0=
542542
github.com/fluxcd/pkg/oci v0.18.0 h1:x5n3gW1lX6wrqvWP4ZkOXJ8LqLKy891uKwifCXSqKi4=
543543
github.com/fluxcd/pkg/oci v0.18.0/go.mod h1:zXoxvE4uuIEOgA98IM5Wv/uRxs7sdbaTlGDjzHb9yiA=
544-
github.com/fluxcd/pkg/runtime v0.27.0 h1:zVA95Z0KvNjvZxEZhvIbJyJIwtaiv1aVttHZ4YB/FzY=
545-
github.com/fluxcd/pkg/runtime v0.27.0/go.mod h1:fC1l4Wv1hnsqPKB46eDZBXF8RMZm5FXeU4bnJkwGkqk=
544+
github.com/fluxcd/pkg/runtime v0.28.0 h1:FtdZk53oMFUKIGykDtWNi3Pv2lXR6NHPWNqLQV5rpPg=
545+
github.com/fluxcd/pkg/runtime v0.28.0/go.mod h1:fC1l4Wv1hnsqPKB46eDZBXF8RMZm5FXeU4bnJkwGkqk=
546546
github.com/fluxcd/pkg/sourceignore v0.3.0 h1:pFO3hKV9ub+2SrNZPZE7xfiRhxsycRrd7JK7qB26nVw=
547547
github.com/fluxcd/pkg/sourceignore v0.3.0/go.mod h1:ak3Tve/KwVzytZ5V2yBlGGpTJ/2oQ9kcP3iuwBOAHGo=
548548
github.com/fluxcd/pkg/ssh v0.7.0 h1:FX5ky8SU9dYwbM6zEIDR3TSveLF01iyS95CtB5Ykpno=

internal/reconcile/summarize/summary_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func TestSummarizeAndPatch(t *testing.T) {
371371
// Check if the object status is valid as per kstatus.
372372
condns := &conditionscheck.Conditions{NegativePolarity: testReadyConditions.NegativePolarity}
373373
checker := conditionscheck.NewChecker(client, condns)
374-
checker.CheckErr(ctx, obj)
374+
checker.WithT(g).CheckErr(ctx, obj)
375375
})
376376
}
377377
}

0 commit comments

Comments
 (0)