...

Source file src/k8s.io/kubernetes/test/e2e/framework/pod/delete.go

Documentation: k8s.io/kubernetes/test/e2e/framework/pod

     1  /*
     2  Copyright 2019 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 pod
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/onsi/ginkgo/v2"
    25  
    26  	v1 "k8s.io/api/core/v1"
    27  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	clientset "k8s.io/client-go/kubernetes"
    30  	"k8s.io/kubernetes/test/e2e/framework"
    31  )
    32  
    33  const (
    34  	// PodDeleteTimeout is how long to wait for a pod to be deleted.
    35  	PodDeleteTimeout = 5 * time.Minute
    36  )
    37  
    38  // DeletePodOrFail deletes the pod of the specified namespace and name. Resilient to the pod
    39  // not existing.
    40  func DeletePodOrFail(ctx context.Context, c clientset.Interface, ns, name string) {
    41  	ginkgo.By(fmt.Sprintf("Deleting pod %s in namespace %s", name, ns))
    42  	err := c.CoreV1().Pods(ns).Delete(ctx, name, metav1.DeleteOptions{})
    43  	if err != nil && apierrors.IsNotFound(err) {
    44  		return
    45  	}
    46  
    47  	expectNoError(err, "failed to delete pod %s in namespace %s", name, ns)
    48  }
    49  
    50  // DeletePodWithWait deletes the passed-in pod and waits for the pod to be terminated. Resilient to the pod
    51  // not existing.
    52  func DeletePodWithWait(ctx context.Context, c clientset.Interface, pod *v1.Pod) error {
    53  	if pod == nil {
    54  		return nil
    55  	}
    56  	return DeletePodWithWaitByName(ctx, c, pod.GetName(), pod.GetNamespace())
    57  }
    58  
    59  // DeletePodWithWaitByName deletes the named and namespaced pod and waits for the pod to be terminated. Resilient to the pod
    60  // not existing.
    61  func DeletePodWithWaitByName(ctx context.Context, c clientset.Interface, podName, podNamespace string) error {
    62  	framework.Logf("Deleting pod %q in namespace %q", podName, podNamespace)
    63  	err := c.CoreV1().Pods(podNamespace).Delete(ctx, podName, metav1.DeleteOptions{})
    64  	if err != nil {
    65  		if apierrors.IsNotFound(err) {
    66  			return nil // assume pod was already deleted
    67  		}
    68  		return fmt.Errorf("pod Delete API error: %w", err)
    69  	}
    70  	framework.Logf("Wait up to %v for pod %q to be fully deleted", PodDeleteTimeout, podName)
    71  	err = WaitForPodNotFoundInNamespace(ctx, c, podName, podNamespace, PodDeleteTimeout)
    72  	if err != nil {
    73  		return fmt.Errorf("pod %q was not deleted: %w", podName, err)
    74  	}
    75  	return nil
    76  }
    77  
    78  // DeletePodWithGracePeriod deletes the passed-in pod. Resilient to the pod not existing.
    79  func DeletePodWithGracePeriod(ctx context.Context, c clientset.Interface, pod *v1.Pod, grace int64) error {
    80  	return DeletePodWithGracePeriodByName(ctx, c, pod.GetName(), pod.GetNamespace(), grace)
    81  }
    82  
    83  // DeletePodsWithGracePeriod deletes the passed-in pods. Resilient to the pods not existing.
    84  func DeletePodsWithGracePeriod(ctx context.Context, c clientset.Interface, pods []v1.Pod, grace int64) error {
    85  	for _, pod := range pods {
    86  		if err := DeletePodWithGracePeriod(ctx, c, &pod, grace); err != nil {
    87  			return err
    88  		}
    89  	}
    90  	return nil
    91  }
    92  
    93  // DeletePodWithGracePeriodByName deletes a pod by name and namespace. Resilient to the pod not existing.
    94  func DeletePodWithGracePeriodByName(ctx context.Context, c clientset.Interface, podName, podNamespace string, grace int64) error {
    95  	framework.Logf("Deleting pod %q in namespace %q", podName, podNamespace)
    96  	err := c.CoreV1().Pods(podNamespace).Delete(ctx, podName, *metav1.NewDeleteOptions(grace))
    97  	if err != nil {
    98  		if apierrors.IsNotFound(err) {
    99  			return nil // assume pod was already deleted
   100  		}
   101  		return fmt.Errorf("pod Delete API error: %w", err)
   102  	}
   103  	return nil
   104  }
   105  

View as plain text