/
cluster_types.go
426 lines (347 loc) · 13.3 KB
/
cluster_types.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha4
import (
"fmt"
"net"
"strings"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
capierrors "sigs.k8s.io/cluster-api/errors"
)
const (
// ClusterFinalizer is the finalizer used by the cluster controller to
// cleanup the cluster resources when a Cluster is being deleted.
ClusterFinalizer = "cluster.cluster.x-k8s.io"
)
// ANCHOR: ClusterSpec
// ClusterSpec defines the desired state of Cluster.
type ClusterSpec struct {
// Paused can be used to prevent controllers from processing the Cluster and all its associated objects.
// +optional
Paused bool `json:"paused,omitempty"`
// Cluster network configuration.
// +optional
ClusterNetwork *ClusterNetwork `json:"clusterNetwork,omitempty"`
// ControlPlaneEndpoint represents the endpoint used to communicate with the control plane.
// +optional
ControlPlaneEndpoint APIEndpoint `json:"controlPlaneEndpoint"`
// ControlPlaneRef is an optional reference to a provider-specific resource that holds
// the details for provisioning the Control Plane for a Cluster.
// +optional
ControlPlaneRef *corev1.ObjectReference `json:"controlPlaneRef,omitempty"`
// InfrastructureRef is a reference to a provider-specific resource that holds the details
// for provisioning infrastructure for a cluster in said provider.
// +optional
InfrastructureRef *corev1.ObjectReference `json:"infrastructureRef,omitempty"`
// This encapsulates the topology for the cluster.
// NOTE: It is required to enable the ClusterTopology
// feature gate flag to activate managed topologies support;
// this feature is highly experimental, and parts of it might still be not implemented.
// +optional
Topology *Topology `json:"topology,omitempty"`
}
// Topology encapsulates the information of the managed resources.
type Topology struct {
// The name of the ClusterClass object to create the topology.
Class string `json:"class"`
// The Kubernetes version of the cluster.
Version string `json:"version"`
// RolloutAfter performs a rollout of the entire cluster one component at a time,
// control plane first and then machine deployments.
// +optional
RolloutAfter *metav1.Time `json:"rolloutAfter,omitempty"`
// ControlPlane describes the cluster control plane.
ControlPlane ControlPlaneTopology `json:"controlPlane"`
// Workers encapsulates the different constructs that form the worker nodes
// for the cluster.
// +optional
Workers *WorkersTopology `json:"workers,omitempty"`
}
// ControlPlaneTopology specifies the parameters for the control plane nodes in the cluster.
type ControlPlaneTopology struct {
Metadata ObjectMeta `json:"metadata,omitempty"`
// Replicas is the number of control plane nodes.
// If the value is nil, the ControlPlane object is created without the number of Replicas
// and it's assumed that the control plane controller does not implement support for this field.
// When specified against a control plane provider that lacks support for this field, this value will be ignored.
// +optional
Replicas *int `json:"replicas,omitempty"`
}
// WorkersTopology represents the different sets of worker nodes in the cluster.
type WorkersTopology struct {
// MachineDeployments is a list of machine deployments in the cluster.
MachineDeployments []MachineDeploymentTopology `json:"machineDeployments,omitempty"`
}
// MachineDeploymentTopology specifies the different parameters for a set of worker nodes in the topology.
// This set of nodes is managed by a MachineDeployment object whose lifecycle is managed by the Cluster controller.
type MachineDeploymentTopology struct {
Metadata ObjectMeta `json:"metadata,omitempty"`
// Class is the name of the MachineDeploymentClass used to create the set of worker nodes.
// This should match one of the deployment classes defined in the ClusterClass object
// mentioned in the `Cluster.Spec.Class` field.
Class string `json:"class"`
// Name is the unique identifier for this MachineDeploymentTopology.
// The value is used with other unique identifiers to create a MachineDeployment's Name
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
// the values are hashed together.
Name string `json:"name"`
// Replicas is the number of worker nodes belonging to this set.
// If the value is nil, the MachineDeployment is created without the number of Replicas (defaulting to zero)
// and it's assumed that an external entity (like cluster autoscaler) is responsible for the management
// of this value.
// +optional
Replicas *int `json:"replicas,omitempty"`
}
// ANCHOR_END: ClusterSpec
// ANCHOR: ClusterNetwork
// ClusterNetwork specifies the different networking
// parameters for a cluster.
type ClusterNetwork struct {
// APIServerPort specifies the port the API Server should bind to.
// Defaults to 6443.
// +optional
APIServerPort *int32 `json:"apiServerPort,omitempty"`
// The network ranges from which service VIPs are allocated.
// +optional
Services *NetworkRanges `json:"services,omitempty"`
// The network ranges from which Pod networks are allocated.
// +optional
Pods *NetworkRanges `json:"pods,omitempty"`
// Domain name for services.
// +optional
ServiceDomain string `json:"serviceDomain,omitempty"`
}
// ANCHOR_END: ClusterNetwork
// ANCHOR: NetworkRanges
// NetworkRanges represents ranges of network addresses.
type NetworkRanges struct {
CIDRBlocks []string `json:"cidrBlocks"`
}
func (n *NetworkRanges) String() string {
if n == nil {
return ""
}
return strings.Join(n.CIDRBlocks, ",")
}
// ANCHOR_END: NetworkRanges
// ANCHOR: ClusterStatus
// ClusterStatus defines the observed state of Cluster.
type ClusterStatus struct {
// FailureDomains is a slice of failure domain objects synced from the infrastructure provider.
FailureDomains FailureDomains `json:"failureDomains,omitempty"`
// FailureReason indicates that there is a fatal problem reconciling the
// state, and will be set to a token value suitable for
// programmatic interpretation.
// +optional
FailureReason *capierrors.ClusterStatusError `json:"failureReason,omitempty"`
// FailureMessage indicates that there is a fatal problem reconciling the
// state, and will be set to a descriptive error message.
// +optional
FailureMessage *string `json:"failureMessage,omitempty"`
// Phase represents the current phase of cluster actuation.
// E.g. Pending, Running, Terminating, Failed etc.
// +optional
Phase string `json:"phase,omitempty"`
// InfrastructureReady is the state of the infrastructure provider.
// +optional
InfrastructureReady bool `json:"infrastructureReady"`
// ControlPlaneReady defines if the control plane is ready.
// +optional
ControlPlaneReady bool `json:"controlPlaneReady,omitempty"`
// Conditions defines current service state of the cluster.
// +optional
Conditions Conditions `json:"conditions,omitempty"`
// ObservedGeneration is the latest generation observed by the controller.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
}
// ANCHOR_END: ClusterStatus
// SetTypedPhase sets the Phase field to the string representation of ClusterPhase.
func (c *ClusterStatus) SetTypedPhase(p ClusterPhase) {
c.Phase = string(p)
}
// GetTypedPhase attempts to parse the Phase field and return
// the typed ClusterPhase representation as described in `machine_phase_types.go`.
func (c *ClusterStatus) GetTypedPhase() ClusterPhase {
switch phase := ClusterPhase(c.Phase); phase {
case
ClusterPhasePending,
ClusterPhaseProvisioning,
ClusterPhaseProvisioned,
ClusterPhaseDeleting,
ClusterPhaseFailed:
return phase
default:
return ClusterPhaseUnknown
}
}
// ANCHOR: APIEndpoint
// APIEndpoint represents a reachable Kubernetes API endpoint.
type APIEndpoint struct {
// The hostname on which the API server is serving.
Host string `json:"host"`
// The port on which the API server is serving.
Port int32 `json:"port"`
}
// IsZero returns true if both host and port are zero values.
func (v APIEndpoint) IsZero() bool {
return v.Host == "" && v.Port == 0
}
// IsValid returns true if both host and port are non-zero values.
func (v APIEndpoint) IsValid() bool {
return v.Host != "" && v.Port != 0
}
// String returns a formatted version HOST:PORT of this APIEndpoint.
func (v APIEndpoint) String() string {
return net.JoinHostPort(v.Host, fmt.Sprintf("%d", v.Port))
}
// ANCHOR_END: APIEndpoint
// +kubebuilder:object:root=true
// +kubebuilder:resource:path=clusters,shortName=cl,scope=Namespaced,categories=cluster-api
// +kubebuilder:storageversion
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Phase",type="string",JSONPath=".status.phase",description="Cluster status such as Pending/Provisioning/Provisioned/Deleting/Failed"
// Cluster is the Schema for the clusters API.
type Cluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ClusterSpec `json:"spec,omitempty"`
Status ClusterStatus `json:"status,omitempty"`
}
// GetConditions returns the set of conditions for this object.
func (c *Cluster) GetConditions() Conditions {
return c.Status.Conditions
}
// SetConditions sets the conditions on this object.
func (c *Cluster) SetConditions(conditions Conditions) {
c.Status.Conditions = conditions
}
// GetIPFamily returns a ClusterIPFamily from the configuration provided.
func (c *Cluster) GetIPFamily() (ClusterIPFamily, error) {
var podCIDRs, serviceCIDRs []string
if c.Spec.ClusterNetwork != nil {
if c.Spec.ClusterNetwork.Pods != nil {
podCIDRs = c.Spec.ClusterNetwork.Pods.CIDRBlocks
}
if c.Spec.ClusterNetwork.Services != nil {
serviceCIDRs = c.Spec.ClusterNetwork.Services.CIDRBlocks
}
}
if len(podCIDRs) == 0 && len(serviceCIDRs) == 0 {
return IPv4IPFamily, nil
}
podsIPFamily, err := ipFamilyForCIDRStrings(podCIDRs)
if err != nil {
return InvalidIPFamily, fmt.Errorf("pods: %s", err)
}
if len(serviceCIDRs) == 0 {
return podsIPFamily, nil
}
servicesIPFamily, err := ipFamilyForCIDRStrings(serviceCIDRs)
if err != nil {
return InvalidIPFamily, fmt.Errorf("services: %s", err)
}
if len(podCIDRs) == 0 {
return servicesIPFamily, nil
}
if podsIPFamily == DualStackIPFamily {
return DualStackIPFamily, nil
} else if podsIPFamily != servicesIPFamily {
return InvalidIPFamily, errors.New("pods and services IP family mismatch")
}
return podsIPFamily, nil
}
func ipFamilyForCIDRStrings(cidrs []string) (ClusterIPFamily, error) {
if len(cidrs) > 2 {
return InvalidIPFamily, errors.New("too many CIDRs specified")
}
var foundIPv4 bool
var foundIPv6 bool
for _, cidr := range cidrs {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return InvalidIPFamily, fmt.Errorf("could not parse CIDR: %s", err)
}
if ip.To4() != nil {
foundIPv4 = true
} else {
foundIPv6 = true
}
}
switch {
case foundIPv4 && foundIPv6:
return DualStackIPFamily, nil
case foundIPv4:
return IPv4IPFamily, nil
case foundIPv6:
return IPv6IPFamily, nil
default:
return InvalidIPFamily, nil
}
}
// ClusterIPFamily defines the types of supported IP families.
type ClusterIPFamily int
// Define the ClusterIPFamily constants.
const (
InvalidIPFamily ClusterIPFamily = iota
IPv4IPFamily
IPv6IPFamily
DualStackIPFamily
)
func (f ClusterIPFamily) String() string {
return [...]string{"InvalidIPFamily", "IPv4IPFamily", "IPv6IPFamily", "DualStackIPFamily"}[f]
}
// +kubebuilder:object:root=true
// ClusterList contains a list of Cluster.
type ClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Cluster `json:"items"`
}
func init() {
SchemeBuilder.Register(&Cluster{}, &ClusterList{})
}
// FailureDomains is a slice of FailureDomains.
type FailureDomains map[string]FailureDomainSpec
// FilterControlPlane returns a FailureDomain slice containing only the domains suitable to be used
// for control plane nodes.
func (in FailureDomains) FilterControlPlane() FailureDomains {
res := make(FailureDomains)
for id, spec := range in {
if spec.ControlPlane {
res[id] = spec
}
}
return res
}
// GetIDs returns a slice containing the ids for failure domains.
func (in FailureDomains) GetIDs() []*string {
ids := make([]*string, 0, len(in))
for id := range in {
ids = append(ids, pointer.StringPtr(id))
}
return ids
}
// FailureDomainSpec is the Schema for Cluster API failure domains.
// It allows controllers to understand how many failure domains a cluster can optionally span across.
type FailureDomainSpec struct {
// ControlPlane determines if this failure domain is suitable for use by control plane machines.
// +optional
ControlPlane bool `json:"controlPlane"`
// Attributes is a free form map of attributes an infrastructure provider might use or require.
// +optional
Attributes map[string]string `json:"attributes,omitempty"`
}