/
machines.go
169 lines (149 loc) · 7.05 KB
/
machines.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package gardener
import (
"context"
"fmt"
"strings"
"time"
machinev1alpha1 "github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
retryutils "github.com/gardener/gardener/pkg/utils/retry"
)
const (
nameLabel = "name"
// MachineSetKind is the kind of the owner reference of a machine set
MachineSetKind = "MachineSet"
// MachineDeploymentKind is the kind of the owner reference of a machine deployment
MachineDeploymentKind = "MachineDeployment"
// NodeLeasePrefix describes the Prefix of the lease that this node is corresponding to
NodeLeasePrefix = "gardener-node-agent-"
)
// BuildOwnerToMachinesMap returns a map that associates `MachineSet` names to the given `machines`.
func BuildOwnerToMachinesMap(machines []machinev1alpha1.Machine) map[string][]machinev1alpha1.Machine {
ownerToMachines := make(map[string][]machinev1alpha1.Machine)
for index, machine := range machines {
if len(machine.OwnerReferences) > 0 {
for _, reference := range machine.OwnerReferences {
if reference.Kind == MachineSetKind {
ownerToMachines[reference.Name] = append(ownerToMachines[reference.Name], machines[index])
}
}
} else if len(machine.Labels) > 0 {
if machineDeploymentName, ok := machine.Labels[nameLabel]; ok {
ownerToMachines[machineDeploymentName] = append(ownerToMachines[machineDeploymentName], machines[index])
}
}
}
return ownerToMachines
}
// BuildOwnerToMachineSetsMap returns a map that associates `MachineDeployment` names to the given `machineSets`.
func BuildOwnerToMachineSetsMap(machineSets []machinev1alpha1.MachineSet) map[string][]machinev1alpha1.MachineSet {
ownerToMachineSets := make(map[string][]machinev1alpha1.MachineSet)
for index, machineSet := range machineSets {
if len(machineSet.OwnerReferences) > 0 {
for _, reference := range machineSet.OwnerReferences {
if reference.Kind == MachineDeploymentKind {
ownerToMachineSets[reference.Name] = append(ownerToMachineSets[reference.Name], machineSets[index])
}
}
} else if len(machineSet.Labels) > 0 {
if machineDeploymentName, ok := machineSet.Labels[nameLabel]; ok {
ownerToMachineSets[machineDeploymentName] = append(ownerToMachineSets[machineDeploymentName], machineSets[index])
}
}
}
return ownerToMachineSets
}
// WaitUntilMachineResourcesDeleted waits for a maximum of 30 minutes until all machine resources have been properly
// deleted by the machine-controller-manager. It polls the status every 5 seconds.
func WaitUntilMachineResourcesDeleted(ctx context.Context, log logr.Logger, reader client.Reader, namespace string) error {
var (
countMachines = -1
countMachineSets = -1
countMachineDeployments = -1
countMachineClasses = -1
countMachineClassSecrets = -1
)
log.Info("Waiting until all machine resources have been deleted")
return retryutils.UntilTimeout(ctx, 5*time.Second, 5*time.Minute, func(ctx context.Context) (bool, error) {
var msg string
// Check whether all machines have been deleted.
if countMachines != 0 {
machineList := &metav1.PartialObjectMetadataList{}
machineList.SetGroupVersionKind(machinev1alpha1.SchemeGroupVersion.WithKind("MachineList"))
if err := reader.List(ctx, machineList, client.InNamespace(namespace)); err != nil {
return retryutils.SevereError(err)
}
countMachines = len(machineList.Items)
msg += fmt.Sprintf("%d machines, ", countMachines)
}
// Check whether all machine sets have been deleted.
if countMachineSets != 0 {
machineSetList := &metav1.PartialObjectMetadataList{}
machineSetList.SetGroupVersionKind(machinev1alpha1.SchemeGroupVersion.WithKind("MachineSetList"))
if err := reader.List(ctx, machineSetList, client.InNamespace(namespace)); err != nil {
return retryutils.SevereError(err)
}
countMachineSets = len(machineSetList.Items)
msg += fmt.Sprintf("%d machine sets, ", countMachineSets)
}
// Check whether all machine deployments have been deleted.
if countMachineDeployments != 0 {
machineDeploymentList := &machinev1alpha1.MachineDeploymentList{}
if err := reader.List(ctx, machineDeploymentList, client.InNamespace(namespace)); err != nil {
return retryutils.SevereError(err)
}
countMachineDeployments = len(machineDeploymentList.Items)
msg += fmt.Sprintf("%d machine deployments, ", countMachineDeployments)
// Check whether an operation failed during the deletion process.
for _, existingMachineDeployment := range machineDeploymentList.Items {
for _, failedMachine := range existingMachineDeployment.Status.FailedMachines {
return retryutils.SevereError(fmt.Errorf("machine %s failed: %s", failedMachine.Name, failedMachine.LastOperation.Description))
}
}
}
// Check whether all machine classes have been deleted.
if countMachineClasses != 0 {
machineClassList := &metav1.PartialObjectMetadataList{}
machineClassList.SetGroupVersionKind(machinev1alpha1.SchemeGroupVersion.WithKind("MachineClassList"))
if err := reader.List(ctx, machineClassList, client.InNamespace(namespace)); err != nil {
return retryutils.SevereError(err)
}
countMachineClasses = len(machineClassList.Items)
msg += fmt.Sprintf("%d machine classes, ", countMachineClasses)
}
// Check whether all machine class secrets have been deleted.
if countMachineClassSecrets != 0 {
count := 0
machineClassSecretsList := &metav1.PartialObjectMetadataList{}
machineClassSecretsList.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("SecretList"))
if err := reader.List(ctx, machineClassSecretsList, client.InNamespace(namespace), client.MatchingLabels(map[string]string{v1beta1constants.GardenerPurpose: v1beta1constants.GardenPurposeMachineClass})); err != nil {
return retryutils.SevereError(err)
}
for _, machineClassSecret := range machineClassSecretsList.Items {
if len(machineClassSecret.Finalizers) != 0 {
count++
}
}
countMachineClassSecrets = count
msg += fmt.Sprintf("%d machine class secrets, ", countMachineClassSecrets)
}
if countMachines != 0 || countMachineSets != 0 || countMachineDeployments != 0 || countMachineClasses != 0 || countMachineClassSecrets != 0 {
log.Info("Waiting until machine resources have been deleted",
"machines", countMachines, "machineSets", countMachineSets, "machineDeployments", countMachineDeployments,
"machineClasses", countMachineClasses, "machineClassSecrets", countMachineClassSecrets)
return retryutils.MinorError(fmt.Errorf("waiting until the following machine resources have been deleted: %s", strings.TrimSuffix(msg, ", ")))
}
return retryutils.Ok()
})
}
// NodeAgentLeaseName returns the name of the Lease object based on the node name.
func NodeAgentLeaseName(nodeName string) string {
return NodeLeasePrefix + nodeName
}