controller-runtime icon indicating copy to clipboard operation
controller-runtime copied to clipboard

How to use new Watches() function ?

Open buzzlightyear2k opened this issue 1 year ago • 2 comments

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)
}

buzzlightyear2k avatar Aug 15 '24 02:08 buzzlightyear2k

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.

sathieu avatar Aug 16 '24 06:08 sathieu

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)

sbueringer avatar Aug 19 '24 10:08 sbueringer

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/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was 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

k8s-triage-robot avatar Nov 17 '24 10:11 k8s-triage-robot

/close

sbueringer avatar Nov 18 '24 08:11 sbueringer

@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.

k8s-ci-robot avatar Nov 18 '24 08:11 k8s-ci-robot