Open philippart opened 1 year ago
Hi. The answer is: yes and no, depending on the scenario.
There is no such functionality to pass the operator's global state to handlers. Especially since there can be several --namespace
options, and they can be globs with inclusions & exclusions, and especially since the actual namespaces can be created/deleted at runtime of the operator, so the set changes all the time.
But you can cheat the system by changing the request from "namespaces watched by the operator" to "namespaces with resources watched by the operator" — and use in-memory indexing to build that list dynamically.
import kopf
@kopf.index('some-indicative-resource1')
@kopf.index('some-indicative-resource2')
def active_namespaces(namespace, **_):
return namespace
@kopf.on.create('kopfexamples')
def createfn(active_namespaces, **_):
namespaces = set(active_namespaces[None])
print(f"I am aware of namespaces: {namespaces}")
The downside is that you cannot see namespaces that Kopf watches over, but which have no resources.
As another route, you can try embedding an operator into your own script, and pass the list of namespaces to handle both to namespaces=…
argument AND to memo=
passed to kopf.operator()
. See more about custom memos.
This works only with the fixed list of namespaces which does not change over time.
Keywords
cli, scopes
Problem
For my use case I need to list all the custom resources of a given kind present in the namespaces that the operator is watching. Rather than hard-coding the list of namespaces or having to read a configmap in some pre-defined namespace, would it be possible to know (api?) which namespaces kopf is watching based on kopf
--namespace
command line argument.