1
16
17 package network
18
19 import (
20 "context"
21 "fmt"
22 "net"
23 "strconv"
24 "time"
25
26 "github.com/onsi/ginkgo/v2"
27 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28 clientset "k8s.io/client-go/kubernetes"
29 "k8s.io/kubernetes/test/e2e/framework"
30 e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
31 "k8s.io/kubernetes/test/e2e/network/common"
32 admissionapi "k8s.io/pod-security-admission/api"
33 )
34
35 var _ = common.SIGDescribe("Connectivity Pod Lifecycle", func() {
36
37 fr := framework.NewDefaultFramework("podlifecycle")
38 fr.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
39
40 var (
41 cs clientset.Interface
42 ns string
43 podClient *e2epod.PodClient
44 )
45 ginkgo.BeforeEach(func(ctx context.Context) {
46 cs = fr.ClientSet
47 ns = fr.Namespace.Name
48 podClient = e2epod.NewPodClient(fr)
49 })
50
51 ginkgo.It("should be able to connect from a Pod to a terminating Pod", func(ctx context.Context) {
52 ginkgo.By("Creating 1 webserver pod able to serve traffic during the grace period of 300 seconds")
53 gracePeriod := int64(100)
54 webserverPod := e2epod.NewAgnhostPod(ns, "webserver-pod", nil, nil, nil, "netexec", "--http-port=80", fmt.Sprintf("--delay-shutdown=%d", gracePeriod))
55 webserverPod.Spec.TerminationGracePeriodSeconds = &gracePeriod
56 webserverPod = podClient.CreateSync(ctx, webserverPod)
57
58 ginkgo.By("Creating 1 client pod that will try to connect to the webserver")
59 pausePod := e2epod.NewAgnhostPod(ns, "pause-pod-1", nil, nil, nil)
60 pausePod = podClient.CreateSync(ctx, pausePod)
61
62 ginkgo.By("Try to connect to the webserver")
63
64 podIPAddress := net.JoinHostPort(webserverPod.Status.PodIP, strconv.Itoa(80))
65 execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
66
67
68
69 err := cs.CoreV1().Pods(ns).Delete(ctx, webserverPod.Name, metav1.DeleteOptions{})
70 framework.ExpectNoError(err, "error deleting webserver pod")
71
72 time.Sleep(15 * time.Second)
73 execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
74 })
75
76 ginkgo.It("should be able to connect to other Pod from a terminating Pod", func(ctx context.Context) {
77 ginkgo.By("Creating 1 webserver pod able to serve traffic during the grace period of 300 seconds")
78 gracePeriod := int64(100)
79 webserverPod := e2epod.NewAgnhostPod(ns, "webserver-pod", nil, nil, nil, "netexec", "--http-port=80", fmt.Sprintf("--delay-shutdown=%d", gracePeriod))
80 webserverPod = podClient.CreateSync(ctx, webserverPod)
81
82 ginkgo.By("Creating 1 client pod that will try to connect to the webservers")
83 pausePod := e2epod.NewAgnhostPod(ns, "pause-pod-1", nil, nil, nil, "netexec", "--http-port=80", fmt.Sprintf("--delay-shutdown=%d", gracePeriod))
84 pausePod.Spec.TerminationGracePeriodSeconds = &gracePeriod
85 pausePod = podClient.CreateSync(ctx, pausePod)
86
87 ginkgo.By("Try to connect to the webserver")
88
89 podIPAddress := net.JoinHostPort(webserverPod.Status.PodIP, strconv.Itoa(80))
90 execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
91
92
93
94 err := cs.CoreV1().Pods(ns).Delete(ctx, pausePod.Name, metav1.DeleteOptions{})
95 framework.ExpectNoError(err, "error deleting client pod")
96
97 time.Sleep(15 * time.Second)
98 execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
99 })
100
101 })
102
View as plain text