-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
788 additions
and
245 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use super::*; | ||
|
||
impl StartEndTimeable for corev1::ContainerState { | ||
fn start_ts(&self) -> anyhow::Result<Option<i64>> { | ||
match self { | ||
corev1::ContainerState { running: Some(r), terminated: None, waiting: None } => { | ||
Ok(Some(r.started_at.as_ref().unwrap().0.timestamp())) | ||
}, | ||
corev1::ContainerState { running: None, terminated: Some(t), waiting: None } => { | ||
Ok(Some(t.started_at.as_ref().unwrap().0.timestamp())) | ||
}, | ||
corev1::ContainerState { running: None, terminated: None, waiting: Some(_) } => Ok(None), | ||
_ => Err(KubernetesError::malformed_container_state(self)), | ||
} | ||
} | ||
|
||
fn end_ts(&self) -> anyhow::Result<Option<i64>> { | ||
match self { | ||
corev1::ContainerState { running: Some(_), terminated: None, waiting: None } => Ok(None), | ||
corev1::ContainerState { running: None, terminated: Some(t), waiting: None } => { | ||
Ok(Some(t.finished_at.as_ref().unwrap().0.timestamp())) | ||
}, | ||
corev1::ContainerState { running: None, terminated: None, waiting: Some(_) } => Ok(None), | ||
_ => Err(KubernetesError::malformed_container_state(self)), | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#[macro_export] | ||
macro_rules! klabel { | ||
($($key:tt=$val:literal),+$(,)?) => { | ||
Some(BTreeMap::from([$(($key.to_string(), $val.to_string())),+])) | ||
}; | ||
} | ||
|
||
pub use std::collections::BTreeMap; | ||
|
||
pub use klabel; |
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 |
---|---|---|
@@ -1,10 +1,58 @@ | ||
mod apiset; | ||
mod container; | ||
mod gvk; | ||
pub mod macros; | ||
mod pod; | ||
mod pod_lifecycle; | ||
mod util; | ||
|
||
pub use apiset::*; | ||
pub use gvk::*; | ||
use k8s_openapi::api::core::v1 as corev1; | ||
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1; | ||
pub use util::*; | ||
|
||
use crate::errors::*; | ||
use crate::macros::partial_ord_eq_ref; | ||
|
||
const LAST_APPLIED_CONFIG_LABEL_KEY: &str = "kubectl.kubernetes.io/last-applied-configuration"; | ||
const DEPL_REVISION_LABEL_KEY: &str = "deployment.kubernetes.io/revision"; | ||
|
||
err_impl! {KubernetesError, | ||
#[error("field not found in struct: {0}")] | ||
FieldNotFound(String), | ||
|
||
#[error("malformed container status: {0:?}")] | ||
MalformedContainerState(corev1::ContainerState), | ||
|
||
#[error("malformed label selector: {0:?}")] | ||
MalformedLabelSelector(metav1::LabelSelectorRequirement), | ||
} | ||
|
||
#[derive(Clone, Debug, Eq, PartialEq)] | ||
pub enum PodLifecycleData { | ||
Empty, | ||
Running(i64), | ||
Finished(i64, i64), | ||
} | ||
partial_ord_eq_ref!(PodLifecycleData); | ||
|
||
pub trait KubeResourceExt { | ||
fn namespaced_name(&self) -> String; | ||
fn matches(&self, sel: &metav1::LabelSelector) -> anyhow::Result<bool>; | ||
} | ||
|
||
pub trait PodExt { | ||
fn spec(&self) -> anyhow::Result<&corev1::PodSpec>; | ||
fn status(&self) -> anyhow::Result<&corev1::PodStatus>; | ||
fn spec_mut(&mut self) -> &mut corev1::PodSpec; | ||
fn status_mut(&mut self) -> &mut corev1::PodStatus; | ||
} | ||
|
||
trait StartEndTimeable { | ||
fn start_ts(&self) -> anyhow::Result<Option<i64>>; | ||
fn end_ts(&self) -> anyhow::Result<Option<i64>>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod util_test; | ||
mod test; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use super::*; | ||
|
||
impl PodExt for corev1::Pod { | ||
fn spec(&self) -> anyhow::Result<&corev1::PodSpec> { | ||
match self.spec.as_ref() { | ||
None => bail!(KubernetesError::field_not_found("pod spec")), | ||
Some(ps) => Ok(ps), | ||
} | ||
} | ||
|
||
fn status(&self) -> anyhow::Result<&corev1::PodStatus> { | ||
match self.status.as_ref() { | ||
None => bail!(KubernetesError::field_not_found("pod status")), | ||
Some(ps) => Ok(ps), | ||
} | ||
} | ||
|
||
fn spec_mut(&mut self) -> &mut corev1::PodSpec { | ||
if self.spec.is_none() { | ||
self.spec = Some(Default::default()); | ||
} | ||
self.spec.as_mut().unwrap() | ||
} | ||
|
||
fn status_mut(&mut self) -> &mut corev1::PodStatus { | ||
if self.status.is_none() { | ||
self.status = Some(Default::default()); | ||
} | ||
self.status.as_mut().unwrap() | ||
} | ||
} |
Oops, something went wrong.