k8s_cluster_api/v1beta1/cluster.rs
1use kube::CustomResource;
2
3use super::*;
4
5pub use common::*;
6pub use condition::*;
7pub use ip_family::ClusterIpFamily;
8pub use ip_family::InvalidIpFamily;
9pub use phase::ClusterPhase;
10
11mod common;
12mod condition;
13mod impls;
14mod ip_family;
15mod phase;
16
17/// ClusterFinalizer is the finalizer used by the cluster controller to
18/// cleanup the cluster resources when a Cluster is being deleted.
19pub const CLUSTER_FINALIZER: &str = "cluster.cluster.x-k8s.io";
20
21/// ClusterSpec defines the desired state of Cluster.
22///
23#[skip_serializing_none]
24#[derive(Clone, Debug, Default, Serialize, Deserialize, CustomResource)]
25#[serde(rename_all = "camelCase")]
26#[kube(
27 group = "cluster.x-k8s.io",
28 version = "v1beta1",
29 kind = "Cluster",
30 plural = "clusters",
31 shortname = "cl",
32 status = "ClusterStatus"
33)]
34#[kube(namespaced)]
35#[kube(schema = "disabled")]
36pub struct ClusterSpec {
37 /// Paused can be used to prevent controllers from processing the Cluster and all its associated objects.
38 pub paused: Option<bool>,
39
40 /// Cluster network configuration.
41 pub cluster_network: Option<ClusterNetwork>,
42
43 /// ControlPlaneEndpoint represents the endpoint used to communicate with the control plane.
44 pub control_plane_endpoint: Option<ApiEndpoint>,
45
46 /// ControlPlaneRef is an optional reference to a provider-specific resource that holds
47 /// the details for provisioning the Control Plane for a Cluster.
48 pub control_plane_ref: Option<corev1::ObjectReference>,
49
50 /// InfrastructureRef is a reference to a provider-specific resource that holds the details
51 /// for provisioning infrastructure for a cluster in said provider.
52 pub infrastructure_ref: Option<corev1::ObjectReference>,
53
54 /// This encapsulates the topology for the cluster.
55 /// NOTE: It is required to enable the ClusterTopology
56 /// feature gate flag to activate managed topologies support;
57 /// this feature is highly experimental, and parts of it might still be not implemented.
58 pub topology: Option<Topology>,
59}
60
61/// Topology encapsulates the information of the managed resources.
62#[skip_serializing_none]
63#[derive(Clone, Debug, Default, Serialize, Deserialize)]
64#[serde(rename_all = "camelCase")]
65pub struct Topology {
66 /// The name of the ClusterClass object to create the topology.
67 pub class: String,
68
69 /// The Kubernetes version of the cluster.
70 pub version: String,
71
72 /// RolloutAfter performs a rollout of the entire cluster one component at a time,
73 /// control plane first and then machine deployments.
74 pub rollout_after: Option<metav1::Time>,
75
76 /// ControlPlane describes the cluster control plane.
77 pub control_plane: Option<ControlPlaneTopology>,
78
79 /// Workers encapsulates the different constructs that form the worker nodes
80 /// for the cluster.
81 pub workers: Option<WorkersTopology>,
82
83 /// Variables can be used to customize the Cluster through
84 /// patches. They must comply to the corresponding
85 /// VariableClasses defined in the ClusterClass.
86 pub variables: Option<Vec<ClusterVariable>>,
87}
88
89/// ControlPlaneTopology specifies the parameters for the control plane nodes in the cluster.
90#[skip_serializing_none]
91#[derive(Clone, Debug, Default, Serialize, Deserialize)]
92pub struct ControlPlaneTopology {
93 /// Metadata is the metadata applied to the machines of the ControlPlane.
94 /// At runtime this metadata is merged with the corresponding metadata from the ClusterClass.
95 ///
96 /// This field is supported if and only if the control plane provider template
97 /// referenced in the ClusterClass is Machine based.
98 pub metadata: Option<ObjectMeta>,
99
100 /// Replicas is the number of control plane nodes.
101 /// If the value is nil, the ControlPlane object is created without the number of Replicas
102 /// and it's assumed that the control plane controller does not implement support for this field.
103 /// When specified against a control plane provider that lacks support for this field, this value will be ignored.
104 pub replicas: Option<i32>,
105}
106
107/// WorkersTopology represents the different sets of worker nodes in the cluster.
108#[skip_serializing_none]
109#[derive(Clone, Debug, Default, Serialize, Deserialize)]
110#[serde(rename_all = "camelCase")]
111pub struct WorkersTopology {
112 /// MachineDeployments is a list of machine deployments in the cluster.
113 pub machine_deployments: Option<Vec<MachineDeploymentTopology>>,
114}
115
116/// MachineDeploymentTopology specifies the different parameters for a set of worker nodes in the topology.
117/// This set of nodes is managed by a MachineDeployment object whose lifecycle is managed by the Cluster controller.
118#[skip_serializing_none]
119#[derive(Clone, Debug, Default, Serialize, Deserialize)]
120pub struct MachineDeploymentTopology {
121 /// Metadata is the metadata applied to the machines of the MachineDeployment.
122 /// At runtime this metadata is merged with the corresponding metadata from the ClusterClass.
123 pub metadata: Option<ObjectMeta>,
124
125 /// Class is the name of the MachineDeploymentClass used to create the set of worker nodes.
126 /// This should match one of the deployment classes defined in the ClusterClass object
127 /// mentioned in the `Cluster.Spec.Class` field.
128 pub class: String,
129
130 /// Name is the unique identifier for this MachineDeploymentTopology.
131 /// The value is used with other unique identifiers to create a MachineDeployment's Name
132 /// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
133 /// the values are hashed together.
134 pub name: String,
135
136 /// Replicas is the number of worker nodes belonging to this set.
137 /// If the value is nil, the MachineDeployment is created without the number of Replicas (defaulting to zero)
138 /// and it's assumed that an external entity (like cluster autoscaler) is responsible for the management
139 /// of this value.
140 pub replicas: Option<i32>,
141}
142
143/// ClusterVariable can be used to customize the Cluster through
144/// patches. It must comply to the corresponding
145/// ClusterClassVariable defined in the ClusterClass.
146#[derive(Clone, Debug, Default, Serialize, Deserialize)]
147pub struct ClusterVariable {
148 /// Name of the variable.
149 pub name: String,
150
151 /// Value of the variable.
152 /// Note: the value will be validated against the schema of the corresponding ClusterClassVariable
153 /// from the ClusterClass.
154 /// Note: We have to use apiextensionsv1.JSON instead of a custom JSON type, because controller-tools has a
155 /// hard-coded schema for apiextensionsv1.JSON which cannot be produced by another type via controller-tools,
156 /// i.e. it's not possible to have no type field.
157 /// Ref: https://github.com/kubernetes-sigs/controller-tools/blob/d0e03a142d0ecdd5491593e941ee1d6b5d91dba6/pkg/crd/known_types.go#L106-L111
158 pub value: apiextensionsv1::JSON,
159}
160
161// ANCHOR_END: ClusterSpec
162
163// ANCHOR: ClusterNetwork
164
165/// ClusterNetwork specifies the different networking
166/// parameters for a cluster.
167#[skip_serializing_none]
168#[derive(Clone, Debug, Default, Serialize, Deserialize)]
169#[serde(rename_all = "camelCase")]
170pub struct ClusterNetwork {
171 /// APIServerPort specifies the port the API Server should bind to.
172 /// Defaults to 6443.
173 pub api_server_port: Option<i32>,
174
175 /// The network ranges from which service VIPs are allocated.
176 pub services: Option<NetworkRanges>,
177
178 /// The network ranges from which Pod networks are allocated.
179 pub pods: Option<NetworkRanges>,
180
181 /// Domain name for services.
182 pub service_domain: Option<String>,
183}
184
185// ANCHOR_END: ClusterNetwork
186
187// ANCHOR: NetworkRanges
188
189/// NetworkRanges represents ranges of network addresses.
190#[derive(Clone, Debug, Serialize, Deserialize)]
191#[serde(rename_all = "camelCase")]
192pub struct NetworkRanges {
193 pub cidr_blocks: Vec<String>,
194}
195
196// ANCHOR_END: NetworkRanges
197
198// ANCHOR: ClusterStatus
199
200/// ClusterStatus defines the observed state of Cluster.
201#[skip_serializing_none]
202#[derive(Clone, Debug, Default, Serialize, Deserialize)]
203#[serde(rename_all = "camelCase")]
204pub struct ClusterStatus {
205 /// FailureDomains is a slice of failure domain objects synced from the infrastructure provider.
206 pub failure_domains: Option<FailureDomains>,
207
208 /// FailureReason indicates that there is a fatal problem reconciling the
209 /// state, and will be set to a token value suitable for
210 /// programmatic interpretation.
211 pub failure_reason: Option<capierrors::ClusterStatusError>,
212
213 /// FailureMessage indicates that there is a fatal problem reconciling the
214 /// state, and will be set to a descriptive error message.
215 pub failure_message: Option<String>,
216
217 /// Phase represents the current phase of cluster actuation.
218 /// E.g. Pending, Running, Terminating, Failed etc.
219 pub phase: Option<ClusterPhase>,
220
221 /// InfrastructureReady is the state of the infrastructure provider.
222 // +optional
223 pub infrastructure_ready: Option<bool>,
224
225 /// ControlPlaneReady defines if the control plane is ready.
226 // +optional
227 pub control_plane_ready: Option<bool>,
228
229 /// Conditions defines current service state of the cluster.
230 // +optional
231 pub conditions: Option<Conditions>,
232
233 /// ObservedGeneration is the latest generation observed by the controller.
234 // +optional
235 pub observed_generation: i64,
236}
237
238// ANCHOR_END: ClusterStatus
239
240// ANCHOR: APIEndpoint
241
242/// APIEndpoint represents a reachable Kubernetes API endpoint.
243#[derive(Clone, Debug, Default, Serialize, Deserialize)]
244pub struct ApiEndpoint {
245 /// The hostname on which the API server is serving.
246 pub host: String,
247
248 /// The port on which the API server is serving.
249 pub port: i32,
250}
251
252// ANCHOR_END: APIEndpoint
253
254/* ############
255
256// +kubebuilder:object:root=true
257// +kubebuilder:resource:path=clusters,shortName=cl,scope=Namespaced,categories=cluster-api
258// +kubebuilder:storageversion
259// +kubebuilder:subresource:status
260// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="Cluster status such as Pending/Provisioning/Provisioned/Deleting/Failed"
261// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Cluster"
262// +kubebuilder:printcolumn:name="Version",type="string",JSONPath=".spec.topology.version",description="Kubernetes version associated with this Cluster"
263
264
265
266// +kubebuilder:object:root=true
267
268// ClusterList contains a list of Cluster.
269type ClusterList struct {
270 metav1.TypeMeta `json:",inline"`
271 metav1.ListMeta `json:"metadata,omitempty"`
272 Items []Cluster `json:"items"`
273}
274
275func init() {
276 SchemeBuilder.Register(&Cluster{}, &ClusterList{})
277}
278
279############ */
280
281/// FailureDomains is a slice of FailureDomains.
282pub type FailureDomains = BTreeMap<String, FailureDomainSpec>;
283
284/* ############
285// FilterControlPlane returns a FailureDomain slice containing only the domains suitable to be used
286// for control plane nodes.
287func (in FailureDomains) FilterControlPlane() FailureDomains {
288 res := make(FailureDomains)
289 for id, spec := range in {
290 if spec.ControlPlane {
291 res[id] = spec
292 }
293 }
294 return res
295}
296
297// GetIDs returns a slice containing the ids for failure domains.
298func (in FailureDomains) GetIDs() []*string {
299 ids := make([]*string, 0, len(in))
300 for id := range in {
301 ids = append(ids, pointer.StringPtr(id))
302 }
303 return ids
304}
305
306############ */
307
308/// FailureDomainSpec is the Schema for Cluster API failure domains.
309/// It allows controllers to understand how many failure domains a cluster can optionally span across.
310#[skip_serializing_none]
311#[derive(Clone, Debug, Default, Serialize, Deserialize)]
312pub struct FailureDomainSpec {
313 /// ControlPlane determines if this failure domain is suitable for use by control plane machines.
314 pub control_plane: Option<bool>,
315
316 /// Attributes is a free form map of attributes an infrastructure provider might use or require.
317 pub attributes: Option<BTreeMap<String, String>>,
318}
319
320#[cfg(test)]
321mod tests;