|
| 1 | +package controllers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.uber.org/mock/gomock" |
| 11 | + corev1 "k8s.io/api/core/v1" |
| 12 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 13 | + "k8s.io/apimachinery/pkg/api/meta" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + clocktesting "k8s.io/utils/clock/testing" |
| 16 | + "k8s.io/utils/ptr" |
| 17 | + "pkg.package-operator.run/boxcutter/machinery" |
| 18 | + machinerytypes "pkg.package-operator.run/boxcutter/machinery/types" |
| 19 | + ctrl "sigs.k8s.io/controller-runtime" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" |
| 23 | + |
| 24 | + ocv1 "github.com/operator-framework/operator-controller/api/v1" |
| 25 | + "github.com/operator-framework/operator-controller/internal/object-controller/controllers" |
| 26 | +) |
| 27 | + |
| 28 | +func TestReferencedSecretsReadOncePerReconcile(t *testing.T) { |
| 29 | + ctx := t.Context() |
| 30 | + mockCtrl := gomock.NewController(t) |
| 31 | + secret := &corev1.Secret{ |
| 32 | + ObjectMeta: metav1.ObjectMeta{Name: "content", Namespace: "source-a"}, |
| 33 | + Immutable: ptr.To(true), Data: map[string][]byte{}, |
| 34 | + } |
| 35 | + cos := newRefTestCOS("packed", ocv1.ObjectSourceRef{}) |
| 36 | + cos.Spec.Phases = nil |
| 37 | + for phaseIndex := range 2 { |
| 38 | + phase := ocv1.ClusterObjectSetPhase{Name: fmt.Sprintf("phase-%d", phaseIndex)} |
| 39 | + for objectIndex := range 25 { |
| 40 | + name := fmt.Sprintf("cm-%d-%d", phaseIndex, objectIndex) |
| 41 | + secret.Data[name] = []byte(fmt.Sprintf(`{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":%q,"namespace":"target"}}`, name)) |
| 42 | + phase.Objects = append(phase.Objects, ocv1.ClusterObjectSetObject{Ref: ocv1.ObjectSourceRef{Name: secret.Name, Namespace: secret.Namespace, Key: name}}) |
| 43 | + } |
| 44 | + cos.Spec.Phases = append(cos.Spec.Phases, phase) |
| 45 | + } |
| 46 | + otherSecret := &corev1.Secret{ |
| 47 | + ObjectMeta: metav1.ObjectMeta{Name: secret.Name, Namespace: "source-b"}, |
| 48 | + Immutable: ptr.To(true), |
| 49 | + Data: map[string][]byte{"other": []byte(`{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"other","namespace":"target"}}`)}, |
| 50 | + } |
| 51 | + cos.Spec.Phases[1].Objects = append(cos.Spec.Phases[1].Objects, ocv1.ClusterObjectSetObject{Ref: ocv1.ObjectSourceRef{Name: otherSecret.Name, Namespace: otherSecret.Namespace, Key: "other"}}) |
| 52 | + reads := map[client.ObjectKey]int{} |
| 53 | + cl := fake.NewClientBuilder().WithScheme(newSchemeWithCoreV1(t)).WithObjects(secret, otherSecret, cos). |
| 54 | + WithStatusSubresource(&ocv1.ClusterObjectSet{}). |
| 55 | + WithInterceptorFuncs(interceptor.Funcs{ |
| 56 | + Get: func(ctx context.Context, cl client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 57 | + if _, ok := obj.(*corev1.Secret); ok { |
| 58 | + reads[key]++ |
| 59 | + } |
| 60 | + return cl.Get(ctx, key, obj, opts...) |
| 61 | + }, |
| 62 | + }).Build() |
| 63 | + engine := newMockRevisionEngineWithReconcile(mockCtrl, |
| 64 | + func(context.Context, machinerytypes.Revision, ...machinerytypes.RevisionReconcileOption) (machinery.RevisionResult, error) { |
| 65 | + return newMockRevisionResult(mockCtrl, revisionResultConfig{inTransition: true}), nil |
| 66 | + }, nil) |
| 67 | + reconciler := &controllers.ClusterObjectSetReconciler{ |
| 68 | + Client: cl, TrackingCache: newMockTrackingCache(mockCtrl, cl, nil), |
| 69 | + RevisionEngineFactory: newMockRevisionEngineFactoryWithEngine(mockCtrl, engine, nil), |
| 70 | + Clock: clocktesting.NewFakeClock(metav1.Now().Time), |
| 71 | + } |
| 72 | + reconcile := func(wantReads int, wantReason string) { |
| 73 | + t.Helper() |
| 74 | + _, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(cos)}) |
| 75 | + require.NoError(t, err) |
| 76 | + require.Equal(t, wantReads, reads[client.ObjectKeyFromObject(secret)]) |
| 77 | + require.Equal(t, wantReads, reads[client.ObjectKeyFromObject(otherSecret)]) |
| 78 | + require.NoError(t, cl.Get(ctx, client.ObjectKeyFromObject(cos), cos)) |
| 79 | + condition := meta.FindStatusCondition(cos.Status.Conditions, ocv1.ClusterObjectSetTypeProgressing) |
| 80 | + require.NotNil(t, condition) |
| 81 | + require.Equal(t, wantReason, condition.Reason) |
| 82 | + } |
| 83 | + reconcile(1, ocv1.ReasonRollingOut) |
| 84 | + reconcile(2, ocv1.ReasonRollingOut) |
| 85 | + |
| 86 | + // Reusing a fetched Secret must not hide a replacement on the next reconcile. |
| 87 | + require.NoError(t, cl.Delete(ctx, secret)) |
| 88 | + changed := secret.DeepCopy() |
| 89 | + changed.ResourceVersion = "" |
| 90 | + changed.Data["cm-0-0"] = []byte(`{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"cm-0-0","namespace":"target"},"data":{"value":"changed"}}`) |
| 91 | + require.NoError(t, cl.Create(ctx, changed)) |
| 92 | + reconcile(3, ocv1.ClusterObjectSetReasonBlocked) |
| 93 | + require.Contains(t, meta.FindStatusCondition(cos.Status.Conditions, ocv1.ClusterObjectSetTypeProgressing).Message, "resolved content of 1 phase(s) has changed") |
| 94 | + |
| 95 | + require.NoError(t, cl.Delete(ctx, changed)) |
| 96 | + secret.ResourceVersion = "" |
| 97 | + require.NoError(t, cl.Create(ctx, secret)) |
| 98 | + reconcile(4, ocv1.ReasonRollingOut) |
| 99 | +} |
| 100 | + |
| 101 | +func TestReferencedSecretReadFailureRetries(t *testing.T) { |
| 102 | + ctx := t.Context() |
| 103 | + mockCtrl := gomock.NewController(t) |
| 104 | + secret := &corev1.Secret{ |
| 105 | + ObjectMeta: metav1.ObjectMeta{Name: "content", Namespace: "source"}, |
| 106 | + Immutable: ptr.To(true), |
| 107 | + Data: map[string][]byte{"object": []byte(`{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"cm","namespace":"target"}}`)}, |
| 108 | + } |
| 109 | + cos := newRefTestCOS("retry", ocv1.ObjectSourceRef{Name: secret.Name, Namespace: secret.Namespace, Key: "object"}) |
| 110 | + failReads := true |
| 111 | + cl := fake.NewClientBuilder().WithScheme(newSchemeWithCoreV1(t)).WithObjects(secret, cos). |
| 112 | + WithStatusSubresource(&ocv1.ClusterObjectSet{}). |
| 113 | + WithInterceptorFuncs(interceptor.Funcs{ |
| 114 | + Get: func(ctx context.Context, cl client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { |
| 115 | + if _, ok := obj.(*corev1.Secret); ok && failReads { |
| 116 | + return apierrors.NewServiceUnavailable("temporary API outage") |
| 117 | + } |
| 118 | + return cl.Get(ctx, key, obj, opts...) |
| 119 | + }, |
| 120 | + }).Build() |
| 121 | + engine := newMockRevisionEngineWithReconcile(mockCtrl, |
| 122 | + func(context.Context, machinerytypes.Revision, ...machinerytypes.RevisionReconcileOption) (machinery.RevisionResult, error) { |
| 123 | + return newMockRevisionResult(mockCtrl, revisionResultConfig{inTransition: true}), nil |
| 124 | + }, nil) |
| 125 | + reconciler := &controllers.ClusterObjectSetReconciler{ |
| 126 | + Client: cl, TrackingCache: newMockTrackingCache(mockCtrl, cl, nil), |
| 127 | + RevisionEngineFactory: newMockRevisionEngineFactoryWithEngine(mockCtrl, engine, nil), |
| 128 | + Clock: clocktesting.NewFakeClock(metav1.Now().Time), |
| 129 | + } |
| 130 | + req := ctrl.Request{NamespacedName: client.ObjectKeyFromObject(cos)} |
| 131 | + for range 2 { |
| 132 | + _, err := reconciler.Reconcile(ctx, req) |
| 133 | + require.ErrorContains(t, err, "temporary API outage") |
| 134 | + require.NoError(t, cl.Get(ctx, req.NamespacedName, cos)) |
| 135 | + condition := meta.FindStatusCondition(cos.Status.Conditions, ocv1.ClusterObjectSetTypeProgressing) |
| 136 | + require.NotNil(t, condition) |
| 137 | + require.Equal(t, metav1.ConditionTrue, condition.Status) |
| 138 | + require.Equal(t, ocv1.ReasonRetrying, condition.Reason) |
| 139 | + } |
| 140 | + failReads = false |
| 141 | + _, err := reconciler.Reconcile(ctx, req) |
| 142 | + require.NoError(t, err) |
| 143 | + require.NoError(t, cl.Get(ctx, req.NamespacedName, cos)) |
| 144 | + require.Equal(t, ocv1.ReasonRollingOut, meta.FindStatusCondition(cos.Status.Conditions, ocv1.ClusterObjectSetTypeProgressing).Reason) |
| 145 | +} |
| 146 | + |
| 147 | +func TestMutableReferencedSecretRequeues(t *testing.T) { |
| 148 | + secret := &corev1.Secret{ |
| 149 | + ObjectMeta: metav1.ObjectMeta{Name: "content", Namespace: "source"}, |
| 150 | + Immutable: ptr.To(false), |
| 151 | + } |
| 152 | + cos := newRefTestCOS("wait-for-immutable-secret", ocv1.ObjectSourceRef{ |
| 153 | + Name: secret.Name, Namespace: secret.Namespace, Key: "object", |
| 154 | + }) |
| 155 | + cl := fake.NewClientBuilder().WithScheme(newSchemeWithCoreV1(t)). |
| 156 | + WithStatusSubresource(&ocv1.ClusterObjectSet{}). |
| 157 | + WithObjects(secret, cos).Build() |
| 158 | + reconciler := &controllers.ClusterObjectSetReconciler{Client: cl} |
| 159 | + |
| 160 | + result, err := reconciler.Reconcile(t.Context(), ctrl.Request{NamespacedName: client.ObjectKeyFromObject(cos)}) |
| 161 | + require.NoError(t, err) |
| 162 | + require.Equal(t, ctrl.Result{RequeueAfter: 10 * time.Second}, result) |
| 163 | + require.NoError(t, cl.Get(t.Context(), client.ObjectKeyFromObject(cos), cos)) |
| 164 | + condition := meta.FindStatusCondition(cos.Status.Conditions, ocv1.ClusterObjectSetTypeProgressing) |
| 165 | + require.NotNil(t, condition) |
| 166 | + require.Equal(t, metav1.ConditionFalse, condition.Status) |
| 167 | + require.Equal(t, ocv1.ClusterObjectSetReasonBlocked, condition.Reason) |
| 168 | + require.Contains(t, condition.Message, "source/content") |
| 169 | +} |
0 commit comments