/
pod.go
74 lines (64 loc) · 1.74 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package mock
import (
"context"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)
type Client struct {
k8s kubernetes.Interface
ns string
}
func (c *Client) CreatePod(ctx context.Context, podName, pvcName string) (*v1.Pod, error) {
req := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
},
Spec: v1.PodSpec{
RestartPolicy: v1.RestartPolicyNever,
Containers: []v1.Container{
{
Name: "main",
Image: "busybox",
Command: []string{"/bin/sh"},
Args: []string{"-c", "echo 'hello world' >> ./temp; sleep 1000"},
VolumeMounts: []v1.VolumeMount{
{
Name: pvcName,
MountPath: "/data",
},
},
},
},
Volumes: []v1.Volume{
{
Name: pvcName,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
},
},
},
}
pod, err := c.k8s.CoreV1().Pods(c.ns).Create(ctx, req, metav1.CreateOptions{})
return pod, err
}
func (c *Client) DeletePod(ctx context.Context, podName, namespace string) error {
return c.k8s.CoreV1().Pods(namespace).Delete(ctx, podName, metav1.DeleteOptions{})
}
func (c *Client) isPodRunning(ctx context.Context, podName, namespace string) wait.ConditionFunc {
return func() (bool, error) {
pod, err := c.k8s.CoreV1().Pods(namespace).Get(ctx, podName, metav1.GetOptions{})
if err != nil {
return false, err
}
return pod.Status.Phase == v1.PodRunning, nil
}
}
func (c *Client) WaitForPod(ctx context.Context, podName, namespace string) error {
return wait.PollImmediate(time.Second, time.Minute, c.isPodRunning(ctx, podName, namespace))
}