/
machine_deleter.go
315 lines (274 loc) · 9.84 KB
/
machine_deleter.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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package deletion
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
v1clientset "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
platformv1 "tkestack.io/tke/api/platform/v1"
v1 "tkestack.io/tke/api/platform/v1"
machineprovider "tkestack.io/tke/pkg/platform/provider/machine"
typesv1 "tkestack.io/tke/pkg/platform/types/v1"
"tkestack.io/tke/pkg/util/log"
)
// MachineDeleterInterface to delete a machine with all resources in it.
type MachineDeleterInterface interface {
Delete(ctx context.Context, name string) error
}
// NewMachineDeleter creates the machineeleter object and returns it.
func NewMachineDeleter(machineClient v1clientset.MachineInterface,
platformClient v1clientset.PlatformV1Interface,
finalizerToken v1.FinalizerName,
deleteWhenDone bool) MachineDeleterInterface {
d := &machineDeleter{
machineClient: machineClient,
platformClient: platformClient,
deleteWhenDone: deleteWhenDone,
finalizerToken: finalizerToken,
}
return d
}
var _ MachineDeleterInterface = &machineDeleter{}
// machineDeleter is used to delete all resources in a given machine.
type machineDeleter struct {
// Client to manipulate the machine.
machineClient v1clientset.MachineInterface
platformClient v1clientset.PlatformV1Interface
// The finalizer token that should be removed from the machine
// when all resources in that machine have been deleted.
finalizerToken v1.FinalizerName
// Also delete the machine when all resources in the machine have been deleted.
deleteWhenDone bool
}
// Delete deletes all resources in the given machine.
// Before deleting resources:
// * It ensures that deletion timestamp is set on the
// machine (does nothing if deletion timestamp is missing).
// * Verifies that the machine is in the "terminating" phase
// (updates the machine phase if it is not yet marked terminating)
// After deleting the resources:
// * It removes finalizer token from the given machine.
// * Deletes the machine if deleteWhenDone is true.
//
// Returns an error if any of those steps fail.
// Returns ResourcesRemainingError if it deleted some resources but needs
// to wait for them to go away.
// Caller is expected to keep calling this until it succeeds.
func (d *machineDeleter) Delete(ctx context.Context, name string) error {
ctx = log.FromContext(ctx).WithName("Delete").WithContext(ctx)
// Multiple controllers may edit a machine during termination
// first get the latest state of the machine before proceeding
// if the machine was deleted already, don't do anything
machine, err := d.machineClient.Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
if machine.DeletionTimestamp == nil {
return nil
}
// ensure that the status is up to date on the machine
// if we get a not found error, we assume the machine is truly gone
machine, err = d.retryOnConflictError(machine, d.updateMachineStatusFunc)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}
// the latest view of the machine asserts that machine is no longer deleting..
if machine.DeletionTimestamp.IsZero() {
return nil
}
// Delete the machine if it is already finalized.
if d.deleteWhenDone && finalized(machine) {
return d.deleteMachine(machine)
}
// there may still be content for us to remove
err = d.deleteAllContent(ctx, machine)
if err != nil {
return err
}
// we have removed content, so mark it finalized by us
machine, err = d.retryOnConflictError(machine, d.finalizeMachine)
if err != nil {
// in normal practice, this should not be possible, but if a deployment is running
// two controllers to do machine deletion that share a common finalizer token it's
// possible that a not found could occur since the other controller would have finished the delete.
if errors.IsNotFound(err) {
return nil
}
return err
}
// Check if we can delete now.
if d.deleteWhenDone && finalized(machine) {
return d.deleteMachine(machine)
}
return nil
}
// Deletes the given machine.
func (d *machineDeleter) deleteMachine(machine *v1.Machine) error {
var opts metav1.DeleteOptions
uid := machine.UID
if len(uid) > 0 {
opts = metav1.DeleteOptions{Preconditions: &metav1.Preconditions{UID: &uid}}
}
err := d.machineClient.Delete(context.Background(), machine.Name, opts)
if err != nil && !errors.IsNotFound(err) {
return err
}
return nil
}
// updateMachineFunc is a function that makes an update to a namespace
type updateMachineFunc func(machine *v1.Machine) (*v1.Machine, error)
// retryOnConflictError retries the specified fn if there was a conflict error
// it will return an error if the UID for an object changes across retry operations.
// TODO RetryOnConflict should be a generic concept in client code
func (d *machineDeleter) retryOnConflictError(machine *v1.Machine, fn updateMachineFunc) (result *v1.Machine, err error) {
latestMachine := machine
for {
result, err = fn(latestMachine)
if err == nil {
return result, nil
}
if !errors.IsConflict(err) {
return nil, err
}
prevMachine := latestMachine
latestMachine, err = d.machineClient.Get(context.Background(), latestMachine.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
if prevMachine.UID != latestMachine.UID {
return nil, fmt.Errorf("machine uid has changed across retries")
}
}
}
// updateMachineStatusFunc will verify that the status of the machine is correct
func (d *machineDeleter) updateMachineStatusFunc(machine *v1.Machine) (*v1.Machine, error) {
if machine.DeletionTimestamp.IsZero() || machine.Status.Phase == v1.MachineTerminating {
return machine, nil
}
newMachine := v1.Machine{}
newMachine.ObjectMeta = machine.ObjectMeta
newMachine.Status = machine.Status
newMachine.Status.Phase = v1.MachineTerminating
return d.machineClient.UpdateStatus(context.Background(), &newMachine, metav1.UpdateOptions{})
}
// finalized returns true if the machine.Spec.Finalizers is an empty list
func finalized(machine *v1.Machine) bool {
return len(machine.Spec.Finalizers) == 0
}
// finalizeMachine removes the specified finalizerToken and finalizes the machine
func (d *machineDeleter) finalizeMachine(machine *v1.Machine) (*v1.Machine, error) {
machineFinalize := v1.Machine{}
machineFinalize.ObjectMeta = machine.ObjectMeta
machineFinalize.Spec = machine.Spec
finalizerSet := sets.NewString()
for i := range machine.Spec.Finalizers {
if machine.Spec.Finalizers[i] != d.finalizerToken {
finalizerSet.Insert(string(machine.Spec.Finalizers[i]))
}
}
machineFinalize.Spec.Finalizers = make([]v1.FinalizerName, 0, len(finalizerSet))
for _, value := range finalizerSet.List() {
machineFinalize.Spec.Finalizers = append(machineFinalize.Spec.Finalizers, v1.FinalizerName(value))
}
machine = &v1.Machine{}
err := d.platformClient.RESTClient().Put().
Resource("machines").
Name(machineFinalize.Name).
SubResource("finalize").
Body(&machineFinalize).
Do(context.Background()).
Into(machine)
if err != nil {
// it was removed already, so life is good
if errors.IsNotFound(err) {
return machine, nil
}
}
return machine, err
}
type deleteResourceFunc func(ctx context.Context, deleter *machineDeleter, machine *v1.Machine) error
var deleteResourceFuncs = []deleteResourceFunc{
deleteMachineProvider,
deleteNode,
}
// deleteAllContent will use the client to delete each resource identified in machine.
func (d *machineDeleter) deleteAllContent(ctx context.Context, machine *v1.Machine) error {
log.FromContext(ctx).Info("deleteAllContent doing")
var errs []error
for _, deleteFunc := range deleteResourceFuncs {
err := deleteFunc(ctx, d, machine)
if err != nil {
// If there is an error, hold on to it but proceed with all the remaining resource.
errs = append(errs, err)
}
}
if len(errs) > 0 {
return utilerrors.NewAggregate(errs)
}
log.FromContext(ctx).Info("deleteAllContent done")
return nil
}
func deleteMachineProvider(ctx context.Context, deleter *machineDeleter, machine *v1.Machine) error {
log.FromContext(ctx).Info("deleteMachineProvider doing")
provider, err := machineprovider.GetProvider(machine.Spec.Type)
if err != nil {
panic(err)
}
cluster, err := typesv1.GetClusterByName(context.Background(), deleter.platformClient, machine.Spec.ClusterName)
if err != nil {
return err
}
err = provider.OnDelete(ctx, machine, cluster)
if err != nil {
return err
}
log.FromContext(ctx).Info("deleteMachineProvider done")
return nil
}
func deleteNode(ctx context.Context, deleter *machineDeleter, machine *v1.Machine) error {
log.FromContext(ctx).Info("deleteNode doing")
cluster, err := typesv1.GetClusterByName(context.Background(), deleter.platformClient, machine.Spec.ClusterName)
if err != nil {
return err
}
if cluster.Status.Phase == platformv1.ClusterTerminating {
return nil
}
clientset, err := cluster.Clientset()
if err != nil {
return err
}
err = clientset.CoreV1().Nodes().Delete(context.Background(), machine.Spec.IP, metav1.DeleteOptions{})
if err != nil {
if !errors.IsNotFound(err) {
return err
}
}
log.FromContext(ctx).Info("deleteNode done")
return nil
}