-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
experiment defining runtime Condition
in terms of option functions
#1498
Draft
clux
wants to merge
1
commit into
main
Choose a base branch
from
condition-option
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ use thiserror::Error; | |
|
||
use crate::watcher::{self, watch_object}; | ||
|
||
/// Wait errors from [`await_condition`] | ||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("failed to probe for whether the condition is fulfilled yet: {0}")] | ||
|
@@ -79,21 +80,18 @@ where | |
/// use k8s_openapi::api::core::v1::Pod; | ||
/// fn my_custom_condition(my_cond: &str) -> impl Condition<Pod> + '_ { | ||
/// move |obj: Option<&Pod>| { | ||
/// if let Some(pod) = &obj { | ||
/// if let Some(status) = &pod.status { | ||
/// if let Some(conds) = &status.conditions { | ||
/// if let Some(pcond) = conds.iter().find(|c| c.type_ == my_cond) { | ||
/// return pcond.status == "True"; | ||
/// } | ||
/// } | ||
/// } | ||
/// } | ||
/// false | ||
/// let cond = obj.status.as_ref()?.conditions.as_ref()?.iter().find(|c| c.type_ == my_cond); | ||
/// Some(cond.status == "True") | ||
/// } | ||
/// } | ||
/// ``` | ||
pub trait Condition<K> { | ||
fn matches_object(&self, obj: Option<&K>) -> bool; | ||
/// Condition function giving a clear answer | ||
fn matches_object(&self, obj: Option<&K>) -> bool { | ||
self.matches_object_option(obj).unwrap_or_default() | ||
} | ||
/// Condition function giving an option wrapped answer | ||
fn matches_object_option(&self, _obj: Option<&K>) -> Option<bool>; | ||
|
||
/// Returns a `Condition` that holds if `self` does not | ||
/// | ||
|
@@ -153,8 +151,13 @@ pub trait Condition<K> { | |
} | ||
} | ||
|
||
impl<K, F: Fn(Option<&K>) -> bool> Condition<K> for F { | ||
fn matches_object(&self, obj: Option<&K>) -> bool { | ||
//impl<K, F: Fn(Option<&K>) -> bool> Condition<K> for F { | ||
// fn matches_object(&self, obj: Option<&K>) -> bool { | ||
// (self)(obj) | ||
// } | ||
//} | ||
impl<K, F: Fn(Option<&K>) -> Option<bool>> Condition<K> for F { | ||
fn matches_object_option(&self, obj: Option<&K>) -> Option<bool> { | ||
(self)(obj) | ||
} | ||
} | ||
|
@@ -176,12 +179,12 @@ pub mod conditions { | |
#[must_use] | ||
pub fn is_deleted<K: Resource>(uid: &str) -> impl Condition<K> + '_ { | ||
move |obj: Option<&K>| { | ||
obj.map_or( | ||
Some(obj.map_or( | ||
// Object is not found, success! | ||
true, | ||
// Object is found, but a changed uid would mean that it was deleted and recreated | ||
|obj| obj.meta().uid.as_deref() != Some(uid), | ||
) | ||
)) | ||
} | ||
} | ||
|
||
|
@@ -192,57 +195,45 @@ pub mod conditions { | |
#[must_use] | ||
pub fn is_crd_established() -> impl Condition<CustomResourceDefinition> { | ||
|obj: Option<&CustomResourceDefinition>| { | ||
if let Some(o) = obj { | ||
if let Some(s) = &o.status { | ||
if let Some(conds) = &s.conditions { | ||
if let Some(pcond) = conds.iter().find(|c| c.type_ == "Established") { | ||
return pcond.status == "True"; | ||
} | ||
} | ||
} | ||
} | ||
false | ||
let cond = obj | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks much better and simpler in terms of readability to me. |
||
.as_ref()? | ||
.status | ||
.as_ref()? | ||
.conditions | ||
.as_ref()? | ||
.iter() | ||
.find(|c| c.type_ == "Established")?; | ||
Some(cond.status == "True") | ||
} | ||
} | ||
|
||
/// An await condition for `Pod` that returns `true` once it is running | ||
#[must_use] | ||
pub fn is_pod_running() -> impl Condition<Pod> { | ||
|obj: Option<&Pod>| { | ||
if let Some(pod) = &obj { | ||
if let Some(status) = &pod.status { | ||
if let Some(phase) = &status.phase { | ||
return phase == "Running"; | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|obj: Option<&Pod>| Some(obj?.status.as_ref()?.phase.as_ref()? == "Running") | ||
} | ||
|
||
/// An await condition for `Job` that returns `true` once it is completed | ||
#[must_use] | ||
pub fn is_job_completed() -> impl Condition<Job> { | ||
|obj: Option<&Job>| { | ||
if let Some(job) = &obj { | ||
if let Some(s) = &job.status { | ||
if let Some(conds) = &s.conditions { | ||
if let Some(pcond) = conds.iter().find(|c| c.type_ == "Complete") { | ||
return pcond.status == "True"; | ||
} | ||
} | ||
} | ||
} | ||
false | ||
let cond = obj? | ||
.status | ||
.as_ref()? | ||
.conditions | ||
.as_ref()? | ||
.iter() | ||
.find(|c| c.type_ == "Complete")?; | ||
Some(cond.status == "True") | ||
} | ||
} | ||
|
||
/// See [`Condition::not`] | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub struct Not<A>(pub(super) A); | ||
impl<A: Condition<K>, K> Condition<K> for Not<A> { | ||
fn matches_object(&self, obj: Option<&K>) -> bool { | ||
!self.0.matches_object(obj) | ||
fn matches_object_option(&self, obj: Option<&K>) -> Option<bool> { | ||
Some(!self.0.matches_object(obj)) | ||
} | ||
} | ||
|
||
|
@@ -254,8 +245,8 @@ pub mod conditions { | |
A: Condition<K>, | ||
B: Condition<K>, | ||
{ | ||
fn matches_object(&self, obj: Option<&K>) -> bool { | ||
self.0.matches_object(obj) && self.1.matches_object(obj) | ||
fn matches_object_option(&self, obj: Option<&K>) -> Option<bool> { | ||
Some(self.0.matches_object(obj) && self.1.matches_object(obj)) | ||
} | ||
} | ||
|
||
|
@@ -267,8 +258,8 @@ pub mod conditions { | |
A: Condition<K>, | ||
B: Condition<K>, | ||
{ | ||
fn matches_object(&self, obj: Option<&K>) -> bool { | ||
self.0.matches_object(obj) || self.1.matches_object(obj) | ||
fn matches_object_option(&self, obj: Option<&K>) -> Option<bool> { | ||
Some(self.0.matches_object(obj) || self.1.matches_object(obj)) | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method can also be named just
matches
?