-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add DeploymentConditionBuilder #993
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0dda9a8
feat: add DeploymentConditionBuilder
razvan 9157290
update changelog
razvan 92dd769
fix cargo doc
razvan ba6e9d8
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan 0391a33
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan c133d9d
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan 50ccbda
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan fd317ca
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan 6163e0d
rename var
razvan b8e38ac
Merge branch 'main' into feat/deployment-condition
razvan 65b04be
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan d3515a8
Update crates/stackable-operator/src/status/condition/deployment.rs
razvan 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 hidden or 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
191 changes: 191 additions & 0 deletions
191
crates/stackable-operator/src/status/condition/deployment.rs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,191 @@ | ||
use std::cmp; | ||
|
||
use k8s_openapi::api::apps::v1::Deployment; | ||
use kube::ResourceExt; | ||
|
||
use crate::status::condition::{ | ||
ClusterCondition, ClusterConditionSet, ClusterConditionStatus, ClusterConditionType, | ||
ConditionBuilder, | ||
}; | ||
|
||
/// Default implementation to build [`ClusterCondition`]s for | ||
/// `Deployment` resources. | ||
/// | ||
/// Currently only the `ClusterConditionType::Available` is implemented. This will be extended | ||
/// to support all `ClusterConditionType`s in the future. | ||
#[derive(Default)] | ||
pub struct DeploymentConditionBuilder { | ||
deployments: Vec<Deployment>, | ||
} | ||
|
||
impl ConditionBuilder for DeploymentConditionBuilder { | ||
fn build_conditions(&self) -> ClusterConditionSet { | ||
vec![self.available()].into() | ||
} | ||
} | ||
|
||
impl DeploymentConditionBuilder { | ||
pub fn add(&mut self, dplmt: Deployment) { | ||
self.deployments.push(dplmt); | ||
} | ||
|
||
fn available(&self) -> ClusterCondition { | ||
let mut available = ClusterConditionStatus::True; | ||
let mut unavailable_resources = vec![]; | ||
for deployment in &self.deployments { | ||
let current_status = Self::deployment_available(deployment); | ||
|
||
if current_status != ClusterConditionStatus::True { | ||
unavailable_resources.push(deployment.name_any()) | ||
} | ||
|
||
available = cmp::max(available, current_status); | ||
} | ||
|
||
// We need to sort here to make sure roles and role groups are not changing position | ||
// due to the HashMap (random order) logic. | ||
unavailable_resources.sort(); | ||
|
||
let message = match available { | ||
ClusterConditionStatus::True => { | ||
"All Deployments have the requested amount of ready replicas.".to_string() | ||
} | ||
ClusterConditionStatus::False => { | ||
format!("Deployment {unavailable_resources:?} missing ready replicas.") | ||
} | ||
ClusterConditionStatus::Unknown => { | ||
"Deployment status cannot be determined.".to_string() | ||
} | ||
}; | ||
|
||
ClusterCondition { | ||
reason: None, | ||
message: Some(message), | ||
status: available, | ||
type_: ClusterConditionType::Available, | ||
last_transition_time: None, | ||
last_update_time: None, | ||
} | ||
} | ||
|
||
/// Returns a condition "Available: True" if the number of requested replicas matches | ||
/// the number of available replicas. In addition, there needs to be at least one replica | ||
/// available. | ||
fn deployment_available(dplmt: &Deployment) -> ClusterConditionStatus { | ||
let requested_replicas = dplmt | ||
.spec | ||
.as_ref() | ||
.and_then(|spec| spec.replicas) | ||
.unwrap_or_default(); | ||
let available_replicas = dplmt | ||
razvan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.status | ||
.as_ref() | ||
.and_then(|status| status.available_replicas) | ||
.unwrap_or_default(); | ||
|
||
if requested_replicas == available_replicas && requested_replicas != 0 { | ||
ClusterConditionStatus::True | ||
} else { | ||
ClusterConditionStatus::False | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use k8s_openapi::api::apps::v1::{Deployment, DeploymentSpec, DeploymentStatus}; | ||
|
||
use crate::status::condition::{ | ||
deployment::DeploymentConditionBuilder, ClusterCondition, ClusterConditionStatus, | ||
ClusterConditionType, ConditionBuilder, | ||
}; | ||
|
||
fn build_deployment(spec_replicas: i32, available_replicas: i32) -> Deployment { | ||
Deployment { | ||
spec: Some(DeploymentSpec { | ||
replicas: Some(spec_replicas), | ||
..DeploymentSpec::default() | ||
}), | ||
status: Some(DeploymentStatus { | ||
available_replicas: Some(available_replicas), | ||
..DeploymentStatus::default() | ||
}), | ||
..Deployment::default() | ||
} | ||
} | ||
|
||
#[test] | ||
fn available() { | ||
let deployment = build_deployment(3, 3); | ||
|
||
assert_eq!( | ||
DeploymentConditionBuilder::deployment_available(&deployment), | ||
ClusterConditionStatus::True | ||
); | ||
} | ||
|
||
#[test] | ||
fn unavailable() { | ||
let deployment = build_deployment(3, 2); | ||
|
||
assert_eq!( | ||
DeploymentConditionBuilder::deployment_available(&deployment), | ||
ClusterConditionStatus::False | ||
); | ||
|
||
let deployment = build_deployment(3, 4); | ||
|
||
assert_eq!( | ||
DeploymentConditionBuilder::deployment_available(&deployment), | ||
ClusterConditionStatus::False | ||
); | ||
} | ||
|
||
#[test] | ||
fn condition_available() { | ||
let mut deployment_condition_builder = DeploymentConditionBuilder::default(); | ||
deployment_condition_builder.add(build_deployment(3, 3)); | ||
|
||
let conditions = deployment_condition_builder.build_conditions(); | ||
|
||
let got = conditions | ||
.conditions | ||
.get::<usize>(ClusterConditionType::Available.into()) | ||
.cloned() | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let expected = ClusterCondition { | ||
type_: ClusterConditionType::Available, | ||
status: ClusterConditionStatus::True, | ||
..ClusterCondition::default() | ||
}; | ||
|
||
assert_eq!(got.type_, expected.type_); | ||
assert_eq!(got.status, expected.status); | ||
} | ||
|
||
#[test] | ||
fn condition_unavailable() { | ||
let mut deployment_condition_builder = DeploymentConditionBuilder::default(); | ||
deployment_condition_builder.add(build_deployment(3, 2)); | ||
|
||
let conditions = deployment_condition_builder.build_conditions(); | ||
|
||
let got = conditions | ||
.conditions | ||
.get::<usize>(ClusterConditionType::Available.into()) | ||
.cloned() | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let expected = ClusterCondition { | ||
type_: ClusterConditionType::Available, | ||
status: ClusterConditionStatus::False, | ||
..ClusterCondition::default() | ||
}; | ||
|
||
assert_eq!(got.type_, expected.type_); | ||
assert_eq!(got.status, expected.status); | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod daemonset; | ||
pub mod deployment; | ||
pub mod operations; | ||
pub mod statefulset; | ||
|
||
|
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.
Uh oh!
There was an error while loading. Please reload this page.