Kubebuilder: Is there any example to use watches method?

Created on 25 Dec 2019  路  4Comments  路  Source: kubernetes-sigs/kubebuilder

I want to observe the pod's status and my CRD change so I can update the CRD's status.

To do this, I think I need add

func (r *ServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
    return ctrl.NewControllerManagedBy(mgr).
        For(&opsv1.Service{}).Watches(&source.Kind{Type: &opsv1.Pod{}}, &handler.EnqueueRequestForObject{}).
        Complete(r)
}

then how to handle in Reconcile() function? thanks.

/kind documentation

kindocumentation

Most helpful comment

return ctrl.NewControllerManagedBy(mgr).
For(&batch.CronJob{}).
Owns(&kbatch.Job{}).
Watches(&source.Kind{Type: &corev1.Node{}}, &handler.EnqueueRequestForObject{}).
Complete(r)

Note: In the above case, when Reconcile is called due to change in Node then the request.NamespacedName will contain /node-name. You can add namespace/name to request using the EnqueueRequestForObject{}.

All 4 comments

That sounds like Owns. Please read the section in the book about this: https://book.kubebuilder.io/cronjob-tutorial/controller-implementation.html#setup

It's unfortunate that the linked references Owns(), there is a Watches() function and the original request was about Watches. Owns() is not the same as Watches, one might want to watch but not own.

return ctrl.NewControllerManagedBy(mgr).
For(&batch.CronJob{}).
Owns(&kbatch.Job{}).
Watches(&source.Kind{Type: &corev1.Node{}}, &handler.EnqueueRequestForObject{}).
Complete(r)

Note: In the above case, when Reconcile is called due to change in Node then the request.NamespacedName will contain /node-name. You can add namespace/name to request using the EnqueueRequestForObject{}.

Hello
Can these Watches be filtered with labels, like I have CRD with to attribs
Var1: value1
Var2: value2.
Then a test pod is created with a test yaml file where labels are set with var1:value1 and var2:value2

I could get notifications for the CRD to create, update and delete in the reconciler.
But I need to get the notification for these pods as well with watches and differentiate in reconciler if it has come from CRD or POD and if the event is created/delete or update.
Please let me know how can I proceed with it.
Thanks

Was this page helpful?
0 / 5 - 0 ratings