@@ -23,6 +23,9 @@ import (
23
23
"github.com/pkg/errors"
24
24
corev1 "k8s.io/api/core/v1"
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
+ "k8s.io/apimachinery/pkg/runtime"
27
+ "sigs.k8s.io/controller-runtime/pkg/client"
28
+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
26
29
27
30
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
28
31
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -32,11 +35,16 @@ import (
32
35
)
33
36
34
37
func TestReconcileTopologyReconciledCondition (t * testing.T ) {
38
+ g := NewWithT (t )
39
+ scheme := runtime .NewScheme ()
40
+ g .Expect (clusterv1 .AddToScheme (scheme )).To (Succeed ())
41
+
35
42
tests := []struct {
36
43
name string
37
44
reconcileErr error
38
45
s * scope.Scope
39
46
cluster * clusterv1.Cluster
47
+ machines []* clusterv1.Machine
40
48
wantConditionStatus corev1.ConditionStatus
41
49
wantConditionReason string
42
50
wantConditionMessage string
@@ -382,7 +390,7 @@ func TestReconcileTopologyReconciledCondition(t *testing.T) {
382
390
wantConditionStatus : corev1 .ConditionTrue ,
383
391
},
384
392
{
385
- name : "should set the condition to false is some machine deployments have not picked the new version because other machine deployments are rolling out (not all replicas ready) " ,
393
+ name : "should set the condition to false is some machine deployments have not picked the new version because other machine deployments are upgrading " ,
386
394
reconcileErr : nil ,
387
395
cluster : & clusterv1.Cluster {},
388
396
s : & scope.Scope {
@@ -404,6 +412,11 @@ func TestReconcileTopologyReconciledCondition(t *testing.T) {
404
412
Object : builder .MachineDeployment ("ns1" , "md0-abc123" ).
405
413
WithReplicas (2 ).
406
414
WithVersion ("v1.22.0" ).
415
+ WithSelector (metav1.LabelSelector {
416
+ MatchLabels : map [string ]string {
417
+ clusterv1 .ClusterTopologyMachineDeploymentNameLabel : "md0" ,
418
+ },
419
+ }).
407
420
WithStatus (clusterv1.MachineDeploymentStatus {
408
421
// MD is not ready because we don't have 2 updated, ready and available replicas.
409
422
Replicas : int32 (2 ),
@@ -418,67 +431,11 @@ func TestReconcileTopologyReconciledCondition(t *testing.T) {
418
431
Object : builder .MachineDeployment ("ns1" , "md1-abc123" ).
419
432
WithReplicas (2 ).
420
433
WithVersion ("v1.21.2" ).
421
- WithStatus (clusterv1.MachineDeploymentStatus {
422
- Replicas : int32 (2 ),
423
- UpdatedReplicas : int32 (2 ),
424
- ReadyReplicas : int32 (2 ),
425
- AvailableReplicas : int32 (2 ),
426
- UnavailableReplicas : int32 (0 ),
434
+ WithSelector (metav1.LabelSelector {
435
+ MatchLabels : map [string ]string {
436
+ clusterv1 .ClusterTopologyMachineDeploymentNameLabel : "md1" ,
437
+ },
427
438
}).
428
- Build (),
429
- },
430
- },
431
- },
432
- UpgradeTracker : func () * scope.UpgradeTracker {
433
- ut := scope .NewUpgradeTracker ()
434
- ut .ControlPlane .PendingUpgrade = false
435
- ut .MachineDeployments .MarkRollingOut ("md0-abc123" )
436
- ut .MachineDeployments .MarkPendingUpgrade ("md1-abc123" )
437
- return ut
438
- }(),
439
- HookResponseTracker : scope .NewHookResponseTracker (),
440
- },
441
- wantConditionStatus : corev1 .ConditionFalse ,
442
- wantConditionReason : clusterv1 .TopologyReconciledMachineDeploymentsUpgradePendingReason ,
443
- wantConditionMessage : "MachineDeployment(s) md1-abc123 upgrade to version v1.22.0 on hold. MachineDeployment(s) md0-abc123 are rolling out" ,
444
- },
445
- {
446
- name : "should set the condition to false if some machine deployments have not picked the new version because other machine deployments are rolling out (unavailable replica)" ,
447
- reconcileErr : nil ,
448
- cluster : & clusterv1.Cluster {},
449
- s : & scope.Scope {
450
- Blueprint : & scope.ClusterBlueprint {
451
- Topology : & clusterv1.Topology {
452
- Version : "v1.22.0" ,
453
- },
454
- },
455
- Current : & scope.ClusterState {
456
- Cluster : & clusterv1.Cluster {},
457
- ControlPlane : & scope.ControlPlaneState {
458
- Object : builder .ControlPlane ("ns1" , "controlplane1" ).
459
- WithVersion ("v1.22.0" ).
460
- WithReplicas (3 ).
461
- Build (),
462
- },
463
- MachineDeployments : scope.MachineDeploymentsStateMap {
464
- "md0" : & scope.MachineDeploymentState {
465
- Object : builder .MachineDeployment ("ns1" , "md0-abc123" ).
466
- WithReplicas (2 ).
467
- WithVersion ("v1.22.0" ).
468
- WithStatus (clusterv1.MachineDeploymentStatus {
469
- // MD is not ready because we still have an unavailable replica.
470
- Replicas : int32 (2 ),
471
- UpdatedReplicas : int32 (2 ),
472
- ReadyReplicas : int32 (2 ),
473
- AvailableReplicas : int32 (2 ),
474
- UnavailableReplicas : int32 (1 ),
475
- }).
476
- Build (),
477
- },
478
- "md1" : & scope.MachineDeploymentState {
479
- Object : builder .MachineDeployment ("ns1" , "md1-abc123" ).
480
- WithReplicas (2 ).
481
- WithVersion ("v1.21.2" ).
482
439
WithStatus (clusterv1.MachineDeploymentStatus {
483
440
Replicas : int32 (2 ),
484
441
UpdatedReplicas : int32 (2 ),
@@ -493,15 +450,25 @@ func TestReconcileTopologyReconciledCondition(t *testing.T) {
493
450
UpgradeTracker : func () * scope.UpgradeTracker {
494
451
ut := scope .NewUpgradeTracker ()
495
452
ut .ControlPlane .PendingUpgrade = false
496
- ut .MachineDeployments .MarkRollingOut ("md0-abc123" )
453
+ ut .MachineDeployments .MarkUpgradingAndRollingOut ("md0-abc123" )
497
454
ut .MachineDeployments .MarkPendingUpgrade ("md1-abc123" )
498
455
return ut
499
456
}(),
500
457
HookResponseTracker : scope .NewHookResponseTracker (),
501
458
},
459
+ machines : []* clusterv1.Machine {
460
+ builder .Machine ("ns1" , "md0-machine0" ).
461
+ WithLabels (map [string ]string {clusterv1 .ClusterTopologyMachineDeploymentNameLabel : "md0" }).
462
+ WithVersion ("v1.21.2" ). // Machine's version does not match MachineDeployment's version
463
+ Build (),
464
+ builder .Machine ("ns1" , "md1-machine0" ).
465
+ WithLabels (map [string ]string {clusterv1 .ClusterTopologyMachineDeploymentNameLabel : "md1" }).
466
+ WithVersion ("v1.21.2" ).
467
+ Build (),
468
+ },
502
469
wantConditionStatus : corev1 .ConditionFalse ,
503
470
wantConditionReason : clusterv1 .TopologyReconciledMachineDeploymentsUpgradePendingReason ,
504
- wantConditionMessage : "MachineDeployment(s) md1-abc123 upgrade to version v1.22.0 on hold. MachineDeployment(s) md0-abc123 are rolling out " ,
471
+ wantConditionMessage : "MachineDeployment(s) md1-abc123 upgrade to version v1.22.0 on hold. MachineDeployment(s) md0-abc123 are upgrading " ,
505
472
},
506
473
{
507
474
name : "should set the condition to false if some machine deployments have not picked the new version because their upgrade has been deferred" ,
@@ -624,7 +591,18 @@ func TestReconcileTopologyReconciledCondition(t *testing.T) {
624
591
t .Run (tt .name , func (t * testing.T ) {
625
592
g := NewWithT (t )
626
593
627
- r := & Reconciler {}
594
+ objs := []client.Object {}
595
+ if tt .s != nil && tt .s .Current != nil {
596
+ for _ , mds := range tt .s .Current .MachineDeployments {
597
+ objs = append (objs , mds .Object )
598
+ }
599
+ }
600
+ for _ , m := range tt .machines {
601
+ objs = append (objs , m )
602
+ }
603
+ fakeClient := fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (objs ... ).Build ()
604
+
605
+ r := & Reconciler {Client : fakeClient }
628
606
err := r .reconcileTopologyReconciledCondition (tt .s , tt .cluster , tt .reconcileErr )
629
607
if tt .wantErr {
630
608
g .Expect (err ).To (HaveOccurred ())
0 commit comments