forked from kubernetes-sigs/cluster-api
/
machine_controller.go
307 lines (253 loc) · 10.5 KB
/
machine_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
/*
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 machine
import (
"context"
"os"
"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/runtime"
"k8s.io/klog"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
controllerError "sigs.k8s.io/cluster-api/pkg/controller/error"
"sigs.k8s.io/cluster-api/pkg/controller/remote"
"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"
)
const (
NodeNameEnvVar = "NODE_NAME"
)
var DefaultActuator Actuator
func AddWithActuator(mgr manager.Manager, actuator Actuator) error {
return add(mgr, newReconciler(mgr, actuator))
}
// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager, actuator Actuator) reconcile.Reconciler {
r := &ReconcileMachine{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
nodeName: os.Getenv(NodeNameEnvVar),
actuator: actuator,
}
if r.nodeName == "" {
klog.Warningf("Environment variable %q is not set, this controller will not protect against deleting its own machine", NodeNameEnvVar)
}
return r
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
// Create a new controller
c, err := controller.New("machine-controller", mgr, controller.Options{Reconciler: r})
if err != nil {
return err
}
// Watch for changes to Machine
return c.Watch(
&source.Kind{Type: &clusterv1.Machine{}},
&handler.EnqueueRequestForObject{},
)
}
// ReconcileMachine reconciles a Machine object
type ReconcileMachine struct {
client.Client
scheme *runtime.Scheme
actuator Actuator
// nodeName is the name of the node on which the machine controller is running, if not present, it is loaded from NODE_NAME.
nodeName string
}
// Reconcile reads that state of the cluster for a Machine object and makes changes based on the state read
// and what is in the Machine.Spec
// +kubebuilder:rbac:groups=cluster.k8s.io,resources=machines;machines/status,verbs=get;list;watch;create;update;patch;delete
func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// TODO(mvladev): Can context be passed from Kubebuilder?
ctx := context.TODO()
// Fetch the Machine instance
m := &clusterv1.Machine{}
if err := r.Client.Get(ctx, request.NamespacedName, m); 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
}
// Implement controller logic here
name := m.Name
klog.Infof("Reconciling Machine %q", name)
// Cluster might be nil as some providers might not require a cluster object
// for machine management.
cluster, err := r.getCluster(ctx, m)
if err != nil {
return reconcile.Result{}, err
}
// Set the ownerRef with foreground deletion if there is a linked cluster.
if cluster != nil && len(m.OwnerReferences) == 0 {
blockOwnerDeletion := true
m.OwnerReferences = append(m.OwnerReferences, metav1.OwnerReference{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
BlockOwnerDeletion: &blockOwnerDeletion,
})
}
// If object hasn't been deleted and doesn't have a finalizer, add one
// Add a finalizer to newly created objects.
if m.ObjectMeta.DeletionTimestamp.IsZero() {
finalizerCount := len(m.Finalizers)
if cluster != nil && !util.Contains(m.Finalizers, metav1.FinalizerDeleteDependents) {
m.Finalizers = append(m.ObjectMeta.Finalizers, metav1.FinalizerDeleteDependents)
}
if !util.Contains(m.Finalizers, clusterv1.MachineFinalizer) {
m.Finalizers = append(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer)
}
if len(m.Finalizers) > finalizerCount {
if err := r.Client.Update(ctx, m); err != nil {
klog.Infof("Failed to add finalizers to machine %q: %v", 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
}
}
if !m.ObjectMeta.DeletionTimestamp.IsZero() {
// no-op if finalizer has been removed.
if !util.Contains(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer) {
klog.Infof("Reconciling machine %q causes a no-op as there is no finalizer", name)
return reconcile.Result{}, nil
}
if !r.isDeleteAllowed(m) {
klog.Infof("Deleting machine hosting this controller is not allowed. Skipping reconciliation of machine %q", name)
return reconcile.Result{}, nil
}
klog.Infof("Reconciling machine %q triggers delete", name)
if err := r.actuator.Delete(ctx, cluster, m); err != nil {
if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok {
klog.Infof("Actuator returned requeue-after error: %v", requeueErr)
return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil
}
klog.Errorf("Failed to delete machine %q: %v", name, err)
return reconcile.Result{}, err
}
if m.Status.NodeRef != nil {
klog.Infof("Deleting node %q for machine %q", m.Status.NodeRef.Name, m.Name)
if err := r.deleteNode(ctx, cluster, m.Status.NodeRef.Name); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("Error deleting node %q for machine %q: %v", m.Status.NodeRef.Name, name, err)
return reconcile.Result{}, err
}
}
// Remove finalizer on successful deletion.
m.ObjectMeta.Finalizers = util.Filter(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer)
if err := r.Client.Update(context.Background(), m); err != nil {
klog.Errorf("Failed to remove finalizer from machine %q: %v", name, err)
return reconcile.Result{}, err
}
klog.Infof("Machine %q deletion successful", name)
return reconcile.Result{}, nil
}
exist, err := r.actuator.Exists(ctx, cluster, m)
if err != nil {
klog.Errorf("Failed to check if machine %q exists: %v", name, err)
return reconcile.Result{}, err
}
if exist {
klog.Infof("Reconciling machine %q triggers idempotent update", name)
if err := r.actuator.Update(ctx, cluster, m); err != nil {
if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok {
klog.Infof("Actuator returned requeue-after error: %v", requeueErr)
return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil
}
klog.Errorf(`Error updating machine "%s/%s": %v`, m.Namespace, name, err)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
// Machine resource created. Machine does not yet exist.
klog.Infof("Reconciling machine object %v triggers idempotent create.", m.ObjectMeta.Name)
if err := r.actuator.Create(ctx, cluster, m); err != nil {
if requeueErr, ok := errors.Cause(err).(controllerError.HasRequeueAfterError); ok {
klog.Infof("Actuator returned requeue-after error: %v", requeueErr)
return reconcile.Result{Requeue: true, RequeueAfter: requeueErr.GetRequeueAfter()}, nil
}
klog.Warningf("Failed to create machine %q: %v", name, err)
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Machine) (*clusterv1.Cluster, error) {
if machine.Labels[clusterv1.MachineClusterLabelName] == "" {
klog.Infof("Machine %q in namespace %q doesn't specify %q label, assuming nil cluster", machine.Name, machine.Namespace, clusterv1.MachineClusterLabelName)
return nil, nil
}
cluster := &clusterv1.Cluster{}
key := client.ObjectKey{
Namespace: machine.Namespace,
Name: machine.Labels[clusterv1.MachineClusterLabelName],
}
if err := r.Client.Get(ctx, key, cluster); err != nil {
return nil, err
}
return cluster, nil
}
// isDeletedAllowed returns false if the Machine we're trying to delete is the
// Machine hosting this controller. This method is meant to be functional
// only when the controllers are running in the workload cluster.
func (r *ReconcileMachine) isDeleteAllowed(machine *clusterv1.Machine) bool {
if r.nodeName == "" || machine.Status.NodeRef == nil {
return true
}
if machine.Status.NodeRef.Name != r.nodeName {
return true
}
node := &corev1.Node{}
if err := r.Client.Get(context.Background(), client.ObjectKey{Name: r.nodeName}, node); err != nil {
klog.Infof("Failed to determine if controller's node %q is associated with machine %q: %v", r.nodeName, machine.Name, err)
return true
}
// When the UID of the machine's node reference and this controller's actual node match then then the request is to
// delete the machine this machine-controller is running on. Return false to not allow machine controller to delete its
// own machine.
return node.UID != machine.Status.NodeRef.UID
}
func (r *ReconcileMachine) deleteNode(ctx context.Context, cluster *clusterv1.Cluster, name string) error {
if cluster == nil {
// Try to retrieve the Node from the local cluster, if no Cluster reference is found.
var node corev1.Node
if err := r.Client.Get(ctx, client.ObjectKey{Name: name}, &node); err != nil {
return err
}
return r.Client.Delete(ctx, &node)
}
// Otherwise, proceed to get the remote cluster client and get the Node.
remoteClient, err := remote.NewClusterClient(r.Client, cluster)
if err != nil {
klog.Errorf("Error creating a remote client for cluster %q while deleting Machine %q, won't retry: %v",
cluster.Name, name, err)
return nil
}
corev1Remote, err := remoteClient.CoreV1()
if err != nil {
klog.Errorf("Error creating a remote client for cluster %q while deleting Machine %q, won't retry: %v",
cluster.Name, name, err)
return nil
}
return corev1Remote.Nodes().Delete(name, &metav1.DeleteOptions{})
}