Skip to main content

k8s_openapi/v1_37/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.
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    pub tolerance: Option<crate::apimachinery::pkg::api::resource::Quantity>,
23}
24
25impl crate::DeepMerge for HPAScalingRules {
26    fn merge_from(&mut self, other: Self) {
27        crate::merge_strategies::list::atomic(&mut self.policies, other.policies);
28        crate::DeepMerge::merge_from(&mut self.select_policy, other.select_policy);
29        crate::DeepMerge::merge_from(&mut self.stabilization_window_seconds, other.stabilization_window_seconds);
30        crate::DeepMerge::merge_from(&mut self.tolerance, other.tolerance);
31    }
32}
33
34impl<'de> crate::serde::Deserialize<'de> for HPAScalingRules {
35    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
36        #[allow(non_camel_case_types)]
37        enum Field {
38            Key_policies,
39            Key_select_policy,
40            Key_stabilization_window_seconds,
41            Key_tolerance,
42            Other,
43        }
44
45        impl<'de> crate::serde::Deserialize<'de> for Field {
46            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
47                struct Visitor;
48
49                impl crate::serde::de::Visitor<'_> for Visitor {
50                    type Value = Field;
51
52                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
53                        f.write_str("field identifier")
54                    }
55
56                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
57                        Ok(match v {
58                            "policies" => Field::Key_policies,
59                            "selectPolicy" => Field::Key_select_policy,
60                            "stabilizationWindowSeconds" => Field::Key_stabilization_window_seconds,
61                            "tolerance" => Field::Key_tolerance,
62                            _ => Field::Other,
63                        })
64                    }
65                }
66
67                deserializer.deserialize_identifier(Visitor)
68            }
69        }
70
71        struct Visitor;
72
73        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
74            type Value = HPAScalingRules;
75
76            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77                f.write_str("HPAScalingRules")
78            }
79
80            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
81                let mut value_policies: Option<std::vec::Vec<crate::api::autoscaling::v2::HPAScalingPolicy>> = None;
82                let mut value_select_policy: Option<std::string::String> = None;
83                let mut value_stabilization_window_seconds: Option<i32> = None;
84                let mut value_tolerance: Option<crate::apimachinery::pkg::api::resource::Quantity> = None;
85
86                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
87                    match key {
88                        Field::Key_policies => value_policies = crate::serde::de::MapAccess::next_value(&mut map)?,
89                        Field::Key_select_policy => value_select_policy = crate::serde::de::MapAccess::next_value(&mut map)?,
90                        Field::Key_stabilization_window_seconds => value_stabilization_window_seconds = crate::serde::de::MapAccess::next_value(&mut map)?,
91                        Field::Key_tolerance => value_tolerance = crate::serde::de::MapAccess::next_value(&mut map)?,
92                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
93                    }
94                }
95
96                Ok(HPAScalingRules {
97                    policies: value_policies,
98                    select_policy: value_select_policy,
99                    stabilization_window_seconds: value_stabilization_window_seconds,
100                    tolerance: value_tolerance,
101                })
102            }
103        }
104
105        deserializer.deserialize_struct(
106            "HPAScalingRules",
107            &[
108                "policies",
109                "selectPolicy",
110                "stabilizationWindowSeconds",
111                "tolerance",
112            ],
113            Visitor,
114        )
115    }
116}
117
118impl crate::serde::Serialize for HPAScalingRules {
119    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
120        let mut state = serializer.serialize_struct(
121            "HPAScalingRules",
122            self.policies.as_ref().map_or(0, |_| 1) +
123            self.select_policy.as_ref().map_or(0, |_| 1) +
124            self.stabilization_window_seconds.as_ref().map_or(0, |_| 1) +
125            self.tolerance.as_ref().map_or(0, |_| 1),
126        )?;
127        if let Some(value) = &self.policies {
128            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "policies", value)?;
129        }
130        if let Some(value) = &self.select_policy {
131            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "selectPolicy", value)?;
132        }
133        if let Some(value) = &self.stabilization_window_seconds {
134            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "stabilizationWindowSeconds", value)?;
135        }
136        if let Some(value) = &self.tolerance {
137            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "tolerance", value)?;
138        }
139        crate::serde::ser::SerializeStruct::end(state)
140    }
141}
142
143#[cfg(feature = "schemars")]
144impl crate::schemars::JsonSchema for HPAScalingRules {
145    fn schema_name() -> std::borrow::Cow<'static, str> {
146        "io.k8s.api.autoscaling.v2.HPAScalingRules".into()
147    }
148
149    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
150        crate::schemars::json_schema!({
151            "description": "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.",
152            "type": "object",
153            "properties": {
154                "policies": {
155                    "description": "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.",
156                    "type": "array",
157                    "items": (__gen.subschema_for::<crate::api::autoscaling::v2::HPAScalingPolicy>()),
158                },
159                "selectPolicy": {
160                    "description": "selectPolicy is used to specify which policy should be used. If not set, the default value Max is used.",
161                    "type": "string",
162                },
163                "stabilizationWindowSeconds": {
164                    "description": "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).",
165                    "type": "integer",
166                    "format": "int32",
167                },
168                "tolerance": ({
169                    let mut schema_obj = __gen.subschema_for::<crate::apimachinery::pkg::api::resource::Quantity>();
170                    schema_obj.ensure_object().insert("description".into(), "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.".into());
171                    schema_obj
172                }),
173            },
174        })
175    }
176}