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
17impl HorizontalPodAutoscalerExt for autoscalingv2::HorizontalPodAutoscaler {
18    fn new<T: ScaleTargetRef>(name: impl ToString, scale_target_ref: &T) -> Self {
19        let metadata = metadata(name);
20        let scale_target_ref = scale_target_ref.scale_target_ref();
21        let spec = autoscalingv2::HorizontalPodAutoscalerSpec {
22            scale_target_ref,
23            // behavior: todo!(),
24            // max_replicas: todo!(),
25            // metrics: todo!(),
26            // min_replicas: todo!(),
27            ..default()
28        };
29
30        Self {
31            metadata,
32            spec: Some(spec),
33            // status: todo!(),
34            ..default()
35        }
36    }
37
38    fn with_max_replicas<T: ScaleTargetRef>(
39        name: impl ToString,
40        max_replicas: i32,
41        scale_target_ref: &T,
42    ) -> Self {
43        let metadata = metadata(name);
44        let scale_target_ref = scale_target_ref.scale_target_ref();
45        let spec = autoscalingv2::HorizontalPodAutoscalerSpec {
46            max_replicas,
47            scale_target_ref,
48            // behavior: todo!(),
49            // metrics: todo!(),
50            // min_replicas: todo!(),
51            ..default()
52        };
53
54        Self {
55            metadata,
56            spec: Some(spec),
57            // status: todo!(),
58            ..default()
59        }
60    }
61
62    fn max_replicas(mut self, max_replicas: i32) -> Self {
63        self.spec_mut().max_replicas = max_replicas;
64        self
65    }
66
67    fn min_replicas(mut self, min_replicas: i32) -> Self {
68        self.spec_mut().min_replicas.replace(min_replicas);
69        self
70    }
71}
72
73impl HasSpec for autoscalingv2::HorizontalPodAutoscaler {
74    type Spec = autoscalingv2::HorizontalPodAutoscalerSpec;
75
76    fn spec_mut(&mut self) -> &mut Self::Spec {
77        self.spec.get_or_insert_default()
78    }
79}
80
81pub trait ScaleTargetRef: openapi::Metadata<Ty = metav1::ObjectMeta> {
82    fn scale_target_ref(&self) -> autoscalingv2::CrossVersionObjectReference;
83}
84
85impl ScaleTargetRef for appsv1::Deployment {
86    fn scale_target_ref(&self) -> autoscalingv2::CrossVersionObjectReference {
87        let api_version = openapi::api_version(self).to_string();
88        let kind = openapi::kind(self).to_string();
89        let name = self.metadata().name.clone().unwrap_or_default();
90
91        autoscalingv2::CrossVersionObjectReference {
92            api_version: Some(api_version),
93            kind,
94            name,
95        }
96    }
97}