kubelet_pods_linux_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // +build linux
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package kubelet
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  20. _ "k8s.io/kubernetes/pkg/apis/core/install"
  21. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  22. "k8s.io/kubernetes/pkg/util/mount"
  23. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  24. "k8s.io/kubernetes/pkg/volume/util/subpath"
  25. )
  26. func TestMakeMounts(t *testing.T) {
  27. bTrue := true
  28. propagationHostToContainer := v1.MountPropagationHostToContainer
  29. propagationBidirectional := v1.MountPropagationBidirectional
  30. propagationNone := v1.MountPropagationNone
  31. testCases := map[string]struct {
  32. container v1.Container
  33. podVolumes kubecontainer.VolumeMap
  34. expectErr bool
  35. expectedErrMsg string
  36. expectedMounts []kubecontainer.Mount
  37. }{
  38. "valid mounts in unprivileged container": {
  39. podVolumes: kubecontainer.VolumeMap{
  40. "disk": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/disk"}},
  41. "disk4": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/host"}},
  42. "disk5": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/var/lib/kubelet/podID/volumes/empty/disk5"}},
  43. },
  44. container: v1.Container{
  45. Name: "container1",
  46. VolumeMounts: []v1.VolumeMount{
  47. {
  48. MountPath: "/etc/hosts",
  49. Name: "disk",
  50. ReadOnly: false,
  51. MountPropagation: &propagationHostToContainer,
  52. },
  53. {
  54. MountPath: "/mnt/path3",
  55. Name: "disk",
  56. ReadOnly: true,
  57. MountPropagation: &propagationNone,
  58. },
  59. {
  60. MountPath: "/mnt/path4",
  61. Name: "disk4",
  62. ReadOnly: false,
  63. },
  64. {
  65. MountPath: "/mnt/path5",
  66. Name: "disk5",
  67. ReadOnly: false,
  68. },
  69. },
  70. },
  71. expectedMounts: []kubecontainer.Mount{
  72. {
  73. Name: "disk",
  74. ContainerPath: "/etc/hosts",
  75. HostPath: "/mnt/disk",
  76. ReadOnly: false,
  77. SELinuxRelabel: false,
  78. Propagation: runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
  79. },
  80. {
  81. Name: "disk",
  82. ContainerPath: "/mnt/path3",
  83. HostPath: "/mnt/disk",
  84. ReadOnly: true,
  85. SELinuxRelabel: false,
  86. Propagation: runtimeapi.MountPropagation_PROPAGATION_PRIVATE,
  87. },
  88. {
  89. Name: "disk4",
  90. ContainerPath: "/mnt/path4",
  91. HostPath: "/mnt/host",
  92. ReadOnly: false,
  93. SELinuxRelabel: false,
  94. Propagation: runtimeapi.MountPropagation_PROPAGATION_PRIVATE,
  95. },
  96. {
  97. Name: "disk5",
  98. ContainerPath: "/mnt/path5",
  99. HostPath: "/var/lib/kubelet/podID/volumes/empty/disk5",
  100. ReadOnly: false,
  101. SELinuxRelabel: false,
  102. Propagation: runtimeapi.MountPropagation_PROPAGATION_PRIVATE,
  103. },
  104. },
  105. expectErr: false,
  106. },
  107. "valid mounts in privileged container": {
  108. podVolumes: kubecontainer.VolumeMap{
  109. "disk": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/disk"}},
  110. "disk4": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/host"}},
  111. "disk5": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/var/lib/kubelet/podID/volumes/empty/disk5"}},
  112. },
  113. container: v1.Container{
  114. Name: "container1",
  115. VolumeMounts: []v1.VolumeMount{
  116. {
  117. MountPath: "/etc/hosts",
  118. Name: "disk",
  119. ReadOnly: false,
  120. MountPropagation: &propagationBidirectional,
  121. },
  122. {
  123. MountPath: "/mnt/path3",
  124. Name: "disk",
  125. ReadOnly: true,
  126. MountPropagation: &propagationHostToContainer,
  127. },
  128. {
  129. MountPath: "/mnt/path4",
  130. Name: "disk4",
  131. ReadOnly: false,
  132. },
  133. },
  134. SecurityContext: &v1.SecurityContext{
  135. Privileged: &bTrue,
  136. },
  137. },
  138. expectedMounts: []kubecontainer.Mount{
  139. {
  140. Name: "disk",
  141. ContainerPath: "/etc/hosts",
  142. HostPath: "/mnt/disk",
  143. ReadOnly: false,
  144. SELinuxRelabel: false,
  145. Propagation: runtimeapi.MountPropagation_PROPAGATION_BIDIRECTIONAL,
  146. },
  147. {
  148. Name: "disk",
  149. ContainerPath: "/mnt/path3",
  150. HostPath: "/mnt/disk",
  151. ReadOnly: true,
  152. SELinuxRelabel: false,
  153. Propagation: runtimeapi.MountPropagation_PROPAGATION_HOST_TO_CONTAINER,
  154. },
  155. {
  156. Name: "disk4",
  157. ContainerPath: "/mnt/path4",
  158. HostPath: "/mnt/host",
  159. ReadOnly: false,
  160. SELinuxRelabel: false,
  161. Propagation: runtimeapi.MountPropagation_PROPAGATION_PRIVATE,
  162. },
  163. },
  164. expectErr: false,
  165. },
  166. "invalid absolute SubPath": {
  167. podVolumes: kubecontainer.VolumeMap{
  168. "disk": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/disk"}},
  169. },
  170. container: v1.Container{
  171. VolumeMounts: []v1.VolumeMount{
  172. {
  173. MountPath: "/mnt/path3",
  174. SubPath: "/must/not/be/absolute",
  175. Name: "disk",
  176. ReadOnly: true,
  177. },
  178. },
  179. },
  180. expectErr: true,
  181. expectedErrMsg: "error SubPath `/must/not/be/absolute` must not be an absolute path",
  182. },
  183. "invalid SubPath with backsteps": {
  184. podVolumes: kubecontainer.VolumeMap{
  185. "disk": kubecontainer.VolumeInfo{Mounter: &stubVolume{path: "/mnt/disk"}},
  186. },
  187. container: v1.Container{
  188. VolumeMounts: []v1.VolumeMount{
  189. {
  190. MountPath: "/mnt/path3",
  191. SubPath: "no/backsteps/../allowed",
  192. Name: "disk",
  193. ReadOnly: true,
  194. },
  195. },
  196. },
  197. expectErr: true,
  198. expectedErrMsg: "unable to provision SubPath `no/backsteps/../allowed`: must not contain '..'",
  199. },
  200. "volume doesn't exist": {
  201. podVolumes: kubecontainer.VolumeMap{},
  202. container: v1.Container{
  203. VolumeMounts: []v1.VolumeMount{
  204. {
  205. MountPath: "/mnt/path3",
  206. Name: "disk",
  207. ReadOnly: true,
  208. },
  209. },
  210. },
  211. expectErr: true,
  212. expectedErrMsg: "cannot find volume \"disk\" to mount into container \"\"",
  213. },
  214. "volume mounter is nil": {
  215. podVolumes: kubecontainer.VolumeMap{
  216. "disk": kubecontainer.VolumeInfo{},
  217. },
  218. container: v1.Container{
  219. VolumeMounts: []v1.VolumeMount{
  220. {
  221. MountPath: "/mnt/path3",
  222. Name: "disk",
  223. ReadOnly: true,
  224. },
  225. },
  226. },
  227. expectErr: true,
  228. expectedErrMsg: "cannot find volume \"disk\" to mount into container \"\"",
  229. },
  230. }
  231. for name, tc := range testCases {
  232. t.Run(name, func(t *testing.T) {
  233. fm := &mount.FakeMounter{}
  234. fsp := &subpath.FakeSubpath{}
  235. pod := v1.Pod{
  236. Spec: v1.PodSpec{
  237. HostNetwork: true,
  238. },
  239. }
  240. mounts, _, err := makeMounts(&pod, "/pod", &tc.container, "fakepodname", "", "", tc.podVolumes, fm, fsp, nil)
  241. // validate only the error if we expect an error
  242. if tc.expectErr {
  243. if err == nil || err.Error() != tc.expectedErrMsg {
  244. t.Fatalf("expected error message `%s` but got `%v`", tc.expectedErrMsg, err)
  245. }
  246. return
  247. }
  248. // otherwise validate the mounts
  249. if err != nil {
  250. t.Fatal(err)
  251. }
  252. assert.Equal(t, tc.expectedMounts, mounts, "mounts of container %+v", tc.container)
  253. })
  254. }
  255. }
  256. func TestMakeBlockVolumes(t *testing.T) {
  257. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  258. defer testKubelet.Cleanup()
  259. kubelet := testKubelet.kubelet
  260. testCases := map[string]struct {
  261. container v1.Container
  262. podVolumes kubecontainer.VolumeMap
  263. expectErr bool
  264. expectedErrMsg string
  265. expectedDevices []kubecontainer.DeviceInfo
  266. }{
  267. "valid volumeDevices in container": {
  268. podVolumes: kubecontainer.VolumeMap{
  269. "disk1": kubecontainer.VolumeInfo{BlockVolumeMapper: &stubBlockVolume{dirPath: "/dev/", volName: "sda"}},
  270. "disk2": kubecontainer.VolumeInfo{BlockVolumeMapper: &stubBlockVolume{dirPath: "/dev/disk/by-path/", volName: "diskPath"}, ReadOnly: true},
  271. "disk3": kubecontainer.VolumeInfo{BlockVolumeMapper: &stubBlockVolume{dirPath: "/dev/disk/by-id/", volName: "diskUuid"}},
  272. "disk4": kubecontainer.VolumeInfo{BlockVolumeMapper: &stubBlockVolume{dirPath: "/var/lib/", volName: "rawdisk"}, ReadOnly: true},
  273. },
  274. container: v1.Container{
  275. Name: "container1",
  276. VolumeDevices: []v1.VolumeDevice{
  277. {
  278. DevicePath: "/dev/sda",
  279. Name: "disk1",
  280. },
  281. {
  282. DevicePath: "/dev/xvda",
  283. Name: "disk2",
  284. },
  285. {
  286. DevicePath: "/dev/xvdb",
  287. Name: "disk3",
  288. },
  289. {
  290. DevicePath: "/mnt/rawdisk",
  291. Name: "disk4",
  292. },
  293. },
  294. },
  295. expectedDevices: []kubecontainer.DeviceInfo{
  296. {
  297. PathInContainer: "/dev/sda",
  298. PathOnHost: "/dev/sda",
  299. Permissions: "mrw",
  300. },
  301. {
  302. PathInContainer: "/dev/xvda",
  303. PathOnHost: "/dev/disk/by-path/diskPath",
  304. Permissions: "r",
  305. },
  306. {
  307. PathInContainer: "/dev/xvdb",
  308. PathOnHost: "/dev/disk/by-id/diskUuid",
  309. Permissions: "mrw",
  310. },
  311. {
  312. PathInContainer: "/mnt/rawdisk",
  313. PathOnHost: "/var/lib/rawdisk",
  314. Permissions: "r",
  315. },
  316. },
  317. expectErr: false,
  318. },
  319. "invalid absolute Path": {
  320. podVolumes: kubecontainer.VolumeMap{
  321. "disk": kubecontainer.VolumeInfo{BlockVolumeMapper: &stubBlockVolume{dirPath: "/dev/", volName: "sda"}},
  322. },
  323. container: v1.Container{
  324. VolumeDevices: []v1.VolumeDevice{
  325. {
  326. DevicePath: "must/be/absolute",
  327. Name: "disk",
  328. },
  329. },
  330. },
  331. expectErr: true,
  332. expectedErrMsg: "error DevicePath `must/be/absolute` must be an absolute path",
  333. },
  334. "volume doesn't exist": {
  335. podVolumes: kubecontainer.VolumeMap{},
  336. container: v1.Container{
  337. VolumeDevices: []v1.VolumeDevice{
  338. {
  339. DevicePath: "/dev/sdaa",
  340. Name: "disk",
  341. },
  342. },
  343. },
  344. expectErr: true,
  345. expectedErrMsg: "cannot find volume \"disk\" to pass into container \"\"",
  346. },
  347. "volume BlockVolumeMapper is nil": {
  348. podVolumes: kubecontainer.VolumeMap{
  349. "disk": kubecontainer.VolumeInfo{},
  350. },
  351. container: v1.Container{
  352. VolumeDevices: []v1.VolumeDevice{
  353. {
  354. DevicePath: "/dev/sdzz",
  355. Name: "disk",
  356. },
  357. },
  358. },
  359. expectErr: true,
  360. expectedErrMsg: "cannot find volume \"disk\" to pass into container \"\"",
  361. },
  362. }
  363. for name, tc := range testCases {
  364. t.Run(name, func(t *testing.T) {
  365. pod := v1.Pod{
  366. Spec: v1.PodSpec{
  367. HostNetwork: true,
  368. },
  369. }
  370. blkutil := volumetest.NewBlockVolumePathHandler()
  371. blkVolumes, err := kubelet.makeBlockVolumes(&pod, &tc.container, tc.podVolumes, blkutil)
  372. // validate only the error if we expect an error
  373. if tc.expectErr {
  374. if err == nil || err.Error() != tc.expectedErrMsg {
  375. t.Fatalf("expected error message `%s` but got `%v`", tc.expectedErrMsg, err)
  376. }
  377. return
  378. }
  379. // otherwise validate the devices
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. assert.Equal(t, tc.expectedDevices, blkVolumes, "devices of container %+v", tc.container)
  384. })
  385. }
  386. }