5

I recently got started with building a Kubernetes operator. I'm using the Fabric8 Java Kubernetes Client but I think my question is more general and also applies to other programming languages and libraries.

When reading through blog posts, documentation or textbooks explaining the operator pattern, I found there seem to be two options to design an operator:

  1. Using an infinite reconcile loop, in which all corresponding Kubernetes objects are retrieved from the API and then some action is performed.
  2. Using informers, which are called whenever an observed Kubernetes resource changes.

However, I don't find any source discussion which option should be used in which case. Are there any best practices?

Sören Henning
  • 326
  • 4
  • 16

1 Answers1

2

You should use both.

When using informers, it's possible that the handler gets the events out of order or even not at all. The former means the handler needs to define and reconcile state - this approach is referred to as level-based, as opposed to edge-based. The latter means reconciliation needs to be triggered on a regular interval to account for that possibility.

The way controller-runtime does things, reconciliation is triggered by cluster events (using informers behind the scenes) related to the resources watched by the controller and on a timer. Also, by design, the event is not passed to the reconciler so that it is forced to define and act on a state.

Galletti_Lance
  • 509
  • 2
  • 4
  • 15
  • Thanks! But if we do reconciliation on a timer anyway, why do we need informers (for an operator) at all? Is it just to get updates faster? – Sören Henning Jan 04 '22 at 11:07
  • 1
    Yes but the timer should be a fallback mechanism when possible because it is more resource intensive. – Galletti_Lance Jan 04 '22 at 13:47
  • Okay, great. Thanks for the reply. I'm still curious how generalizeable the aspects of resource usage and "reliability" of the informers are, when considering different frameworks such as Kubebuilder, Fabric8 Kubernetes Client etc. – Sören Henning Jan 06 '22 at 12:48
  • I'm sorry I don't quite understand - are you asking how comparable the informer implementation is across these different frameworks? or if the above recommendation depends on the framework? – Galletti_Lance Jan 06 '22 at 13:51
  • 1
    I think both: Is it rather an implementation detail that both should be used (andd hence depends on the framework) or is it due to the way the Kubernetes API should be used. I asked the fabric8 maintainers on their view on this topic and also got a very helpful reply, which I would like to share here as well: https://github.com/fabric8io/kubernetes-client/discussions/3717 – Sören Henning Jan 14 '22 at 15:43