Description
The Reconcile Loop provides a basic building block that allows writing simple operators.
When an operator starts to perform some "more advanced" operations the logic can easily become messy, here I would like to explore what the SDK can offer to help structuring the code of more complex workflows.
To start the discussion I can put on the table an early experiment I ran to model the Reconciliation loop as a Finite State Machine:
https://github.com/andreaTP/poc-operator-mutable-jar/blob/34d65d9626b7a1202ba1a8197366f49e2f2c0aa6/src/main/java/org/keycloak/ControllerFSM.java#L16-L21
This implementation is based on an opinionated decision:
- use (at least part of) the
Status
field of the CR to store the status
Running the example you can already see in action a few of the "benefits" of this approach:
- The workflow description is extremely evident and clear
- Easier to read (at least for me since I have done a ton of Actor programming 😛 )
- The steps of the workflow are isolated and they can be run/be tested in isolation
- Easy debugging: when something fails you need to debug only the current state and not the entire reconcile loop
- A primitive but working control over state transitions
There are various additional possibilities if we decide to build on top of this foundation, such as:
- The retry mechanism of the SDK can be configurable on each state
- The compensation action caused by a failing state can be defined specifically for each state
- Timeouts bound to specific states
- Configurable automatic logging of the states and the transitions
- ...