github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/utils/common/pod.go (about)

     1  package common
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
     9  	"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
    10  	corev1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/apimachinery/pkg/labels"
    13  	"k8s.io/apimachinery/pkg/util/wait"
    14  )
    15  
    16  // GetPod returns the pod object from a given namespace and pod name
    17  func (s *SuiteController) GetPod(namespace, podName string) (*corev1.Pod, error) {
    18  	return s.KubeInterface().CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
    19  }
    20  
    21  func (s *SuiteController) IsPodRunning(podName, namespace string) wait.ConditionFunc {
    22  	return func() (bool, error) {
    23  		pod, err := s.GetPod(namespace, podName)
    24  		if err != nil {
    25  			return false, nil
    26  		}
    27  		switch pod.Status.Phase {
    28  		case corev1.PodRunning:
    29  			return true, nil
    30  		case corev1.PodFailed, corev1.PodSucceeded:
    31  			return false, fmt.Errorf("pod %q ran to completion", pod.Name)
    32  		}
    33  		return false, nil
    34  	}
    35  }
    36  
    37  // Checks phases of a given pod name in a given namespace
    38  func (s *SuiteController) IsPodSuccessful(podName, namespace string) wait.ConditionFunc {
    39  	return func() (bool, error) {
    40  		pod, err := s.GetPod(namespace, podName)
    41  		if err != nil {
    42  			return false, nil
    43  		}
    44  		switch pod.Status.Phase {
    45  		case corev1.PodSucceeded:
    46  			return true, nil
    47  		case corev1.PodFailed:
    48  			return false, fmt.Errorf("pod %q has failed", pod.Name)
    49  		}
    50  		return false, nil
    51  	}
    52  }
    53  
    54  // TaskPodExists checks if a task have a pod
    55  func TaskPodExists(tr *v1beta1.TaskRun) wait.ConditionFunc {
    56  	return func() (bool, error) {
    57  		if tr.Status.PodName != "" {
    58  			return true, nil
    59  		}
    60  		return false, nil
    61  	}
    62  }
    63  
    64  // ListPods return a list of pods from a namespace by labels and selection limits
    65  func (s *SuiteController) ListPods(namespace, labelKey, labelValue string, selectionLimit int64) (*corev1.PodList, error) {
    66  	labelSelector := metav1.LabelSelector{MatchLabels: map[string]string{labelKey: labelValue}}
    67  	listOptions := metav1.ListOptions{
    68  		LabelSelector: labels.Set(labelSelector.MatchLabels).String(),
    69  		Limit:         selectionLimit,
    70  	}
    71  	return s.KubeInterface().CoreV1().Pods(namespace).List(context.TODO(), listOptions)
    72  }
    73  
    74  // Wait for a pod selector until exists
    75  func (s *SuiteController) WaitForPodSelector(
    76  	fn func(podName, namespace string) wait.ConditionFunc, namespace, labelKey string, labelValue string,
    77  	timeout int, selectionLimit int64) error {
    78  	podList, err := s.ListPods(namespace, labelKey, labelValue, selectionLimit)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	if len(podList.Items) == 0 {
    83  		return fmt.Errorf("no pods in %s with label key %s and label value %s", namespace, labelKey, labelValue)
    84  	}
    85  
    86  	for i := range podList.Items {
    87  		if err := utils.WaitUntil(fn(podList.Items[i].Name, namespace), time.Duration(timeout)*time.Second); err != nil {
    88  			return err
    89  		}
    90  	}
    91  	return nil
    92  }