kubernetesoperator-sdk

Kubernetes operator with 2 controllers


I have an operator that has 2 controllers in it. The controllerA watches for CRD_A and if it finds a CR (we can have only one CR of this type in the cluster) of this type A the controller creates a podA and sets the CR as the owner of the podA. The controllerB watches for CRD_B, if it finds CR of type B the controller checks if podA exists, and it setup the pod by sending an HTTP request to the podA with the info from the CR. This is a simple overview of the operator's work.

The problem is that when the podA is deleted (by me or Kubernetes wants to reschedule it) the reconcile of controllerA is triggered because CR_A is the owner of podA and it creates a new podA. But I also want controllerB to be reconciled because it has to setup the podA, because now it is not reconciled because there is no connection between the podA and controllerB.

What is the right way to trigger reconcile of controllerB when such an event happens? I cannot set two CRs as owners of the pod. I think somehow controllerA should send reconcile event to controllerB but I don't how this can happen and is this the right way?


Solution

  • I think you were asking in Slack about this last week but the rough answer is "use a watch map". https://github.com/coderanger/migrations-operator/blob/088a3b832f0acab4bfe02c03a4404628c5ddfd97/components/migrations.go#L63-L91 is an example of one. It gets the event from the low level watch (an instance of A) and then you write some code to match that back to which root object to reconcile (an instance of B).