Skip to content

Commit 2f50071

Browse files
committed
Minor optimizations for CompositionRevision watch handler
Detect whether a revision is compatible earlier and more often, and return early. Signed-off-by: Nic Cope <[email protected]>
1 parent 6787270 commit 2f50071

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

internal/controller/apiextensions/definition/handlers.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,51 @@ func EnqueueForCompositionRevision(of resource.CompositeKind, c client.Reader, l
4343
CreateFunc: func(ctx context.Context, e kevent.CreateEvent, q workqueue.TypedRateLimitingInterface[reconcile.Request]) {
4444
rev, ok := e.Object.(*v1.CompositionRevision)
4545
if !ok {
46-
// should not happen
4746
return
4847
}
4948

50-
// TODO(negz): Check whether the revision's compositeTypeRef matches
51-
// the supplied CompositeKind. If it doesn't, we can return early.
49+
// We don't know what composition this revision is for,
50+
// so we can't determine whether an XR might use it.
51+
// This should never happen in practice - the
52+
// composition controller sets this label when it
53+
// creates a revision.
54+
compName := rev.Labels[v1.LabelCompositionName]
55+
if compName == "" {
56+
return
57+
}
58+
59+
// This handler is for a specific type of XR. This
60+
// revisionisn't compatible with that type.
61+
if rev.Spec.CompositeTypeRef.APIVersion != schema.GroupVersionKind(of).GroupVersion().String() {
62+
return
63+
}
64+
if rev.Spec.CompositeTypeRef.Kind != of.Kind {
65+
return
66+
}
5267

53-
// get all XRs
5468
xrs := kunstructured.UnstructuredList{}
5569
xrs.SetGroupVersionKind(schema.GroupVersionKind(of))
5670
xrs.SetKind(schema.GroupVersionKind(of).Kind + "List")
5771
// TODO(negz): Index XRs by composition revision name?
5872
if err := c.List(ctx, &xrs); err != nil {
59-
// logging is most we can do here. This is a programming error if it happens.
73+
// Logging is most we can do here. This is a programming error if it happens.
6074
log.Info("cannot list in CompositionRevision handler", "type", schema.GroupVersionKind(of).String(), "error", err)
6175
return
6276
}
6377

64-
// enqueue all those that reference the Composition of this revision
65-
compName := rev.Labels[v1.LabelCompositionName]
66-
// TODO(negz): Check this before we get all XRs.
67-
if compName == "" {
68-
return
69-
}
78+
// Enqueue all those that reference the composition of
79+
// this revision.
7080
for _, u := range xrs.Items {
7181
xr := composite.Unstructured{Unstructured: u}
7282

73-
// only automatic
83+
// We only care about XRs that would
84+
// automatically update to this new revision.
7485
if pol := xr.GetCompositionUpdatePolicy(); pol != nil && *pol == xpv1.UpdateManual {
7586
continue
7687
}
7788

78-
// only those that reference the right Composition
89+
// We only care about XRs that reference the
90+
// composition this revision derives from.
7991
if ref := xr.GetCompositionReference(); ref == nil || ref.Name != compName {
8092
continue
8193
}

internal/controller/apiextensions/definition/handlers_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestEnqueueForCompositionRevisionFunc(t *testing.T) {
3131
event kevent.CreateEvent
3232
}
3333
type want struct {
34-
added []interface{}
34+
added []any
3535
}
3636

3737
dog := schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "Dog"}
@@ -89,11 +89,14 @@ func TestEnqueueForCompositionRevisionFunc(t *testing.T) {
8989
v1.LabelCompositionName: "dachshund",
9090
},
9191
},
92+
Spec: v1.CompositionRevisionSpec{
93+
CompositeTypeRef: v1.TypeReferenceTo(dog),
94+
},
9295
},
9396
},
9497
},
9598
want: want{
96-
added: []interface{}{reconcile.Request{NamespacedName: types.NamespacedName{
99+
added: []any{reconcile.Request{NamespacedName: types.NamespacedName{
97100
Namespace: "ns",
98101
Name: "obj1",
99102
}}},
@@ -125,6 +128,9 @@ func TestEnqueueForCompositionRevisionFunc(t *testing.T) {
125128
v1.LabelCompositionName: "dachshund",
126129
},
127130
},
131+
Spec: v1.CompositionRevisionSpec{
132+
CompositeTypeRef: v1.TypeReferenceTo(dog),
133+
},
128134
},
129135
},
130136
},
@@ -156,6 +162,9 @@ func TestEnqueueForCompositionRevisionFunc(t *testing.T) {
156162
v1.LabelCompositionName: "dachshund",
157163
},
158164
},
165+
Spec: v1.CompositionRevisionSpec{
166+
CompositeTypeRef: v1.TypeReferenceTo(dog),
167+
},
159168
},
160169
},
161170
},
@@ -203,11 +212,14 @@ func TestEnqueueForCompositionRevisionFunc(t *testing.T) {
203212
v1.LabelCompositionName: "dachshund",
204213
},
205214
},
215+
Spec: v1.CompositionRevisionSpec{
216+
CompositeTypeRef: v1.TypeReferenceTo(dog),
217+
},
206218
},
207219
},
208220
},
209221
want: want{
210-
added: []interface{}{
222+
added: []any{
211223
reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "ns", Name: "obj1"}},
212224
reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "ns", Name: "obj2"}},
213225
},
@@ -229,7 +241,7 @@ func TestEnqueueForCompositionRevisionFunc(t *testing.T) {
229241

230242
type rateLimitingQueueMock struct {
231243
workqueue.TypedRateLimitingInterface[reconcile.Request]
232-
added []interface{}
244+
added []any
233245
}
234246

235247
func (f *rateLimitingQueueMock) Add(item reconcile.Request) {

0 commit comments

Comments
 (0)