k8s_openapi_ext/ext/
hpa.rs

1use super::*;
2
3pub trait HorizontalPodAutoscalerExt: super::ResourceBuilder + Sized {
4    fn new<T: ScaleTargetRef>(name: impl ToString, scale_target_ref: &T) -> Self;
5
6    fn with_max_replicas<T: ScaleTargetRef>(
7        name: impl ToString,
8        max: i32,
9        scale_target_ref: &T,
10    ) -> Self;
11
12    fn max_replicas(self, max_replicas: i32) -> Self;
13
14    fn min_replicas(self, min_replicas: i32) -> Self;
15
16    fn metrics(self, metrics: impl IntoIterator<Item = autoscalingv2::MetricSpec>) -> Self;
17
18    fn metric(self, metric: autoscalingv2::MetricSpec) -> Self {
19        self.metrics([metric])
20    }
21
22    fn behavior(self, behavior: autoscalingv2::HorizontalPodAutoscalerBehavior) -> Self;
23
24    fn scale_up(self, rules: autoscalingv2::HPAScalingRules) -> Self;
25
26    fn scale_down(self, rules: autoscalingv2::HPAScalingRules) -> Self;
27}
28
29impl HorizontalPodAutoscalerExt for autoscalingv2::HorizontalPodAutoscaler {
30    fn new<T: ScaleTargetRef>(name: impl ToString, scale_target_ref: &T) -> Self {
31        let metadata = metadata(name);
32        let scale_target_ref = scale_target_ref.scale_target_ref();
33        let spec = autoscalingv2::HorizontalPodAutoscalerSpec {
34            scale_target_ref,
35            // behavior: todo!(),
36            // max_replicas: todo!(),
37            // metrics: todo!(),
38            // min_replicas: todo!(),
39            ..default()
40        };
41
42        Self {
43            metadata,
44            spec: Some(spec),
45            // status: todo!(),
46            ..default()
47        }
48    }
49
50    fn with_max_replicas<T: ScaleTargetRef>(
51        name: impl ToString,
52        max_replicas: i32,
53        scale_target_ref: &T,
54    ) -> Self {
55        let metadata = metadata(name);
56        let scale_target_ref = scale_target_ref.scale_target_ref();
57        let spec = autoscalingv2::HorizontalPodAutoscalerSpec {
58            max_replicas,
59            scale_target_ref,
60            // behavior: todo!(),
61            // metrics: todo!(),
62            // min_replicas: todo!(),
63            ..default()
64        };
65
66        Self {
67            metadata,
68            spec: Some(spec),
69            // status: todo!(),
70            ..default()
71        }
72    }
73
74    fn max_replicas(mut self, max_replicas: i32) -> Self {
75        self.spec_mut().max_replicas = max_replicas;
76        self
77    }
78
79    fn min_replicas(mut self, min_replicas: i32) -> Self {
80        self.spec_mut().min_replicas.replace(min_replicas);
81        self
82    }
83
84    fn metrics(mut self, metrics: impl IntoIterator<Item = autoscalingv2::MetricSpec>) -> Self {
85        self.spec_mut()
86            .metrics
87            .get_or_insert_default()
88            .extend(metrics);
89        self
90    }
91
92    fn behavior(mut self, behavior: autoscalingv2::HorizontalPodAutoscalerBehavior) -> Self {
93        self.spec_mut().behavior.replace(behavior);
94        self
95    }
96
97    fn scale_up(mut self, rules: autoscalingv2::HPAScalingRules) -> Self {
98        self.spec_mut()
99            .behavior
100            .get_or_insert_default()
101            .scale_up
102            .replace(rules);
103        self
104    }
105
106    fn scale_down(mut self, rules: autoscalingv2::HPAScalingRules) -> Self {
107        self.spec_mut()
108            .behavior
109            .get_or_insert_default()
110            .scale_down
111            .replace(rules);
112        self
113    }
114}
115
116impl HasSpec for autoscalingv2::HorizontalPodAutoscaler {
117    type Spec = autoscalingv2::HorizontalPodAutoscalerSpec;
118
119    fn spec_mut(&mut self) -> &mut Self::Spec {
120        self.spec.get_or_insert_default()
121    }
122}
123
124pub trait ScaleTargetRef: openapi::Metadata<Ty = metav1::ObjectMeta> {
125    fn scale_target_ref(&self) -> autoscalingv2::CrossVersionObjectReference;
126}
127
128impl ScaleTargetRef for appsv1::Deployment {
129    fn scale_target_ref(&self) -> autoscalingv2::CrossVersionObjectReference {
130        let api_version = openapi::api_version(self).to_string();
131        let kind = openapi::kind(self).to_string();
132        let name = self.metadata().name.clone().unwrap_or_default();
133
134        autoscalingv2::CrossVersionObjectReference {
135            api_version: Some(api_version),
136            kind,
137            name,
138        }
139    }
140}