k8s.io/kubernetes@v1.29.3/test/e2e/network/pod_lifecycle.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    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  		// Wait until we are able to connect to the Pod
    64  		podIPAddress := net.JoinHostPort(webserverPod.Status.PodIP, strconv.Itoa(80))
    65  		execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
    66  
    67  		// webserver should continue to serve traffic after delete
    68  		// since will be gracefully terminating
    69  		err := cs.CoreV1().Pods(ns).Delete(ctx, webserverPod.Name, metav1.DeleteOptions{})
    70  		framework.ExpectNoError(err, "error deleting webserver pod")
    71  		// wait some time to ensure the test hit the pod during the terminating phase
    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  		// Wait until we are able to connect to the Pod
    89  		podIPAddress := net.JoinHostPort(webserverPod.Status.PodIP, strconv.Itoa(80))
    90  		execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
    91  
    92  		// pod client should continue to connect to the webserver after delete
    93  		// since will be gracefully terminating
    94  		err := cs.CoreV1().Pods(ns).Delete(ctx, pausePod.Name, metav1.DeleteOptions{})
    95  		framework.ExpectNoError(err, "error deleting client pod")
    96  		// wait some time to ensure the test hit the pod during the terminating
    97  		time.Sleep(15 * time.Second)
    98  		execHostnameTest(*pausePod, podIPAddress, webserverPod.Name)
    99  	})
   100  
   101  })