@@ -15,6 +15,7 @@ import (
15
15
corev1 "k8s.io/api/core/v1"
16
16
apierrors "k8s.io/apimachinery/pkg/api/errors"
17
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18
+ utilerrors "k8s.io/apimachinery/pkg/util/errors"
18
19
"k8s.io/utils/pointer"
19
20
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
20
21
"sigs.k8s.io/cluster-api/util/conditions"
@@ -26,6 +27,7 @@ import (
26
27
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako"
27
28
ako_operator "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/ako-operator"
28
29
"github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/test/builder"
30
+ "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/test/util"
29
31
testutil "github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pkg/test/util"
30
32
)
31
33
@@ -90,11 +92,28 @@ func intgTestAkoDeploymentConfigController() {
90
92
Expect (err ).To (BeNil ())
91
93
}
92
94
}
93
- deleteObjects := func (objs ... client.Object ) {
94
- for _ , o := range objs {
95
- // ignore error
96
- _ = ctx .Client .Delete (ctx .Context , o )
95
+
96
+ ensureObjectsDeleted := func (obj ... client.Object ) error {
97
+ errs := []error {}
98
+ for _ , o := range obj {
99
+ if err := ctx .Client .Get (ctx , client .ObjectKeyFromObject (o ), o ); err != nil {
100
+ if apierrors .IsNotFound (err ) {
101
+ return nil
102
+ }
103
+ errs = append (errs , err )
104
+ }
105
+
106
+ if err := ctx .Client .Delete (ctx , o ); err != nil {
107
+ if ! apierrors .IsNotFound (err ) {
108
+ errs = append (errs , err )
109
+ }
110
+ }
111
+ }
112
+
113
+ if len (errs ) > 0 {
114
+ return utilerrors .NewAggregate (errs )
97
115
}
116
+ return nil
98
117
}
99
118
getCluster := func (obj * clusterv1.Cluster , name , namespace string ) error {
100
119
err := ctx .Client .Get (ctx .Context , client.ObjectKey {
@@ -182,7 +201,19 @@ func intgTestAkoDeploymentConfigController() {
182
201
}
183
202
_ , ok := obj .Labels [label ]
184
203
return expect == ok
185
- }, "30s" , "5s" ).Should (BeTrue ())
204
+ }).Should (BeTrue ())
205
+ }
206
+
207
+ ensureClusterAviLabelValueMatchExpectation := func (key client.ObjectKey , label , value string , expect bool ) {
208
+ Eventually (func () bool {
209
+ obj := & clusterv1.Cluster {}
210
+ err := ctx .Client .Get (ctx .Context , key , obj )
211
+ if err != nil {
212
+ return false
213
+ }
214
+ v := obj .Labels [label ]
215
+ return (v == value ) == expect
216
+ }).Should (BeTrue ())
186
217
}
187
218
188
219
ensureSubnetMatchExpectation := func (newIPAddrEnd string , expect bool ) {
@@ -306,25 +337,15 @@ func intgTestAkoDeploymentConfigController() {
306
337
307
338
})
308
339
AfterEach (func () {
309
- latestCluster := & clusterv1.Cluster {}
310
- if err := getCluster (latestCluster , cluster .Name , cluster .Namespace ); err == nil {
311
- latestCluster .Finalizers = nil
312
- deleteObjects (latestCluster )
313
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
340
+ latestCluster := & clusterv1.Cluster {
341
+ ObjectMeta : metav1.ObjectMeta {
314
342
Name : cluster .Name ,
315
343
Namespace : cluster .Namespace ,
316
- }, & clusterv1. Cluster {}, false )
344
+ },
317
345
}
318
- deleteObjects (cluster )
319
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
320
- Name : cluster .Name ,
321
- Namespace : cluster .Namespace ,
322
- }, & clusterv1.Cluster {}, false )
323
- deleteObjects (akoDeploymentConfig )
324
- deleteObjects (controllerCredentials , controllerCA )
325
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
326
- Name : akoDeploymentConfig .Name ,
327
- }, & akoov1alpha1.AKODeploymentConfig {}, false )
346
+ Eventually (func () error {
347
+ return ensureObjectsDeleted (latestCluster , akoDeploymentConfig , controllerCredentials , controllerCA )
348
+ }, "30s" , "5s" ).Should (BeNil ())
328
349
err := os .Setenv (ako_operator .IsControlPlaneHAProvider , "False" )
329
350
Expect (err ).ShouldNot (HaveOccurred ())
330
351
})
@@ -369,27 +390,22 @@ func intgTestAkoDeploymentConfigController() {
369
390
BeforeEach (func () {
370
391
createObjects (akoDeploymentConfig , cluster , controllerCredentials , controllerCA )
371
392
conditions .MarkTrue (cluster , clusterv1 .ReadyCondition )
372
- err = ctx .Client .Status ().Update (ctx , cluster )
373
- Expect (err ).To (BeNil ())
393
+ Eventually (func () error {
394
+ err = ctx .Client .Status ().Update (ctx , cluster )
395
+ return err
396
+ }).Should (BeNil ())
374
397
_ = kcfg .CreateSecret (ctx , ctx .Client , cluster )
375
398
})
376
399
AfterEach (func () {
377
- latestCluster := & clusterv1.Cluster {}
378
- if err := getCluster (latestCluster , cluster .Name , cluster .Namespace ); err == nil {
379
- latestCluster .Finalizers = nil
380
- deleteObjects (latestCluster )
381
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
400
+ latestCluster := & clusterv1.Cluster {
401
+ ObjectMeta : metav1.ObjectMeta {
382
402
Name : cluster .Name ,
383
403
Namespace : cluster .Namespace ,
384
- }, & clusterv1. Cluster {}, false )
404
+ },
385
405
}
386
-
387
- deleteObjects (akoDeploymentConfig )
388
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
389
- Name : akoDeploymentConfig .Name ,
390
- }, & akoov1alpha1.AKODeploymentConfig {}, false )
391
-
392
- deleteObjects (controllerCredentials , controllerCA )
406
+ Eventually (func () error {
407
+ return ensureObjectsDeleted (latestCluster , akoDeploymentConfig , controllerCredentials , controllerCA )
408
+ }, "30s" , "5s" ).Should (BeNil ())
393
409
})
394
410
When ("there is no matching cluster" , func () {
395
411
It ("cluster should not have ClusterFinalizer" , func () {
@@ -618,18 +634,15 @@ func intgTestAkoDeploymentConfigController() {
618
634
When ("the cluster is being deleted " , func () {
619
635
When ("the cluster is ready" , func () {
620
636
BeforeEach (func () {
621
- latestCluster := & clusterv1.Cluster {}
622
- err := getCluster (latestCluster , cluster .Name , cluster .Namespace )
623
- Expect (err ).To (BeNil ())
624
- conditions .MarkTrue (latestCluster , akoov1alpha1 .AviResourceCleanupSucceededCondition )
625
- err = ctx .Client .Status ().Update (ctx , latestCluster )
626
- Expect (err ).To (BeNil ())
627
- deleteObjects (latestCluster )
628
-
629
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
630
- Name : cluster .Name ,
631
- Namespace : cluster .Namespace ,
632
- }, & clusterv1.Cluster {}, false )
637
+ latestCluster := & clusterv1.Cluster {
638
+ ObjectMeta : metav1.ObjectMeta {
639
+ Name : cluster .Name ,
640
+ Namespace : cluster .Namespace ,
641
+ },
642
+ }
643
+ Eventually (func () error {
644
+ return ensureObjectsDeleted (latestCluster )
645
+ }).Should (BeNil ())
633
646
})
634
647
635
648
//Reconcile -> reconcileNormal -> r.userReconciler.ReconcileAviUserDelete
@@ -647,13 +660,13 @@ func intgTestAkoDeploymentConfigController() {
647
660
Expect (err ).To (BeNil ())
648
661
conditions .MarkFalse (obj , clusterv1 .ReadyCondition , clusterv1 .DeletingReason , clusterv1 .ConditionSeverityInfo , "" )
649
662
conditions .MarkTrue (obj , akoov1alpha1 .AviResourceCleanupSucceededCondition )
650
- err = ctx . Client . Status (). Update ( ctx , obj )
651
- Expect ( err ). To ( BeNil () )
652
- deleteObjects ( obj )
653
- ensureRuntimeObjectMatchExpectation (client. ObjectKey {
654
- Name : obj . Name ,
655
- Namespace : obj . Namespace ,
656
- }, & clusterv1. Cluster {}, false )
663
+ Eventually ( func () error {
664
+ err = ctx . Client . Status (). Update ( ctx , obj )
665
+ return err
666
+ }). Should ( BeNil ())
667
+ Eventually ( func () error {
668
+ return ensureObjectsDeleted ( obj )
669
+ }). Should ( BeNil () )
657
670
})
658
671
659
672
//Reconcile -> reconcileNormal -> r.userReconciler.ReconcileAviUserDelete
@@ -671,10 +684,9 @@ func intgTestAkoDeploymentConfigController() {
671
684
// Reconcile -> reconcileDelete
672
685
When ("AKODeploymentConfig is being deleted" , func () {
673
686
BeforeEach (func () {
674
- deleteObjects (akoDeploymentConfig )
675
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
676
- Name : akoDeploymentConfig .Name ,
677
- }, & akoov1alpha1.AKODeploymentConfig {}, false )
687
+ Eventually (func () error {
688
+ return ensureObjectsDeleted (akoDeploymentConfig )
689
+ }).Should (BeNil ())
678
690
})
679
691
680
692
// Reconcile -> reconcileDelete -> phases.ReconcilePhases(normal)
@@ -704,11 +716,9 @@ func intgTestAkoDeploymentConfigController() {
704
716
705
717
When ("the cluster is being deleted " , func () {
706
718
BeforeEach (func () {
707
- deleteObjects (cluster )
708
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
709
- Name : cluster .Name ,
710
- Namespace : cluster .Namespace ,
711
- }, & clusterv1.Cluster {}, false )
719
+ Eventually (func () error {
720
+ return ensureObjectsDeleted (cluster )
721
+ }).Should (BeNil ())
712
722
})
713
723
714
724
//Reconcile -> reconcileDelete -> r.reconcileClustersDelete -> r.clusterReconciler.ReconcileAddonSecretDelete
@@ -722,7 +732,7 @@ func intgTestAkoDeploymentConfigController() {
722
732
})
723
733
})
724
734
725
- // Tests for adding & removing the networking.tkg.tanzu.vmware.com/avi-skip-default-adc labels
735
+ // Tests when there are multpile ADC selecting the same cluster.
726
736
// When there is matching cluster for ADC -> and when there is another ADC install-ako-for-all
727
737
defaultAkoDeploymentConfig := staticDefaultAkoDeploymentConfig .DeepCopy ()
728
738
defaultAkoDeploymentConfigWithNonEmptyClusterSelector := staticDefaultAkoDeploymentConfig .DeepCopy ()
@@ -732,14 +742,16 @@ func intgTestAkoDeploymentConfigController() {
732
742
733
743
defaultADCTestCaseInputs := []DefaultADCTestCaseInput {
734
744
{
735
- Name : "there is default ADC install-ako-for-all" ,
736
- DefaultADC : defaultAkoDeploymentConfig ,
745
+ Name : "there is default ADC install-ako-for-all" ,
746
+ DefaultADC : defaultAkoDeploymentConfig ,
747
+ HasSelector : false ,
737
748
},
738
749
{
739
750
// This test case covers the bug https://github.com/vmware-tanzu/load-balancer-operator-for-kubernetes/pull/81
740
751
// The bug was triggerred when the default workload ADC install-ako-for-all has non-empty cluster selector
741
- Name : "there is default ADC with non-empty clusterSelector" ,
742
- DefaultADC : defaultAkoDeploymentConfigWithNonEmptyClusterSelector ,
752
+ Name : "there is default ADC with non-empty clusterSelector" ,
753
+ DefaultADC : defaultAkoDeploymentConfigWithNonEmptyClusterSelector ,
754
+ HasSelector : true ,
743
755
},
744
756
}
745
757
@@ -755,17 +767,20 @@ func intgTestAkoDeploymentConfigController() {
755
767
})
756
768
757
769
AfterEach (func () {
758
- deleteObjects (defaultADC )
759
- ensureRuntimeObjectMatchExpectation (client.ObjectKey {
760
- Name : akoov1alpha1 .WorkloadClusterAkoDeploymentConfig ,
761
- }, & akoov1alpha1.AKODeploymentConfig {}, false )
770
+ Eventually (func () error {
771
+ return ensureObjectsDeleted (defaultADC )
772
+ }).Should (BeNil ())
773
+ ensureClusterAviLabelMatchExpectation (client.ObjectKey {
774
+ Name : cluster .Name ,
775
+ Namespace : cluster .Namespace ,
776
+ }, akoov1alpha1 .AviClusterLabel , false )
762
777
})
763
778
764
779
It ("is selected by a customized ADC" , func () {
765
- ensureClusterAviLabelMatchExpectation (client.ObjectKey {
780
+ ensureClusterAviLabelValueMatchExpectation (client.ObjectKey {
766
781
Name : cluster .Name ,
767
782
Namespace : cluster .Namespace ,
768
- }, akoov1alpha1 .AviClusterLabel , true )
783
+ }, akoov1alpha1 .AviClusterLabel , util . CustomADCName , true )
769
784
})
770
785
771
786
When ("no longer selected by a customized ADC" , func () {
@@ -781,11 +796,18 @@ func intgTestAkoDeploymentConfigController() {
781
796
}, "test" , false )
782
797
})
783
798
784
- It ("should drop the AviClusterLabel) " , func () {
785
- ensureClusterAviLabelMatchExpectation (client.ObjectKey {
799
+ It ("picks up the default ADC if there is no selector on default ADC " , func () {
800
+ ensureClusterAviLabelValueMatchExpectation (client.ObjectKey {
786
801
Name : cluster .Name ,
787
802
Namespace : cluster .Namespace ,
788
- }, akoov1alpha1 .AviClusterLabel , false )
803
+ }, akoov1alpha1 .AviClusterLabel , akoov1alpha1 .WorkloadClusterAkoDeploymentConfig , ! tc .HasSelector )
804
+ })
805
+
806
+ It ("should drop the AviClusterLabel for custom ADC" , func () {
807
+ ensureClusterAviLabelValueMatchExpectation (client.ObjectKey {
808
+ Name : cluster .Name ,
809
+ Namespace : cluster .Namespace ,
810
+ }, akoov1alpha1 .AviClusterLabel , util .CustomADCName , false )
789
811
})
790
812
})
791
813
})
@@ -795,6 +817,7 @@ func intgTestAkoDeploymentConfigController() {
795
817
}
796
818
797
819
type DefaultADCTestCaseInput struct {
798
- Name string // test case name
799
- DefaultADC * akoov1alpha1.AKODeploymentConfig // default ADC input
820
+ Name string // test case name
821
+ DefaultADC * akoov1alpha1.AKODeploymentConfig // default ADC input
822
+ HasSelector bool // whether the default ADC has a cluster selector
800
823
}
0 commit comments