#
k8s_openapi/v1_35/api/core/v1/
container_port.rs

    1// Generated from definition io.k8s.api.core.v1.ContainerPort
    2
    3/// ContainerPort represents a network port in a single container.
    4#[derive(Clone, Debug, Default, PartialEq)]
    5pub struct ContainerPort {
    6    /// Number of port to expose on the pod's IP address. This must be a valid port number, 0 \< x \< 65536.
    7    pub container_port: i32,
    8
    9    /// What host IP to bind the external port to.
    10    pub host_ip: Option<std::string::String>,
    11
    12    /// Number of port to expose on the host. If specified, this must be a valid port number, 0 \< x \< 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.
    13    pub host_port: Option<i32>,
    14
    15    /// If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
    16    pub name: Option<std::string::String>,
    17
    18    /// Protocol for port. Must be UDP, TCP, or SCTP. Defaults to "TCP".
    19    pub protocol: Option<std::string::String>,
    20}
    21
    22impl crate::DeepMerge for ContainerPort {
    23    fn merge_from(&mut self, other: Self) {
    24        crate::DeepMerge::merge_from(&mut self.container_port, other.container_port);
    25        crate::DeepMerge::merge_from(&mut self.host_ip, other.host_ip);
    26        crate::DeepMerge::merge_from(&mut self.host_port, other.host_port);
    27        crate::DeepMerge::merge_from(&mut self.name, other.name);
    28        crate::DeepMerge::merge_from(&mut self.protocol, other.protocol);
    29    }
    30}
    31
    32impl<'de> crate::serde::Deserialize<'de> for ContainerPort {
    33    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    34        #[allow(non_camel_case_types)]
    35        enum Field {
    36            Key_container_port,
    37            Key_host_ip,
    38            Key_host_port,
    39            Key_name,
    40            Key_protocol,
    41            Other,
    42        }
    43
    44        impl<'de> crate::serde::Deserialize<'de> for Field {
    45            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    46                struct Visitor;
    47
    48                impl crate::serde::de::Visitor<'_> for Visitor {
    49                    type Value = Field;
    50
    51                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    52                        f.write_str("field identifier")
    53                    }
    54
    55                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    56                        Ok(match v {
    57                            "containerPort" => Field::Key_container_port,
    58                            "hostIP" => Field::Key_host_ip,
    59                            "hostPort" => Field::Key_host_port,
    60                            "name" => Field::Key_name,
    61                            "protocol" => Field::Key_protocol,
    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 = ContainerPort;
    75
    76            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    77                f.write_str("ContainerPort")
    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_container_port: Option<i32> = None;
    82                let mut value_host_ip: Option<std::string::String> = None;
    83                let mut value_host_port: Option<i32> = None;
    84                let mut value_name: Option<std::string::String> = None;
    85                let mut value_protocol: Option<std::string::String> = None;
    86
    87                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
    88                    match key {
    89                        Field::Key_container_port => value_container_port = crate::serde::de::MapAccess::next_value(&mut map)?,
    90                        Field::Key_host_ip => value_host_ip = crate::serde::de::MapAccess::next_value(&mut map)?,
    91                        Field::Key_host_port => value_host_port = crate::serde::de::MapAccess::next_value(&mut map)?,
    92                        Field::Key_name => value_name = crate::serde::de::MapAccess::next_value(&mut map)?,
    93                        Field::Key_protocol => value_protocol = 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(ContainerPort {
    99                    container_port: value_container_port.unwrap_or_default(),
    100                    host_ip: value_host_ip,
    101                    host_port: value_host_port,
    102                    name: value_name,
    103                    protocol: value_protocol,
    104                })
    105            }
    106        }
    107
    108        deserializer.deserialize_struct(
    109            "ContainerPort",
    110            &[
    111                "containerPort",
    112                "hostIP",
    113                "hostPort",
    114                "name",
    115                "protocol",
    116            ],
    117            Visitor,
    118        )
    119    }
    120}
    121
    122impl crate::serde::Serialize for ContainerPort {
    123    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
    124        let mut state = serializer.serialize_struct(
    125            "ContainerPort",
    126            1 +
    127            self.host_ip.as_ref().map_or(0, |_| 1) +
    128            self.host_port.as_ref().map_or(0, |_| 1) +
    129            self.name.as_ref().map_or(0, |_| 1) +
    130            self.protocol.as_ref().map_or(0, |_| 1),
    131        )?;
    132        crate::serde::ser::SerializeStruct::serialize_field(&mut state, "containerPort", &self.container_port)?;
    133        if let Some(value) = &self.host_ip {
    134            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "hostIP", value)?;
    135        }
    136        if let Some(value) = &self.host_port {
    137            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "hostPort", value)?;
    138        }
    139        if let Some(value) = &self.name {
    140            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "name", value)?;
    141        }
    142        if let Some(value) = &self.protocol {
    143            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "protocol", value)?;
    144        }
    145        crate::serde::ser::SerializeStruct::end(state)
    146    }
    147}
    148
    149#[cfg(feature = "schemars")]
    150impl crate::schemars::JsonSchema for ContainerPort {
    151    fn schema_name() -> std::borrow::Cow<'static, str> {
    152        "io.k8s.api.core.v1.ContainerPort".into()
    153    }
    154
    155    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
    156        crate::schemars::json_schema!({
    157            "description": "ContainerPort represents a network port in a single container.",
    158            "type": "object",
    159            "properties": {
    160                "containerPort": {
    161                    "description": "Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536.",
    162                    "type": "integer",
    163                    "format": "int32",
    164                },
    165                "hostIP": {
    166                    "description": "What host IP to bind the external port to.",
    167                    "type": "string",
    168                },
    169                "hostPort": {
    170                    "description": "Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.",
    171                    "type": "integer",
    172                    "format": "int32",
    173                },
    174                "name": {
    175                    "description": "If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.",
    176                    "type": "string",
    177                },
    178                "protocol": {
    179                    "description": "Protocol for port. Must be UDP, TCP, or SCTP. Defaults to \"TCP\".",
    180                    "type": "string",
    181                },
    182            },
    183            "required": [
    184                "containerPort",
    185            ],
    186        })
    187    }
    188}