how to distinguish the action when entering the reconcile() function? #2511
-
I want to know how to get the API action(( like patch create update...)) apply to any resource watched when triggering the reconcile() function. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @a85302543, The k8s api raise events when a resource is created, edited and deleted. By Watching Resources we can re-trigger the reconcile to ensure a desired stated on the cluster. You can define them in the function
When developing operators, it is essential for the controller’s reconciliation loop to be idempotent. By following the Operator pattern you will create Controllers which provide a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster. Breaking this recommendation goes against the design principles of controller-runtime and may lead to unforeseen consequences such as resources becoming stuck and requiring manual intervention. Reconcile() The reconcile function is responsible for synchronizing the resources and their specifications according to the business logic implemented on them. In this way, it works like a loop, and it does not stop until all conditionals match its implementation. The following is pseudo-code with an example that clarifies it.
The following are a few possible return options to restart the Reconcile:
Note: For more details, check the Reconcile and its Result godoc. You can check that after you create an API and its controller ( You can check an example of the Memcached Operator built by following the SDK tutorial (https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/) here: operator-sdk/testdata/go/v3/memcached-operator/. Building your own operator commonly involves extending the Kubernetes API itself. It is helpful to understand exactly how Custom Resource Definitions interact with the Kubernetes API. Also, the Kubebuilder documentation on Groups and Versions and Kinds may be helpful to better understand these concepts as they relate to operators. 💡Recommended
Please, use the issues and the channels if you need additional help. |
Beta Was this translation helpful? Give feedback.
-
Closing since it is answered. |
Beta Was this translation helpful? Give feedback.
Hi @a85302543,
The k8s api raise events when a resource is created, edited and deleted. By Watching Resources we can re-trigger the reconcile to ensure a desired stated on the cluster.
You can define them in the function
SetupWithManager
that you can find in the controller.go. In the following example, we will be setting up controller Watches in a simplified way which has the controller Builder: