/
pod.go
120 lines (101 loc) · 3.29 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package pod
import (
"bytes"
"os"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/utils/pointer"
testclient "github.com/openshift/sriov-tests/pkg/util/client"
"github.com/openshift/sriov-tests/pkg/util/namespaces"
)
const hostnameLabel = "kubernetes.io/hostname"
func getDefinition() *corev1.Pod {
podObject := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "testpod-",
Namespace: namespaces.Test},
Spec: corev1.PodSpec{
TerminationGracePeriodSeconds: pointer.Int64Ptr(0),
Containers: []corev1.Container{{Name: "test",
Image: "quay.io/schseba/utility-container:latest",
Command: []string{"/bin/bash", "-c", "sleep INF"}}}}}
return podObject
}
func DefineWithNetworks(networks []string) *corev1.Pod {
podObject := getDefinition()
podObject.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": strings.Join(networks, ",")}
return podObject
}
func DefineWithHostNetwork(nodeName string) *corev1.Pod {
podObject := getDefinition()
podObject.Spec.HostNetwork = true
podObject.Spec.NodeSelector = map[string]string{
"kubernetes.io/hostname": nodeName,
}
return podObject
}
// RedefineAsPrivileged uppdates the pod to be privileged
func RedefineAsPrivileged(pod *corev1.Pod) *corev1.Pod {
pod.Spec.Containers[0].SecurityContext = &corev1.SecurityContext{}
b := true
pod.Spec.Containers[0].SecurityContext.Privileged = &b
return pod
}
// RedefineWithHostNetwork uppdates the pod definition Spec.HostNetwork to true
func RedefineWithHostNetwork(pod *corev1.Pod) *corev1.Pod {
pod.Spec.HostNetwork = true
return pod
}
// RedefineWithNodeSelector uppdates the pod definition with a node selector
func RedefineWithNodeSelector(pod *corev1.Pod, node string) *corev1.Pod {
pod.Spec.NodeSelector = map[string]string{
hostnameLabel: node,
}
return pod
}
// RedefineWithCommand updates the pod defintion with a different command
func RedefineWithCommand(pod *corev1.Pod, command []string, args []string) *corev1.Pod {
pod.Spec.Containers[0].Command = command
pod.Spec.Containers[0].Args = args
return pod
}
// RedefineWithRestartPolicy updates the pod defintion with a restart policy
func RedefineWithRestartPolicy(pod *corev1.Pod, restartPolicy corev1.RestartPolicy) *corev1.Pod {
pod.Spec.RestartPolicy = restartPolicy
return pod
}
// ExecCommand runs command in the pod and returns buffer output
func ExecCommand(cs *testclient.ClientSet, pod *corev1.Pod, command ...string) (string, string, error) {
var buf, errbuf bytes.Buffer
req := cs.CoreV1Interface.RESTClient().
Post().
Namespace(pod.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: pod.Spec.Containers[0].Name,
Command: command,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: true,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(cs.Config, "POST", req.URL())
if err != nil {
return buf.String(), errbuf.String(), err
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: os.Stdin,
Stdout: &buf,
Stderr: &errbuf,
Tty: true,
})
if err != nil {
return buf.String(), errbuf.String(), err
}
return buf.String(), errbuf.String(), nil
}