k8s_openapi/v1_33/api/autoscaling/v2/
hpa_scaling_rules.rs

1// Generated from definition io.k8s.api.autoscaling.v2.HPAScalingRules
2
3/// HPAScalingRules configures the scaling behavior for one direction via scaling Policy Rules and a configurable metric tolerance.
4///
5/// Scaling Policy Rules are applied after calculating DesiredReplicas from metrics for the HPA. They can limit the scaling velocity by specifying scaling policies. They can prevent flapping by specifying the stabilization window, so that the number of replicas is not set instantly, instead, the safest value from the stabilization window is chosen.
6///
7/// The tolerance is applied to the metric values and prevents scaling too eagerly for small metric variations. (Note that setting a tolerance requires enabling the alpha HPAConfigurableTolerance feature gate.)
8#[derive(Clone, Debug, Default, PartialEq)]
9pub struct HPAScalingRules {
10    /// policies is a list of potential scaling polices which can be used during scaling. If not set, use the default values: - For scale up: allow doubling the number of pods, or an absolute change of 4 pods in a 15s window. - For scale down: allow all pods to be removed in a 15s window.
11    pub policies: Option<std::vec::Vec<crate::api::autoscaling::v2::HPAScalingPolicy>>,
12
13    /// selectPolicy is used to specify which policy should be used. If not set, the default value Max is used.
14    pub select_policy: Option<std::string::String>,
15
16    /// stabilizationWindowSeconds is the number of seconds for which past recommendations should be considered while scaling up or scaling down. StabilizationWindowSeconds must be greater than or equal to zero and less than or equal to 3600 (one hour). If not set, use the default values: - For scale up: 0 (i.e. no stabilization is done). - For scale down: 300 (i.e. the stabilization window is 300 seconds long).
17    pub stabilization_window_seconds: Option<i32>,
18
19    /// tolerance is the tolerance on the ratio between the current and desired metric value under which no updates are made to the desired number of replicas (e.g. 0.01 for 1%). Must be greater than or equal to zero. If not set, the default cluster-wide tolerance is applied (by default 10%).
20    ///
21    /// For example, if autoscaling is configured with a memory consumption target of 100Mi, and scale-down and scale-up tolerances of 5% and 1% respectively, scaling will be triggered when the actual consumption falls below 95Mi or exceeds 101Mi.
22    ///
23    /// This is an alpha field and requires enabling the HPAConfigurableTolerance feature gate.
24    pub tolerance: Option<crate::apimachinery::pkg::api::resource::Quantity>,
25}
26
27impl crate::DeepMerge for HPAScalingRules {
28    fn merge_from(&mut self, other: Self) {
29        crate::merge_strategies::list::atomic(&mut self.policies, other.policies);
30        crate::DeepMerge::merge_from(&mut self.select_policy, other.select_policy);
31        crate::DeepMerge::merge_from(&mut self.stabilization_window_seconds, other.stabilization_window_seconds);
32        crate::DeepMerge::merge_from(&mut self.tolerance, other.tolerance);
33    }
34}
35
36impl<'de> crate::serde::Deserialize<'de> for HPAScalingRules {
37    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
38        #[allow(non_camel_case_types)]
39        enum Field {
40            Key_policies,
41            Key_select_policy,
42            Key_stabilization_window_seconds,
43            Key_tolerance,
44            Other,
45        }
46
47        impl<'de> crate::serde::Deserialize<'de> for Field {
48            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
49                struct Visitor;
50
51                impl crate::serde::de::Visitor<'_> for Visitor {
52                    type Value = Field;
53
54                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
55                        f.write_str("field identifier")
56                    }
57
58                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
59                        Ok(match v {
60                            "policies" => Field::Key_policies,
61                            "selectPolicy" => Field::Key_select_policy,
62                            "stabilizationWindowSeconds" => Field::Key_stabilization_window_seconds,
63                            "tolerance" => Field::Key_tolerance,
64                            _ => Field::Other,
65                        })
66                    }
67                }
68
69                deserializer.deserialize_identifier(Visitor)
70            }
71        }
72
73        struct Visitor;
74
75        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
76            type Value = HPAScalingRules;
77
78            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
79                f.write_str("HPAScalingRules")
80            }
81
82            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
83                let mut value_policies: Option<std::vec::Vec<crate::api::autoscaling::v2::HPAScalingPolicy>> = None;
84                let mut value_select_policy: Option<std::string::String> = None;
85                let mut value_stabilization_window_seconds: Option<i32> = None;
86                let mut value_tolerance: Option<crate::apimachinery::pkg::api::resource::Quantity> = None;
87
88                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
89                    match key {
90                        Field::Key_policies => value_policies = crate::serde::de::MapAccess::next_value(&mut map)?,
91                        Field::Key_select_policy => value_select_policy = crate::serde::de::MapAccess::next_value(&mut map)?,
92                        Field::Key_stabilization_window_seconds => value_stabilization_window_seconds = crate::serde::de::MapAccess::next_value(&mut map)?,
93                        Field::Key_tolerance => value_tolerance = crate::serde::de::MapAccess::next_value(&mut map)?,
94                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
95                    }
96                }
97
98                Ok(HPAScalingRules {
99                    policies: value_policies,
100                    select_policy: value_select_policy,
101                    stabilization_window_seconds: value_stabilization_window_seconds,
102                    tolerance: value_tolerance,
103                })
104            }
105        }
106
107        deserializer.deserialize_struct(
108            "HPAScalingRules",
109            &[
110                "policies",
111                "selectPolicy",
112                "stabilizationWindowSeconds",
113                "tolerance",
114            ],
115            Visitor,
116        )
117    }
118}
119
120impl crate::serde::Serialize for HPAScalingRules {
121    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
122        let mut state = serializer.serialize_struct(
123            "HPAScalingRules",
124            self.policies.as_ref().map_or(0, |_| 1) +
125            self.select_policy.as_ref().map_or(0, |_| 1) +
126            self.stabilization_window_seconds.as_ref().map_or(0, |_| 1) +
127            self.tolerance.as_ref().map_or(0, |_| 1),
128        )?;
129        if let Some(value) = &self.policies {
130            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "policies", value)?;
131        }
132        if let Some(value) = &self.select_policy {
133            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "selectPolicy", value)?;
134        }
135        if let Some(value) = &self.stabilization_window_seconds {
136            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "stabilizationWindowSeconds", value)?;
137        }
138        if let Some(value) = &self.tolerance {
139            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "tolerance", value)?;
140        }
141        crate::serde::ser::SerializeStruct::end(state)
142    }
143}
144
145#[cfg(feature = "schemars")]
146impl crate::schemars::JsonSchema for HPAScalingRules {
147    fn schema_name() -> std::string::String {
148        "io.k8s.api.autoscaling.v2.HPAScalingRules".into()
149    }
150
151    fn json_schema(__gen: &mut crate::schemars::gen::SchemaGenerator) -> crate::schemars::schema::Schema {
152        crate::schemars::schema::Schema::Object(crate::schemars::schema::SchemaObject {
153            metadata: Some(std::boxed::Box::new(crate::schemars::schema::Metadata {
154                description: Some("HPAScalingRules configures the scaling behavior for one direction via scaling Policy Rules and a configurable metric tolerance.\n\nScaling Policy Rules are applied after calculating DesiredReplicas from metrics for the HPA. They can limit the scaling velocity by specifying scaling policies. They can prevent flapping by specifying the stabilization window, so that the number of replicas is not set instantly, instead, the safest value from the stabilization window is chosen.\n\nThe tolerance is applied to the metric values and prevents scaling too eagerly for small metric variations. (Note that setting a tolerance requires enabling the alpha HPAConfigurableTolerance feature gate.)".into()),
155                ..Default::default()
156            })),
157            instance_type: Some(crate::schemars::schema::SingleOrVec::Single(std::boxed::Box::new(crate::schemars::schema::InstanceType::Object))),
158            object: Some(std::boxed::Box::new(crate::schemars::schema::ObjectValidation {
159                properties: [
160                    (
161                        "policies".into(),
162                        crate::schemars::schema::Schema::Object(crate::schemars::schema::SchemaObject {
163                            metadata: Some(std::boxed::Box::new(crate::schemars::schema::Metadata {
164                                description: Some("policies is a list of potential scaling polices which can be used during scaling. If not set, use the default values: - For scale up: allow doubling the number of pods, or an absolute change of 4 pods in a 15s window. - For scale down: allow all pods to be removed in a 15s window.".into()),
165                                ..Default::default()
166                            })),
167                            instance_type: Some(crate::schemars::schema::SingleOrVec::Single(std::boxed::Box::new(crate::schemars::schema::InstanceType::Array))),
168                            array: Some(std::boxed::Box::new(crate::schemars::schema::ArrayValidation {
169                                items: Some(crate::schemars::schema::SingleOrVec::Single(std::boxed::Box::new(__gen.subschema_for::<crate::api::autoscaling::v2::HPAScalingPolicy>()))),
170                                ..Default::default()
171                            })),
172                            ..Default::default()
173                        }),
174                    ),
175                    (
176                        "selectPolicy".into(),
177                        crate::schemars::schema::Schema::Object(crate::schemars::schema::SchemaObject {
178                            metadata: Some(std::boxed::Box::new(crate::schemars::schema::Metadata {
179                                description: Some("selectPolicy is used to specify which policy should be used. If not set, the default value Max is used.".into()),
180                                ..Default::default()
181                            })),
182                            instance_type: Some(crate::schemars::schema::SingleOrVec::Single(std::boxed::Box::new(crate::schemars::schema::InstanceType::String))),
183                            ..Default::default()
184                        }),
185                    ),
186                    (
187                        "stabilizationWindowSeconds".into(),
188                        crate::schemars::schema::Schema::Object(crate::schemars::schema::SchemaObject {
189                            metadata: Some(std::boxed::Box::new(crate::schemars::schema::Metadata {
190                                description: Some("stabilizationWindowSeconds is the number of seconds for which past recommendations should be considered while scaling up or scaling down. StabilizationWindowSeconds must be greater than or equal to zero and less than or equal to 3600 (one hour). If not set, use the default values: - For scale up: 0 (i.e. no stabilization is done). - For scale down: 300 (i.e. the stabilization window is 300 seconds long).".into()),
191                                ..Default::default()
192                            })),
193                            instance_type: Some(crate::schemars::schema::SingleOrVec::Single(std::boxed::Box::new(crate::schemars::schema::InstanceType::Integer))),
194                            format: Some("int32".into()),
195                            ..Default::default()
196                        }),
197                    ),
198                    (
199                        "tolerance".into(),
200                        {
201                            let mut schema_obj = __gen.subschema_for::<crate::apimachinery::pkg::api::resource::Quantity>().into_object();
202                            schema_obj.metadata = Some(std::boxed::Box::new(crate::schemars::schema::Metadata {
203                                description: Some("tolerance is the tolerance on the ratio between the current and desired metric value under which no updates are made to the desired number of replicas (e.g. 0.01 for 1%). Must be greater than or equal to zero. If not set, the default cluster-wide tolerance is applied (by default 10%).\n\nFor example, if autoscaling is configured with a memory consumption target of 100Mi, and scale-down and scale-up tolerances of 5% and 1% respectively, scaling will be triggered when the actual consumption falls below 95Mi or exceeds 101Mi.\n\nThis is an alpha field and requires enabling the HPAConfigurableTolerance feature gate.".into()),
204                                ..Default::default()
205                            }));
206                            crate::schemars::schema::Schema::Object(schema_obj)
207                        },
208                    ),
209                ].into(),
210                ..Default::default()
211            })),
212            ..Default::default()
213        })
214    }
215}