1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::fmt;

use super::*;

impl Cluster {
    pub fn infrastructure(&self) -> Option<&corev1::ObjectReference> {
        self.spec.infrastructure_ref.as_ref()
    }

    pub fn control_plane(&self) -> Option<&corev1::ObjectReference> {
        self.spec.control_plane_ref.as_ref()
    }

    pub fn infrastructure_ready(&self) -> bool {
        self.status
            .as_ref()
            .and_then(|status| status.infrastructure_ready)
            .unwrap_or_default()
    }

    pub fn control_plane_ready(&self) -> bool {
        self.status
            .as_ref()
            .and_then(|status| status.control_plane_ready)
            .unwrap_or_default()
    }

    pub fn cluster_network(&self) -> Option<&ClusterNetwork> {
        self.spec.cluster_network.as_ref()
    }

    pub fn topology(&self) -> Option<&Topology> {
        self.spec.topology.as_ref()
    }

    pub fn phase(&self) -> ClusterPhase {
        self.status
            .as_ref()
            .map_or(ClusterPhase::Unknown, |status| status.phase())
    }

    pub fn conditions(&self) -> Option<&Conditions> {
        self.status
            .as_ref()
            .and_then(|status| status.conditions.as_ref())
    }

    pub fn conditions_mut(&mut self) -> Option<&mut Conditions> {
        self.status
            .as_mut()
            .and_then(|status| status.conditions.as_mut())
    }

    pub fn pod_cidrs(&self) -> Option<&[String]> {
        self.cluster_network()
            .and_then(|cn| cn.pods.as_ref())
            .map(|ranges| ranges.cidr_blocks.as_slice())
    }

    pub fn service_cidrs(&self) -> Option<&[String]> {
        self.cluster_network()
            .and_then(|cn| cn.pods.as_ref())
            .map(|ranges| ranges.cidr_blocks.as_slice())
    }

    /// GetIPFamily returns a ClusterIPFamily from the configuration provided.
    pub fn get_ip_family(&self) -> Result<ClusterIpFamily, InvalidIpFamily> {
        let pod_cidrs = self.pod_cidrs().unwrap_or_default();
        let service_cidrs = self.service_cidrs().unwrap_or_default();

        if pod_cidrs.is_empty() && service_cidrs.is_empty() {
            return Ok(ClusterIpFamily::Ipv4IpFamily);
        }

        let pods_family = ClusterIpFamily::ip_family_for_cidr_strings(pod_cidrs)?;
        let services_family = ClusterIpFamily::ip_family_for_cidr_strings(service_cidrs)?;

        if pods_family == services_family {
            Ok(pods_family)
        } else {
            Err(InvalidIpFamily::IpFamilyMismatch)
        }
    }
}

impl fmt::Display for NetworkRanges {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.cidr_blocks.join(","))
    }
}

impl ClusterStatus {
    pub fn phase(&self) -> ClusterPhase {
        self.phase.unwrap_or(ClusterPhase::Unknown)
    }
}

impl ApiEndpoint {
    /// IsZero returns true if both host and port are zero values.
    pub fn is_zero(&self) -> bool {
        self.host.is_empty() && self.port == 0
    }

    /// IsValid returns true if both host and port are non-zero values.
    pub fn is_valid(&self) -> bool {
        !self.host.is_empty() && self.port != 0
    }
}

impl fmt::Display for ApiEndpoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.host, self.port)
    }
}