diff --git a/pkg/controller/controller_utils.go b/pkg/controller/controller_utils.go index c3d0716b15..661b71f96a 100644 --- a/pkg/controller/controller_utils.go +++ b/pkg/controller/controller_utils.go @@ -717,7 +717,8 @@ func GetFakeMachineFromTemplate(template *v1alpha1.MachineTemplateSpec, parentOb desiredAnnotations := getMachinesAnnotationSet(template, parentMetaObj) prefix := getMachinesPrefix(parentMetaObj.GetName()) - prefix = prefix + "-" + uuid.New().String()[:5] + // Use the full UUID as the suffix to avoid name collisions when many machines are created in a single reconcile. + prefix = prefix + "-" + uuid.New().String() machine := &v1alpha1.Machine{ ObjectMeta: metav1.ObjectMeta{ Labels: desiredLabels, diff --git a/pkg/controller/deployment_test.go b/pkg/controller/deployment_test.go index 9bc8e05b69..d6dfe4a8a3 100644 --- a/pkg/controller/deployment_test.go +++ b/pkg/controller/deployment_test.go @@ -8,10 +8,12 @@ import ( "context" "errors" "fmt" + "slices" "strings" "time" "github.com/gardener/machine-controller-manager/pkg/util/annotations" + labelsutil "github.com/gardener/machine-controller-manager/pkg/util/labels" "github.com/gardener/machine-controller-manager/pkg/util/provider/machineutils" "k8s.io/utils/ptr" @@ -1966,25 +1968,44 @@ var _ = Describe("machineDeployment", func() { return nil }, ), - // flaky test because of reusing same testMachine for mutilple tests. Entry("should set MachinePriority=1 for the machines named in TriggerDeletionByMCM annotation in the MachineDeployment", - func(testMachineDeployment *machinev1.MachineDeployment, _ *machinev1.MachineSet) { + func(testMachineDeployment *machinev1.MachineDeployment, testMachineSet *machinev1.MachineSet) { testMachineDeployment.Annotations[machineutils.TriggerDeletionByMCM] = fmt.Sprintf("%s~%s", testMachine.Name, time.Now().Format(time.RFC3339)) + testMachineSet.Spec.Selector = labelsutil.CloneSelectorAndAddLabel(testMachineSet.Spec.Selector, machinev1.DefaultMachineDeploymentUniqueLabelKey, "testhash") }, func(_ *machinev1.MachineDeployment, _ []machinev1.MachineSet, machines []machinev1.Machine, _ *corev1.Node) error { - Expect(machines[0].Annotations[machineutils.MachinePriority]).To(Equal("1")) + idx := slices.IndexFunc(machines, func(m machinev1.Machine) bool { + return m.Name == "Machine-test" + }) + if idx == -1 { + return errors.New("machine \"Machine-test\" not found") + } + if machines[idx].Annotations[machineutils.MachinePriority] != "1" { + return errors.New("expected MachinePriority=1 on machine \"Machine-test\"") + } return nil }, ), Entry("set LDRCBST annotation on the machineSet and TriggerDeletionByMCM annotation is not set on the machineSet", - func(testMachineDeployment *machinev1.MachineDeployment, _ *machinev1.MachineSet) { + func(testMachineDeployment *machinev1.MachineDeployment, testMachineSet *machinev1.MachineSet) { testMachineDeployment.Annotations[machineutils.LastDeploymentReplicaChangeByScalerTime] = ts testMachineDeployment.Annotations[machineutils.TriggerDeletionByMCM] = fmt.Sprintf("%s~%s", testMachine.Name, time.Now().Format(time.RFC3339)) + testMachineSet.Spec.Selector = labelsutil.CloneSelectorAndAddLabel(testMachineSet.Spec.Selector, machinev1.DefaultMachineDeploymentUniqueLabelKey, "testhash") }, func(_ *machinev1.MachineDeployment, mcs []machinev1.MachineSet, _ []machinev1.Machine, _ *corev1.Node) error { - Expect(mcs[0].Annotations[machineutils.LastDeploymentReplicaChangeByScalerTime]).To(Equal(ts)) - _, exists := mcs[0].Annotations[machineutils.TriggerDeletionByMCM] - Expect(exists).To(BeFalse()) + idx := slices.IndexFunc(mcs, func(ms machinev1.MachineSet) bool { + return ms.Name == "MachineSet-test" + }) + if idx == -1 { + return errors.New("machineSet \"MachineSet-test\" not found") + } + ms := &mcs[idx] + if ms.Annotations[machineutils.LastDeploymentReplicaChangeByScalerTime] != ts { + return errors.New("expected LastDeploymentReplicaChangeByScalerTime annotation to be preserved on the machineSet") + } + if _, exists := ms.Annotations[machineutils.TriggerDeletionByMCM]; exists { + return errors.New("TriggerDeletionByMCM annotation should not be set on the machineSet") + } return nil }, ),