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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::fmt;
use super::*;
impl Cluster {
pub fn with_name(name: &str) -> Self {
let spec = ClusterSpec::default();
Self::new(name, spec)
}
#[must_use]
pub fn set_pod_cidr_block(self, pod_cidr_block: &str) -> Self {
let pods = NetworkRanges {
cidr_blocks: vec![pod_cidr_block.to_string()],
};
let networks = self.spec.cluster_network.unwrap_or_default();
let cluster_networks = ClusterNetwork {
pods: Some(pods),
..networks
};
Self {
spec: ClusterSpec {
cluster_network: Some(cluster_networks),
..self.spec
},
..self
}
}
#[must_use]
pub fn set_control_plane(self, control_plane: &ControlPlane) -> Self {
let control_plane_ref = control_plane.object_ref();
Self {
spec: ClusterSpec {
control_plane_ref: Some(control_plane_ref),
..self.spec
},
..self
}
}
#[must_use]
pub fn set_infrastructure(self, infrastructure: &Infrastructure) -> Self {
let infrastructure_ref = infrastructure.object_ref();
Self {
spec: ClusterSpec {
infrastructure_ref: Some(infrastructure_ref),
..self.spec
},
..self
}
}
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())
}
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 {
pub fn is_zero(&self) -> bool {
self.host.is_empty() && self.port == 0
}
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)
}
}
impl ClusterSpec {
pub fn new() -> Self {
Self {
cluster_network: None,
control_plane_ref: None,
infrastructure_ref: None,
..Self::default()
}
}
}