Skip to content

Commit 6140c34

Browse files
committed
review fixes
1 parent c83c628 commit 6140c34

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

internal/controllers/machinedeployment/machinedeployment_controller.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, md *clusterv1.MachineD
313313
for _, ms := range msList {
314314
if ms.DeletionTimestamp.IsZero() {
315315
log.Info("Deleting MachineSet", "MachineSet", klog.KObj(ms))
316-
if err := r.Client.Delete(ctx, ms); err != nil {
316+
if err := r.Client.Delete(ctx, ms); err != nil && !apierrors.IsNotFound(err) {
317317
return errors.Wrapf(err, "failed to delete MachineSet %s", klog.KObj(ms))
318318
}
319319
}
@@ -335,7 +335,8 @@ func (r *Reconciler) getAndAdoptMachineSetsForDeployment(ctx context.Context, md
335335
filtered := make([]*clusterv1.MachineSet, 0, len(machineSets.Items))
336336
for idx := range machineSets.Items {
337337
ms := &machineSets.Items[idx]
338-
log.WithValues("MachineSet", klog.KObj(ms))
338+
log := log.WithValues("MachineSet", klog.KObj(ms))
339+
ctx := ctrl.LoggerInto(ctx, log)
339340
selector, err := metav1.LabelSelectorAsSelector(&md.Spec.Selector)
340341
if err != nil {
341342
log.Error(err, "Skipping MachineSet, failed to get label selector from spec selector")

internal/controllers/machinedeployment/machinedeployment_controller_test.go

+4-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package machinedeployment
1818

1919
import (
2020
"context"
21-
"sort"
2221
"testing"
2322
"time"
2423

@@ -1001,12 +1000,15 @@ func TestReconciler_reconcileDelete(t *testing.T) {
10011000
"some": "labelselector",
10021001
}
10031002
md := builder.MachineDeployment("default", "md0").WithClusterName("test").Build()
1003+
md.Finalizers = []string{
1004+
clusterv1.MachineDeploymentFinalizer,
1005+
}
10041006
md.DeletionTimestamp = ptr.To(metav1.Now())
10051007
md.Spec.Selector = metav1.LabelSelector{
10061008
MatchLabels: labels,
10071009
}
10081010
mdWithoutFinalizer := md.DeepCopy()
1009-
mdWithoutFinalizer.Finalizers = nil
1011+
mdWithoutFinalizer.Finalizers = []string{}
10101012
tests := []struct {
10111013
name string
10121014
machineDeployment *clusterv1.MachineDeployment
@@ -1057,7 +1059,6 @@ func TestReconciler_reconcileDelete(t *testing.T) {
10571059
Client: c,
10581060
recorder: record.NewFakeRecorder(32),
10591061
}
1060-
// Mark machineDeployment to be in deletion.
10611062

10621063
err := r.reconcileDelete(ctx, tt.machineDeployment)
10631064
if tt.expectError {
@@ -1070,21 +1071,6 @@ func TestReconciler_reconcileDelete(t *testing.T) {
10701071

10711072
machineSetList := &clusterv1.MachineSetList{}
10721073
g.Expect(c.List(ctx, machineSetList, client.InNamespace("default"))).ToNot(HaveOccurred())
1073-
1074-
// Remove ResourceVersion so we can actually compare.
1075-
for i, m := range machineSetList.Items {
1076-
m.ResourceVersion = ""
1077-
machineSetList.Items[i] = m
1078-
}
1079-
1080-
sort.Slice(machineSetList.Items, func(i, j int) bool {
1081-
return machineSetList.Items[i].GetName() < machineSetList.Items[j].GetName()
1082-
})
1083-
sort.Slice(tt.wantMachineSets, func(i, j int) bool {
1084-
return tt.wantMachineSets[i].GetName() < tt.wantMachineSets[j].GetName()
1085-
})
1086-
1087-
g.Expect(machineSetList.Items).To(BeComparableTo(tt.wantMachineSets))
10881074
})
10891075
}
10901076
}

internal/controllers/machineset/machineset_controller.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, machineSet *clusterv1.
348348
for _, machine := range machineList {
349349
if machine.DeletionTimestamp.IsZero() {
350350
log.Info("Deleting Machine", "Machine", klog.KObj(machine))
351-
if err := r.Client.Delete(ctx, machine); err != nil {
351+
if err := r.Client.Delete(ctx, machine); err != nil && !apierrors.IsNotFound(err) {
352352
return errors.Wrapf(err, "failed to delete Machine %s", klog.KObj(machine))
353353
}
354354
}
@@ -381,6 +381,7 @@ func (r *Reconciler) getAndAdoptMachinesForMachineSet(ctx context.Context, machi
381381
for idx := range allMachines.Items {
382382
machine := &allMachines.Items[idx]
383383
log := log.WithValues("Machine", klog.KObj(machine))
384+
ctx := ctrl.LoggerInto(ctx, log)
384385
if shouldExcludeMachine(machineSet, machine) {
385386
continue
386387
}

internal/controllers/machineset/machineset_controller_test.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package machineset
1818

1919
import (
2020
"fmt"
21-
"sort"
2221
"testing"
2322
"time"
2423

@@ -2136,12 +2135,15 @@ func TestReconciler_reconcileDelete(t *testing.T) {
21362135
"some": "labelselector",
21372136
}
21382137
ms := builder.MachineSet("default", "ms0").WithClusterName("test").Build()
2138+
ms.Finalizers = []string{
2139+
clusterv1.MachineSetFinalizer,
2140+
}
21392141
ms.DeletionTimestamp = ptr.To(metav1.Now())
21402142
ms.Spec.Selector = metav1.LabelSelector{
21412143
MatchLabels: labels,
21422144
}
21432145
msWithoutFinalizer := ms.DeepCopy()
2144-
msWithoutFinalizer.Finalizers = nil
2146+
msWithoutFinalizer.Finalizers = []string{}
21452147
tests := []struct {
21462148
name string
21472149
machineSet *clusterv1.MachineSet
@@ -2207,19 +2209,11 @@ func TestReconciler_reconcileDelete(t *testing.T) {
22072209
g.Expect(c.List(ctx, machineList, client.InNamespace("default"))).ToNot(HaveOccurred())
22082210

22092211
// Remove ResourceVersion so we can actually compare.
2210-
for i, m := range machineList.Items {
2211-
m.ResourceVersion = ""
2212-
machineList.Items[i] = m
2212+
for i := range machineList.Items {
2213+
machineList.Items[i].ResourceVersion = ""
22132214
}
22142215

2215-
sort.Slice(machineList.Items, func(i, j int) bool {
2216-
return machineList.Items[i].GetName() < machineList.Items[j].GetName()
2217-
})
2218-
sort.Slice(tt.wantMachines, func(i, j int) bool {
2219-
return tt.wantMachines[i].GetName() < tt.wantMachines[j].GetName()
2220-
})
2221-
2222-
g.Expect(machineList.Items).To(BeComparableTo(tt.wantMachines))
2216+
g.Expect(machineList.Items).To(ConsistOf(tt.wantMachines))
22232217
})
22242218
}
22252219
}

util/log/log.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,25 @@ func getOwners(ctx context.Context, c client.Client, obj metav1.Object) ([]owner
111111
// five objects. On more than five objects it outputs the first five objects and
112112
// adds information about how much more are in the given list.
113113
func ObjNamesString[T client.Object](objs []T) string {
114+
shortenedBy := 0
115+
if len(objs) > 5 {
116+
shortenedBy = len(objs) - 5
117+
objs = objs[:5]
118+
}
119+
120+
stringList := []string{}
121+
for _, obj := range objs {
122+
stringList = append(stringList, obj.GetName())
123+
}
124+
114125
objNames := make([]string, len(objs))
115126
for _, obj := range objs {
116127
objNames = append(objNames, obj.GetName())
117128
}
118129

119-
if len(objNames) <= 5 {
120-
return strings.Join(objNames, ", ")
130+
if shortenedBy > 0 {
131+
stringList = append(stringList, fmt.Sprintf("... (%d more)", shortenedBy))
121132
}
122133

123-
return fmt.Sprintf("%s and %d more", strings.Join(objNames[:5], ", "), len(objNames)-5)
134+
return strings.Join(stringList, ", ")
124135
}

0 commit comments

Comments
 (0)