Skip to main content

k8s_openapi/v1_36/api/autoscaling/v2/
metric_spec.rs

1// Generated from definition io.k8s.api.autoscaling.v2.MetricSpec
2
3/// MetricSpec specifies how to scale based on a single metric (only `type` and one other matching field should be set at once).
4#[derive(Clone, Debug, Default, PartialEq)]
5pub struct MetricSpec {
6    /// containerResource refers to a resource metric (such as those specified in requests and limits) known to Kubernetes describing a single container in each pod of the current scale target (e.g. CPU or memory). Such metrics are built in to Kubernetes, and have special scaling options on top of those available to normal per-pod metrics using the "pods" source.
7    pub container_resource: Option<crate::api::autoscaling::v2::ContainerResourceMetricSource>,
8
9    /// external refers to a global metric that is not associated with any Kubernetes object. It allows autoscaling based on information coming from components running outside of cluster (for example length of queue in cloud messaging service, or QPS from loadbalancer running outside of cluster).
10    pub external: Option<crate::api::autoscaling::v2::ExternalMetricSource>,
11
12    /// object refers to a metric describing a single kubernetes object (for example, hits-per-second on an Ingress object).
13    pub object: Option<crate::api::autoscaling::v2::ObjectMetricSource>,
14
15    /// pods refers to a metric describing each pod in the current scale target (for example, transactions-processed-per-second).  The values will be averaged together before being compared to the target value.
16    pub pods: Option<crate::api::autoscaling::v2::PodsMetricSource>,
17
18    /// resource refers to a resource metric (such as those specified in requests and limits) known to Kubernetes describing each pod in the current scale target (e.g. CPU or memory). Such metrics are built in to Kubernetes, and have special scaling options on top of those available to normal per-pod metrics using the "pods" source.
19    pub resource: Option<crate::api::autoscaling::v2::ResourceMetricSource>,
20
21    /// type is the type of metric source.  It should be one of "ContainerResource", "External", "Object", "Pods" or "Resource", each mapping to a matching field in the object.
22    pub type_: std::string::String,
23}
24
25impl crate::DeepMerge for MetricSpec {
26    fn merge_from(&mut self, other: Self) {
27        crate::DeepMerge::merge_from(&mut self.container_resource, other.container_resource);
28        crate::DeepMerge::merge_from(&mut self.external, other.external);
29        crate::DeepMerge::merge_from(&mut self.object, other.object);
30        crate::DeepMerge::merge_from(&mut self.pods, other.pods);
31        crate::DeepMerge::merge_from(&mut self.resource, other.resource);
32        crate::DeepMerge::merge_from(&mut self.type_, other.type_);
33    }
34}
35
36impl<'de> crate::serde::Deserialize<'de> for MetricSpec {
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_container_resource,
41            Key_external,
42            Key_object,
43            Key_pods,
44            Key_resource,
45            Key_type_,
46            Other,
47        }
48
49        impl<'de> crate::serde::Deserialize<'de> for Field {
50            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
51                struct Visitor;
52
53                impl crate::serde::de::Visitor<'_> for Visitor {
54                    type Value = Field;
55
56                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57                        f.write_str("field identifier")
58                    }
59
60                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
61                        Ok(match v {
62                            "containerResource" => Field::Key_container_resource,
63                            "external" => Field::Key_external,
64                            "object" => Field::Key_object,
65                            "pods" => Field::Key_pods,
66                            "resource" => Field::Key_resource,
67                            "type" => Field::Key_type_,
68                            _ => Field::Other,
69                        })
70                    }
71                }
72
73                deserializer.deserialize_identifier(Visitor)
74            }
75        }
76
77        struct Visitor;
78
79        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
80            type Value = MetricSpec;
81
82            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
83                f.write_str("MetricSpec")
84            }
85
86            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
87                let mut value_container_resource: Option<crate::api::autoscaling::v2::ContainerResourceMetricSource> = None;
88                let mut value_external: Option<crate::api::autoscaling::v2::ExternalMetricSource> = None;
89                let mut value_object: Option<crate::api::autoscaling::v2::ObjectMetricSource> = None;
90                let mut value_pods: Option<crate::api::autoscaling::v2::PodsMetricSource> = None;
91                let mut value_resource: Option<crate::api::autoscaling::v2::ResourceMetricSource> = None;
92                let mut value_type_: Option<std::string::String> = None;
93
94                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
95                    match key {
96                        Field::Key_container_resource => value_container_resource = crate::serde::de::MapAccess::next_value(&mut map)?,
97                        Field::Key_external => value_external = crate::serde::de::MapAccess::next_value(&mut map)?,
98                        Field::Key_object => value_object = crate::serde::de::MapAccess::next_value(&mut map)?,
99                        Field::Key_pods => value_pods = crate::serde::de::MapAccess::next_value(&mut map)?,
100                        Field::Key_resource => value_resource = crate::serde::de::MapAccess::next_value(&mut map)?,
101                        Field::Key_type_ => value_type_ = crate::serde::de::MapAccess::next_value(&mut map)?,
102                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
103                    }
104                }
105
106                Ok(MetricSpec {
107                    container_resource: value_container_resource,
108                    external: value_external,
109                    object: value_object,
110                    pods: value_pods,
111                    resource: value_resource,
112                    type_: value_type_.unwrap_or_default(),
113                })
114            }
115        }
116
117        deserializer.deserialize_struct(
118            "MetricSpec",
119            &[
120                "containerResource",
121                "external",
122                "object",
123                "pods",
124                "resource",
125                "type",
126            ],
127            Visitor,
128        )
129    }
130}
131
132impl crate::serde::Serialize for MetricSpec {
133    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
134        let mut state = serializer.serialize_struct(
135            "MetricSpec",
136            1 +
137            self.container_resource.as_ref().map_or(0, |_| 1) +
138            self.external.as_ref().map_or(0, |_| 1) +
139            self.object.as_ref().map_or(0, |_| 1) +
140            self.pods.as_ref().map_or(0, |_| 1) +
141            self.resource.as_ref().map_or(0, |_| 1),
142        )?;
143        if let Some(value) = &self.container_resource {
144            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "containerResource", value)?;
145        }
146        if let Some(value) = &self.external {
147            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "external", value)?;
148        }
149        if let Some(value) = &self.object {
150            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", value)?;
151        }
152        if let Some(value) = &self.pods {
153            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "pods", value)?;
154        }
155        if let Some(value) = &self.resource {
156            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "resource", value)?;
157        }
158        crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", &self.type_)?;
159        crate::serde::ser::SerializeStruct::end(state)
160    }
161}
162
163#[cfg(feature = "schemars")]
164impl crate::schemars::JsonSchema for MetricSpec {
165    fn schema_name() -> std::borrow::Cow<'static, str> {
166        "io.k8s.api.autoscaling.v2.MetricSpec".into()
167    }
168
169    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
170        crate::schemars::json_schema!({
171            "description": "MetricSpec specifies how to scale based on a single metric (only `type` and one other matching field should be set at once).",
172            "type": "object",
173            "properties": {
174                "containerResource": ({
175                    let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ContainerResourceMetricSource>();
176                    schema_obj.ensure_object().insert("description".into(), "containerResource refers to a resource metric (such as those specified in requests and limits) known to Kubernetes describing a single container in each pod of the current scale target (e.g. CPU or memory). Such metrics are built in to Kubernetes, and have special scaling options on top of those available to normal per-pod metrics using the \"pods\" source.".into());
177                    schema_obj
178                }),
179                "external": ({
180                    let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ExternalMetricSource>();
181                    schema_obj.ensure_object().insert("description".into(), "external refers to a global metric that is not associated with any Kubernetes object. It allows autoscaling based on information coming from components running outside of cluster (for example length of queue in cloud messaging service, or QPS from loadbalancer running outside of cluster).".into());
182                    schema_obj
183                }),
184                "object": ({
185                    let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ObjectMetricSource>();
186                    schema_obj.ensure_object().insert("description".into(), "object refers to a metric describing a single kubernetes object (for example, hits-per-second on an Ingress object).".into());
187                    schema_obj
188                }),
189                "pods": ({
190                    let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::PodsMetricSource>();
191                    schema_obj.ensure_object().insert("description".into(), "pods refers to a metric describing each pod in the current scale target (for example, transactions-processed-per-second).  The values will be averaged together before being compared to the target value.".into());
192                    schema_obj
193                }),
194                "resource": ({
195                    let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ResourceMetricSource>();
196                    schema_obj.ensure_object().insert("description".into(), "resource refers to a resource metric (such as those specified in requests and limits) known to Kubernetes describing each pod in the current scale target (e.g. CPU or memory). Such metrics are built in to Kubernetes, and have special scaling options on top of those available to normal per-pod metrics using the \"pods\" source.".into());
197                    schema_obj
198                }),
199                "type": {
200                    "description": "type is the type of metric source.  It should be one of \"ContainerResource\", \"External\", \"Object\", \"Pods\" or \"Resource\", each mapping to a matching field in the object.",
201                    "type": "string",
202                },
203            },
204            "required": [
205                "type",
206            ],
207        })
208    }
209}
210
211#[cfg(feature = "schemars08")]
212impl crate::schemars08::JsonSchema for MetricSpec {
213    fn schema_name() -> std::string::String {
214        "io.k8s.api.autoscaling.v2.MetricSpec".into()
215    }
216
217    fn json_schema(__gen: &mut crate::schemars08::gen::SchemaGenerator) -> crate::schemars08::schema::Schema {
218        crate::schemars08::schema::Schema::Object(crate::schemars08::schema::SchemaObject {
219            metadata: Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
220                description: Some("MetricSpec specifies how to scale based on a single metric (only `type` and one other matching field should be set at once).".into()),
221                ..Default::default()
222            })),
223            instance_type: Some(crate::schemars08::schema::SingleOrVec::Single(std::boxed::Box::new(crate::schemars08::schema::InstanceType::Object))),
224            object: Some(std::boxed::Box::new(crate::schemars08::schema::ObjectValidation {
225                properties: [
226                    (
227                        "containerResource".into(),
228                        {
229                            let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ContainerResourceMetricSource>().into_object();
230                            schema_obj.metadata = Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
231                                description: Some("containerResource refers to a resource metric (such as those specified in requests and limits) known to Kubernetes describing a single container in each pod of the current scale target (e.g. CPU or memory). Such metrics are built in to Kubernetes, and have special scaling options on top of those available to normal per-pod metrics using the \"pods\" source.".into()),
232                                ..Default::default()
233                            }));
234                            crate::schemars08::schema::Schema::Object(schema_obj)
235                        },
236                    ),
237                    (
238                        "external".into(),
239                        {
240                            let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ExternalMetricSource>().into_object();
241                            schema_obj.metadata = Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
242                                description: Some("external refers to a global metric that is not associated with any Kubernetes object. It allows autoscaling based on information coming from components running outside of cluster (for example length of queue in cloud messaging service, or QPS from loadbalancer running outside of cluster).".into()),
243                                ..Default::default()
244                            }));
245                            crate::schemars08::schema::Schema::Object(schema_obj)
246                        },
247                    ),
248                    (
249                        "object".into(),
250                        {
251                            let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ObjectMetricSource>().into_object();
252                            schema_obj.metadata = Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
253                                description: Some("object refers to a metric describing a single kubernetes object (for example, hits-per-second on an Ingress object).".into()),
254                                ..Default::default()
255                            }));
256                            crate::schemars08::schema::Schema::Object(schema_obj)
257                        },
258                    ),
259                    (
260                        "pods".into(),
261                        {
262                            let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::PodsMetricSource>().into_object();
263                            schema_obj.metadata = Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
264                                description: Some("pods refers to a metric describing each pod in the current scale target (for example, transactions-processed-per-second).  The values will be averaged together before being compared to the target value.".into()),
265                                ..Default::default()
266                            }));
267                            crate::schemars08::schema::Schema::Object(schema_obj)
268                        },
269                    ),
270                    (
271                        "resource".into(),
272                        {
273                            let mut schema_obj = __gen.subschema_for::<crate::api::autoscaling::v2::ResourceMetricSource>().into_object();
274                            schema_obj.metadata = Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
275                                description: Some("resource refers to a resource metric (such as those specified in requests and limits) known to Kubernetes describing each pod in the current scale target (e.g. CPU or memory). Such metrics are built in to Kubernetes, and have special scaling options on top of those available to normal per-pod metrics using the \"pods\" source.".into()),
276                                ..Default::default()
277                            }));
278                            crate::schemars08::schema::Schema::Object(schema_obj)
279                        },
280                    ),
281                    (
282                        "type".into(),
283                        crate::schemars08::schema::Schema::Object(crate::schemars08::schema::SchemaObject {
284                            metadata: Some(std::boxed::Box::new(crate::schemars08::schema::Metadata {
285                                description: Some("type is the type of metric source.  It should be one of \"ContainerResource\", \"External\", \"Object\", \"Pods\" or \"Resource\", each mapping to a matching field in the object.".into()),
286                                ..Default::default()
287                            })),
288                            instance_type: Some(crate::schemars08::schema::SingleOrVec::Single(std::boxed::Box::new(crate::schemars08::schema::InstanceType::String))),
289                            ..Default::default()
290                        }),
291                    ),
292                ].into(),
293                required: [
294                    "type".into(),
295                ].into(),
296                ..Default::default()
297            })),
298            ..Default::default()
299        })
300    }
301}