forked from kubernetes-sigs/cluster-api
/
machinedeployment_controller.go
414 lines (344 loc) · 14.5 KB
/
machinedeployment_controller.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
/*
Copyright 2018 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 machinedeployment
import (
"context"
"reflect"
"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/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/util"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
// controllerName is the name of this controller
const controllerName = "machinedeployment-controller"
var (
// controllerKind contains the schema.GroupVersionKind for this controller type.
controllerKind = v1alpha1.SchemeGroupVersion.WithKind("MachineDeployment")
)
// ReconcileMachineDeployment reconciles a MachineDeployment object.
type ReconcileMachineDeployment struct {
client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
}
// newReconciler returns a new reconcile.Reconciler.
func newReconciler(mgr manager.Manager) *ReconcileMachineDeployment {
return &ReconcileMachineDeployment{Client: mgr.GetClient(), scheme: mgr.GetScheme(), recorder: mgr.GetRecorder(controllerName)}
}
// Add creates a new MachineDeployment Controller and adds it to the Manager with default RBAC.
func Add(mgr manager.Manager) error {
r := newReconciler(mgr)
return add(mgr, newReconciler(mgr), r.MachineSetToDeployments)
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler.
func add(mgr manager.Manager, r reconcile.Reconciler, mapFn handler.ToRequestsFunc) error {
// Create a new controller.
c, err := controller.New(controllerName, mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to MachineDeployment.
err = c.Watch(&source.Kind{
Type: &v1alpha1.MachineDeployment{}},
&handler.EnqueueRequestForObject{},
)
if err != nil {
return err
}
// Watch for changes to MachineSet and reconcile the owner MachineDeployment.
err = c.Watch(
&source.Kind{Type: &v1alpha1.MachineSet{}},
&handler.EnqueueRequestForOwner{OwnerType: &v1alpha1.MachineDeployment{}, IsController: true},
)
if err != nil {
return err
}
// Watch for changes to MachineSets using a mapping function to MachineDeployment.
// This watcher is required for use cases like adoption. In case a MachineSet doesn't have
// a controller reference, it'll look for potential matching MachineDeployments to reconcile.
err = c.Watch(
&source.Kind{Type: &v1alpha1.MachineSet{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: mapFn},
)
if err != nil {
return err
}
return nil
}
// Reconcile reads that state of the cluster for a MachineDeployment object and makes changes based on the state read
// and what is in the MachineDeployment.Spec.
//
// +kubebuilder:rbac:groups=cluster.k8s.io,resources=machinedeployments;machinedeployments/status,verbs=get;list;watch;create;update;patch;delete
func (r *ReconcileMachineDeployment) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// Fetch the MachineDeployment instance
d := &v1alpha1.MachineDeployment{}
ctx := context.TODO()
if err := r.Get(context.TODO(), request.NamespacedName, d); err != nil {
if apierrors.IsNotFound(err) {
// Object not found, return. Created objects are automatically garbage collected.
// For additional cleanup logic use finalizers.
return reconcile.Result{}, nil
}
// Error reading the object - requeue the request.
return reconcile.Result{}, err
}
// Ignore deleted MachineDeployments, this can happen when foregroundDeletion
// is enabled
if d.DeletionTimestamp != nil {
return reconcile.Result{}, nil
}
result, err := r.reconcile(ctx, d)
if err != nil {
klog.Errorf("Failed to reconcile MachineDeployment %q: %v", request.NamespacedName, err)
r.recorder.Eventf(d, corev1.EventTypeWarning, "ReconcileError", "%v", err)
}
return result, err
}
func (r *ReconcileMachineDeployment) reconcile(ctx context.Context, d *v1alpha1.MachineDeployment) (reconcile.Result, error) {
v1alpha1.PopulateDefaultsMachineDeployment(d)
everything := metav1.LabelSelector{}
if reflect.DeepEqual(d.Spec.Selector, &everything) {
if d.Status.ObservedGeneration < d.Generation {
d.Status.ObservedGeneration = d.Generation
if err := r.Status().Update(context.Background(), d); err != nil {
klog.Warningf("Failed to update status for MachineDeployment %q: %v", d.Name, err)
return reconcile.Result{}, err
}
}
return reconcile.Result{Requeue: true}, nil
}
// Make sure that label selector can match the template's labels.
// TODO(vincepri): Move to a validation (admission) webhook when supported.
selector, err := metav1.LabelSelectorAsSelector(&d.Spec.Selector)
if err != nil {
return reconcile.Result{}, errors.Wrapf(err, "failed to parse MachineDeployment %q label selector", d.Name)
}
if !selector.Matches(labels.Set(d.Spec.Template.Labels)) {
return reconcile.Result{}, errors.Errorf("failed validation on MachineDeployment %q label selector, cannot match Machine template labels", d.Name)
}
// Cluster might be nil as some providers might not require a cluster object
// for machine management.
cluster, err := r.getCluster(d)
if err != nil {
return reconcile.Result{}, err
}
// Set the ownerRef with foreground deletion if there is a linked cluster.
if cluster != nil && len(d.OwnerReferences) == 0 {
blockOwnerDeletion := true
d.OwnerReferences = append(d.OwnerReferences, metav1.OwnerReference{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
BlockOwnerDeletion: &blockOwnerDeletion,
})
}
// Add foregroundDeletion finalizer if MachineDeployment isn't deleted and linked to a cluster.
if cluster != nil &&
d.ObjectMeta.DeletionTimestamp.IsZero() &&
!util.Contains(d.Finalizers, metav1.FinalizerDeleteDependents) {
d.Finalizers = append(d.ObjectMeta.Finalizers, metav1.FinalizerDeleteDependents)
if err := r.Client.Update(context.Background(), d); err != nil {
klog.Infof("Failed to add finalizers to MachineSet %q: %v", d.Name, err)
return reconcile.Result{}, err
}
// Since adding the finalizer updates the object return to avoid later update issues
return reconcile.Result{Requeue: true}, nil
}
msList, err := r.getMachineSetsForDeployment(d)
if err != nil {
return reconcile.Result{}, err
}
machineMap, err := r.getMachineMapForDeployment(d, msList)
if err != nil {
return reconcile.Result{}, err
}
if d.Spec.Paused {
return reconcile.Result{}, r.sync(d, msList, machineMap)
}
switch d.Spec.Strategy.Type {
case common.RollingUpdateMachineDeploymentStrategyType:
return reconcile.Result{}, r.rolloutRolling(d, msList, machineMap)
}
return reconcile.Result{}, errors.Errorf("unexpected deployment strategy type: %s", d.Spec.Strategy.Type)
}
// getCluster reuturns the Cluster associated with the MachineDeployment, if any.
func (r *ReconcileMachineDeployment) getCluster(d *v1alpha1.MachineDeployment) (*v1alpha1.Cluster, error) {
if d.Spec.Template.Labels[v1alpha1.MachineClusterLabelName] == "" {
klog.Infof("Deployment %q in namespace %q doesn't specify %q label, assuming nil cluster", d.Name, d.Namespace, v1alpha1.MachineClusterLabelName)
return nil, nil
}
cluster := &v1alpha1.Cluster{}
key := client.ObjectKey{
Namespace: d.Namespace,
Name: d.Spec.Template.Labels[v1alpha1.MachineClusterLabelName],
}
if err := r.Client.Get(context.Background(), key, cluster); err != nil {
return nil, err
}
return cluster, nil
}
// getMachineSetsForDeployment returns a list of MachineSets associated with a MachineDeployment.
func (r *ReconcileMachineDeployment) getMachineSetsForDeployment(d *v1alpha1.MachineDeployment) ([]*v1alpha1.MachineSet, error) {
// List all MachineSets to find those we own but that no longer match our selector.
machineSets := &v1alpha1.MachineSetList{}
listOptions := &client.ListOptions{Namespace: d.Namespace}
if err := r.Client.List(context.Background(), listOptions, machineSets); err != nil {
return nil, err
}
filtered := make([]*v1alpha1.MachineSet, 0, len(machineSets.Items))
for idx := range machineSets.Items {
ms := &machineSets.Items[idx]
selector, err := metav1.LabelSelectorAsSelector(&d.Spec.Selector)
if err != nil {
klog.Errorf("Skipping MachineSet %q, failed to get label selector from spec selector: %v", ms.Name, err)
continue
}
// If a MachineDeployment with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() {
klog.Warningf("Skipping MachineSet %q as the selector is empty", ms.Name)
continue
}
if !selector.Matches(labels.Set(ms.Labels)) {
klog.V(4).Infof("Skipping MachineSet %v, label mismatch", ms.Name)
continue
}
// Attempt to adopt machine if it meets previous conditions and it has no controller references.
if metav1.GetControllerOf(ms) == nil {
if err := r.adoptOrphan(d, ms); err != nil {
r.recorder.Eventf(d, corev1.EventTypeWarning, "FailedAdopt", "Failed to adopt MachineSet %q: %v", ms.Name, err)
klog.Warningf("Failed to adopt MachineSet %q into MachineDeployment %q: %v", ms.Name, d.Name, err)
continue
}
r.recorder.Eventf(d, corev1.EventTypeNormal, "SuccessfulAdopt", "Adopted MachineSet %q", ms.Name)
}
if !metav1.IsControlledBy(ms, d) {
continue
}
filtered = append(filtered, ms)
}
return filtered, nil
}
// adoptOrphan sets the MachineDeployment as a controller OwnerReference to the MachineSet.
func (r *ReconcileMachineDeployment) adoptOrphan(deployment *v1alpha1.MachineDeployment, machineSet *v1alpha1.MachineSet) error {
newRef := *metav1.NewControllerRef(deployment, controllerKind)
machineSet.OwnerReferences = append(machineSet.OwnerReferences, newRef)
return r.Client.Update(context.Background(), machineSet)
}
// getMachineMapForDeployment returns the Machines managed by a Deployment.
//
// It returns a map from MachineSet UID to a list of Machines controlled by that MachineSet,
// according to the Machine's ControllerRef.
func (r *ReconcileMachineDeployment) getMachineMapForDeployment(d *v1alpha1.MachineDeployment, msList []*v1alpha1.MachineSet) (map[types.UID]*v1alpha1.MachineList, error) {
// TODO(droot): double check if previous selector maps correctly to new one.
// _, err := metav1.LabelSelectorAsSelector(&d.Spec.Selector)
// Get all Machines that potentially belong to this Deployment.
selector, err := metav1.LabelSelectorAsMap(&d.Spec.Selector)
if err != nil {
return nil, err
}
machines := &v1alpha1.MachineList{}
listOptions := &client.ListOptions{Namespace: d.Namespace}
if err = r.Client.List(context.Background(), listOptions.MatchingLabels(selector), machines); err != nil {
return nil, err
}
// Group Machines by their controller (if it's in msList).
machineMap := make(map[types.UID]*v1alpha1.MachineList, len(msList))
for _, ms := range msList {
machineMap[ms.UID] = &v1alpha1.MachineList{}
}
for idx := range machines.Items {
machine := &machines.Items[idx]
// Do not ignore inactive Machines because Recreate Deployments need to verify that no
// Machines from older versions are running before spinning up new Machines.
controllerRef := metav1.GetControllerOf(machine)
if controllerRef == nil {
continue
}
// Only append if we care about this UID.
if machineList, ok := machineMap[controllerRef.UID]; ok {
machineList.Items = append(machineList.Items, *machine)
}
}
return machineMap, nil
}
// getMachineDeploymentsForMachineSet returns a list of MachineDeployments that could potentially match a MachineSet.
func (r *ReconcileMachineDeployment) getMachineDeploymentsForMachineSet(ms *v1alpha1.MachineSet) []*v1alpha1.MachineDeployment {
if len(ms.Labels) == 0 {
klog.Warningf("No machine deployments found for MachineSet %q because it has no labels", ms.Name)
return nil
}
dList := &v1alpha1.MachineDeploymentList{}
listOptions := &client.ListOptions{Namespace: ms.Namespace}
if err := r.Client.List(context.Background(), listOptions, dList); err != nil {
klog.Warningf("Failed to list machine deployments: %v", err)
return nil
}
deployments := make([]*v1alpha1.MachineDeployment, 0, len(dList.Items))
for idx, d := range dList.Items {
selector, err := metav1.LabelSelectorAsSelector(&d.Spec.Selector)
if err != nil {
continue
}
// If a deployment with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(ms.Labels)) {
continue
}
deployments = append(deployments, &dList.Items[idx])
}
return deployments
}
// MachineSetTodeployments is a handler.ToRequestsFunc to be used to enqeue requests for reconciliation
// for MachineDeployments that might adopt an orphaned MachineSet.
func (r *ReconcileMachineDeployment) MachineSetToDeployments(o handler.MapObject) []reconcile.Request {
result := []reconcile.Request{}
ms := &v1alpha1.MachineSet{}
key := client.ObjectKey{Namespace: o.Meta.GetNamespace(), Name: o.Meta.GetName()}
if err := r.Client.Get(context.Background(), key, ms); err != nil {
if !apierrors.IsNotFound(err) {
klog.Errorf("Unable to retrieve MachineSet %q for possible MachineDeployment adoption: %v", key, err)
}
return nil
}
// Check if the controller reference is already set and
// return an empty result when one is found.
for _, ref := range ms.ObjectMeta.OwnerReferences {
if ref.Controller != nil && *ref.Controller {
return result
}
}
mds := r.getMachineDeploymentsForMachineSet(ms)
if len(mds) == 0 {
klog.V(4).Infof("Found no machine set for machine: %v", ms.Name)
return nil
}
for _, md := range mds {
name := client.ObjectKey{Namespace: md.Namespace, Name: md.Name}
result = append(result, reconcile.Request{NamespacedName: name})
}
return result
}