-
Notifications
You must be signed in to change notification settings - Fork 115
/
pods.go
37 lines (30 loc) · 999 Bytes
/
pods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package helper
import (
"context"
"fmt"
"log"
"time"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
kubev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// WaitForPodPhase until in target, checking n times and sleeping dur between them. Last known phase is returned.
func (h *H) WaitForPodPhase(ctx context.Context, pod *kubev1.Pod, target kubev1.PodPhase, n int, dur time.Duration) (phase kubev1.PodPhase) {
var err error
for i := 0; i < n; i++ {
if pod, err = h.Kube().CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}); err != nil {
log.Println(err)
} else if pod != nil {
phase = pod.Status.Phase
// stop checking if Pod has reached state or failed
if phase == target || phase == kubev1.PodFailed {
return
}
}
ginkgo.GinkgoLogr.Info(fmt.Sprintf("Waiting for Pod '%s/%s' to be %s, currently %s...", pod.Namespace, pod.Name, target, phase))
time.Sleep(dur)
}
Expect(phase).NotTo(BeEmpty())
return
}