forked from gardener/gardener
/
shoot_control_delete.go
392 lines (364 loc) · 18.4 KB
/
shoot_control_delete.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
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 shoot
import (
"time"
gardenv1beta1 "github.com/gardener/gardener/pkg/apis/garden/v1beta1"
"github.com/gardener/gardener/pkg/apis/garden/v1beta1/helper"
"github.com/gardener/gardener/pkg/operation"
botanistpkg "github.com/gardener/gardener/pkg/operation/botanist"
cloudbotanistpkg "github.com/gardener/gardener/pkg/operation/cloudbotanist"
"github.com/gardener/gardener/pkg/operation/common"
hybridbotanistpkg "github.com/gardener/gardener/pkg/operation/hybridbotanist"
"github.com/gardener/gardener/pkg/utils"
"github.com/gardener/gardener/pkg/utils/flow"
"github.com/gardener/gardener/pkg/utils/kubernetes"
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/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
)
// deleteShoot deletes a Shoot cluster entirely.
// It receives a Garden object <garden> which stores the Shoot object.
func (c *defaultControl) deleteShoot(o *operation.Operation) *gardenv1beta1.LastError {
// If the .status.uid field is empty, then we assume that there has never been any operation running for this Shoot
// cluster. This implies that there can not be any resource which we have to delete. We accept the deletion.
if len(o.Shoot.Info.Status.UID) == 0 {
o.Logger.Info("`.status.uid` is empty, assuming Shoot cluster did never exist. Deletion accepted.")
return nil
}
// We create botanists (which will do the actual work).
botanist, err := botanistpkg.New(o)
if err != nil {
return formatError("Failed to create a Botanist", err)
}
// We first check whether the namespace in the Seed cluster does exist - if it does not, then we assume that
// all resources have already been deleted. We can delete the Shoot resource as a consequence.
namespace, err := botanist.K8sSeedClient.GetNamespace(o.Shoot.SeedNamespace)
if apierrors.IsNotFound(err) {
o.Logger.Infof("Did not find '%s' namespace in the Seed cluster - nothing to be done", o.Shoot.SeedNamespace)
return nil
}
if err != nil {
return formatError("Failed to retrieve the Shoot namespace in the Seed cluster", err)
}
seedCloudBotanist, err := cloudbotanistpkg.New(o, common.CloudPurposeSeed)
if err != nil {
return formatError("Failed to create a Seed CloudBotanist", err)
}
shootCloudBotanist, err := cloudbotanistpkg.New(o, common.CloudPurposeShoot)
if err != nil {
return formatError("Failed to create a Shoot CloudBotanist", err)
}
hybridBotanist, err := hybridbotanistpkg.New(o, botanist, seedCloudBotanist, shootCloudBotanist)
if err != nil {
return formatError("Failed to create a HybridBotanist", err)
}
// We check whether the Shoot namespace in the Seed cluster is already in a terminating state, i.e. whether
// we have tried to delete it in a previous run. In that case, we do not need to cleanup Shoot resource because
// that would have already been done.
// We also check whether the kube-apiserver pod exists in the Shoot namespace within the Seed cluster. If it does not,
// then we assume that it has never been deployed successfully. We follow that no resources can have been deployed
// at all in the Shoot cluster, thus there is nothing to delete at all.
kubeAPIServerFound := false
podList, err := botanist.K8sSeedClient.ListPods(o.Shoot.SeedNamespace, metav1.ListOptions{
LabelSelector: "app=kubernetes,role=apiserver",
})
if err != nil {
return formatError("Failed to retrieve the list of pods running in the Shoot namespace in the Seed cluster", err)
}
for _, pod := range podList.Items {
if pod.DeletionTimestamp == nil {
kubeAPIServerFound = true
break
}
}
var (
nonTerminatingNamespace = namespace.Status.Phase != corev1.NamespaceTerminating
cleanupShootResources = nonTerminatingNamespace && kubeAPIServerFound
defaultInterval = 5 * time.Second
defaultTimeout = 30 * time.Second
isCloud = o.Shoot.Info.Spec.Cloud.Local == nil
g = flow.NewGraph("Shoot cluster deletion")
// We need to ensure that the deployed cloud provider secret is up-to-date. In case it has changed then we
// need to redeploy the cloud provider config (containing the secrets for some cloud providers) as well as
// restart the components using the secrets (cloud controller, controller manager). We also need to update all
// existing machine class secrets.
deployCloudProviderSecret = g.Add(flow.Task{
Name: "Deploying cloud provider account secret",
Fn: flow.SimpleTaskFn(botanist.DeployCloudProviderSecret).DoIf(cleanupShootResources),
})
refreshMachineClassSecrets = g.Add(flow.Task{
Name: "Refreshing machine class secrets",
Fn: flow.SimpleTaskFn(hybridBotanist.RefreshMachineClassSecrets).DoIf(cleanupShootResources).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(deployCloudProviderSecret),
})
refreshCloudProviderConfig = g.Add(flow.Task{
Name: "Refreshing cloud provider configuration",
Fn: flow.SimpleTaskFn(hybridBotanist.RefreshCloudProviderConfig).DoIf(cleanupShootResources).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(deployCloudProviderSecret),
})
refreshCloudControllerManager = g.Add(flow.Task{
Name: "Refreshing cloud controller manager checksums",
Fn: flow.SimpleTaskFn(botanist.RefreshCloudControllerManagerChecksums).DoIf(cleanupShootResources).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(deployCloudProviderSecret, refreshCloudProviderConfig),
})
refreshKubeControllerManager = g.Add(flow.Task{
Name: "Refreshing Kubernetes controller manager checksums",
Fn: flow.SimpleTaskFn(botanist.RefreshKubeControllerManagerChecksums).DoIf(cleanupShootResources).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(deployCloudProviderSecret, refreshCloudProviderConfig),
})
wakeUpControllers = g.Add(flow.Task{
Name: "Waking up kube-/cloud-/machine-controller-manager to ensure proper cleanup of resources",
Fn: flow.SimpleTaskFn(botanist.WakeUpControllers).DoIf(o.Shoot.IsHibernated && cleanupShootResources),
})
// Deletion of monitoring stack (to avoid false positive alerts) and kube-addon-manager (to avoid redeploying
// resources).
initializeShootClients = g.Add(flow.Task{
Name: "Initializing connection to Shoot",
Fn: flow.SimpleTaskFn(botanist.InitializeShootClients).DoIf(cleanupShootResources).RetryUntilTimeout(defaultInterval, 2*time.Minute),
Dependencies: flow.NewTaskIDs(deployCloudProviderSecret, refreshMachineClassSecrets, refreshCloudProviderConfig, refreshCloudControllerManager, refreshKubeControllerManager),
})
deleteSeedMonitoring = g.Add(flow.Task{
Name: "Deleting Shoot monitoring stack in Seed",
Fn: flow.SimpleTaskFn(botanist.DeleteSeedMonitoring).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(initializeShootClients),
})
deleteKubeAddonManager = g.Add(flow.Task{
Name: "Deleting Kubernetes addon manager",
Fn: flow.SimpleTaskFn(botanist.DeleteKubeAddonManager).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(initializeShootClients),
})
deleteClusterAutoscaler = g.Add(flow.Task{
Name: "Deleting cluster autoscaler",
Fn: flow.SimpleTaskFn(botanist.DeleteClusterAutoscaler).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(initializeShootClients),
})
waitUntilKubeAddonManagerDeleted = g.Add(flow.Task{
Name: "Waiting until Kubernetes addon manager has been deleted",
Fn: flow.SimpleTaskFn(botanist.WaitUntilKubeAddonManagerDeleted),
Dependencies: flow.NewTaskIDs(deleteKubeAddonManager),
})
waitForControllersToBeActive = g.Add(flow.Task{
Name: "Waiting until both cloud-controller-manager and kube-controller-manager are active",
Fn: flow.SimpleTaskFn(botanist.WaitForControllersToBeActive).DoIf(cleanupShootResources).RetryUntilTimeout(defaultInterval, defaultTimeout),
Dependencies: flow.NewTaskIDs(refreshKubeControllerManager, refreshCloudControllerManager, wakeUpControllers, waitUntilKubeAddonManagerDeleted),
})
// We need to clean up the cluster resources which may have footprints in the infrastructure (such as
// LoadBalancers, volumes, ...). We do that by deleting all namespaces other than the three standard
// namespaces which cannot be deleted (kube-system, default, kube-public). In those three namespaces
// we delete all CRDs, workload, services and PVCs.
// If the deletion of a CRD or an API service fails, then it is force deleted.
cleanCustomResourceDefinitions = g.Add(flow.Task{
Name: "Cleaning custom resource definitions",
Fn: flow.SimpleTaskFn(botanist.CleanCustomResourceDefinitions).RetryUntilTimeout(defaultInterval, 5*time.Minute).DoIf(cleanupShootResources).
RecoverTimeout(flow.SimpleTaskFn(botanist.ForceDeleteCustomResourceDefinitions).RetryUntilTimeout(defaultInterval, 2*time.Minute).ToRecoverFn()),
Dependencies: flow.NewTaskIDs(waitUntilKubeAddonManagerDeleted, deleteClusterAutoscaler, waitForControllersToBeActive),
})
cleanCustomAPIServices = g.Add(flow.Task{
Name: "Cleaning custom API service definitions",
Fn: flow.SimpleTaskFn(botanist.CleanupCustomAPIServices).RetryUntilTimeout(defaultInterval, 5*time.Minute).DoIf(cleanupShootResources).
RecoverTimeout(flow.SimpleTaskFn(botanist.ForceDeleteCustomAPIServices).RetryUntilTimeout(defaultInterval, 2*time.Minute).ToRecoverFn()),
Dependencies: flow.NewTaskIDs(waitUntilKubeAddonManagerDeleted, deleteClusterAutoscaler, waitForControllersToBeActive),
})
cleanKubernetesResources = g.Add(flow.Task{
Name: "Cleaning kubernetes resources",
Fn: flow.SimpleTaskFn(botanist.CleanKubernetesResources).RetryUntilTimeout(defaultInterval, 5*time.Minute).DoIf(cleanupShootResources),
Dependencies: flow.NewTaskIDs(cleanCustomResourceDefinitions, cleanCustomAPIServices),
})
destroyMachines = g.Add(flow.Task{
Name: "Destroying Shoot workers",
Fn: flow.SimpleTaskFn(hybridBotanist.DestroyMachines).RetryUntilTimeout(defaultInterval, defaultTimeout).DoIf(isCloud),
Dependencies: flow.NewTaskIDs(cleanKubernetesResources),
})
destroyNginxIngressResources = g.Add(flow.Task{
Name: "Destroying ingress DNS record",
Fn: flow.SimpleTaskFn(botanist.DestroyIngressDNSRecord),
Dependencies: flow.NewTaskIDs(cleanKubernetesResources),
})
destroyKube2IAMResources = g.Add(flow.Task{
Name: "Destroying Kube2IAM resources",
Fn: flow.SimpleTaskFn(shootCloudBotanist.DestroyKube2IAMResources),
Dependencies: flow.NewTaskIDs(cleanKubernetesResources),
})
destroyInfrastructure = g.Add(flow.Task{
Name: "Destroying Shoot infrastructure",
Fn: flow.SimpleTaskFn(shootCloudBotanist.DestroyInfrastructure),
Dependencies: flow.NewTaskIDs(cleanKubernetesResources, destroyMachines),
})
destroyExternalDomainDNSRecord = g.Add(flow.Task{
Name: "Destroying external domain DNS record",
Fn: flow.SimpleTaskFn(botanist.DestroyExternalDomainDNSRecord),
Dependencies: flow.NewTaskIDs(cleanKubernetesResources),
})
syncPointTerraformers = flow.NewTaskIDs(
deleteSeedMonitoring,
destroyNginxIngressResources,
destroyKube2IAMResources,
destroyInfrastructure,
destroyExternalDomainDNSRecord,
)
deleteKubeAPIServer = g.Add(flow.Task{
Name: "Deleting Kubernetes API server",
Fn: flow.SimpleTaskFn(botanist.DeleteKubeAPIServer).Retry(defaultInterval),
Dependencies: flow.NewTaskIDs(syncPointTerraformers),
})
deleteBackupInfrastructure = g.Add(flow.Task{
Name: "Deleting backup infrastructure",
Fn: flow.SimpleTaskFn(botanist.DeleteBackupInfrastructure),
Dependencies: flow.NewTaskIDs(deleteKubeAPIServer),
})
destroyInternalDomainDNSRecord = g.Add(flow.Task{
Name: "Destroying internal domain DNS record",
Fn: flow.SimpleTaskFn(botanist.DestroyInternalDomainDNSRecord),
Dependencies: flow.NewTaskIDs(syncPointTerraformers),
})
deleteNamespace = g.Add(flow.Task{
Name: "Deleting Shoot namespace in Seed",
Fn: flow.SimpleTaskFn(botanist.DeleteNamespace).Retry(defaultInterval),
Dependencies: flow.NewTaskIDs(syncPointTerraformers, destroyInternalDomainDNSRecord, deleteBackupInfrastructure, deleteKubeAPIServer),
})
_ = g.Add(flow.Task{
Name: "Waiting until Shoot namespace in Seed has been deleted",
Fn: flow.SimpleTaskFn(botanist.WaitUntilSeedNamespaceDeleted),
Dependencies: flow.NewTaskIDs(deleteNamespace),
})
_ = g.Add(flow.Task{
Name: "Deleting Garden secrets",
Fn: flow.SimpleTaskFn(botanist.DeleteGardenSecrets).Retry(defaultInterval),
Dependencies: flow.NewTaskIDs(deleteNamespace),
})
f = g.Compile()
)
err = f.Run(flow.Opts{
Logger: o.Logger,
ProgressReporter: o.ReportShootProgress,
})
if err != nil {
o.Logger.Errorf("Error deleting Shoot %q: %+v", o.Shoot.Info.Name, err)
return &gardenv1beta1.LastError{
Codes: helper.ExtractErrorCodes(flow.Causes(err)),
Description: helper.FormatLastErrDescription(err),
}
}
o.Logger.Infof("Successfully deleted Shoot %q", o.Shoot.Info.Name)
return nil
}
func (c *defaultControl) updateShootStatusDeleteStart(o *operation.Operation) error {
var (
status = o.Shoot.Info.Status
now = metav1.Now()
)
newShoot, err := kubernetes.TryUpdateShootStatus(c.k8sGardenClient.GardenClientset(), retry.DefaultRetry, o.Shoot.Info.ObjectMeta,
func(shoot *gardenv1beta1.Shoot) (*gardenv1beta1.Shoot, error) {
if status.RetryCycleStartTime == nil || (status.LastOperation != nil && status.LastOperation.Type != gardenv1beta1.ShootLastOperationTypeDelete) {
shoot.Status.RetryCycleStartTime = &now
}
if len(status.TechnicalID) == 0 {
shoot.Status.TechnicalID = o.Shoot.SeedNamespace
}
shoot.Status.Gardener = *o.GardenerInfo
shoot.Status.ObservedGeneration = o.Shoot.Info.Generation
shoot.Status.LastOperation = &gardenv1beta1.LastOperation{
Type: gardenv1beta1.ShootLastOperationTypeDelete,
State: gardenv1beta1.ShootLastOperationStateProcessing,
Progress: 1,
Description: "Deletion of Shoot cluster in progress.",
LastUpdateTime: now,
}
return shoot, nil
})
if err == nil {
o.Shoot.Info = newShoot
}
return err
}
func (c *defaultControl) updateShootStatusDeleteSuccess(o *operation.Operation) error {
newShoot, err := kubernetes.TryUpdateShootStatus(c.k8sGardenClient.GardenClientset(), retry.DefaultRetry, o.Shoot.Info.ObjectMeta,
func(shoot *gardenv1beta1.Shoot) (*gardenv1beta1.Shoot, error) {
shoot.Status.RetryCycleStartTime = nil
shoot.Status.LastError = nil
shoot.Status.LastOperation = &gardenv1beta1.LastOperation{
Type: gardenv1beta1.ShootLastOperationTypeDelete,
State: gardenv1beta1.ShootLastOperationStateSucceeded,
Progress: 100,
Description: "Shoot cluster has been successfully deleted.",
LastUpdateTime: metav1.Now(),
}
return shoot, nil
})
if err != nil {
return err
}
o.Shoot.Info = newShoot
// Remove finalizer
finalizers := sets.NewString(o.Shoot.Info.Finalizers...)
finalizers.Delete(gardenv1beta1.GardenerName)
o.Shoot.Info.Finalizers = finalizers.List()
newShoot, err = c.k8sGardenClient.GardenClientset().GardenV1beta1().Shoots(o.Shoot.Info.Namespace).Update(o.Shoot.Info)
if err != nil {
return err
}
o.Shoot.Info = newShoot
// Wait until the above modifications are reflected in the cache to prevent unwanted reconcile
// operations (sometimes the cache is not synced fast enough).
return wait.PollImmediate(time.Second, 30*time.Second, func() (bool, error) {
shoot, err := c.k8sGardenInformers.Shoots().Lister().Shoots(o.Shoot.Info.Namespace).Get(o.Shoot.Info.Name)
if apierrors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
lastOperation := shoot.Status.LastOperation
if !sets.NewString(shoot.Finalizers...).Has(gardenv1beta1.GardenerName) && lastOperation != nil && lastOperation.Type == gardenv1beta1.ShootLastOperationTypeDelete && lastOperation.State == gardenv1beta1.ShootLastOperationStateSucceeded {
return true, nil
}
return false, nil
})
}
func (c *defaultControl) updateShootStatusDeleteError(o *operation.Operation, lastError *gardenv1beta1.LastError) (gardenv1beta1.ShootLastOperationState, error) {
var (
state = gardenv1beta1.ShootLastOperationStateFailed
description = lastError.Description
)
newShoot, err := kubernetes.TryUpdateShootStatus(c.k8sGardenClient.GardenClientset(), retry.DefaultRetry, o.Shoot.Info.ObjectMeta,
func(shoot *gardenv1beta1.Shoot) (*gardenv1beta1.Shoot, error) {
if !utils.TimeElapsed(shoot.Status.RetryCycleStartTime, c.config.Controllers.Shoot.RetryDuration.Duration) {
description += " Operation will be retried."
state = gardenv1beta1.ShootLastOperationStateError
} else {
shoot.Status.RetryCycleStartTime = nil
}
shoot.Status.Gardener = *o.GardenerInfo
shoot.Status.LastError = lastError
shoot.Status.LastOperation.Type = gardenv1beta1.ShootLastOperationTypeDelete
shoot.Status.LastOperation.State = state
shoot.Status.LastOperation.Description = description
shoot.Status.LastOperation.LastUpdateTime = metav1.Now()
return shoot, nil
},
)
if err == nil {
o.Shoot.Info = newShoot
}
o.Logger.Error(description)
newShootAfterLabel, err := kubernetes.TryUpdateShootLabels(c.k8sGardenClient.GardenClientset(), retry.DefaultRetry, o.Shoot.Info.ObjectMeta, shootHealthyLabelTransform(false))
if err == nil {
o.Shoot.Info = newShootAfterLabel
}
return state, err
}