Skip to content

Commit

Permalink
e2e: add routine to wait for pod readiness
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Mar 5, 2024
1 parent bae23eb commit ff4846e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions e2e/internal/kubeclient/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
}
}
}

0 comments on commit ff4846e

Please sign in to comment.