github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/pkg/ha/pods.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package ha
     5  
     6  import (
     7  	"context"
     8  	"strings"
     9  
    10  	"github.com/onsi/gomega"
    11  	"go.uber.org/zap"
    12  	corev1 "k8s.io/api/core/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  	"k8s.io/client-go/kubernetes"
    15  )
    16  
    17  func EventuallyPodsReady(log *zap.SugaredLogger, cs *kubernetes.Clientset) {
    18  	var pods *corev1.PodList
    19  	gomega.Eventually(func() (string, error) {
    20  		var err error
    21  		pods, err = cs.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
    22  		if err != nil {
    23  			log.Info("Failed to get pods: %v", err)
    24  			return "", err
    25  		}
    26  
    27  		// Assume all pods are ready.  If debug enabled, log status of each pod that is not ready yet
    28  		notReadyPod := ""
    29  		for _, pod := range pods.Items {
    30  			// Skips helm-operation-* pods in cattle-system since they sometimes have a status of error during install.
    31  			if pod.Namespace == "cattle-system" && strings.Contains(pod.Name, "helm-operation-") {
    32  				continue
    33  			}
    34  			if !IsPodReadyOrCompleted(pod) {
    35  				log.Debugf("Pod [%s] in namespace [%s] not ready or completed [%s]", pod.Name, pod.Namespace, string(pod.Status.Phase))
    36  				notReadyPod = pod.Namespace + "/" + pod.Name
    37  			}
    38  		}
    39  		return notReadyPod, nil
    40  
    41  	}, longWaitTimeout, longPollingInterval).Should(gomega.BeEmpty())
    42  }
    43  
    44  func IsPodReadyOrCompleted(pod corev1.Pod) bool {
    45  	switch pod.Status.Phase {
    46  	case corev1.PodSucceeded:
    47  		return true
    48  	case corev1.PodRunning:
    49  		for _, c := range pod.Status.Conditions {
    50  			if c.Type == corev1.PodReady {
    51  				return c.Status == corev1.ConditionTrue
    52  			}
    53  		}
    54  		return false
    55  	default:
    56  		return false
    57  	}
    58  }