Skip to content

Commit c033f00

Browse files
committed
Add unit test to check for apply once logic re-entry
1 parent 3a4bb5f commit c033f00

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

exp/addons/internal/controllers/clusterresourceset_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
298298
errList = append(errList, err)
299299
}
300300

301-
resourceScope, scopeError := reconcileScopeForResource(clusterResourceSet, resource, resourceSetBinding, unstructuredObj)
302-
if scopeError == nil && !resourceScope.needsApply() {
301+
resourceScope, err := reconcileScopeForResource(clusterResourceSet, resource, resourceSetBinding, unstructuredObj)
302+
if err == nil && !resourceScope.needsApply() {
303303
continue
304304
}
305305

@@ -312,8 +312,8 @@ func (r *ClusterResourceSetReconciler) ApplyClusterResourceSet(ctx context.Conte
312312
LastAppliedTime: &metav1.Time{Time: time.Now().UTC()},
313313
})
314314

315-
if scopeError != nil {
316-
errList = append(errList, scopeError)
315+
if err != nil {
316+
errList = append(errList, err)
317317
continue
318318
}
319319

exp/addons/internal/controllers/clusterresourceset_helpers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3535
"k8s.io/apimachinery/pkg/types"
3636
kerrors "k8s.io/apimachinery/pkg/util/errors"
37+
"k8s.io/klog/v2"
3738
"sigs.k8s.io/controller-runtime/pkg/client"
3839

3940
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -112,7 +113,7 @@ func createUnstructured(ctx context.Context, c client.Client, obj *unstructured.
112113
err,
113114
"creating object %s %s",
114115
obj.GroupVersionKind(),
115-
klog.Obj(obj),
116+
klog.KObj(obj),
116117
)
117118
}
118119

exp/addons/internal/controllers/clusterresourceset_scope.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/pkg/errors"
2323
apierrors "k8s.io/apimachinery/pkg/api/errors"
2424
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
"k8s.io/klog/v2"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627

2728
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
@@ -128,7 +129,7 @@ func (r *reconcileStrategyScope) apply(ctx context.Context, c client.Client, obj
128129
err,
129130
"reading object %s %s",
130131
obj.GroupVersionKind(),
131-
klog.Obj(obj),
132+
klog.KObj(obj),
132133
)
133134
}
134135

exp/addons/internal/controllers/clusterresourceset_scope_test.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
23-
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2427
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
29+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2530
)
2631

2732
func TestReconcileStrategyScopeNeedsApply(t *testing.T) {
@@ -185,3 +190,61 @@ func TestReconcileApplyOnceScopeNeedsApply(t *testing.T) {
185190
})
186191
}
187192
}
193+
194+
func TestReconcileApplyOnceScopeApply(t *testing.T) {
195+
tests := []struct {
196+
name string
197+
existingObjs []client.Object
198+
obj *unstructured.Unstructured
199+
wantErr string
200+
}{
201+
{
202+
name: "object doesn't exist",
203+
obj: &unstructured.Unstructured{
204+
Object: map[string]interface{}{
205+
"apiVersion": "v1",
206+
"kind": "ConfigMap",
207+
"metadata": map[string]interface{}{
208+
"name": "my-cm",
209+
"namespace": "that-ns",
210+
},
211+
},
212+
},
213+
},
214+
{
215+
name: "object exists",
216+
existingObjs: []client.Object{
217+
&corev1.ConfigMap{
218+
ObjectMeta: metav1.ObjectMeta{
219+
Name: "my-cm",
220+
Namespace: "that-ns",
221+
},
222+
},
223+
},
224+
obj: &unstructured.Unstructured{
225+
Object: map[string]interface{}{
226+
"apiVersion": "v1",
227+
"kind": "ConfigMap",
228+
"metadata": map[string]interface{}{
229+
"name": "my-cm",
230+
"namespace": "that-ns",
231+
},
232+
},
233+
},
234+
},
235+
}
236+
for _, tt := range tests {
237+
t.Run(tt.name, func(t *testing.T) {
238+
gs := NewWithT(t)
239+
ctx := context.Background()
240+
client := fake.NewClientBuilder().WithObjects(tt.existingObjs...).Build()
241+
scope := &reconcileApplyOnceScope{}
242+
err := scope.apply(ctx, client, tt.obj)
243+
if tt.wantErr == "" {
244+
gs.Expect(err).NotTo(HaveOccurred())
245+
} else {
246+
gs.Expect(err).To(MatchError(ContainSubstring(tt.wantErr)))
247+
}
248+
})
249+
}
250+
}

0 commit comments

Comments
 (0)