k8s_openapi/v1_32/api/core/v1/
lifecycle.rs

1// Generated from definition io.k8s.api.core.v1.Lifecycle
2
3/// Lifecycle describes actions that the management system should take in response to container lifecycle events. For the PostStart and PreStop lifecycle handlers, management of the container blocks until the action is complete, unless the container process fails, in which case the handler is aborted.
4#[derive(Clone, Debug, Default, PartialEq)]
5pub struct Lifecycle {
6    /// PostStart is called immediately after a container is created. If the handler fails, the container is terminated and restarted according to its restart policy. Other management of the container blocks until the hook completes. More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
7    pub post_start: Option<crate::api::core::v1::LifecycleHandler>,
8
9    /// PreStop is called immediately before a container is terminated due to an API request or management event such as liveness/startup probe failure, preemption, resource contention, etc. The handler is not called if the container crashes or exits. The Pod's termination grace period countdown begins before the PreStop hook is executed. Regardless of the outcome of the handler, the container will eventually terminate within the Pod's termination grace period (unless delayed by finalizers). Other management of the container blocks until the hook completes or until the termination grace period is reached. More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
10    pub pre_stop: Option<crate::api::core::v1::LifecycleHandler>,
11}
12
13impl crate::DeepMerge for Lifecycle {
14    fn merge_from(&mut self, other: Self) {
15        crate::DeepMerge::merge_from(&mut self.post_start, other.post_start);
16        crate::DeepMerge::merge_from(&mut self.pre_stop, other.pre_stop);
17    }
18}
19
20impl<'de> crate::serde::Deserialize<'de> for Lifecycle {
21    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
22        #[allow(non_camel_case_types)]
23        enum Field {
24            Key_post_start,
25            Key_pre_stop,
26            Other,
27        }
28
29        impl<'de> crate::serde::Deserialize<'de> for Field {
30            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
31                struct Visitor;
32
33                impl crate::serde::de::Visitor<'_> for Visitor {
34                    type Value = Field;
35
36                    fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37                        f.write_str("field identifier")
38                    }
39
40                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
41                        Ok(match v {
42                            "postStart" => Field::Key_post_start,
43                            "preStop" => Field::Key_pre_stop,
44                            _ => Field::Other,
45                        })
46                    }
47                }
48
49                deserializer.deserialize_identifier(Visitor)
50            }
51        }
52
53        struct Visitor;
54
55        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
56            type Value = Lifecycle;
57
58            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59                f.write_str("Lifecycle")
60            }
61
62            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
63                let mut value_post_start: Option<crate::api::core::v1::LifecycleHandler> = None;
64                let mut value_pre_stop: Option<crate::api::core::v1::LifecycleHandler> = None;
65
66                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
67                    match key {
68                        Field::Key_post_start => value_post_start = crate::serde::de::MapAccess::next_value(&mut map)?,
69                        Field::Key_pre_stop => value_pre_stop = crate::serde::de::MapAccess::next_value(&mut map)?,
70                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
71                    }
72                }
73
74                Ok(Lifecycle {
75                    post_start: value_post_start,
76                    pre_stop: value_pre_stop,
77                })
78            }
79        }
80
81        deserializer.deserialize_struct(
82            "Lifecycle",
83            &[
84                "postStart",
85                "preStop",
86            ],
87            Visitor,
88        )
89    }
90}
91
92impl crate::serde::Serialize for Lifecycle {
93    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
94        let mut state = serializer.serialize_struct(
95            "Lifecycle",
96            self.post_start.as_ref().map_or(0, |_| 1) +
97            self.pre_stop.as_ref().map_or(0, |_| 1),
98        )?;
99        if let Some(value) = &self.post_start {
100            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "postStart", value)?;
101        }
102        if let Some(value) = &self.pre_stop {
103            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "preStop", value)?;
104        }
105        crate::serde::ser::SerializeStruct::end(state)
106    }
107}
108
109#[cfg(feature = "schemars")]
110impl crate::schemars::JsonSchema for Lifecycle {
111    fn schema_name() -> String {
112        "io.k8s.api.core.v1.Lifecycle".to_owned()
113    }
114
115    fn json_schema(__gen: &mut crate::schemars::gen::SchemaGenerator) -> crate::schemars::schema::Schema {
116        crate::schemars::schema::Schema::Object(crate::schemars::schema::SchemaObject {
117            metadata: Some(Box::new(crate::schemars::schema::Metadata {
118                description: Some("Lifecycle describes actions that the management system should take in response to container lifecycle events. For the PostStart and PreStop lifecycle handlers, management of the container blocks until the action is complete, unless the container process fails, in which case the handler is aborted.".to_owned()),
119                ..Default::default()
120            })),
121            instance_type: Some(crate::schemars::schema::SingleOrVec::Single(Box::new(crate::schemars::schema::InstanceType::Object))),
122            object: Some(Box::new(crate::schemars::schema::ObjectValidation {
123                properties: [
124                    (
125                        "postStart".to_owned(),
126                        {
127                            let mut schema_obj = __gen.subschema_for::<crate::api::core::v1::LifecycleHandler>().into_object();
128                            schema_obj.metadata = Some(Box::new(crate::schemars::schema::Metadata {
129                                description: Some("PostStart is called immediately after a container is created. If the handler fails, the container is terminated and restarted according to its restart policy. Other management of the container blocks until the hook completes. More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks".to_owned()),
130                                ..Default::default()
131                            }));
132                            crate::schemars::schema::Schema::Object(schema_obj)
133                        },
134                    ),
135                    (
136                        "preStop".to_owned(),
137                        {
138                            let mut schema_obj = __gen.subschema_for::<crate::api::core::v1::LifecycleHandler>().into_object();
139                            schema_obj.metadata = Some(Box::new(crate::schemars::schema::Metadata {
140                                description: Some("PreStop is called immediately before a container is terminated due to an API request or management event such as liveness/startup probe failure, preemption, resource contention, etc. The handler is not called if the container crashes or exits. The Pod's termination grace period countdown begins before the PreStop hook is executed. Regardless of the outcome of the handler, the container will eventually terminate within the Pod's termination grace period (unless delayed by finalizers). Other management of the container blocks until the hook completes or until the termination grace period is reached. More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks".to_owned()),
141                                ..Default::default()
142                            }));
143                            crate::schemars::schema::Schema::Object(schema_obj)
144                        },
145                    ),
146                ].into(),
147                ..Default::default()
148            })),
149            ..Default::default()
150        })
151    }
152}