-
Notifications
You must be signed in to change notification settings - Fork 156
/
node.go
118 lines (93 loc) · 4.53 KB
/
node.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 clusterdeletion
import (
"context"
"fmt"
clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
eviction "github.com/kubermatic/machine-controller/pkg/node/eviction/types"
kubermaticv1 "k8c.io/kubermatic/v2/pkg/apis/kubermatic/v1"
kuberneteshelper "k8c.io/kubermatic/v2/pkg/kubernetes"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (d *Deletion) cleanupNodes(ctx context.Context, cluster *kubermaticv1.Cluster) error {
if !kuberneteshelper.HasFinalizer(cluster, kubermaticv1.NodeDeletionFinalizer) {
return nil
}
if cluster.Status.NamespaceName == "" {
return kuberneteshelper.TryRemoveFinalizer(ctx, d.seedClient, cluster, kubermaticv1.NodeDeletionFinalizer)
}
userClusterClient, err := d.userClusterClientGetter()
if err != nil {
return err
}
nodes := &corev1.NodeList{}
if err := userClusterClient.List(ctx, nodes); err != nil {
return fmt.Errorf("failed to get user cluster nodes: %w", err)
}
// If we delete a cluster, we should disable the eviction on the nodes
for _, node := range nodes.Items {
if node.Annotations[eviction.SkipEvictionAnnotationKey] == "true" {
continue
}
oldNode := node.DeepCopy()
if node.Annotations == nil {
node.Annotations = map[string]string{}
}
node.Annotations[eviction.SkipEvictionAnnotationKey] = "true"
if err := userClusterClient.Patch(ctx, &node, ctrlruntimeclient.MergeFrom(oldNode)); err != nil {
return fmt.Errorf("failed to add the annotation '%s=true' to node '%s': %w", eviction.SkipEvictionAnnotationKey, node.Name, err)
}
}
listOpts := ctrlruntimeclient.InNamespace(metav1.NamespaceSystem)
machineDeploymentList := &clusterv1alpha1.MachineDeploymentList{}
if err := userClusterClient.List(ctx, machineDeploymentList, listOpts); err != nil && !meta.IsNoMatchError(err) {
return fmt.Errorf("failed to list MachineDeployments: %w", err)
}
if len(machineDeploymentList.Items) > 0 {
if err = userClusterClient.DeleteAllOf(ctx, &clusterv1alpha1.MachineDeployment{}, listOpts); err != nil {
return fmt.Errorf("failed to delete MachineDeployments: %w", err)
}
// Return here to make sure we don't attempt to delete MachineSets until the MachineDeployment is actually gone
d.recorder.Eventf(cluster, corev1.EventTypeNormal, "NodeCleanup", "Waiting for %d MachineDeployment(s) to be destroyed.", len(machineDeploymentList.Items))
return nil
}
machineSetList := &clusterv1alpha1.MachineSetList{}
if err := userClusterClient.List(ctx, machineSetList, listOpts); err != nil && !meta.IsNoMatchError(err) {
return fmt.Errorf("failed to list MachineSets: %w", err)
}
if len(machineSetList.Items) > 0 {
if err = userClusterClient.DeleteAllOf(ctx, &clusterv1alpha1.MachineSet{}, listOpts); err != nil {
return fmt.Errorf("failed to delete MachineSets: %w", err)
}
// Return here to make sure we don't attempt to delete Machines until the MachineSet is actually gone
d.recorder.Eventf(cluster, corev1.EventTypeNormal, "NodeCleanup", "Waiting for %d MachineSet(s) to be destroyed.", len(machineSetList.Items))
return nil
}
machineList := &clusterv1alpha1.MachineList{}
if err := userClusterClient.List(ctx, machineList, listOpts); err != nil && !meta.IsNoMatchError(err) {
return fmt.Errorf("failed to get Machines: %w", err)
}
if len(machineList.Items) > 0 {
if err = userClusterClient.DeleteAllOf(ctx, &clusterv1alpha1.Machine{}, listOpts); err != nil {
return fmt.Errorf("failed to delete Machines: %w", err)
}
d.recorder.Eventf(cluster, corev1.EventTypeNormal, "NodeCleanup", "Waiting for %d Machine(s) to be destroyed.", len(machineList.Items))
return nil
}
d.recorder.Event(cluster, corev1.EventTypeNormal, "NodeCleanup", "Cleanup has been completed, all machines have been destroyed.")
return kuberneteshelper.TryRemoveFinalizer(ctx, d.seedClient, cluster, kubermaticv1.NodeDeletionFinalizer)
}