k8s_openapi_ext/ext/
pod_template_spec.rs

1use super::*;
2
3pub trait PodTemplateSpecExt: Sized {
4    fn new() -> Self;
5
6    fn label(self, key: impl ToString, value: impl ToString) -> Self {
7        self.labels([(key, value)])
8    }
9
10    fn labels(self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
11
12    fn annotation(self, key: impl ToString, value: impl ToString) -> Self {
13        self.annotations([(key, value)])
14    }
15
16    fn annotations(
17        self,
18        annotations: impl IntoIterator<Item = (impl ToString, impl ToString)>,
19    ) -> Self;
20
21    /// Set recommended label 'app.kubernetes.io/name'
22    ///
23    fn app_name(self, name: impl ToString) -> Self {
24        self.labels([(label::APP_NAME, name)])
25    }
26
27    /// Set recommended label 'app.kubernetes.io/instance'
28    ///
29    fn app_instance(self, instance: impl ToString) -> Self {
30        self.labels([(label::APP_INSTANCE, instance)])
31    }
32
33    /// Set recommended label 'app.kubernetes.io/version'
34    ///
35    fn app_version(self, version: impl ToString) -> Self {
36        self.labels([(label::APP_VERSION, version)])
37    }
38
39    /// Set recommended label 'app.kubernetes.io/component'
40    ///
41    fn app_component(self, component: impl ToString) -> Self {
42        self.labels([(label::APP_COMPONENT, component)])
43    }
44
45    /// Set recommended label 'app.kubernetes.io/part-of'
46    ///
47    fn app_part_of(self, part_of: impl ToString) -> Self {
48        self.labels([(label::APP_PART_OF, part_of)])
49    }
50
51    /// Set recommended label 'app.kubernetes.io/managed-by'
52    ///
53    fn app_managed_by(self, managed_by: impl ToString) -> Self {
54        self.labels([(label::APP_MANAGED_BY, managed_by)])
55    }
56
57    fn pod_spec(self, spec: corev1::PodSpec) -> Self;
58}
59
60impl PodTemplateSpecExt for corev1::PodTemplateSpec {
61    fn new() -> Self {
62        Self {
63            metadata: None,
64            spec: None,
65        }
66    }
67
68    fn labels(mut self, labels: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
69        let labels = labels
70            .into_iter()
71            .map(|(key, value)| (key.to_string(), value.to_string()));
72        self.metadata
73            .get_or_insert_default()
74            .labels
75            .get_or_insert_default()
76            .extend(labels);
77        self
78    }
79
80    fn annotations(
81        mut self,
82        annotations: impl IntoIterator<Item = (impl ToString, impl ToString)>,
83    ) -> Self {
84        let annotations = annotations
85            .into_iter()
86            .map(|(key, value)| (key.to_string(), value.to_string()));
87        self.metadata
88            .get_or_insert_default()
89            .annotations
90            .get_or_insert_default()
91            .extend(annotations);
92        self
93    }
94
95    fn pod_spec(self, spec: corev1::PodSpec) -> Self {
96        Self {
97            spec: Some(spec),
98            ..self
99        }
100    }
101}
102
103impl HasSpec for corev1::PodTemplateSpec {
104    type Spec = corev1::PodSpec;
105
106    fn spec_mut(&mut self) -> &mut Self::Spec {
107        self.spec.get_or_insert_default()
108    }
109}