Browse code

Custom pod/service objects for network diagnostics

Ravi Sankar Penta authored on 2016/10/04 05:55:43
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,135 @@
0
+package network
1
+
2
+// Resource objects used by network diagnostics
3
+import (
4
+	"fmt"
5
+	"strings"
6
+
7
+	kapi "k8s.io/kubernetes/pkg/api"
8
+	kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
9
+	"k8s.io/kubernetes/pkg/util/intstr"
10
+
11
+	"github.com/openshift/origin/pkg/diagnostics/networkpod/util"
12
+)
13
+
14
+const (
15
+	busyboxImage               = "docker.io/busybox"
16
+	networkDiagTestPodSelector = "network-diag-pod-name"
17
+
18
+	testPodImage   = "docker.io/openshift/hello-openshift"
19
+	testPodPort    = 9876
20
+	testTargetPort = 8080
21
+)
22
+
23
+func GetNetworkDiagnosticsPod(command []string, podName, nodeName string) *kapi.Pod {
24
+	privileged := true
25
+	hostRootVolName := "host-root-dir"
26
+	secretVolName := "kconfig-secret"
27
+	secretDirBaseName := "secrets"
28
+	gracePeriod := int64(0)
29
+
30
+	pod := &kapi.Pod{
31
+		ObjectMeta: kapi.ObjectMeta{Name: podName},
32
+		Spec: kapi.PodSpec{
33
+			RestartPolicy:                 kapi.RestartPolicyNever,
34
+			TerminationGracePeriodSeconds: &gracePeriod,
35
+			SecurityContext: &kapi.PodSecurityContext{
36
+				HostPID:     true,
37
+				HostIPC:     true,
38
+				HostNetwork: true,
39
+			},
40
+			NodeName: nodeName,
41
+			Containers: []kapi.Container{
42
+				{
43
+					Name:            podName,
44
+					Image:           busyboxImage,
45
+					ImagePullPolicy: kapi.PullIfNotPresent,
46
+					SecurityContext: &kapi.SecurityContext{
47
+						Privileged: &privileged,
48
+					},
49
+					Env: []kapi.EnvVar{
50
+						{
51
+							Name:  kclientcmd.RecommendedConfigPathEnvVar,
52
+							Value: fmt.Sprintf("/%s/%s", secretDirBaseName, strings.ToLower(kclientcmd.RecommendedConfigPathEnvVar)),
53
+						},
54
+					},
55
+					VolumeMounts: []kapi.VolumeMount{
56
+						{
57
+							Name:      hostRootVolName,
58
+							MountPath: util.NetworkDiagContainerMountPath,
59
+						},
60
+						{
61
+							Name:      secretVolName,
62
+							MountPath: fmt.Sprintf("%s/%s", util.NetworkDiagContainerMountPath, secretDirBaseName),
63
+							ReadOnly:  true,
64
+						},
65
+					},
66
+					Command: command,
67
+				},
68
+			},
69
+			Volumes: []kapi.Volume{
70
+				{
71
+					Name: hostRootVolName,
72
+					VolumeSource: kapi.VolumeSource{
73
+						HostPath: &kapi.HostPathVolumeSource{
74
+							Path: "/",
75
+						},
76
+					},
77
+				},
78
+				{
79
+					Name: secretVolName,
80
+					VolumeSource: kapi.VolumeSource{
81
+						Secret: &kapi.SecretVolumeSource{
82
+							SecretName: util.NetworkDiagSecretName,
83
+						},
84
+					},
85
+				},
86
+			},
87
+		},
88
+	}
89
+	return pod
90
+}
91
+
92
+func GetTestPod(podName, nodeName string) *kapi.Pod {
93
+	gracePeriod := int64(0)
94
+
95
+	return &kapi.Pod{
96
+		ObjectMeta: kapi.ObjectMeta{
97
+			Name: podName,
98
+			Labels: map[string]string{
99
+				networkDiagTestPodSelector: podName,
100
+			},
101
+		},
102
+		Spec: kapi.PodSpec{
103
+			RestartPolicy:                 kapi.RestartPolicyNever,
104
+			TerminationGracePeriodSeconds: &gracePeriod,
105
+			NodeName:                      nodeName,
106
+			Containers: []kapi.Container{
107
+				{
108
+					Name:            podName,
109
+					Image:           testPodImage,
110
+					ImagePullPolicy: kapi.PullIfNotPresent,
111
+				},
112
+			},
113
+		},
114
+	}
115
+}
116
+
117
+func GetTestService(serviceName, podName, nodeName string) *kapi.Service {
118
+	return &kapi.Service{
119
+		ObjectMeta: kapi.ObjectMeta{Name: serviceName},
120
+		Spec: kapi.ServiceSpec{
121
+			Type: kapi.ServiceTypeClusterIP,
122
+			Selector: map[string]string{
123
+				networkDiagTestPodSelector: podName,
124
+			},
125
+			Ports: []kapi.ServicePort{
126
+				{
127
+					Protocol:   kapi.ProtocolTCP,
128
+					Port:       testPodPort,
129
+					TargetPort: intstr.FromInt(testTargetPort),
130
+				},
131
+			},
132
+		},
133
+	}
134
+}