Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter by arbitrary callback function #98

Closed
8 tasks done
kopf-archiver bot opened this issue Aug 18, 2020 · 0 comments
Closed
8 tasks done

Filter by arbitrary callback function #98

kopf-archiver bot opened this issue Aug 18, 2020 · 0 comments
Labels
archive enhancement New feature or request

Comments

@kopf-archiver
Copy link

kopf-archiver bot commented Aug 18, 2020

An issue by nolar at 2019-06-02 12:56:13+00:00
Original URL: zalando-incubator/kopf#98
 

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.

Commented by Jc2k at 2019-11-27 10:51:47+00:00
 

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).


Commented by nolar at 2019-11-27 12:45:30+00:00
 

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/nolar/kopf/blob/0.23.2/kopf/reactor/registries.py#L570-L623


Commented by Jc2k at 2019-11-27 22:31:11+00:00
 

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


Commented by nolar at 2020-01-29 14:14:21+00:00
 

Released as kopf==0.25.

Jc2k Thank you very much for your contribution!

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

No branches or pull requests

0 participants