/
gardenlet_delete.go
144 lines (124 loc) · 6.07 KB
/
gardenlet_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
// Copyright (c) 2021 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 controller
import (
"context"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gardener/gardener/landscaper/pkg/gardenlet/chart"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/utils/retry"
)
// Delete removes all deployed Gardenlet resources from the Seed cluster.
func (g Landscaper) Delete(ctx context.Context) error {
seed := &gardencorev1beta1.Seed{ObjectMeta: metav1.ObjectMeta{
Name: g.gardenletConfiguration.SeedConfig.Name,
}}
exists, err := g.seedExists(ctx, seed)
if err != nil {
return fmt.Errorf("failed to check if Seed %q exists in the Garden cluster. Aborting. %w", g.gardenletConfiguration.SeedConfig.Name, err)
}
if exists {
shootList := &gardencorev1beta1.ShootList{}
if err := g.gardenClient.Client().List(ctx, shootList); err != nil {
return fmt.Errorf("failed to list shoots: %w", err)
}
if isSeedUsedByAnyShoot(g.gardenletConfiguration.SeedConfig.Name, shootList.Items) {
return fmt.Errorf("cannot delete seed '%s' which is still used by at least one shoot", g.gardenletConfiguration.SeedConfig.Name)
}
err = g.deleteSeedAndWait(ctx, seed)
if err != nil {
return err
}
}
chartApplier := g.seedClient.ChartApplier()
values, err := g.computeGardenletChartValues(nil)
if err != nil {
return fmt.Errorf("failed to compute gardenlet chart values: %w", err)
}
applier := chart.NewGardenletChartApplier(chartApplier, values, g.chartPath)
if err := applier.Destroy(ctx); err != nil {
return fmt.Errorf("failed to delete the Gardenlet resources from the Seed cluster %q: %w", g.gardenletConfiguration.SeedConfig.Name, err)
}
// delete the Seed secret containing the Seed cluster kubeconfig from the Garden cluster
if g.gardenletConfiguration.SeedConfig.Spec.SecretRef != nil {
if err := g.gardenClient.Client().Delete(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: g.gardenletConfiguration.SeedConfig.Spec.SecretRef.Name, Namespace: g.gardenletConfiguration.SeedConfig.Spec.SecretRef.Namespace}}); client.IgnoreNotFound(err) != nil {
return fmt.Errorf("failed to delete the secret from the Garden cluster (%s/%s) containing the Seed cluster's kubeconfig: %w", g.gardenletConfiguration.SeedConfig.Spec.SecretRef.Namespace, g.gardenletConfiguration.SeedConfig.Spec.SecretRef.Name, err)
}
}
// delete the seed-backup secret from the Garden cluster
if g.imports.SeedBackupCredentials != nil && g.gardenletConfiguration.SeedConfig.Spec.Backup != nil {
if err := g.gardenClient.Client().Delete(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: g.gardenletConfiguration.SeedConfig.Spec.Backup.SecretRef.Name, Namespace: g.gardenletConfiguration.SeedConfig.Spec.Backup.SecretRef.Namespace}}); client.IgnoreNotFound(err) != nil {
return fmt.Errorf("failed to delete the Seed backup secret (%s/%s) from the Garden cluster: %w", g.gardenletConfiguration.SeedConfig.Spec.Backup.SecretRef.Namespace, g.gardenletConfiguration.SeedConfig.Spec.Backup.SecretRef.Name, err)
}
}
g.log.Infof("Successfully deleted Gardenlet resources for Seed %q", g.gardenletConfiguration.SeedConfig.Name)
return nil
}
// deleteSeedAndWait waits until the Seed resource has been deleted from the Garden cluster
func (g Landscaper) deleteSeedAndWait(ctx context.Context, seed *gardencorev1beta1.Seed) error {
if err := retry.UntilTimeout(ctx, 10*time.Second, 2*time.Minute, func(ctx context.Context) (done bool, err error) {
err = g.gardenClient.Client().Delete(ctx, seed)
if err != nil {
g.log.Infof("Error deleting seed %q: %v", g.gardenletConfiguration.SeedConfig.Name, err)
return retry.MinorError(err)
}
return retry.Ok()
}); err != nil {
return fmt.Errorf("failed to delete seed %q: %w", g.gardenletConfiguration.SeedConfig.Name, err)
}
if err := retry.UntilTimeout(ctx, 10*time.Second, 10*time.Minute, func(ctx context.Context) (done bool, err error) {
seedExists, err := g.seedExists(ctx, seed)
if err != nil {
g.log.Infof("Error while waiting for seed to be deleted: %s", err.Error())
return retry.MinorError(err)
}
if !seedExists {
g.log.Infof("Seed %q has been deleted successfully", seed.Name)
return retry.Ok()
}
g.log.Infof("Waiting for seed %s to be deleted", seed.Name)
return retry.MinorError(fmt.Errorf("seed %q still exists", seed.Name))
}); err != nil {
return fmt.Errorf("failed waiting for the deletion of Seed %q: %w", g.gardenletConfiguration.SeedConfig.Name, err)
}
return nil
}
// seedExists checks if the given Seed resource exists in the garden cluster
func (g *Landscaper) seedExists(ctx context.Context, seed *gardencorev1beta1.Seed) (bool, error) {
err := g.gardenClient.Client().Get(ctx, client.ObjectKey{Name: seed.Name}, seed)
if err != nil {
if apierrors.IsNotFound(err) || g.isIntegrationTest {
return false, nil
}
return false, err
}
return true, nil
}
// isSeedUsedByAnyShoot checks whether there is a shoot cluster referencing the provided seed name
func isSeedUsedByAnyShoot(seedName string, shoots []gardencorev1beta1.Shoot) bool {
for _, shoot := range shoots {
if shoot.Spec.SeedName != nil && *shoot.Spec.SeedName == seedName {
return true
}
if shoot.Status.SeedName != nil && *shoot.Status.SeedName == seedName {
return true
}
}
return false
}