Skip to content

Commit a7e1481

Browse files
committed
fixes
1 parent fe3b43a commit a7e1481

File tree

2 files changed

+186
-24
lines changed

2 files changed

+186
-24
lines changed

internal/controllers/machineset/machineset_controller_status.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func setMachinesReadyCondition(ctx context.Context, machineSet *clusterv1.Machin
202202
v1beta2conditions.Set(machineSet, metav1.Condition{
203203
Type: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
204204
Status: metav1.ConditionUnknown,
205-
Reason: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
205+
Reason: clusterv1.MachineSetMachinesReadyInternalErrorV1Beta2Reason,
206206
Message: "Please check controller logs for errors",
207207
})
208208
return
@@ -242,7 +242,7 @@ func setMachinesUpToDateCondition(ctx context.Context, machineSet *clusterv1.Mac
242242
v1beta2conditions.Set(machineSet, metav1.Condition{
243243
Type: clusterv1.MachineSetMachinesUpToDateV1Beta2Condition,
244244
Status: metav1.ConditionUnknown,
245-
Reason: clusterv1.MachineSetMachinesUpToDateV1Beta2Condition,
245+
Reason: clusterv1.MachineSetMachinesUpToDateInternalErrorV1Beta2Reason,
246246
Message: "Please check controller logs for errors",
247247
})
248248
return
@@ -258,7 +258,7 @@ func setMachinesUpToDateCondition(ctx context.Context, machineSet *clusterv1.Mac
258258
}
259259

260260
upToDateCondition, err := v1beta2conditions.NewAggregateCondition(
261-
machines, clusterv1.MachinesUpToDateV1Beta2Condition,
261+
machines, clusterv1.MachineUpToDateV1Beta2Condition,
262262
v1beta2conditions.TargetConditionType(clusterv1.MachineSetMachinesUpToDateV1Beta2Condition),
263263
)
264264
if err != nil {

internal/controllers/machineset/machineset_controller_status_test.go

+183-21
Original file line numberDiff line numberDiff line change
@@ -284,26 +284,14 @@ func Test_setScalingUpCondition(t *testing.T) {
284284
}
285285
}
286286

287-
func testMachine(name string, staleDeleting bool) *clusterv1.Machine {
288-
m := &clusterv1.Machine{
289-
ObjectMeta: metav1.ObjectMeta{
290-
Name: name,
291-
},
292-
}
293-
if staleDeleting {
294-
m.DeletionTimestamp = ptr.To(metav1.Time{Time: time.Now().Add(-1 * time.Hour)})
295-
}
296-
return m
297-
}
298-
299287
func Test_setScalingDownCondition(t *testing.T) {
300-
defaultMachineSet := &clusterv1.MachineSet{
288+
machineSet := &clusterv1.MachineSet{
301289
Spec: clusterv1.MachineSetSpec{
302290
Replicas: ptr.To[int32](0),
303291
},
304292
}
305293

306-
machineSet1Replica := defaultMachineSet.DeepCopy()
294+
machineSet1Replica := machineSet.DeepCopy()
307295
machineSet1Replica.Spec.Replicas = ptr.To[int32](1)
308296

309297
tests := []struct {
@@ -314,7 +302,7 @@ func Test_setScalingDownCondition(t *testing.T) {
314302
}{
315303
{
316304
name: "getAndAdoptMachines failed",
317-
ms: defaultMachineSet,
305+
ms: machineSet,
318306
machines: nil,
319307
expectCondition: metav1.Condition{
320308
Type: clusterv1.MachineSetScalingDownV1Beta2Condition,
@@ -325,7 +313,7 @@ func Test_setScalingDownCondition(t *testing.T) {
325313
},
326314
{
327315
name: "not scaling down and no machines",
328-
ms: defaultMachineSet,
316+
ms: machineSet,
329317
machines: []*clusterv1.Machine{},
330318
expectCondition: metav1.Condition{
331319
Type: clusterv1.MachineSetScalingDownV1Beta2Condition,
@@ -345,7 +333,7 @@ func Test_setScalingDownCondition(t *testing.T) {
345333
},
346334
{
347335
name: "scaling down to zero",
348-
ms: defaultMachineSet,
336+
ms: machineSet,
349337
machines: []*clusterv1.Machine{
350338
testMachine("machine-1", false),
351339
},
@@ -419,19 +407,105 @@ func Test_setScalingDownCondition(t *testing.T) {
419407
}
420408

421409
func Test_setMachinesReadyCondition(t *testing.T) {
410+
machineSet := &clusterv1.MachineSet{}
411+
412+
readyCondition := metav1.Condition{
413+
Type: clusterv1.MachineReadyV1Beta2Condition,
414+
Status: metav1.ConditionTrue,
415+
Reason: v1beta2conditions.MultipleInfoReportedReason,
416+
}
417+
422418
tests := []struct {
423419
name string
424420
machineSet *clusterv1.MachineSet
425421
machines []*clusterv1.Machine
426422
expectCondition metav1.Condition
427423
}{
428-
// TODO: Add test cases.
424+
{
425+
name: "getAndAdoptMachines failed",
426+
machineSet: machineSet,
427+
machines: nil,
428+
expectCondition: metav1.Condition{
429+
Type: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
430+
Status: metav1.ConditionUnknown,
431+
Reason: clusterv1.MachineSetMachinesReadyInternalErrorV1Beta2Reason,
432+
Message: "Please check controller logs for errors",
433+
},
434+
},
435+
{
436+
name: "no machines",
437+
machineSet: machineSet,
438+
machines: []*clusterv1.Machine{},
439+
expectCondition: metav1.Condition{
440+
Type: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
441+
Status: metav1.ConditionTrue,
442+
Reason: clusterv1.MachineSetMachinesReadyNoReplicasV1Beta2Reason,
443+
},
444+
},
445+
{
446+
name: "all machines are ready",
447+
machineSet: machineSet,
448+
machines: []*clusterv1.Machine{
449+
createMachine("machine-1", readyCondition),
450+
createMachine("machine-2", readyCondition),
451+
},
452+
expectCondition: metav1.Condition{
453+
Type: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
454+
Status: metav1.ConditionTrue,
455+
Reason: v1beta2conditions.MultipleInfoReportedReason,
456+
},
457+
},
458+
{
459+
name: "one ready, one has nothing reported",
460+
machineSet: machineSet,
461+
machines: []*clusterv1.Machine{
462+
createMachine("machine-1", readyCondition),
463+
createMachine("machine-2"),
464+
},
465+
expectCondition: metav1.Condition{
466+
Type: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
467+
Status: metav1.ConditionUnknown,
468+
Reason: v1beta2conditions.NotYetReportedReason,
469+
Message: "Condition Ready not yet reported from Machine machine-2",
470+
},
471+
},
472+
{
473+
name: "one ready, one reporting not ready, one reporting unknown, one reporting deleting",
474+
machineSet: machineSet,
475+
machines: []*clusterv1.Machine{
476+
createMachine("machine-1", readyCondition),
477+
createMachine("machine-2", metav1.Condition{
478+
Type: clusterv1.MachineReadyV1Beta2Condition,
479+
Status: metav1.ConditionFalse,
480+
Reason: "SomeReason",
481+
Message: "HealthCheckSucceeded: Some message",
482+
}),
483+
createMachine("machine-3", metav1.Condition{
484+
Type: clusterv1.MachineReadyV1Beta2Condition,
485+
Status: metav1.ConditionUnknown,
486+
Reason: "SomeUnknownReason",
487+
Message: "Some unknown message",
488+
}),
489+
createMachine("machine-4", metav1.Condition{
490+
Type: clusterv1.MachineReadyV1Beta2Condition,
491+
Status: metav1.ConditionFalse,
492+
Reason: clusterv1.MachineDeletingV1Beta2Reason,
493+
Message: "Deleting: Machine deletion in progress, stage: DrainingNode",
494+
}),
495+
},
496+
expectCondition: metav1.Condition{
497+
Type: clusterv1.MachineSetMachinesReadyV1Beta2Condition,
498+
Status: metav1.ConditionFalse,
499+
Reason: v1beta2conditions.MultipleIssuesReportedReason,
500+
Message: "Deleting: Machine deletion in progress, stage: DrainingNode from Machine machine-4; HealthCheckSucceeded: Some message from Machine machine-2; Some unknown message from Machine machine-3",
501+
},
502+
},
429503
}
430504
for _, tt := range tests {
431505
t.Run(tt.name, func(t *testing.T) {
432506
g := NewWithT(t)
433507

434-
setMachinesReadyCondition(ctx, tt.machineSet, tt.machines, true)
508+
setMachinesReadyCondition(ctx, tt.machineSet, tt.machines, tt.machines != nil)
435509

436510
condition := v1beta2conditions.Get(tt.machineSet, clusterv1.MachineSetMachinesReadyV1Beta2Condition)
437511
g.Expect(condition).ToNot(BeNil())
@@ -441,23 +515,111 @@ func Test_setMachinesReadyCondition(t *testing.T) {
441515
}
442516

443517
func Test_setMachinesUpToDateCondition(t *testing.T) {
518+
machineSet := &clusterv1.MachineSet{}
519+
444520
tests := []struct {
445521
name string
446522
machineSet *clusterv1.MachineSet
447523
machines []*clusterv1.Machine
448524
expectCondition metav1.Condition
449525
}{
450-
// TODO: Add test cases.
526+
{
527+
name: "getAndAdoptMachines failed",
528+
machineSet: machineSet,
529+
machines: nil,
530+
expectCondition: metav1.Condition{
531+
Type: clusterv1.MachineSetMachinesUpToDateV1Beta2Condition,
532+
Status: metav1.ConditionUnknown,
533+
Reason: clusterv1.MachineSetMachinesUpToDateInternalErrorV1Beta2Reason,
534+
Message: "Please check controller logs for errors",
535+
},
536+
},
537+
{
538+
name: "One machine up-to-date",
539+
machineSet: machineSet,
540+
machines: []*clusterv1.Machine{
541+
createMachine("up-to-date-1", metav1.Condition{
542+
Type: clusterv1.MachineUpToDateV1Beta2Condition,
543+
Status: metav1.ConditionTrue,
544+
Reason: "some-reason-1",
545+
}),
546+
},
547+
expectCondition: metav1.Condition{
548+
Type: clusterv1.MachineSetMachinesUpToDateV1Beta2Condition,
549+
Status: metav1.ConditionTrue,
550+
Reason: "some-reason-1",
551+
Message: "",
552+
},
553+
},
554+
{
555+
name: "One machine unknown",
556+
machineSet: machineSet,
557+
machines: []*clusterv1.Machine{
558+
createMachine("unknown-1", metav1.Condition{
559+
Type: clusterv1.MachineUpToDateV1Beta2Condition,
560+
Status: metav1.ConditionUnknown,
561+
Reason: "some-unknown-reason-1",
562+
Message: "some unknown message",
563+
}),
564+
},
565+
expectCondition: metav1.Condition{
566+
Type: clusterv1.MachineSetMachinesUpToDateV1Beta2Condition,
567+
Status: metav1.ConditionUnknown,
568+
Reason: "some-unknown-reason-1",
569+
Message: "some unknown message from Machine unknown-1",
570+
},
571+
},
572+
{
573+
name: "One machine not up-to-date",
574+
machineSet: machineSet,
575+
machines: []*clusterv1.Machine{
576+
createMachine("not-up-to-date-machine-1", metav1.Condition{
577+
Type: clusterv1.MachineUpToDateV1Beta2Condition,
578+
Status: metav1.ConditionFalse,
579+
Reason: "some-not-up-to-date-reason",
580+
Message: "some not up-to-date message",
581+
}),
582+
},
583+
expectCondition: metav1.Condition{
584+
Type: clusterv1.MachineSetMachinesUpToDateV1Beta2Condition,
585+
Status: metav1.ConditionFalse,
586+
Reason: "some-not-up-to-date-reason",
587+
Message: "some not up-to-date message from Machine not-up-to-date-machine-1",
588+
},
589+
},
451590
}
452591
for _, tt := range tests {
453592
t.Run(tt.name, func(t *testing.T) {
454593
g := NewWithT(t)
455594

456-
setMachinesUpToDateCondition(ctx, tt.machineSet, tt.machines, true)
595+
setMachinesUpToDateCondition(ctx, tt.machineSet, tt.machines, tt.machines != nil)
457596

458597
condition := v1beta2conditions.Get(tt.machineSet, clusterv1.MachineSetMachinesUpToDateV1Beta2Condition)
459598
g.Expect(condition).ToNot(BeNil())
460599
g.Expect(*condition).To(v1beta2conditions.MatchCondition(tt.expectCondition, v1beta2conditions.IgnoreLastTransitionTime(true)))
461600
})
462601
}
463602
}
603+
604+
func createMachine(name string, conditions ...metav1.Condition) *clusterv1.Machine {
605+
m := &clusterv1.Machine{
606+
ObjectMeta: metav1.ObjectMeta{
607+
Name: name,
608+
Namespace: metav1.NamespaceDefault,
609+
},
610+
}
611+
612+
for _, condition := range conditions {
613+
v1beta2conditions.Set(m, condition)
614+
}
615+
616+
return m
617+
}
618+
619+
func testMachine(name string, staleDeleting bool) *clusterv1.Machine {
620+
m := createMachine(name)
621+
if staleDeleting {
622+
m.DeletionTimestamp = ptr.To(metav1.Time{Time: time.Now().Add(-1 * time.Hour)})
623+
}
624+
return m
625+
}

0 commit comments

Comments
 (0)