k8s_openapi_ext/ext/
statefulset.rs

1use super::*;
2
3pub trait StatefulSetExt {
4    /// Create a new StatefulSet with the given name.
5    fn new(name: impl ToString) -> Self;
6
7    /// Set the spec for the StatefulSet.
8    fn spec(self, spec: appsv1::StatefulSetSpec) -> Self;
9
10    /// Set the number of replicas for the StatefulSet.
11    fn replicas(self, replicas: i32) -> Self;
12
13    /// Set the selector for the StatefulSet.
14    fn selector(self, selector: metav1::LabelSelector) -> Self;
15
16    /// Set the matchLabels for the StatefulSet's selector.
17    fn match_labels(
18        self,
19        match_labels: impl IntoIterator<Item = (impl ToString, impl ToString)>,
20    ) -> Self;
21
22    /// Set the service name for the StatefulSet.
23    fn service_name(self, service_name: impl ToString) -> Self;
24
25    /// Set the pod template spec for the StatefulSet.
26    fn template(self, template: corev1::PodTemplateSpec) -> Self;
27
28    fn ordinals(self, ordinals: i32) -> Self;
29}
30
31impl StatefulSetExt for appsv1::StatefulSet {
32    fn new(name: impl ToString) -> Self {
33        let metadata = metadata(name);
34        Self {
35            metadata,
36            // spec: todo!(),
37            // status: todo!(),
38            ..default()
39        }
40    }
41
42    fn spec(self, spec: appsv1::StatefulSetSpec) -> Self {
43        Self {
44            spec: Some(spec),
45            ..self
46        }
47    }
48
49    fn replicas(mut self, replicas: i32) -> Self {
50        self.spec_mut().replicas.replace(replicas);
51        self
52    }
53
54    fn selector(mut self, selector: metav1::LabelSelector) -> Self {
55        self.spec_mut().selector = selector;
56        self
57    }
58
59    fn match_labels(
60        mut self,
61        match_labels: impl IntoIterator<Item = (impl ToString, impl ToString)>,
62    ) -> Self {
63        self.spec_mut().selector = metav1::LabelSelector::match_labels(match_labels);
64        self
65    }
66
67    fn service_name(mut self, service_name: impl ToString) -> Self {
68        self.spec_mut()
69            .service_name
70            .replace(service_name.to_string());
71        self
72    }
73
74    fn template(mut self, template: corev1::PodTemplateSpec) -> Self {
75        self.spec_mut().template = template;
76        self
77    }
78
79    fn ordinals(mut self, start: i32) -> Self {
80        self.spec_mut().ordinals = Some(appsv1::StatefulSetOrdinals { start: Some(start) });
81        self
82    }
83}
84
85impl HasSpec for appsv1::StatefulSet {
86    type Spec = appsv1::StatefulSetSpec;
87
88    fn spec_mut(&mut self) -> &mut Self::Spec {
89        self.spec.get_or_insert_default()
90    }
91}