#
k8s_openapi/v1_35/api/networking/v1/
network_policy_peer.rs

    1// Generated from definition io.k8s.api.networking.v1.NetworkPolicyPeer
    2
    3/// NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed
    4#[derive(Clone, Debug, Default, PartialEq)]
    5pub struct NetworkPolicyPeer {
    6    /// ipBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be.
    7    pub ip_block: Option<crate::api::networking::v1::IPBlock>,
    8
    9    /// namespaceSelector selects namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces.
    10    ///
    11    /// If podSelector is also set, then the NetworkPolicyPeer as a whole selects the pods matching podSelector in the namespaces selected by namespaceSelector. Otherwise it selects all pods in the namespaces selected by namespaceSelector.
    12    pub namespace_selector: Option<crate::apimachinery::pkg::apis::meta::v1::LabelSelector>,
    13
    14    /// podSelector is a label selector which selects pods. This field follows standard label selector semantics; if present but empty, it selects all pods.
    15    ///
    16    /// If namespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the pods matching podSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the pods matching podSelector in the policy's own namespace.
    17    pub pod_selector: Option<crate::apimachinery::pkg::apis::meta::v1::LabelSelector>,
    18}
    19
    20impl crate::DeepMerge for NetworkPolicyPeer {
    21    fn merge_from(&mut self, other: Self) {
    22        crate::DeepMerge::merge_from(&mut self.ip_block, other.ip_block);
    23        crate::DeepMerge::merge_from(&mut self.namespace_selector, other.namespace_selector);
    24        crate::DeepMerge::merge_from(&mut self.pod_selector, other.pod_selector);
    25    }
    26}
    27
    28impl<'de> crate::serde::Deserialize<'de> for NetworkPolicyPeer {
    29    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    30        #[allow(non_camel_case_types)]
    31        enum Field {
    32            Key_ip_block,
    33            Key_namespace_selector,
    34            Key_pod_selector,
    35            Other,
    36        }
    37
    38        impl<'de> crate::serde::Deserialize<'de> for Field {
    39            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    40                struct Visitor;
    41
    42                impl crate::serde::de::Visitor<'_> for Visitor {
    43                    type Value = Field;
    44
    45                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    46                        f.write_str("field identifier")
    47                    }
    48
    49                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    50                        Ok(match v {
    51                            "ipBlock" => Field::Key_ip_block,
    52                            "namespaceSelector" => Field::Key_namespace_selector,
    53                            "podSelector" => Field::Key_pod_selector,
    54                            _ => Field::Other,
    55                        })
    56                    }
    57                }
    58
    59                deserializer.deserialize_identifier(Visitor)
    60            }
    61        }
    62
    63        struct Visitor;
    64
    65        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
    66            type Value = NetworkPolicyPeer;
    67
    68            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    69                f.write_str("NetworkPolicyPeer")
    70            }
    71
    72            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
    73                let mut value_ip_block: Option<crate::api::networking::v1::IPBlock> = None;
    74                let mut value_namespace_selector: Option<crate::apimachinery::pkg::apis::meta::v1::LabelSelector> = None;
    75                let mut value_pod_selector: Option<crate::apimachinery::pkg::apis::meta::v1::LabelSelector> = None;
    76
    77                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
    78                    match key {
    79                        Field::Key_ip_block => value_ip_block = crate::serde::de::MapAccess::next_value(&mut map)?,
    80                        Field::Key_namespace_selector => value_namespace_selector = crate::serde::de::MapAccess::next_value(&mut map)?,
    81                        Field::Key_pod_selector => value_pod_selector = crate::serde::de::MapAccess::next_value(&mut map)?,
    82                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
    83                    }
    84                }
    85
    86                Ok(NetworkPolicyPeer {
    87                    ip_block: value_ip_block,
    88                    namespace_selector: value_namespace_selector,
    89                    pod_selector: value_pod_selector,
    90                })
    91            }
    92        }
    93
    94        deserializer.deserialize_struct(
    95            "NetworkPolicyPeer",
    96            &[
    97                "ipBlock",
    98                "namespaceSelector",
    99                "podSelector",
    100            ],
    101            Visitor,
    102        )
    103    }
    104}
    105
    106impl crate::serde::Serialize for NetworkPolicyPeer {
    107    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
    108        let mut state = serializer.serialize_struct(
    109            "NetworkPolicyPeer",
    110            self.ip_block.as_ref().map_or(0, |_| 1) +
    111            self.namespace_selector.as_ref().map_or(0, |_| 1) +
    112            self.pod_selector.as_ref().map_or(0, |_| 1),
    113        )?;
    114        if let Some(value) = &self.ip_block {
    115            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "ipBlock", value)?;
    116        }
    117        if let Some(value) = &self.namespace_selector {
    118            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "namespaceSelector", value)?;
    119        }
    120        if let Some(value) = &self.pod_selector {
    121            crate::serde::ser::SerializeStruct::serialize_field(&mut state, "podSelector", value)?;
    122        }
    123        crate::serde::ser::SerializeStruct::end(state)
    124    }
    125}
    126
    127#[cfg(feature = "schemars")]
    128impl crate::schemars::JsonSchema for NetworkPolicyPeer {
    129    fn schema_name() -> std::borrow::Cow<'static, str> {
    130        "io.k8s.api.networking.v1.NetworkPolicyPeer".into()
    131    }
    132
    133    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
    134        crate::schemars::json_schema!({
    135            "description": "NetworkPolicyPeer describes a peer to allow traffic to/from. Only certain combinations of fields are allowed",
    136            "type": "object",
    137            "properties": {
    138                "ipBlock": ({
    139                    let mut schema_obj = __gen.subschema_for::<crate::api::networking::v1::IPBlock>();
    140                    schema_obj.ensure_object().insert("description".into(), "ipBlock defines policy on a particular IPBlock. If this field is set then neither of the other fields can be.".into());
    141                    schema_obj
    142                }),
    143                "namespaceSelector": ({
    144                    let mut schema_obj = __gen.subschema_for::<crate::apimachinery::pkg::apis::meta::v1::LabelSelector>();
    145                    schema_obj.ensure_object().insert("description".into(), "namespaceSelector selects namespaces using cluster-scoped labels. This field follows standard label selector semantics; if present but empty, it selects all namespaces.\n\nIf podSelector is also set, then the NetworkPolicyPeer as a whole selects the pods matching podSelector in the namespaces selected by namespaceSelector. Otherwise it selects all pods in the namespaces selected by namespaceSelector.".into());
    146                    schema_obj
    147                }),
    148                "podSelector": ({
    149                    let mut schema_obj = __gen.subschema_for::<crate::apimachinery::pkg::apis::meta::v1::LabelSelector>();
    150                    schema_obj.ensure_object().insert("description".into(), "podSelector is a label selector which selects pods. This field follows standard label selector semantics; if present but empty, it selects all pods.\n\nIf namespaceSelector is also set, then the NetworkPolicyPeer as a whole selects the pods matching podSelector in the Namespaces selected by NamespaceSelector. Otherwise it selects the pods matching podSelector in the policy's own namespace.".into());
    151                    schema_obj
    152                }),
    153            },
    154        })
    155    }
    156}