sk_core/k8s/
mod.rs

1mod apiset;
2mod container_state;
3mod gvk;
4mod lease;
5mod owners;
6mod pod_ext;
7mod pod_lifecycle;
8mod sim;
9mod util;
10
11pub use apiset::*;
12pub use gvk::*;
13pub use lease::*;
14pub use owners::OwnersCache;
15use serde::{
16    Deserialize,
17    Serialize,
18};
19pub use sim::*;
20pub use util::*;
21
22use crate::errors::*;
23use crate::macros::partial_ord_eq_ref;
24use crate::prelude::*;
25
26const LAST_APPLIED_CONFIG_LABEL_KEY: &str = "kubectl.kubernetes.io/last-applied-configuration";
27const DEPL_REVISION_LABEL_KEY: &str = "deployment.kubernetes.io/revision";
28
29err_impl! {KubernetesError,
30    #[error("field not found in struct: {0}")]
31    FieldNotFound(String),
32
33    #[error("lease has different owner: {0}")]
34    LeaseHeldByOther(String),
35
36    #[error("malformed container status: {0:?}")]
37    MalformedContainerState(corev1::ContainerState),
38
39    #[error("malformed label selector: {0:?}")]
40    MalformedLabelSelector(metav1::LabelSelectorRequirement),
41}
42
43#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
44pub enum PodLifecycleData {
45    Empty,
46    Running(i64),
47    Finished(i64, i64),
48}
49partial_ord_eq_ref!(PodLifecycleData);
50
51pub trait KubeResourceExt {
52    fn namespaced_name(&self) -> String;
53    fn matches(&self, sel: &metav1::LabelSelector) -> anyhow::Result<bool>;
54}
55
56pub trait PodExt {
57    fn labels_contains_key(&self, key: &str) -> bool;
58    fn spec(&self) -> anyhow::Result<&corev1::PodSpec>;
59    fn stable_spec(&self) -> anyhow::Result<corev1::PodSpec>;
60    fn status(&self) -> anyhow::Result<&corev1::PodStatus>;
61}
62
63pub trait OpenApiResourceExt {
64    fn type_meta() -> TypeMeta;
65}
66
67impl<T: k8s_openapi::Resource> OpenApiResourceExt for T {
68    fn type_meta() -> TypeMeta {
69        TypeMeta {
70            api_version: T::API_VERSION.into(),
71            kind: T::KIND.into(),
72        }
73    }
74}
75
76trait StartEndTimeable {
77    fn start_ts(&self) -> anyhow::Result<Option<i64>>;
78    fn end_ts(&self) -> anyhow::Result<Option<i64>>;
79}
80
81#[cfg(test)]
82pub mod tests;