1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pub use k8s_openapi::apimachinery::pkg::apis::meta::v1::{ListMeta, ObjectMeta};
use k8s_openapi::Metadata;

/// An accessor trait for Metadata
///
/// This for a subset of kubernetes type that do not end in List
/// These types, using ObjectMeta, SHOULD all have required properties:
/// - .metadata
/// - .metadata.name
/// And these optional properties:
/// - .metadata.namespace
/// - .metadata.resource_version
///
/// This avoids a bunch of the unnecessary unwrap mechanics for apps
pub trait Meta: Metadata {
    fn meta(&self) -> &ObjectMeta;
    fn name(&self) -> String;
    fn namespace(&self) -> Option<String>;
    fn resource_ver(&self) -> Option<String>;
}

/// Implement accessor trait for any ObjectMeta-using kubernetes Resource
impl<K> Meta for K
where
    K: Metadata<Ty = ObjectMeta>,
{
    fn meta(&self) -> &ObjectMeta {
        self.metadata().expect("kind has metadata")
    }

    fn name(&self) -> String {
        self.meta().name.clone().expect("kind has metadata.name")
    }

    fn resource_ver(&self) -> Option<String> {
        self.meta().resource_version.clone()
    }

    fn namespace(&self) -> Option<String> {
        self.meta().namespace.clone()
    }
}

/// A convenience struct for ad-hoc serialization
///
/// Mostly useful for `Object`
#[derive(Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "camelCase")]
pub struct TypeMeta {
    /// The version of the API
    pub api_version: String,

    /// The name of the API
    pub kind: String,
}