Skip to content

Commit

Permalink
✨ Allow to use builder.OnlyMetadata option with Watches
Browse files Browse the repository at this point in the history
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 <vincepri@vmware.com>
(cherry picked from commit df54dc5)
  • Loading branch information
vincepri committed Nov 10, 2020
1 parent 81225c5 commit 8da4f34
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
9 changes: 8 additions & 1 deletion pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,17 @@ func (blder *Builder) doWatch() error {

// Do the watch requests
for _, w := range blder.watchRequest {
// If the source of this watch is of type *source.Kind, project it.
if srckind, ok := w.src.(*source.Kind); ok {
typeForSrc, err := blder.project(srckind.Type)
if err != nil {
return err
}
srckind.Type = typeForSrc
}
if err := blder.ctrl.Watch(w.src, w.eventhandler, blder.predicates...); err != nil {
return err
}

}
return nil
}
Expand Down
52 changes: 50 additions & 2 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,60 @@ var _ = Describe("application", func() {
Expect(err).NotTo(HaveOccurred())
})

It("should support watching For & Owns as metadata", func() {
It("should support watching For, Owns, and Watch as metadata", func() {
statefulSetMaps := make(chan *metav1.PartialObjectMetadata)

bldr := ControllerManagedBy(mgr).
For(OnlyMetadata(&appsv1.Deployment{})).
Owns(OnlyMetadata(&appsv1.ReplicaSet{}))
Owns(OnlyMetadata(&appsv1.ReplicaSet{})).
Watches(&source.Kind{Type: OnlyMetadata(&appsv1.StatefulSet{})},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(func(o handler.MapObject) []reconcile.Request {
ometa := o.Object.(*metav1.PartialObjectMetadata)
statefulSetMaps <- ometa
return nil
}),
})

doReconcileTest("8", stop, bldr, mgr, true)

By("Creating a new stateful set")
set := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test1",
Labels: map[string]string{
"foo": "bar",
},
},
Spec: appsv1.StatefulSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
},
},
},
},
},
}
err := mgr.GetClient().Create(context.TODO(), set)
Expect(err).NotTo(HaveOccurred())

By("Checking that the mapping function has been called")
Eventually(func() bool {
metaSet := <-statefulSetMaps
Expect(metaSet.Name).To(Equal(set.Name))
Expect(metaSet.Namespace).To(Equal(set.Namespace))
Expect(metaSet.Labels).To(Equal(set.Labels))
return true
}).Should(BeTrue())
})
})
})
Expand Down

0 comments on commit 8da4f34

Please sign in to comment.