forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shootmigrationtest.go
286 lines (253 loc) · 11.9 KB
/
shootmigrationtest.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
// Copyright (c) 2020 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 framework
import (
"context"
"fmt"
"os"
"reflect"
"sort"
"strings"
resourcesv1alpha1 "github.com/gardener/gardener-resource-manager/pkg/apis/resources/v1alpha1"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
"github.com/gardener/gardener/pkg/utils"
dnsv1alpha1 "github.com/gardener/external-dns-management/pkg/apis/dns/v1alpha1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ShootMigrationTest represents a shoot migration test.
// It can be used to test the migration of shoots between various seeds.
type ShootMigrationTest struct {
GardenerFramework *GardenerFramework
Config *ShootMigrationConfig
TargetSeedClient kubernetes.Interface
SourceSeedClient kubernetes.Interface
ShootClient kubernetes.Interface
TargetSeed *gardencorev1beta1.Seed
SourceSeed *gardencorev1beta1.Seed
ComparisonElementsBeforeMigration ShootComparisonElements
ComparisonElementsAfterMigration ShootComparisonElements
Shoot gardencorev1beta1.Shoot
SeedShootNamespace string
MigrationTime metav1.Time
}
// ShootMigrationConfig is the configuration for a shoot migration test that will be filled with user provided data
type ShootMigrationConfig struct {
TargetSeedName string
SourceSeedName string
ShootName string
ShootNamespace string
}
// ShootComparisonElements contains details about Machines and Nodes that will be compared during the tests
type ShootComparisonElements struct {
MachineNames []string
MachineNodes []string
NodeNames []string
}
// NewShootMigrationTest creates a new simple shoot migration test
func NewShootMigrationTest(f *GardenerFramework, cfg *ShootMigrationConfig) *ShootMigrationTest {
t := &ShootMigrationTest{
GardenerFramework: f,
Config: cfg,
}
return t
}
// MigrateShoot triggers shoot migration by changing the value of "shoot.Spec.SeedName" to the value of "ShootMigrationConfig.TargetSeedName"
func (t *ShootMigrationTest) MigrateShoot(ctx context.Context) error {
// Dump gardener state if delete shoot is in exit handler
if os.Getenv("TM_PHASE") == "Exit" {
if shootFramework, err := t.GardenerFramework.NewShootFramework(&t.Shoot); err == nil {
shootFramework.DumpState(ctx)
} else {
t.GardenerFramework.DumpState(ctx)
}
}
t.MigrationTime = metav1.Now()
return t.GardenerFramework.MigrateShoot(ctx, &t.Shoot, t.TargetSeed, func(shoot *gardencorev1beta1.Shoot) error {
t := gardencorev1beta1.Toleration{}
t.Key = SeedTaintTestRun
t.Value = pointer.StringPtr(GetTestRunID())
if shoot.Spec.Tolerations == nil {
shoot.Spec.Tolerations = make([]gardencorev1beta1.Toleration, 0)
} else {
for _, t := range shoot.Spec.Tolerations {
if t.Key == SeedTaintTestRun {
return nil
}
}
}
shoot.Spec.Tolerations = append(shoot.Spec.Tolerations, t)
return nil
})
}
// GetNodeNames uses the shootClient to fetch all Node names from the Shoot
func (t *ShootMigrationTest) GetNodeNames(ctx context.Context, shootClient kubernetes.Interface) (nodeNames []string, err error) {
nodeList := corev1.NodeList{}
t.GardenerFramework.Logger.Infof("Getting node names...")
if err := shootClient.Client().List(ctx, &nodeList); err != nil {
return nil, err
}
nodeNames = make([]string, len(nodeList.Items))
for i, node := range nodeList.Items {
t.GardenerFramework.Logger.Infof("%d. %s", i, node.Name)
nodeNames[i] = node.Name
}
sort.Strings(nodeNames)
return
}
// GetMachineDetails uses the seedClient to fetch all Machine names and the names of their corresponding Nodes
func (t *ShootMigrationTest) GetMachineDetails(ctx context.Context, seedClient kubernetes.Interface) (machineNames, machineNodes []string, err error) {
machineList := unstructured.UnstructuredList{}
machineList.SetAPIVersion("machine.sapcloud.io/v1alpha1")
machineList.SetKind("Machine")
t.GardenerFramework.Logger.Infof("Getting machine details in namespace: %s", t.SeedShootNamespace)
if err := seedClient.Client().List(ctx, &machineList, client.InNamespace(t.SeedShootNamespace)); err != nil {
t.GardenerFramework.Logger.Errorf("Error while getting Machine details, %s", err.Error())
return nil, nil, err
}
t.GardenerFramework.Logger.Infof("Found: %d Machine items", len(machineList.Items))
machineNames = make([]string, len(machineList.Items))
machineNodes = make([]string, len(machineList.Items))
for i, machine := range machineList.Items {
t.GardenerFramework.Logger.Infof("%d. Machine Name: %s, Node Name: %s", i, machine.GetName(), machine.GetLabels()["node"])
machineNames[i] = machine.GetName()
machineNodes[i] = machine.GetLabels()["node"]
}
sort.Strings(machineNames)
sort.Strings(machineNodes)
return
}
// PopulateBeforeMigrationComparisonElements fills the ShootMigrationTest.ComparisonElementsBeforeMigration with the necessary Machine details and Node names
func (t *ShootMigrationTest) PopulateBeforeMigrationComparisonElements(ctx context.Context) (err error) {
t.ComparisonElementsBeforeMigration.MachineNames, t.ComparisonElementsBeforeMigration.MachineNodes, err = t.GetMachineDetails(ctx, t.SourceSeedClient)
if err != nil {
return
}
t.ComparisonElementsBeforeMigration.NodeNames, err = t.GetNodeNames(ctx, t.ShootClient)
return
}
// PopulateAfterMigrationComparisonElements fills the ShootMigrationTest.ComparisonElementsAfterMigration with the necessary Machine details and Node names
func (t *ShootMigrationTest) PopulateAfterMigrationComparisonElements(ctx context.Context) (err error) {
t.ComparisonElementsAfterMigration.MachineNames, t.ComparisonElementsAfterMigration.MachineNodes, err = t.GetMachineDetails(ctx, t.TargetSeedClient)
if err != nil {
return
}
t.ComparisonElementsAfterMigration.NodeNames, err = t.GetNodeNames(ctx, t.ShootClient)
return
}
// CompareElementsAfterMigration compares the Machine details, Node names and Pod statuses before and after migration and returns error if there are diferences.
func (t *ShootMigrationTest) CompareElementsAfterMigration() error {
if !reflect.DeepEqual(t.ComparisonElementsBeforeMigration.MachineNames, t.ComparisonElementsAfterMigration.MachineNames) {
return errors.Errorf("initial Machines %s, do not match after-migrate Machines %s", t.ComparisonElementsBeforeMigration.MachineNames, t.ComparisonElementsAfterMigration.MachineNames)
}
if !reflect.DeepEqual(t.ComparisonElementsBeforeMigration.MachineNodes, t.ComparisonElementsAfterMigration.MachineNodes) {
return errors.Errorf("initial Machine Nodes (label) %s, do not match after-migrate Machine Nodes (label) %s", t.ComparisonElementsBeforeMigration.MachineNodes, t.ComparisonElementsAfterMigration.MachineNodes)
}
if !reflect.DeepEqual(t.ComparisonElementsBeforeMigration.NodeNames, t.ComparisonElementsAfterMigration.NodeNames) {
return errors.Errorf("initial Nodes %s, do not match after-migrate Nodes %s", t.ComparisonElementsBeforeMigration.NodeNames, t.ComparisonElementsAfterMigration.NodeNames)
}
if !reflect.DeepEqual(t.ComparisonElementsAfterMigration.MachineNodes, t.ComparisonElementsAfterMigration.NodeNames) {
return errors.Errorf("machine Nodes (label) %s, do not match after-migrate Nodes %s", t.ComparisonElementsAfterMigration.MachineNodes, t.ComparisonElementsAfterMigration.NodeNames)
}
return nil
}
// Check the timestamp of all objects that the resource-manager creates in the Shoot cluster.
// The timestamp should not be after the ShootMigrationTest.MigrationTime
func (t *ShootMigrationTest) CheckObjectsTimestamp(ctx context.Context, mrExcludeList []string) error {
mrList := &resourcesv1alpha1.ManagedResourceList{}
if err := t.TargetSeedClient.Client().List(
ctx,
mrList,
client.InNamespace(t.SeedShootNamespace),
); err != nil {
return err
}
for _, mr := range mrList.Items {
if mr.Spec.Class == nil || *mr.Spec.Class != "seed" {
if !utils.ValueExists(mr.GetName(), mrExcludeList) {
t.GardenerFramework.Logger.Infof("=== Managed Resource: %s/%s ===", mr.Namespace, mr.Name)
for _, r := range mr.Status.Resources {
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(r.APIVersion)
obj.SetKind(r.Kind)
if err := t.ShootClient.Client().Get(ctx, client.ObjectKey{Namespace: r.Namespace, Name: r.Name}, obj); err != nil {
return err
}
createionTimestamp := obj.GetCreationTimestamp()
t.GardenerFramework.Logger.Infof("Object: %s %s/%s Created At: %s", obj.GetKind(), obj.GetNamespace(), obj.GetName(), createionTimestamp)
if t.MigrationTime.Before(&createionTimestamp) {
t.GardenerFramework.Logger.Errorf("object: %s %s/%s Created At: %s is created after the Shoot migration %s", obj.GetKind(), obj.GetNamespace(), obj.GetName(), createionTimestamp, t.MigrationTime)
return errors.Errorf("object: %s %s/%s Created At: %s is created after the Shoot migration %s", obj.GetKind(), obj.GetNamespace(), obj.GetName(), createionTimestamp, t.MigrationTime)
}
}
}
}
}
return nil
}
// CheckForOrphanedNonNamespacedResources checks if there are orphaned resources left on the target seed after the shoot migration.
// The function checks for Cluster, DNSOwner, BackupEntry, ClusterRoleBinding, ClusterRole and PersistentVolume
func (t *ShootMigrationTest) CheckForOrphanedNonNamespacedResources(ctx context.Context) error {
seedClientScheme := t.SourceSeedClient.Client().Scheme()
if err := extensionsv1alpha1.AddToScheme(seedClientScheme); err != nil {
return err
}
leakedObjects := []string{}
for _, obj := range []client.ObjectList{
&extensionsv1alpha1.ClusterList{},
&dnsv1alpha1.DNSOwnerList{},
&v1alpha1.BackupEntryList{},
&rbacv1.ClusterRoleBindingList{},
&rbacv1.ClusterRoleList{},
} {
if err := t.SourceSeedClient.Client().List(ctx, obj, client.InNamespace(corev1.NamespaceAll)); err != nil {
return err
}
if err := meta.EachListItem(obj, func(object runtime.Object) error {
if strings.Contains(object.(client.Object).GetName(), t.SeedShootNamespace) {
leakedObjects = append(leakedObjects, fmt.Sprintf("%s/%s", object.(client.Object).GetObjectKind(), object.(client.Object).GetName()))
}
return nil
}); err != nil {
return err
}
}
pvList := &corev1.PersistentVolumeList{}
if err := t.SourceSeedClient.Client().List(ctx, pvList, client.InNamespace(corev1.NamespaceAll)); err != nil {
return err
}
if err := meta.EachListItem(pvList, func(obj runtime.Object) error {
pv := obj.(*corev1.PersistentVolume)
if strings.Contains(pv.Spec.ClaimRef.Namespace, t.SeedShootNamespace) {
leakedObjects = append(leakedObjects, fmt.Sprintf("PersistentVolume/%s", pv.GetName()))
}
return nil
}); err != nil {
return err
}
if len(leakedObjects) > 0 {
return errors.Errorf("the following object(s) still exists in the source seed %v", leakedObjects)
}
return nil
}