Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Filter by arbitrary callback function #98

Closed
8 tasks done
nolar opened this issue Jun 2, 2019 · 4 comments
Closed
8 tasks done

Filter by arbitrary callback function #98

nolar opened this issue Jun 2, 2019 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@nolar
Copy link
Contributor

nolar commented Jun 2, 2019

In some cases, it might be needed that only a subset of resources is served and handled. Some of such filtering are suggested in #45 #58, but it can be anything arbitrary.

Without such filtering, the logs will be cluttered by the resource events/causes that are of no interest to the operator.

For this, a callback function can be specified on the handler declaration:

KNOWN_PODS = {}

@kopf.on.event('', 'v1', 'pods'
               when=lambda event, uid, **_:  event=='ADDED' and uid not in KNOWN_PODS)
def new_pod_created(**kwargs):
    pass

The callback should be called via the same invocation protocol as the handlers (see kopf.reactor.invocation), i.e. with all the handler's kwargs — be that an event-handler or a cause-handler (different sets of kwargs).

If the callback returns true, then the object is handled as normally. When it returns false, the handler is ignored, maybe the whole event is ignored (if there are no other handlers) — and therefore produces no single line of logs at all.

No additional kwargs are injected for such filtering.


Related:

Checklist:

  • Acceptance criteria:
    • Event handler registered with the filtering callback function/lambda/partial.
    • Callback is invoked every time an event is arrived and/or cause is detected.
    • Events/handlers are filtered by the result of this callback invocation.
    • No logs about handler or event/cause are produced is the handler was filtered out.
    • Minimum overhead and not extra calls if the callback is not specified (the default).
  • Tests.
  • Documentation.
@nolar nolar mentioned this issue Jun 2, 2019
8 tasks
@nolar nolar added the enhancement New feature or request label Jun 2, 2019
@nolar nolar changed the title Filter by an arbitrary callback Filter by arbitrary callback function Jun 2, 2019
@Jc2k
Copy link
Contributor

Jc2k commented Nov 27, 2019

This would be really handy for me.

I was thinking about writing a cert-manager plugin with kopf - something like this.

With this feature I would be able to filter out CertificateRequests that weren't "assigned" to my operator by looking at spec.issuerRef.

(Presumably these objects would then not be tagged with the kopf last-configuration annotations etc too).

@nolar
Copy link
Contributor Author

nolar commented Nov 27, 2019

I think, it is easily implementable now. Since at least some filters were added in #123 (by labels/annotations), adding more filters is easy.

A starting place to look for: https://github.com/zalando-incubator/kopf/blob/0.23.2/kopf/reactor/registries.py#L570-L623

@Jc2k
Copy link
Contributor

Jc2k commented Nov 27, 2019

Thanks for the hint. I've had a bash at this in #258.

@nolar
Copy link
Contributor Author

nolar commented Jan 29, 2020

Released as kopf==0.25.

@Jc2k Thank you very much for your contribution!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants