diff --git a/e2e/internal/kubeclient/deploy.go b/e2e/internal/kubeclient/deploy.go index ab012463d..705b06666 100644 --- a/e2e/internal/kubeclient/deploy.go +++ b/e2e/internal/kubeclient/deploy.go @@ -4,8 +4,10 @@ import ( "context" "fmt" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/dynamic" ) @@ -40,3 +42,31 @@ func (c *Kubeclient) Deploy(ctx context.Context, manifests ...*unstructured.Unst } return nil } + +// WaitForPod watches the given pod and blocks until it meets the condition Ready=True. +func (c *Kubeclient) WaitForPod(ctx context.Context, namespace, name string) error { + watcher, err := c.client.CoreV1().Pods(namespace).Watch(ctx, metav1.ListOptions{FieldSelector: "metadata.name=" + name}) + if err != nil { + return err + } + for { + select { + case evt := <-watcher.ResultChan(): + switch evt.Type { + case watch.Added: + fallthrough + case watch.Modified: + pod := evt.Object.(*corev1.Pod) + for _, cond := range pod.Status.Conditions { + if cond.Type == corev1.PodReady && cond.Status == corev1.ConditionTrue { + return nil + } + } + default: + return fmt.Errorf("unexpected watch event while waiting for pod %s/%s: %#v", namespace, name, evt.Object) + } + case <-ctx.Done(): + return ctx.Err() + } + } +}