-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathcontrol_plane.go
429 lines (349 loc) · 14.9 KB
/
control_plane.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
427
428
429
/*
Copyright 2022 SUSE.
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 rke2
import (
"context"
"sort"
"github.com/go-logr/logr"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/util/collections"
capifd "sigs.k8s.io/cluster-api/util/failuredomains"
"sigs.k8s.io/cluster-api/util/patch"
bootstrapv1 "github.com/rancher/cluster-api-provider-rke2/bootstrap/api/v1beta1"
controlplanev1 "github.com/rancher/cluster-api-provider-rke2/controlplane/api/v1beta1"
)
// ControlPlane holds business logic around control planes.
// It should never need to connect to a service, that responsibility lies outside of this struct.
// Going forward we should be trying to add more logic to here and reduce the amount of logic in the reconciler.
type ControlPlane struct {
RCP *controlplanev1.RKE2ControlPlane
Cluster *clusterv1.Cluster
Machines collections.Machines
machinesPatchHelpers map[string]*patch.Helper
// reconciliationTime is the time of the current reconciliation, and should be used for all "now" calculations
reconciliationTime metav1.Time
rke2Configs map[string]*bootstrapv1.RKE2Config
infraResources map[string]*unstructured.Unstructured
}
// NewControlPlane returns an instantiated ControlPlane.
func NewControlPlane(
ctx context.Context,
client client.Client,
cluster *clusterv1.Cluster,
rcp *controlplanev1.RKE2ControlPlane,
ownedMachines collections.Machines,
) (*ControlPlane, error) {
infraObjects, err := getInfraResources(ctx, client, ownedMachines)
if err != nil {
return nil, err
}
rke2Configs, err := getRKE2Configs(ctx, client, ownedMachines)
if err != nil {
return nil, err
}
patchHelpers := map[string]*patch.Helper{}
for name, machine := range ownedMachines {
patchHelper, err := patch.NewHelper(machine, client)
if err != nil {
if machine.Status.NodeRef != nil {
_ = machine.Status.NodeRef.Name
}
return nil, err
}
patchHelpers[name] = patchHelper
}
return &ControlPlane{
RCP: rcp,
Cluster: cluster,
Machines: ownedMachines,
machinesPatchHelpers: patchHelpers,
rke2Configs: rke2Configs,
infraResources: infraObjects,
reconciliationTime: metav1.Now(),
}, nil
}
// Logger returns a logger with useful context.
func (c *ControlPlane) Logger() logr.Logger {
return klog.Background().WithValues("namespace", c.RCP.Namespace, "name", c.RCP.Name, "cluster-name", c.Cluster.Name)
}
// FailureDomains returns a slice of failure domain objects synced from the infrastructure provider into Cluster.Status.
func (c *ControlPlane) FailureDomains() clusterv1.FailureDomains {
if c.Cluster.Status.FailureDomains == nil {
return clusterv1.FailureDomains{}
}
return c.Cluster.Status.FailureDomains
}
// Version returns the RKE2ControlPlane's version.
func (c *ControlPlane) Version() *string {
version := c.RCP.GetDesiredVersion()
return &version
}
// InfrastructureRef returns the RKE2ControlPlane's infrastructure template.
func (c *ControlPlane) InfrastructureRef() *corev1.ObjectReference {
return &c.RCP.Spec.MachineTemplate.InfrastructureRef
}
// AsOwnerReference returns an owner reference to the RKE2ControlPlane.
func (c *ControlPlane) AsOwnerReference() *metav1.OwnerReference {
return &metav1.OwnerReference{
APIVersion: controlplanev1.GroupVersion.String(),
Kind: "RKE2ControlPlane",
Name: c.RCP.Name,
UID: c.RCP.UID,
}
}
// MachineInFailureDomainWithMostMachines returns the first matching failure domain with machines that has the most control-plane machines on it.
func (c *ControlPlane) MachineInFailureDomainWithMostMachines(ctx context.Context, machines collections.Machines) (*clusterv1.Machine, error) {
fd := c.FailureDomainWithMostMachines(ctx, machines)
machinesInFailureDomain := machines.Filter(collections.InFailureDomains(fd))
machineToMark := machinesInFailureDomain.Oldest()
if machineToMark == nil {
return nil, errors.New("failed to pick control plane Machine to mark for deletion")
}
return machineToMark, nil
}
// MachineWithDeleteAnnotation returns a machine that has been annotated with DeleteMachineAnnotation key.
func (c *ControlPlane) MachineWithDeleteAnnotation(machines collections.Machines) collections.Machines {
// See if there are any machines with DeleteMachineAnnotation key.
annotatedMachines := machines.Filter(collections.HasAnnotationKey(clusterv1.DeleteMachineAnnotation))
// If there are, return list of annotated machines.
return annotatedMachines
}
// FailureDomainWithMostMachines returns a fd which exists both in machines and control-plane machines and has the most
// control-plane machines on it.
func (c *ControlPlane) FailureDomainWithMostMachines(ctx context.Context, machines collections.Machines) *string {
// See if there are any Machines that are not in currently defined failure domains first.
notInFailureDomains := machines.Filter(
collections.Not(collections.InFailureDomains(c.FailureDomains().FilterControlPlane().GetIDs()...)),
)
if len(notInFailureDomains) > 0 {
// return the failure domain for the oldest Machine not in the current list of failure domains
// this could be either nil (no failure domain defined) or a failure domain that is no longer defined
// in the cluster status.
return notInFailureDomains.Oldest().Spec.FailureDomain
}
return capifd.PickMost(ctx, c.Cluster.Status.FailureDomains.FilterControlPlane(), c.Machines, machines)
}
// NextFailureDomainForScaleUp returns the failure domain with the fewest number of up-to-date machines.
func (c *ControlPlane) NextFailureDomainForScaleUp(ctx context.Context) *string {
if len(c.Cluster.Status.FailureDomains.FilterControlPlane()) == 0 {
return nil
}
return capifd.PickFewest(ctx, c.FailureDomains().FilterControlPlane(), c.Machines, c.UpToDateMachines())
}
// InitialControlPlaneConfig returns a new RKE2ConfigSpec that is to be used for an initializing control plane.
func (c *ControlPlane) InitialControlPlaneConfig() *bootstrapv1.RKE2ConfigSpec {
bootstrapSpec := c.RCP.Spec.RKE2ConfigSpec.DeepCopy()
return bootstrapSpec
}
// JoinControlPlaneConfig returns a new RKE2ConfigSpec that is to be used for joining control planes.
func (c *ControlPlane) JoinControlPlaneConfig() *bootstrapv1.RKE2ConfigSpec {
bootstrapSpec := c.RCP.Spec.RKE2ConfigSpec.DeepCopy()
return bootstrapSpec
}
// GenerateRKE2Config generates a new RKE2 config for creating new control plane nodes.
func (c *ControlPlane) GenerateRKE2Config(spec *bootstrapv1.RKE2ConfigSpec) *bootstrapv1.RKE2Config {
// Create an owner reference without a controller reference because the owning controller is the machine controller
owner := metav1.OwnerReference{
APIVersion: controlplanev1.GroupVersion.String(),
Kind: "RKE2ControlPlane",
Name: c.RCP.Name,
UID: c.RCP.UID,
}
bootstrapConfig := &bootstrapv1.RKE2Config{
ObjectMeta: metav1.ObjectMeta{
Name: names.SimpleNameGenerator.GenerateName(c.RCP.Name + "-"),
Namespace: c.RCP.Namespace,
Labels: ControlPlaneLabelsForCluster(c.Cluster.Name),
OwnerReferences: []metav1.OwnerReference{
owner,
},
},
Spec: *spec,
}
return bootstrapConfig
}
// ControlPlaneLabelsForCluster returns a set of labels to add to a control plane machine for this specific cluster.
func ControlPlaneLabelsForCluster(clusterName string) map[string]string {
return map[string]string{
clusterv1.ClusterNameLabel: clusterName,
clusterv1.MachineControlPlaneLabel: "",
}
}
// NewMachine returns a machine configured to be a part of the control plane.
func (c *ControlPlane) NewMachine(infraRef, bootstrapRef *corev1.ObjectReference, failureDomain *string) *clusterv1.Machine {
return &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: names.SimpleNameGenerator.GenerateName(c.RCP.Name + "-"),
Namespace: c.RCP.Namespace,
Labels: ControlPlaneLabelsForCluster(c.Cluster.Name),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(c.RCP, controlplanev1.GroupVersion.WithKind("RKE2ControlPlane")),
},
},
Spec: clusterv1.MachineSpec{
ClusterName: c.Cluster.Name,
Version: c.Version(),
InfrastructureRef: *infraRef,
Bootstrap: clusterv1.Bootstrap{
ConfigRef: bootstrapRef,
},
FailureDomain: failureDomain,
},
}
}
// NeedsReplacementNode determines if the control plane needs to create a replacement node during upgrade.
func (c *ControlPlane) NeedsReplacementNode() bool {
// Can't do anything with an unknown number of desired replicas.
if c.RCP.Spec.Replicas == nil {
return false
}
// if the number of existing machines is exactly 1 > than the number of replicas.
return len(c.Machines)+1 == int(*c.RCP.Spec.Replicas)
}
// HasDeletingMachine returns true if any machine in the control plane is in the process of being deleted.
func (c *ControlPlane) HasDeletingMachine() bool {
return len(c.Machines.Filter(collections.HasDeletionTimestamp)) > 0
}
// DeletingMachines returns machines in the control plane that are in the process of being deleted.
func (c *ControlPlane) DeletingMachines() collections.Machines {
return c.Machines.Filter(collections.HasDeletionTimestamp)
}
// SortedByDeletionTimestamp returns the machines sorted by deletion timestamp.
func (c *ControlPlane) SortedByDeletionTimestamp(s collections.Machines) []*clusterv1.Machine {
res := make(machinesByDeletionTimestamp, 0, len(s))
for _, value := range s {
res = append(res, value)
}
sort.Sort(res)
return res
}
// MachinesNeedingRollout return a list of machines that need to be rolled out.
func (c *ControlPlane) MachinesNeedingRollout() collections.Machines {
// Ignore machines to be deleted.
machines := c.Machines.Filter(collections.Not(collections.HasDeletionTimestamp))
// Return machines if they are scheduled for rollout or if with an outdated configuration.
return machines.AnyFilter(
// Machines that do not match with RCP config.
collections.Not(matchesRCPConfiguration(c.infraResources, c.rke2Configs, c.RCP)),
)
}
// UpToDateMachines returns the machines that are up to date with the control
// plane's configuration and therefore do not require rollout.
func (c *ControlPlane) UpToDateMachines() collections.Machines {
return c.Machines.Difference(c.MachinesNeedingRollout())
}
// getInfraResources fetches the external infrastructure resource for each machine in the collection
// and returns a map of machine.Name -> infraResource.
func getInfraResources(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) {
result := map[string]*unstructured.Unstructured{}
for _, m := range machines {
infraObj, err := external.Get(ctx, cl, &m.Spec.InfrastructureRef)
if err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
continue
}
return nil, errors.Wrapf(err, "failed to retrieve infra obj for machine %q", m.Name)
}
result[m.Name] = infraObj
}
return result, nil
}
// getRKE2Configs fetches the RKE2 config for each machine in the collection and returns a map of machine.Name -> RKE2Config.
func getRKE2Configs(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*bootstrapv1.RKE2Config, error) {
result := map[string]*bootstrapv1.RKE2Config{}
for name, m := range machines {
bootstrapRef := m.Spec.Bootstrap.ConfigRef
if bootstrapRef == nil {
continue
}
machineConfig := &bootstrapv1.RKE2Config{}
if err := cl.Get(ctx, client.ObjectKey{Name: bootstrapRef.Name, Namespace: m.Namespace}, machineConfig); err != nil {
if apierrors.IsNotFound(errors.Cause(err)) {
continue
}
if m.Status.NodeRef != nil {
name = m.Status.NodeRef.Name
}
return nil, errors.Wrapf(err, "failed to retrieve bootstrap config for machine %q with node %s", m.Name, name)
}
result[name] = machineConfig
}
return result, nil
}
// UnhealthyMachines returns the list of control plane machines marked as unhealthy by MHC.
func (c *ControlPlane) UnhealthyMachines() collections.Machines {
return c.Machines.Filter(collections.IsUnhealthy)
}
// HealthyMachines returns the list of control plane machines not marked as unhealthy by MHC.
func (c *ControlPlane) HealthyMachines() collections.Machines {
return c.Machines.Filter(collections.Not(collections.IsUnhealthy))
}
// HasUnhealthyMachine returns true if any machine in the control plane is marked as unhealthy by MHC.
func (c *ControlPlane) HasUnhealthyMachine() bool {
return len(c.UnhealthyMachines()) > 0
}
// PatchMachines patches the machines in the control plane.
func (c *ControlPlane) PatchMachines(ctx context.Context) error {
errList := []error{}
for name := range c.Machines {
machine := c.Machines[name]
if helper, ok := c.machinesPatchHelpers[name]; ok {
if err := helper.Patch(ctx, machine, patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
controlplanev1.MachineAgentHealthyCondition,
controlplanev1.MachineEtcdMemberHealthyCondition,
controlplanev1.NodeMetadataUpToDate,
}}); err != nil {
if machine.Status.NodeRef != nil {
_ = machine.Status.NodeRef.Name
}
errList = append(errList, err)
}
continue
}
if machine.Status.NodeRef != nil {
name = machine.Status.NodeRef.Name
}
errList = append(errList, errors.Errorf("failed to get patch helper for machine %s with node %s", machine.Name, name))
}
return kerrors.NewAggregate(errList)
}
// machinesByDeletionTimestamp sorts a list of Machines by deletion timestamp, using their names as a tie breaker.
// Machines without DeletionTimestamp go after machines with this field set.
type machinesByDeletionTimestamp []*clusterv1.Machine
func (o machinesByDeletionTimestamp) Len() int { return len(o) }
func (o machinesByDeletionTimestamp) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o machinesByDeletionTimestamp) Less(i, j int) bool {
if o[i].DeletionTimestamp == nil && o[j].DeletionTimestamp == nil {
return o[i].Name < o[j].Name
}
if o[i].DeletionTimestamp == nil {
return false
}
if o[j].DeletionTimestamp == nil {
return true
}
if o[i].DeletionTimestamp.Equal(o[j].DeletionTimestamp) {
return o[i].Name < o[j].Name
}
return o[i].DeletionTimestamp.Before(o[j].DeletionTimestamp)
}