@@ -28,6 +28,7 @@ import (
28
28
sgpWrapper "github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/resource/k8s/sgp"
29
29
"github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/utils"
30
30
"github.com/samber/lo"
31
+ "sigs.k8s.io/controller-runtime/pkg/client"
31
32
32
33
. "github.com/onsi/ginkgo/v2"
33
34
. "github.com/onsi/gomega"
@@ -513,8 +514,108 @@ var _ = Describe("Branch ENI Pods", func() {
513
514
})
514
515
})
515
516
})
517
+
518
+ Describe ("Test Network Connectivity on Delete and Recreation of Pod" , func () {
519
+ Context ("creating statefulset with network connectivity" , func () {
520
+ var resourceMap map [v1.ResourceName ]resource.Quantity
521
+ var statefulSet * appsv1.StatefulSet
522
+ var container v1.Container
523
+
524
+ BeforeEach (func () {
525
+ resourceMap = map [v1.ResourceName ]resource.Quantity {
526
+ config .ResourceNamePodENI : resource .MustParse ("1" ),
527
+ }
528
+ })
529
+
530
+ JustBeforeEach (func () {
531
+ container = manifest .NewBusyBoxContainerBuilder ().
532
+ Resources (v1.ResourceRequirements {
533
+ Limits : resourceMap ,
534
+ Requests : resourceMap ,
535
+ }).
536
+ Command ([]string { "/bin/sh" , "-c" ,
537
+ "while true; do if ping -c 1 google.com; then echo 'Successfully pinged google.com'; else echo 'Failed to ping google.com'; exit 1; fi; sleep 30; done" ,
538
+ }).
539
+ Name ("network-test" ).
540
+ Image ("busybox" ).
541
+ Build ()
542
+
543
+ statefulSet = manifest .NewDefaultStatefulSetBuilder ().
544
+ Namespace (namespace ).
545
+ Name ("network-test" ).
546
+ PodLabel (podLabelKey , podLabelValue ).
547
+ Container (container ).
548
+ Build ()
549
+ })
550
+
551
+ JustAfterEach (func () {
552
+ By ("deleting the statefulset" )
553
+ err = frameWork .K8sClient .Delete (ctx , statefulSet )
554
+ Expect (err ).ToNot (HaveOccurred ())
555
+ })
556
+
557
+ Context ("when statefulset is created with network connectivity requirements" , func () {
558
+ It ("should have all pods running with network access" , func () {
559
+ By ("creating security group policy" )
560
+ sgpWrapper .CreateSecurityGroupPolicy (frameWork .K8sClient , ctx , securityGroupPolicy )
561
+
562
+ By ("creating statefulset" )
563
+ err = frameWork .K8sClient .Create (ctx , statefulSet )
564
+ Expect (err ).ToNot (HaveOccurred ())
565
+
566
+ By ("waiting for statefulset pods to be ready" )
567
+ Eventually (func () bool {
568
+ err := frameWork .K8sClient .Get (ctx , client.ObjectKey {
569
+ Namespace : namespace ,
570
+ Name : statefulSet .Name ,
571
+ }, statefulSet )
572
+ if err != nil {
573
+ return false
574
+ }
575
+ return statefulSet .Status .ReadyReplicas == * statefulSet .Spec .Replicas
576
+ }, 300 * time .Second , 5 * time .Second ).Should (BeTrue ())
577
+
578
+ By ("verifying network connectivity of all pods" )
579
+ verify .VerifyNetworkingOfAllPodUsingENI (namespace , podLabelKey , podLabelValue ,
580
+ securityGroups )
581
+
582
+ By ("force deleting one pod to verify recreation" )
583
+ pods := & v1.PodList {}
584
+ err = frameWork .K8sClient .List (ctx , pods , client .InNamespace (namespace ),
585
+ client .MatchingLabels (map [string ]string {podLabelKey : podLabelValue }))
586
+ Expect (err ).ToNot (HaveOccurred ())
587
+ Expect (pods .Items ).ToNot (BeEmpty ())
588
+
589
+ // Force delete the first pod
590
+ err = frameWork .K8sClient .Delete (ctx , & pods .Items [0 ], client .GracePeriodSeconds (0 ))
591
+ Expect (err ).ToNot (HaveOccurred ())
592
+
593
+ By ("waiting for pod to be recreated" )
594
+ Eventually (func () bool {
595
+ err := frameWork .K8sClient .Get (ctx , client.ObjectKey {
596
+ Namespace : namespace ,
597
+ Name : statefulSet .Name ,
598
+ }, statefulSet )
599
+ if err != nil {
600
+ return false
601
+ }
602
+ return statefulSet .Status .ReadyReplicas == * statefulSet .Spec .Replicas
603
+ }, 300 * time .Second , 5 * time .Second ).Should (BeTrue ())
604
+
605
+ By ("verifying network connectivity after pod recreation" )
606
+ verify .VerifyNetworkingOfAllPodUsingENI (namespace , podLabelKey , podLabelValue ,
607
+ securityGroups )
608
+ })
609
+ })
610
+ })
611
+ })
516
612
})
517
613
614
+ // Helper functions
615
+ func int32Ptr (i int32 ) * int32 {
616
+ return & i
617
+ }
618
+
518
619
func CreateServiceAccount (serviceAccount * v1.ServiceAccount ) {
519
620
By ("create a service account" )
520
621
err := frameWork .K8sClient .Create (ctx , serviceAccount )
0 commit comments