Skip to content

Commit 0b13f4e

Browse files
Updates conditions pkg to use []machinev1.Condition
1 parent a681794 commit 0b13f4e

File tree

10 files changed

+76
-66
lines changed

10 files changed

+76
-66
lines changed

pkg/controller/machine/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (r *ReconcileMachine) Reconcile(ctx context.Context, request reconcile.Requ
165165

166166
// Get the original state of conditions now so that they can be used to calculate the patch later.
167167
// This must be a copy otherwise the referenced slice will be modified by later machine conditions changes.
168-
originalConditions := m.Status.Conditions.DeepCopy()
168+
originalConditions := conditions.DeepCopyConditions(m.Status.Conditions)
169169

170170
if errList := validateMachine(m); len(errList) > 0 {
171171
err := fmt.Errorf("%v: machine validation failed: %v", machineName, errList.ToAggregate().Error())
@@ -419,7 +419,7 @@ func (r *ReconcileMachine) updateStatus(ctx context.Context, machine *machinev1.
419419

420420
// Conditions need to be deep copied as they are set outside of this function.
421421
// They will be restored after any updates to the base (done by patching annotations).
422-
conditions := machine.Status.Conditions.DeepCopy()
422+
conditions := conditions.DeepCopyConditions(machine.Status.Conditions)
423423

424424
// A call to Patch will mutate our local copy of the machine to match what is stored in the API.
425425
// Before we make any changes to the status subresource on our local copy, we need to patch the object first,

pkg/controller/machine/controller_test.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ func TestReconcileRequest(t *testing.T) {
328328
},
329329
},
330330
Status: machinev1.MachineStatus{
331-
Conditions: machinev1.Conditions{*conditions.TrueCondition(machinev1.MachineDrained)},
331+
AuthoritativeAPI: machinev1.MachineAuthorityMachineAPI,
332+
Conditions: []machinev1.Condition{*conditions.TrueCondition(machinev1.MachineDrained)},
332333
},
333334
}
334335

@@ -570,7 +571,7 @@ func TestReconcileRequest(t *testing.T) {
570571
func TestUpdateStatus(t *testing.T) {
571572
drainableTrue := conditions.TrueCondition(machinev1.MachineDrainable)
572573
terminableTrue := conditions.TrueCondition(machinev1.MachineTerminable)
573-
defaultLifecycleConditions := machinev1.Conditions{*drainableTrue, *terminableTrue}
574+
defaultLifecycleConditions := []machinev1.Condition{*drainableTrue, *terminableTrue}
574575

575576
testCases := []struct {
576577
name string
@@ -579,8 +580,8 @@ func TestUpdateStatus(t *testing.T) {
579580
annotations map[string]string
580581
existingProviderStatus string
581582
expectedProviderStatus string
582-
conditions machinev1.Conditions
583-
originalConditions machinev1.Conditions
583+
conditions []machinev1.Condition
584+
originalConditions []machinev1.Condition
584585
updated bool
585586
}{
586587
{
@@ -641,7 +642,7 @@ func TestUpdateStatus(t *testing.T) {
641642
phase: machinev1.PhaseRunning,
642643
err: nil,
643644
annotations: nil,
644-
conditions: machinev1.Conditions{
645+
conditions: []machinev1.Condition{
645646
*conditions.TrueCondition(machinev1.InstanceExistsCondition),
646647
*drainableTrue,
647648
*terminableTrue,
@@ -653,12 +654,12 @@ func TestUpdateStatus(t *testing.T) {
653654
phase: machinev1.PhaseRunning,
654655
err: nil,
655656
annotations: nil,
656-
conditions: machinev1.Conditions{
657+
conditions: []machinev1.Condition{
657658
*conditions.FalseCondition(machinev1.InstanceExistsCondition, machinev1.InstanceMissingReason, machinev1.ConditionSeverityWarning, "message"),
658659
*drainableTrue,
659660
*terminableTrue,
660661
},
661-
originalConditions: machinev1.Conditions{
662+
originalConditions: []machinev1.Condition{
662663
*conditions.TrueCondition(machinev1.InstanceExistsCondition),
663664
},
664665
updated: true,
@@ -668,12 +669,12 @@ func TestUpdateStatus(t *testing.T) {
668669
phase: machinev1.PhaseRunning,
669670
err: nil,
670671
annotations: nil,
671-
conditions: machinev1.Conditions{
672+
conditions: []machinev1.Condition{
672673
*conditions.TrueCondition(machinev1.InstanceExistsCondition),
673674
*drainableTrue,
674675
*terminableTrue,
675676
},
676-
originalConditions: machinev1.Conditions{
677+
originalConditions: []machinev1.Condition{
677678
*conditions.TrueCondition(machinev1.InstanceExistsCondition),
678679
},
679680
updated: false,
@@ -734,7 +735,7 @@ func TestUpdateStatus(t *testing.T) {
734735
}
735736

736737
// Set the phase to Running initially
737-
g.Expect(reconciler.updateStatus(context.TODO(), machine, machinev1.PhaseRunning, nil, machinev1.Conditions{})).To(Succeed())
738+
g.Expect(reconciler.updateStatus(context.TODO(), machine, machinev1.PhaseRunning, nil, []machinev1.Condition{})).To(Succeed())
738739
// validate persisted object
739740
got := machinev1.Machine{}
740741
g.Expect(reconciler.Client.Get(context.TODO(), namespacedName, &got)).To(Succeed())
@@ -1088,92 +1089,92 @@ func TestSetLifecycleHookConditions(t *testing.T) {
10881089

10891090
testCases := []struct {
10901091
name string
1091-
existingConditions machinev1.Conditions
1092+
existingConditions []machinev1.Condition
10921093
lifecycleHooks machinev1.LifecycleHooks
1093-
expectedConditions machinev1.Conditions
1094+
expectedConditions []machinev1.Condition
10941095
}{
10951096
{
10961097
name: "with a fresh machine",
1097-
expectedConditions: machinev1.Conditions{
1098+
expectedConditions: []machinev1.Condition{
10981099
*drainableTrue,
10991100
*terminableTrue,
11001101
},
11011102
},
11021103
{
11031104
name: "with an unrelated condition",
1104-
existingConditions: machinev1.Conditions{
1105+
existingConditions: []machinev1.Condition{
11051106
*unrelatedCondition,
11061107
},
1107-
expectedConditions: machinev1.Conditions{
1108+
expectedConditions: []machinev1.Condition{
11081109
*unrelatedCondition,
11091110
*drainableTrue,
11101111
*terminableTrue,
11111112
},
11121113
},
11131114
{
11141115
name: "with a pre-drain hook",
1115-
existingConditions: machinev1.Conditions{
1116+
existingConditions: []machinev1.Condition{
11161117
*drainableTrue,
11171118
*terminableTrue,
11181119
},
11191120
lifecycleHooks: machinev1.LifecycleHooks{
11201121
PreDrain: []machinev1.LifecycleHook{preDrainHook},
11211122
},
1122-
expectedConditions: machinev1.Conditions{
1123+
expectedConditions: []machinev1.Condition{
11231124
*drainableFalse,
11241125
*terminableTrue,
11251126
},
11261127
},
11271128
{
11281129
name: "with a pre-terminate hook",
1129-
existingConditions: machinev1.Conditions{
1130+
existingConditions: []machinev1.Condition{
11301131
*drainableTrue,
11311132
*terminableTrue,
11321133
},
11331134
lifecycleHooks: machinev1.LifecycleHooks{
11341135
PreTerminate: []machinev1.LifecycleHook{preTerminateHook},
11351136
},
1136-
expectedConditions: machinev1.Conditions{
1137+
expectedConditions: []machinev1.Condition{
11371138
*drainableTrue,
11381139
*terminableFalse,
11391140
},
11401141
},
11411142
{
11421143
name: "with a both a pre-drain and pre-terminate hook",
1143-
existingConditions: machinev1.Conditions{
1144+
existingConditions: []machinev1.Condition{
11441145
*drainableTrue,
11451146
*terminableTrue,
11461147
},
11471148
lifecycleHooks: machinev1.LifecycleHooks{
11481149
PreDrain: []machinev1.LifecycleHook{preDrainHook},
11491150
PreTerminate: []machinev1.LifecycleHook{preTerminateHook},
11501151
},
1151-
expectedConditions: machinev1.Conditions{
1152+
expectedConditions: []machinev1.Condition{
11521153
*drainableFalse,
11531154
*terminableFalse,
11541155
},
11551156
},
11561157
{
11571158
name: "with multiple pre-drain hooks",
1158-
existingConditions: machinev1.Conditions{
1159+
existingConditions: []machinev1.Condition{
11591160
*drainableTrue,
11601161
*terminableTrue,
11611162
},
11621163
lifecycleHooks: machinev1.LifecycleHooks{
11631164
PreDrain: []machinev1.LifecycleHook{preDrainHook, otherPreDrainHook},
11641165
},
1165-
expectedConditions: machinev1.Conditions{
1166+
expectedConditions: []machinev1.Condition{
11661167
*drainableFalseWithOther,
11671168
*terminableTrue,
11681169
},
11691170
},
11701171
{
11711172
name: "with hooks are removed",
1172-
existingConditions: machinev1.Conditions{
1173+
existingConditions: []machinev1.Condition{
11731174
*drainableFalse,
11741175
*terminableFalse,
11751176
},
1176-
expectedConditions: machinev1.Conditions{
1177+
expectedConditions: []machinev1.Condition{
11771178
*drainableTrue,
11781179
*terminableTrue,
11791180
},

pkg/controller/machine/drain_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func TestDrainControllerReconcileRequest(t *testing.T) {
6969
}, recorder
7070
}
7171

72-
getDrainedConditions := func(msg string) machinev1.Conditions {
72+
getDrainedConditions := func(msg string) []machinev1.Condition {
7373
condition := conditions.TrueCondition(machinev1.MachineDrained)
7474
condition.Message = msg
75-
return machinev1.Conditions{*condition}
75+
return []machinev1.Condition{*condition}
7676
}
7777

7878
t.Run("ignore machine not in the deleting phase", func(t *testing.T) {

pkg/controller/machinehealthcheck/machinehealthcheck_controller_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func TestReconcile(t *testing.T) {
265265
ExpectedMachines: IntPtr(1),
266266
CurrentHealthy: IntPtr(0),
267267
RemediationsAllowed: 0,
268-
Conditions: machinev1.Conditions{
268+
Conditions: []machinev1.Condition{
269269
remediationAllowedCondition,
270270
},
271271
},
@@ -296,7 +296,7 @@ func TestReconcile(t *testing.T) {
296296
ExpectedMachines: IntPtr(1),
297297
CurrentHealthy: IntPtr(1),
298298
RemediationsAllowed: 1,
299-
Conditions: machinev1.Conditions{
299+
Conditions: []machinev1.Condition{
300300
remediationAllowedCondition,
301301
},
302302
},
@@ -315,7 +315,7 @@ func TestReconcile(t *testing.T) {
315315
ExpectedMachines: IntPtr(1),
316316
CurrentHealthy: IntPtr(0),
317317
RemediationsAllowed: 0,
318-
Conditions: machinev1.Conditions{
318+
Conditions: []machinev1.Condition{
319319
remediationAllowedCondition,
320320
},
321321
},
@@ -337,7 +337,7 @@ func TestReconcile(t *testing.T) {
337337
ExpectedMachines: IntPtr(1),
338338
CurrentHealthy: IntPtr(0),
339339
RemediationsAllowed: 0,
340-
Conditions: machinev1.Conditions{
340+
Conditions: []machinev1.Condition{
341341
remediationAllowedCondition,
342342
},
343343
},
@@ -356,7 +356,7 @@ func TestReconcile(t *testing.T) {
356356
ExpectedMachines: IntPtr(0),
357357
CurrentHealthy: IntPtr(0),
358358
RemediationsAllowed: 0,
359-
Conditions: machinev1.Conditions{
359+
Conditions: []machinev1.Condition{
360360
remediationAllowedCondition,
361361
},
362362
},
@@ -375,7 +375,7 @@ func TestReconcile(t *testing.T) {
375375
ExpectedMachines: IntPtr(0),
376376
CurrentHealthy: IntPtr(0),
377377
RemediationsAllowed: 0,
378-
Conditions: machinev1.Conditions{
378+
Conditions: []machinev1.Condition{
379379
remediationAllowedCondition,
380380
},
381381
},
@@ -394,7 +394,7 @@ func TestReconcile(t *testing.T) {
394394
ExpectedMachines: IntPtr(1),
395395
CurrentHealthy: IntPtr(0),
396396
RemediationsAllowed: 0,
397-
Conditions: machinev1.Conditions{
397+
Conditions: []machinev1.Condition{
398398
remediationAllowedCondition,
399399
},
400400
},
@@ -415,7 +415,7 @@ func TestReconcile(t *testing.T) {
415415
ExpectedMachines: IntPtr(1),
416416
CurrentHealthy: IntPtr(0),
417417
RemediationsAllowed: 0,
418-
Conditions: machinev1.Conditions{
418+
Conditions: []machinev1.Condition{
419419
remediationAllowedCondition,
420420
},
421421
},
@@ -434,7 +434,7 @@ func TestReconcile(t *testing.T) {
434434
ExpectedMachines: IntPtr(1),
435435
CurrentHealthy: IntPtr(0),
436436
RemediationsAllowed: 0,
437-
Conditions: machinev1.Conditions{
437+
Conditions: []machinev1.Condition{
438438
remediationAllowedCondition,
439439
},
440440
},
@@ -453,7 +453,7 @@ func TestReconcile(t *testing.T) {
453453
ExpectedMachines: IntPtr(1),
454454
CurrentHealthy: IntPtr(1),
455455
RemediationsAllowed: 0,
456-
Conditions: machinev1.Conditions{
456+
Conditions: []machinev1.Condition{
457457
remediationAllowedCondition,
458458
},
459459
},
@@ -474,7 +474,7 @@ func TestReconcile(t *testing.T) {
474474
ExpectedMachines: IntPtr(1),
475475
CurrentHealthy: IntPtr(0),
476476
RemediationsAllowed: 0,
477-
Conditions: machinev1.Conditions{
477+
Conditions: []machinev1.Condition{
478478
{
479479
Type: machinev1.RemediationAllowedCondition,
480480
Status: corev1.ConditionFalse,
@@ -528,7 +528,7 @@ func TestReconcileExternalRemediationTemplate(t *testing.T) {
528528
ExpectedMachines: IntPtr(1),
529529
CurrentHealthy: IntPtr(1),
530530
RemediationsAllowed: 1,
531-
Conditions: machinev1.Conditions{
531+
Conditions: []machinev1.Condition{
532532
remediationAllowedCondition,
533533
},
534534
},
@@ -550,7 +550,7 @@ func TestReconcileExternalRemediationTemplate(t *testing.T) {
550550
ExpectedMachines: IntPtr(1),
551551
CurrentHealthy: IntPtr(0),
552552
RemediationsAllowed: 0,
553-
Conditions: machinev1.Conditions{
553+
Conditions: []machinev1.Condition{
554554
remediationAllowedCondition,
555555
},
556556
},
@@ -572,7 +572,7 @@ func TestReconcileExternalRemediationTemplate(t *testing.T) {
572572
ExpectedMachines: IntPtr(1),
573573
CurrentHealthy: IntPtr(0),
574574
RemediationsAllowed: 0,
575-
Conditions: machinev1.Conditions{
575+
Conditions: []machinev1.Condition{
576576
remediationAllowedCondition,
577577
},
578578
},

pkg/util/conditions/conditions.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package conditions
22

33
import (
4+
machinev1 "github.com/openshift/api/machine/v1beta1"
45
appsv1 "k8s.io/api/apps/v1"
56
corev1 "k8s.io/api/core/v1"
67
)
@@ -24,3 +25,11 @@ func GetDeploymentCondition(deployment *appsv1.Deployment, conditionType appsv1.
2425
}
2526
return nil
2627
}
28+
29+
func DeepCopyConditions(in []machinev1.Condition) []machinev1.Condition {
30+
out := make([]machinev1.Condition, 0)
31+
for _, cond := range in {
32+
out = append(out, *cond.DeepCopy())
33+
}
34+
return out
35+
}

pkg/util/conditions/gettersetter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type GetterSetter interface {
1616
metav1.Object
1717

1818
// GetConditions returns the list of conditions for a machine API object.
19-
GetConditions() machinev1.Conditions
19+
GetConditions() []machinev1.Condition
2020

2121
// SetConditions sets the list of conditions for a machine API object.
22-
SetConditions(machinev1.Conditions)
22+
SetConditions([]machinev1.Condition)
2323
}
2424

2525
// Get returns the condition with the given type, if the condition does not exists,

0 commit comments

Comments
 (0)