@@ -22,6 +22,7 @@ import (
22
22
23
23
v1 "k8s.io/api/core/v1"
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
+ "k8s.io/apimachinery/pkg/util/sets"
25
26
"k8s.io/apimachinery/pkg/util/uuid"
26
27
"k8s.io/kubernetes/pkg/kubelet/events"
27
28
"k8s.io/kubernetes/test/e2e/framework"
@@ -40,6 +41,86 @@ var _ = framework.KubeDescribe("Security Context", func() {
40
41
podClient = f .PodClient ()
41
42
})
42
43
44
+ ginkgo .Context ("when creating a pod in the host PID namespace" , func () {
45
+ makeHostPidPod := func (podName , image string , command []string , hostPID bool ) * v1.Pod {
46
+ return & v1.Pod {
47
+ ObjectMeta : metav1.ObjectMeta {
48
+ Name : podName ,
49
+ },
50
+ Spec : v1.PodSpec {
51
+ RestartPolicy : v1 .RestartPolicyNever ,
52
+ HostPID : hostPID ,
53
+ Containers : []v1.Container {
54
+ {
55
+ Image : image ,
56
+ Name : podName ,
57
+ Command : command ,
58
+ },
59
+ },
60
+ },
61
+ }
62
+ }
63
+ createAndWaitHostPidPod := func (podName string , hostPID bool ) {
64
+ podClient .Create (makeHostPidPod (podName ,
65
+ framework .BusyBoxImage ,
66
+ []string {"sh" , "-c" , "pidof nginx || true" },
67
+ hostPID ,
68
+ ))
69
+
70
+ podClient .WaitForSuccess (podName , framework .PodStartTimeout )
71
+ }
72
+
73
+ nginxPid := ""
74
+ ginkgo .BeforeEach (func () {
75
+ nginxPodName := "nginx-hostpid-" + string (uuid .NewUUID ())
76
+ podClient .CreateSync (makeHostPidPod (nginxPodName ,
77
+ imageutils .GetE2EImage (imageutils .Nginx ),
78
+ nil ,
79
+ true ,
80
+ ))
81
+
82
+ output := f .ExecShellInContainer (nginxPodName , nginxPodName ,
83
+ "cat /var/run/nginx.pid" )
84
+ nginxPid = strings .TrimSpace (output )
85
+ })
86
+
87
+ ginkgo .It ("should show its pid in the host PID namespace [LinuxOnly] [NodeFeature:HostAccess]" , func () {
88
+ busyboxPodName := "busybox-hostpid-" + string (uuid .NewUUID ())
89
+ createAndWaitHostPidPod (busyboxPodName , true )
90
+ logs , err := e2epod .GetPodLogs (f .ClientSet , f .Namespace .Name , busyboxPodName , busyboxPodName )
91
+ if err != nil {
92
+ framework .Failf ("GetPodLogs for pod %q failed: %v" , busyboxPodName , err )
93
+ }
94
+
95
+ pids := strings .TrimSpace (logs )
96
+ framework .Logf ("Got nginx's pid %q from pod %q" , pids , busyboxPodName )
97
+ if pids == "" {
98
+ framework .Failf ("nginx's pid should be seen by hostpid containers" )
99
+ }
100
+
101
+ pidSets := sets .NewString (strings .Split (pids , " " )... )
102
+ if ! pidSets .Has (nginxPid ) {
103
+ framework .Failf ("nginx's pid should be seen by hostpid containers" )
104
+ }
105
+ })
106
+
107
+ ginkgo .It ("should not show its pid in the non-hostpid containers [LinuxOnly] [NodeFeature:HostAccess]" , func () {
108
+ busyboxPodName := "busybox-non-hostpid-" + string (uuid .NewUUID ())
109
+ createAndWaitHostPidPod (busyboxPodName , false )
110
+ logs , err := e2epod .GetPodLogs (f .ClientSet , f .Namespace .Name , busyboxPodName , busyboxPodName )
111
+ if err != nil {
112
+ framework .Failf ("GetPodLogs for pod %q failed: %v" , busyboxPodName , err )
113
+ }
114
+
115
+ pids := strings .TrimSpace (logs )
116
+ framework .Logf ("Got nginx's pid %q from pod %q" , pids , busyboxPodName )
117
+ pidSets := sets .NewString (strings .Split (pids , " " )... )
118
+ if pidSets .Has (nginxPid ) {
119
+ framework .Failf ("nginx's pid should not be seen by non-hostpid containers" )
120
+ }
121
+ })
122
+ })
123
+
43
124
ginkgo .Context ("When creating a container with runAsUser" , func () {
44
125
makeUserPod := func (podName , image string , command []string , userid int64 ) * v1.Pod {
45
126
return & v1.Pod {
0 commit comments