...
1
16
17 package node
18
19 import (
20 "context"
21 "fmt"
22 "time"
23
24 v1 "k8s.io/api/core/v1"
25 apierrors "k8s.io/apimachinery/pkg/api/errors"
26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27 "k8s.io/apimachinery/pkg/util/uuid"
28 podutil "k8s.io/kubernetes/pkg/api/v1/pod"
29 e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
30 )
31
32 const (
33
34 ContainerStatusRetryTimeout = time.Minute * 5
35
36 ContainerStatusPollInterval = time.Second * 1
37 )
38
39
40
41 type ConformanceContainer struct {
42 Container v1.Container
43 RestartPolicy v1.RestartPolicy
44 Volumes []v1.Volume
45 ImagePullSecrets []string
46
47 PodClient *e2epod.PodClient
48 podName string
49 PodSecurityContext *v1.PodSecurityContext
50 }
51
52
53 func (cc *ConformanceContainer) Create(ctx context.Context) {
54 cc.podName = cc.Container.Name + string(uuid.NewUUID())
55 imagePullSecrets := []v1.LocalObjectReference{}
56 for _, s := range cc.ImagePullSecrets {
57 imagePullSecrets = append(imagePullSecrets, v1.LocalObjectReference{Name: s})
58 }
59 pod := &v1.Pod{
60 ObjectMeta: metav1.ObjectMeta{
61 Name: cc.podName,
62 },
63 Spec: v1.PodSpec{
64 RestartPolicy: cc.RestartPolicy,
65 Containers: []v1.Container{
66 cc.Container,
67 },
68 SecurityContext: cc.PodSecurityContext,
69 Volumes: cc.Volumes,
70 ImagePullSecrets: imagePullSecrets,
71 },
72 }
73 cc.PodClient.Create(ctx, pod)
74 }
75
76
77 func (cc *ConformanceContainer) Delete(ctx context.Context) error {
78 return cc.PodClient.Delete(ctx, cc.podName, *metav1.NewDeleteOptions(0))
79 }
80
81
82 func (cc *ConformanceContainer) IsReady(ctx context.Context) (bool, error) {
83 pod, err := cc.PodClient.Get(ctx, cc.podName, metav1.GetOptions{})
84 if err != nil {
85 return false, err
86 }
87 return podutil.IsPodReady(pod), nil
88 }
89
90
91 func (cc *ConformanceContainer) GetPhase(ctx context.Context) (v1.PodPhase, error) {
92 pod, err := cc.PodClient.Get(ctx, cc.podName, metav1.GetOptions{})
93 if err != nil {
94
95 return v1.PodSucceeded, err
96 }
97 return pod.Status.Phase, nil
98 }
99
100
101 func (cc *ConformanceContainer) GetStatus(ctx context.Context) (v1.ContainerStatus, error) {
102 pod, err := cc.PodClient.Get(ctx, cc.podName, metav1.GetOptions{})
103 if err != nil {
104 return v1.ContainerStatus{}, err
105 }
106 statuses := pod.Status.ContainerStatuses
107 if len(statuses) != 1 || statuses[0].Name != cc.Container.Name {
108 return v1.ContainerStatus{}, fmt.Errorf("unexpected container statuses %v", statuses)
109 }
110 return statuses[0], nil
111 }
112
113
114 func (cc *ConformanceContainer) Present(ctx context.Context) (bool, error) {
115 _, err := cc.PodClient.Get(ctx, cc.podName, metav1.GetOptions{})
116 if err == nil {
117 return true, nil
118 }
119 if apierrors.IsNotFound(err) {
120 return false, nil
121 }
122 return false, err
123 }
124
125
126 type ContainerState string
127
128 const (
129
130 ContainerStateWaiting ContainerState = "Waiting"
131
132 ContainerStateRunning ContainerState = "Running"
133
134 ContainerStateTerminated ContainerState = "Terminated"
135
136 ContainerStateUnknown ContainerState = "Unknown"
137 )
138
139
140 func GetContainerState(state v1.ContainerState) ContainerState {
141 if state.Waiting != nil {
142 return ContainerStateWaiting
143 }
144 if state.Running != nil {
145 return ContainerStateRunning
146 }
147 if state.Terminated != nil {
148 return ContainerStateTerminated
149 }
150 return ContainerStateUnknown
151 }
152
View as plain text