@@ -16,6 +16,7 @@ package perpodsg_test
1616import (
1717 "time"
1818
19+ cninode "github.com/aws/amazon-vpc-resource-controller-k8s/apis/vpcresources/v1alpha1"
1920 "github.com/aws/amazon-vpc-resource-controller-k8s/apis/vpcresources/v1beta1"
2021 "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config"
2122 "github.com/aws/amazon-vpc-resource-controller-k8s/pkg/provider/branch"
@@ -26,6 +27,7 @@ import (
2627 podWrapper "github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/resource/k8s/pod"
2728 sgpWrapper "github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/resource/k8s/sgp"
2829 "github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/utils"
30+ "github.com/samber/lo"
2931
3032 . "github.com/onsi/ginkgo/v2"
3133 . "github.com/onsi/gomega"
@@ -35,6 +37,29 @@ import (
3537 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3638)
3739
40+ var _ = Describe ("CNINode Veification" , func () {
41+ Describe ("verify CNINode mapping to nodes" , func () {
42+ Context ("when nodes are ready" , func () {
43+ It ("should have same number of CNINode no matter which mode" , func () {
44+ cniNodes , err := frameWork .NodeManager .GetCNINodeList ()
45+ Expect (err ).NotTo (HaveOccurred ())
46+ nodes , err := frameWork .NodeManager .GetNodeList ()
47+ Expect (err ).NotTo (HaveOccurred ())
48+ Expect (len (nodes .Items )).To (Equal (len (cniNodes .Items )))
49+ nameMatched := true
50+ for _ , node := range nodes .Items {
51+ if ! lo .ContainsBy (cniNodes .Items , func (cniNode cninode.CNINode ) bool {
52+ return cniNode .Name == node .Name
53+ }) {
54+ nameMatched = false
55+ }
56+ }
57+ Expect (nameMatched ).To (BeTrue ())
58+ })
59+ })
60+ })
61+ })
62+
3863var _ = Describe ("Branch ENI Pods" , func () {
3964 var (
4065 securityGroupPolicy * v1beta1.SecurityGroupPolicy
@@ -427,44 +452,55 @@ var _ = Describe("Branch ENI Pods", func() {
427452 It ("pod should not run when un-managed and run when managed" , func () {
428453 node := targetedNodes [0 ]
429454
430- By ("verifying node has trunk ENI label present" )
431- // This label is added by IPAM-D
432- _ , found := node .Labels [config .HasTrunkAttachedLabel ]
433- Expect (found ).To (BeTrue ())
434-
435- // This should never happens as once the trunk is attached,
436- // this label will not be removed again. This is for testing
437- // purposes to make a managed node an un-managed node
438- By ("removing the has-trunk-attached label from the node" )
439- err = frameWork .NodeManager .RemoveLabels (targetedNodes ,
440- map [string ]string {config .HasTrunkAttachedLabel : "true" })
441-
442- firstPod := podTemplate .DeepCopy ()
443- By ("creating a Pod on the un-managed node and verifying it fails" )
444- _ , err = frameWork .PodManager .CreateAndWaitTillPodIsRunning (ctx , firstPod , utils .ResourceCreationTimeout )
445- Expect (err ).To (HaveOccurred ())
446-
447- By ("deleting the pod" )
448- err = frameWork .PodManager .DeleteAndWaitTillPodIsDeleted (ctx , firstPod )
449- Expect (err ).ToNot (HaveOccurred ())
450-
451- // Currently we wait for some time before removing the trunk from cache
452- // to allow evicted Pods's event to be received and their Branch ENIs be
453- // removed. In this period if we try to make the node managed again, it will
454- // fail
455- time .Sleep (branch .NodeDeleteRequeueRequestDelay )
456-
457- By ("adding the has trunk ENI label" )
458- err = frameWork .NodeManager .AddLabels (targetedNodes ,
459- map [string ]string {config .HasTrunkAttachedLabel : "true" })
455+ By ("verifying node has CNINode present" )
456+ cniNode , err := frameWork .NodeManager .GetCNINode (& node )
460457 Expect (err ).ToNot (HaveOccurred ())
461-
462- By ("creating the Pod on now managed node and verify it runs" )
463- secondPod := podTemplate .DeepCopy ()
464- secondPod , err = frameWork .PodManager .CreateAndWaitTillPodIsRunning (ctx , secondPod , utils .ResourceCreationTimeout )
465- Expect (err ).ToNot (HaveOccurred ())
466-
467- verify .VerifyNetworkingOfPodUsingENI (* secondPod , []string {securityGroupID1 })
458+ Expect (cniNode .Name ).To (Equal (node .Name ))
459+
460+ // we don't support changing SGP managed node to unmanaged node
461+ // after using CNINode, no longer like node label the feature in CNINode Spec shouldn't be modified
462+ // only run this test for old label based mode
463+ if ! lo .ContainsBy (cniNode .Spec .Features , func (addedFeature cninode.Feature ) bool {
464+ return addedFeature .Name == cninode .SecurityGroupsForPods
465+ }) {
466+ if _ , found := node .Labels [config .HasTrunkAttachedLabel ]; found {
467+ // This should never happens as once the trunk is attached,
468+ // this label will not be removed again. This is for testing
469+ // purposes to make a managed node an un-managed node
470+ By ("removing the has-trunk-attached label from the node" )
471+ err = frameWork .NodeManager .RemoveLabels (targetedNodes ,
472+ map [string ]string {config .HasTrunkAttachedLabel : "true" })
473+ Expect (err ).To (HaveOccurred ())
474+
475+ firstPod := podTemplate .DeepCopy ()
476+ By ("creating a Pod on the un-managed node and verifying it fails" )
477+ _ , err = frameWork .PodManager .CreateAndWaitTillPodIsRunning (ctx , firstPod , utils .ResourceCreationTimeout )
478+ Expect (err ).To (HaveOccurred ())
479+
480+ By ("deleting the pod" )
481+ err = frameWork .PodManager .DeleteAndWaitTillPodIsDeleted (ctx , firstPod )
482+ Expect (err ).ToNot (HaveOccurred ())
483+
484+ // Currently we wait for some time before removing the trunk from cache
485+ // to allow evicted Pods's event to be received and their Branch ENIs be
486+ // removed. In this period if we try to make the node managed again, it will
487+ // fail
488+ time .Sleep (branch .NodeDeleteRequeueRequestDelay )
489+
490+ By ("adding the has trunk ENI label" )
491+ err = frameWork .NodeManager .AddLabels (targetedNodes ,
492+ map [string ]string {config .HasTrunkAttachedLabel : "true" })
493+ Expect (err ).ToNot (HaveOccurred ())
494+
495+ By ("creating the Pod on now managed node and verify it runs" )
496+ secondPod := podTemplate .DeepCopy ()
497+ secondPod , err = frameWork .PodManager .CreateAndWaitTillPodIsRunning (ctx , secondPod , utils .ResourceCreationTimeout )
498+ Expect (err ).ToNot (HaveOccurred ())
499+
500+ verify .VerifyNetworkingOfPodUsingENI (* secondPod , []string {securityGroupID1 })
501+
502+ }
503+ }
468504 })
469505 })
470506
@@ -485,7 +521,7 @@ var _ = Describe("Branch ENI Pods", func() {
485521 pod := podTemplate .DeepCopy ()
486522
487523 By ("creating pod which should not run since controller is down" )
488- pod , err = frameWork .PodManager .CreateAndWaitTillPodIsRunning (ctx , pod , time .Second * 10 )
524+ _ , err = frameWork .PodManager .CreateAndWaitTillPodIsRunning (ctx , pod , time .Second * 10 )
489525 Expect (err ).To (HaveOccurred ())
490526
491527 By ("scaling the controller deployment to 2" )
0 commit comments