Skip to content

Commit 8da4f34

Browse files
committed
✨ Allow to use builder.OnlyMetadata option with Watches
This change allows builders to use builder.OnlyMetadata when creating extra watches with .Watches(...). The code inspects the passed source.Source, and if it's of type *source.Kind, it calls the internal project method that allows to use metav1.PartialObjectMetadata in mapping functions. Signed-off-by: Vince Prignano <[email protected]> (cherry picked from commit df54dc5)
1 parent 81225c5 commit 8da4f34

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

pkg/builder/controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,17 @@ func (blder *Builder) doWatch() error {
201201

202202
// Do the watch requests
203203
for _, w := range blder.watchRequest {
204+
// If the source of this watch is of type *source.Kind, project it.
205+
if srckind, ok := w.src.(*source.Kind); ok {
206+
typeForSrc, err := blder.project(srckind.Type)
207+
if err != nil {
208+
return err
209+
}
210+
srckind.Type = typeForSrc
211+
}
204212
if err := blder.ctrl.Watch(w.src, w.eventhandler, blder.predicates...); err != nil {
205213
return err
206214
}
207-
208215
}
209216
return nil
210217
}

pkg/builder/controller_test.go

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,60 @@ var _ = Describe("application", func() {
216216
Expect(err).NotTo(HaveOccurred())
217217
})
218218

219-
It("should support watching For & Owns as metadata", func() {
219+
It("should support watching For, Owns, and Watch as metadata", func() {
220+
statefulSetMaps := make(chan *metav1.PartialObjectMetadata)
221+
220222
bldr := ControllerManagedBy(mgr).
221223
For(OnlyMetadata(&appsv1.Deployment{})).
222-
Owns(OnlyMetadata(&appsv1.ReplicaSet{}))
224+
Owns(OnlyMetadata(&appsv1.ReplicaSet{})).
225+
Watches(&source.Kind{Type: OnlyMetadata(&appsv1.StatefulSet{})},
226+
&handler.EnqueueRequestsFromMapFunc{
227+
ToRequests: handler.ToRequestsFunc(func(o handler.MapObject) []reconcile.Request {
228+
ometa := o.Object.(*metav1.PartialObjectMetadata)
229+
statefulSetMaps <- ometa
230+
return nil
231+
}),
232+
})
223233

224234
doReconcileTest("8", stop, bldr, mgr, true)
235+
236+
By("Creating a new stateful set")
237+
set := &appsv1.StatefulSet{
238+
ObjectMeta: metav1.ObjectMeta{
239+
Namespace: "default",
240+
Name: "test1",
241+
Labels: map[string]string{
242+
"foo": "bar",
243+
},
244+
},
245+
Spec: appsv1.StatefulSetSpec{
246+
Selector: &metav1.LabelSelector{
247+
MatchLabels: map[string]string{"foo": "bar"},
248+
},
249+
Template: corev1.PodTemplateSpec{
250+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}},
251+
Spec: corev1.PodSpec{
252+
Containers: []corev1.Container{
253+
{
254+
Name: "nginx",
255+
Image: "nginx",
256+
},
257+
},
258+
},
259+
},
260+
},
261+
}
262+
err := mgr.GetClient().Create(context.TODO(), set)
263+
Expect(err).NotTo(HaveOccurred())
264+
265+
By("Checking that the mapping function has been called")
266+
Eventually(func() bool {
267+
metaSet := <-statefulSetMaps
268+
Expect(metaSet.Name).To(Equal(set.Name))
269+
Expect(metaSet.Namespace).To(Equal(set.Namespace))
270+
Expect(metaSet.Labels).To(Equal(set.Labels))
271+
return true
272+
}).Should(BeTrue())
225273
})
226274
})
227275
})

0 commit comments

Comments
 (0)