@@ -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,117 @@ 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 statefulSet * appsv1.StatefulSet
521
+
522
+ JustBeforeEach (func () {
523
+ statefulSet = & appsv1.StatefulSet {
524
+ ObjectMeta : metav1.ObjectMeta {
525
+ Name : "network-test" ,
526
+ Namespace : namespace ,
527
+ },
528
+ Spec : appsv1.StatefulSetSpec {
529
+ ServiceName : "network-test" ,
530
+ Replicas : int32Ptr (2 ),
531
+ Selector : & metav1.LabelSelector {
532
+ MatchLabels : map [string ]string {
533
+ podLabelKey : podLabelValue ,
534
+ },
535
+ },
536
+ Template : v1.PodTemplateSpec {
537
+ ObjectMeta : metav1.ObjectMeta {
538
+ Labels : map [string ]string {
539
+ podLabelKey : podLabelValue ,
540
+ },
541
+ },
542
+ Spec : v1.PodSpec {
543
+ Containers : []v1.Container {
544
+ {
545
+ Name : "network-test" ,
546
+ Image : "busybox" ,
547
+ Command : []string {
548
+ "/bin/sh" ,
549
+ "-c" ,
550
+ "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" ,
551
+ },
552
+ },
553
+ },
554
+ },
555
+ },
556
+ },
557
+ }
558
+ })
559
+
560
+ JustAfterEach (func () {
561
+ By ("deleting the statefulset" )
562
+ err = frameWork .K8sClient .Delete (ctx , statefulSet )
563
+ Expect (err ).ToNot (HaveOccurred ())
564
+ })
565
+
566
+ Context ("when statefulset is created with network connectivity requirements" , func () {
567
+ It ("should have all pods running with network access" , func () {
568
+ By ("creating security group policy" )
569
+ sgpWrapper .CreateSecurityGroupPolicy (frameWork .K8sClient , ctx , securityGroupPolicy )
570
+
571
+ By ("creating statefulset" )
572
+ err = frameWork .K8sClient .Create (ctx , statefulSet )
573
+ Expect (err ).ToNot (HaveOccurred ())
574
+
575
+ By ("waiting for statefulset pods to be ready" )
576
+ Eventually (func () bool {
577
+ err := frameWork .K8sClient .Get (ctx , client.ObjectKey {
578
+ Namespace : namespace ,
579
+ Name : statefulSet .Name ,
580
+ }, statefulSet )
581
+ if err != nil {
582
+ return false
583
+ }
584
+ return statefulSet .Status .ReadyReplicas == * statefulSet .Spec .Replicas
585
+ }, 300 * time .Second , 5 * time .Second ).Should (BeTrue ())
586
+
587
+ By ("verifying network connectivity of all pods" )
588
+ verify .VerifyNetworkingOfAllPodUsingENI (namespace , podLabelKey , podLabelValue ,
589
+ securityGroups )
590
+
591
+ By ("force deleting one pod to verify recreation" )
592
+ pods := & v1.PodList {}
593
+ err = frameWork .K8sClient .List (ctx , pods , client .InNamespace (namespace ),
594
+ client .MatchingLabels (map [string ]string {podLabelKey : podLabelValue }))
595
+ Expect (err ).ToNot (HaveOccurred ())
596
+ Expect (pods .Items ).ToNot (BeEmpty ())
597
+
598
+ // Force delete the first pod
599
+ err = frameWork .K8sClient .Delete (ctx , & pods .Items [0 ], client .GracePeriodSeconds (0 ))
600
+ Expect (err ).ToNot (HaveOccurred ())
601
+
602
+ By ("waiting for pod to be recreated" )
603
+ Eventually (func () bool {
604
+ err := frameWork .K8sClient .Get (ctx , client.ObjectKey {
605
+ Namespace : namespace ,
606
+ Name : statefulSet .Name ,
607
+ }, statefulSet )
608
+ if err != nil {
609
+ return false
610
+ }
611
+ return statefulSet .Status .ReadyReplicas == * statefulSet .Spec .Replicas
612
+ }, 300 * time .Second , 5 * time .Second ).Should (BeTrue ())
613
+
614
+ By ("verifying network connectivity after pod recreation" )
615
+ verify .VerifyNetworkingOfAllPodUsingENI (namespace , podLabelKey , podLabelValue ,
616
+ securityGroups )
617
+ })
618
+ })
619
+ })
620
+ })
516
621
})
517
622
623
+ // Helper functions
624
+ func int32Ptr (i int32 ) * int32 {
625
+ return & i
626
+ }
627
+
518
628
func CreateServiceAccount (serviceAccount * v1.ServiceAccount ) {
519
629
By ("create a service account" )
520
630
err := frameWork .K8sClient .Create (ctx , serviceAccount )
0 commit comments