-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathpod.go
34 lines (30 loc) · 1.14 KB
/
pod.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
package e2e
import (
"context"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func ensurePod(ctx context.Context, s *Scenario, pod *corev1.Pod) {
kube := s.Runtime.Cluster.Kube
if len(pod.Name) > 63 {
pod.Name = pod.Name[:63]
s.T.Logf("truncated pod name to %q", pod.Name)
}
s.T.Logf("creating pod %q", pod.Name)
_, err := kube.Typed.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{})
require.NoErrorf(s.T, err, "failed to create pod %q", pod.Name)
s.T.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Minute)
defer cancel()
err := kube.Typed.CoreV1().Pods(pod.Namespace).Delete(ctx, pod.Name, metav1.DeleteOptions{GracePeriodSeconds: to.Ptr(int64(0))})
if err != nil {
s.T.Logf("couldn't not delete pod %s: %v", pod.Name, err)
}
s.T.Logf("deleted pod %q", pod.Name)
})
_, err = kube.WaitUntilPodRunning(ctx, s.T, pod.Namespace, "", "metadata.name="+pod.Name)
require.NoErrorf(s.T, err, "failed to wait for pod %q to be in running state", pod.Name)
}