mount_propagation.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package node
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/kubernetes/test/e2e/framework"
  20. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  21. "k8s.io/kubernetes/test/e2e/storage/utils"
  22. imageutils "k8s.io/kubernetes/test/utils/image"
  23. "github.com/onsi/ginkgo"
  24. )
  25. func preparePod(name string, node *v1.Node, propagation *v1.MountPropagationMode, hostDir string) *v1.Pod {
  26. const containerName = "cntr"
  27. bTrue := true
  28. var oneSecond int64 = 1
  29. // The pod prepares /mnt/test/<podname> and sleeps.
  30. cmd := fmt.Sprintf("mkdir /mnt/test/%[1]s; sleep 3600", name)
  31. pod := &v1.Pod{
  32. ObjectMeta: metav1.ObjectMeta{
  33. Name: name,
  34. },
  35. Spec: v1.PodSpec{
  36. NodeName: node.Name,
  37. Containers: []v1.Container{
  38. {
  39. Name: containerName,
  40. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  41. Command: []string{"sh", "-c", cmd},
  42. VolumeMounts: []v1.VolumeMount{
  43. {
  44. Name: "host",
  45. MountPath: "/mnt/test",
  46. MountPropagation: propagation,
  47. },
  48. },
  49. SecurityContext: &v1.SecurityContext{
  50. Privileged: &bTrue,
  51. },
  52. },
  53. },
  54. Volumes: []v1.Volume{
  55. {
  56. Name: "host",
  57. VolumeSource: v1.VolumeSource{
  58. HostPath: &v1.HostPathVolumeSource{
  59. Path: hostDir,
  60. },
  61. },
  62. },
  63. },
  64. // speed up termination of the pod
  65. TerminationGracePeriodSeconds: &oneSecond,
  66. },
  67. }
  68. return pod
  69. }
  70. var _ = SIGDescribe("Mount propagation", func() {
  71. f := framework.NewDefaultFramework("mount-propagation")
  72. ginkgo.It("should propagate mounts to the host", func() {
  73. // This test runs two pods: master and slave with respective mount
  74. // propagation on common /var/lib/kubelet/XXXX directory. Both mount a
  75. // tmpfs to a subdirectory there. We check that these mounts are
  76. // propagated to the right places.
  77. hostExec := utils.NewHostExec(f)
  78. defer hostExec.Cleanup()
  79. // Pick a node where all pods will run.
  80. node, err := e2enode.GetRandomReadySchedulableNode(f.ClientSet)
  81. framework.ExpectNoError(err)
  82. // Fail the test if the namespace is not set. We expect that the
  83. // namespace is unique and we might delete user data if it's not.
  84. if len(f.Namespace.Name) == 0 {
  85. framework.ExpectNotEqual(f.Namespace.Name, "")
  86. return
  87. }
  88. // hostDir is the directory that's shared via HostPath among all pods.
  89. // Make sure it's random enough so we don't clash with another test
  90. // running in parallel.
  91. hostDir := "/var/lib/kubelet/" + f.Namespace.Name
  92. defer func() {
  93. cleanCmd := fmt.Sprintf("rm -rf %q", hostDir)
  94. hostExec.IssueCommand(cleanCmd, node)
  95. }()
  96. podClient := f.PodClient()
  97. bidirectional := v1.MountPropagationBidirectional
  98. master := podClient.CreateSync(preparePod("master", node, &bidirectional, hostDir))
  99. hostToContainer := v1.MountPropagationHostToContainer
  100. slave := podClient.CreateSync(preparePod("slave", node, &hostToContainer, hostDir))
  101. none := v1.MountPropagationNone
  102. private := podClient.CreateSync(preparePod("private", node, &none, hostDir))
  103. defaultPropagation := podClient.CreateSync(preparePod("default", node, nil, hostDir))
  104. // Check that the pods sees directories of each other. This just checks
  105. // that they have the same HostPath, not the mount propagation.
  106. podNames := []string{master.Name, slave.Name, private.Name, defaultPropagation.Name}
  107. for _, podName := range podNames {
  108. for _, dirName := range podNames {
  109. cmd := fmt.Sprintf("test -d /mnt/test/%s", dirName)
  110. f.ExecShellInPod(podName, cmd)
  111. }
  112. }
  113. // Each pod mounts one tmpfs to /mnt/test/<podname> and puts a file there.
  114. for _, podName := range podNames {
  115. cmd := fmt.Sprintf("mount -t tmpfs e2e-mount-propagation-%[1]s /mnt/test/%[1]s; echo %[1]s > /mnt/test/%[1]s/file", podName)
  116. f.ExecShellInPod(podName, cmd)
  117. // unmount tmpfs when the test finishes
  118. cmd = fmt.Sprintf("umount /mnt/test/%s", podName)
  119. defer f.ExecShellInPod(podName, cmd)
  120. }
  121. // The host mounts one tmpfs to testdir/host and puts a file there so we
  122. // can check mount propagation from the host to pods.
  123. cmd := fmt.Sprintf("mkdir %[1]q/host; mount -t tmpfs e2e-mount-propagation-host %[1]q/host; echo host > %[1]q/host/file", hostDir)
  124. err = hostExec.IssueCommand(cmd, node)
  125. framework.ExpectNoError(err)
  126. defer func() {
  127. cmd := fmt.Sprintf("umount %q/host", hostDir)
  128. hostExec.IssueCommand(cmd, node)
  129. }()
  130. // Now check that mounts are propagated to the right containers.
  131. // expectedMounts is map of pod name -> expected mounts visible in the
  132. // pod.
  133. expectedMounts := map[string]sets.String{
  134. // Master sees only its own mount and not the slave's one.
  135. "master": sets.NewString("master", "host"),
  136. // Slave sees master's mount + itself.
  137. "slave": sets.NewString("master", "slave", "host"),
  138. // Private sees only its own mount
  139. "private": sets.NewString("private"),
  140. // Default (=private) sees only its own mount
  141. "default": sets.NewString("default"),
  142. }
  143. dirNames := append(podNames, "host")
  144. for podName, mounts := range expectedMounts {
  145. for _, mountName := range dirNames {
  146. cmd := fmt.Sprintf("cat /mnt/test/%s/file", mountName)
  147. stdout, stderr, err := f.ExecShellInPodWithFullOutput(podName, cmd)
  148. framework.Logf("pod %s mount %s: stdout: %q, stderr: %q error: %v", podName, mountName, stdout, stderr, err)
  149. msg := fmt.Sprintf("When checking pod %s and directory %s", podName, mountName)
  150. shouldBeVisible := mounts.Has(mountName)
  151. if shouldBeVisible {
  152. framework.ExpectNoError(err, "%s: failed to run %q", msg, cmd)
  153. framework.ExpectEqual(stdout, mountName, msg)
  154. } else {
  155. // We *expect* cat to return error here
  156. framework.ExpectError(err, msg)
  157. }
  158. }
  159. }
  160. // Check that the mounts are/are not propagated to the host.
  161. // Host can see mount from master
  162. cmd = fmt.Sprintf("test `cat %q/master/file` = master", hostDir)
  163. err = hostExec.IssueCommand(cmd, node)
  164. framework.ExpectNoError(err, "host should see mount from master")
  165. // Host can't see mount from slave
  166. cmd = fmt.Sprintf("test ! -e %q/slave/file", hostDir)
  167. err = hostExec.IssueCommand(cmd, node)
  168. framework.ExpectNoError(err, "host shouldn't see mount from slave")
  169. })
  170. })