nolar / kopf

A Python framework to write Kubernetes operators in just a few lines of code

Home Page:https://kopf.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Update external service based on CRD and label selector

martinohansen opened this issue · comments

Hi 👋

Thanks for this wonderful project. I hope I can find advice here on how to build this kind of operator:

I’m trying to manage an external resource (External) based on a custom resource definition (CRD). The CRD includes a label selector in its spec that targets one or more Kubernetes services (SVCs).

The operator needs to patch External when there are changes in either the CRD or any of the SVCs. This means the SVCs that the operator monitors can vary over time, both with and without changes to the CRD.

What is the suggested way of doing this?

Thanks in advance.

I think you could dynamically register a handler, but not sure about un-registering.
https://kopf.readthedocs.io/en/stable/handlers/#registering

Maybe you could have a generic handler for services and adjust some global state which is then interpreted by a filter callback?
https://kopf.readthedocs.io/en/stable/filters/#callback-filters

e.g.

filter_spec = {}


@kopf.on.create('my-crd')
def my_crd_handler(spec, **_):
    filter_spec['something'] = spec.whatever


def filter_function(spec, **_):
    # inspect filter_spec and return True | False.
    pass
    

@kopf.on.update('', 'v1', 'service', when=filter_function)
def my_service_handler(spec, **_):
    # do your thing with service resource
    pass