How to use new Watches() function ?
I have 2 CRD named Elastalert and ElastalertRule, how to write a Watches() func to watch whenever ElastAlertRule which name is equal to ElastAlert created then it auto trigger to reconcile the following Elastalert, can you give me example ?
func (r *ElastalertReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
//log := ctrl.LoggerFrom(ctx)
return ctrl.NewControllerManagedBy(mgr).
For(&elastalertv1.Elastalert{}).
Watches(
source.Kind(mgr.GetCache(), &elastalertv1.ElastalertRule{}, )
&handler.EnqueueRequestForObject{},
).
Owns(&elastalertv1.ElastalertRule{}).
Complete(r)
}
There is a example for WatchesRawSource :
https://github.com/kubernetes-sigs/controller-runtime/blob/b901db121e1f53c47ec9f9683fad90a546688c3e/examples/typed/main.go#L46-L52
If you adapt to your use-case:
func (r *ElastalertReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
//log := ctrl.LoggerFrom(ctx)
return ctrl.NewControllerManagedBy(mgr).
For(&elastalertv1.Elastalert{}).
WatchesRawSource(source.Kind(
mgr.GetCache(),
&elastalertv1.ElastalertRule{},
handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, elastAlert *elastalertv1.ElastalertRule) []ctrl.Request {
return []ctrl.Request{{
NamespacedName: elastAlert.Spec.ElastAlertRef,
}}
})),
).
Owns(&elastalertv1.ElastalertRule{}).
Complete(r)
}
I have not tested it.
NB: I have the same use-case for Grafana Org Operator.
It depends on what sort of logic you want for enqueue. Based on your issue description, I think the following should work
return ctrl.NewControllerManagedBy(mgr).
For(&elastalertv1.Elastalert{}).
Watches(
&elastalertv1.ElastalertRule{},
&handler.EnqueueRequestForObject{},
).
Complete(r)
The Kubernetes project currently lacks enough contributors to adequately respond to all issues.
This bot triages un-triaged issues according to the following rules:
- After 90d of inactivity,
lifecycle/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas applied, the issue is closed
You can:
- Mark this issue as fresh with
/remove-lifecycle stale - Close this issue with
/close - Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale
/close
@sbueringer: Closing this issue.
In response to this:
/close
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.