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

Add Predicate trait to allow combination + fallbacks #1228

Merged
merged 6 commits into from Jun 20, 2023
Merged

Conversation

clux
Copy link
Member

@clux clux commented Jun 12, 2023

Converts predicates from a Fn(&K -> Option<u64>) to a trait Predicate that is implemented by fns of the same signature.

In controller-runtime terminology; this allows us to "or" predicates through Predicate::fallback, and allows us to "and" predicates through Predicate::combine.

Have decided to go for Fallback/Combine rather than Or/And because it feels confusing to use or on a fn returning Option (and i don't think it's the logical relation maps to the fallback/combination sufficiently well as touched on in #1225).

Usage with controllers:

use kube::runtime::{predicates, Predicate};

let predicate1 = predicates::labels.combine(predicates::annotations);
let predicate2 = predicates::generation.fallback(predicates::resource_version);

let stream = reflector(writer, watcher(api, cfg))
    .applied_objects()
    .predicate_filter(predicateX);

Controller::for_stream(stream, reader)
    .shutdown_on_signal()
    .run(reconcile, error_policy, context)
    .for_each(|_| futures::future::ready(()))
    .await;

This is a minor change to an unstable feature. The signature of predicates is preserved, but the stream interface now takes an impl Predicate rather than a function of type Fn(&K -> Option<u64>) - but these functions get a blanket impl of Predicate so this should not be noticeable for users.

Signed-off-by: clux <sszynrae@gmail.com>
@clux clux linked an issue Jun 12, 2023 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Jun 12, 2023

Codecov Report

Merging #1228 (1aa0192) into main (2ad3827) will decrease coverage by 0.10%.
The diff coverage is 47.05%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1228      +/-   ##
==========================================
- Coverage   73.12%   73.03%   -0.10%     
==========================================
  Files          69       69              
  Lines        5515     5525      +10     
==========================================
+ Hits         4033     4035       +2     
- Misses       1482     1490       +8     
Impacted Files Coverage Δ
kube-core/src/request.rs 98.71% <ø> (ø)
kube-runtime/src/utils/mod.rs 64.15% <ø> (ø)
kube-runtime/src/utils/watch_ext.rs 0.00% <0.00%> (ø)
kube-runtime/src/utils/predicate.rs 74.19% <50.00%> (-10.43%) ⬇️

clux added 2 commits June 13, 2023 11:01
Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
@clux clux changed the title Add Predicate trait to allow implementing Fallback Add Predicate trait to allow combination + fallbacks Jun 16, 2023
Signed-off-by: clux <sszynrae@gmail.com>
@clux clux added the unstable feature that unstable feature gating label Jun 19, 2023
@clux clux added this to the 0.84.0 milestone Jun 19, 2023
@clux clux marked this pull request as ready for review June 19, 2023 14:31
clux added 2 commits June 19, 2023 15:34
Signed-off-by: clux <sszynrae@gmail.com>
@clux clux added the changelog-add changelog added category for prs label Jun 19, 2023
@clux clux merged commit 5806a2d into main Jun 20, 2023
16 of 17 checks passed
@clux clux deleted the predicate-fallback branch June 20, 2023 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-add changelog added category for prs unstable feature that unstable feature gating
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow combining runtime predicates
2 participants