kubelet_volumes_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. Copyright 2016 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 kubelet
  14. import (
  15. "fmt"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/apimachinery/pkg/types"
  22. core "k8s.io/client-go/testing"
  23. "k8s.io/kubernetes/pkg/volume"
  24. volumetest "k8s.io/kubernetes/pkg/volume/testing"
  25. "k8s.io/kubernetes/pkg/volume/util"
  26. )
  27. func TestListVolumesForPod(t *testing.T) {
  28. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  29. defer testKubelet.Cleanup()
  30. kubelet := testKubelet.kubelet
  31. pod := podWithUIDNameNsSpec("12345678", "foo", "test", v1.PodSpec{
  32. Volumes: []v1.Volume{
  33. {
  34. Name: "vol1",
  35. VolumeSource: v1.VolumeSource{
  36. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  37. PDName: "fake-device1",
  38. },
  39. },
  40. },
  41. {
  42. Name: "vol2",
  43. VolumeSource: v1.VolumeSource{
  44. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  45. PDName: "fake-device2",
  46. },
  47. },
  48. },
  49. },
  50. })
  51. stopCh := runVolumeManager(kubelet)
  52. defer close(stopCh)
  53. kubelet.podManager.SetPods([]*v1.Pod{pod})
  54. err := kubelet.volumeManager.WaitForAttachAndMount(pod)
  55. assert.NoError(t, err)
  56. podName := util.GetUniquePodName(pod)
  57. volumesToReturn, volumeExsit := kubelet.ListVolumesForPod(types.UID(podName))
  58. assert.True(t, volumeExsit, "expected to find volumes for pod %q", podName)
  59. outerVolumeSpecName1 := "vol1"
  60. assert.NotNil(t, volumesToReturn[outerVolumeSpecName1], "key %s", outerVolumeSpecName1)
  61. outerVolumeSpecName2 := "vol2"
  62. assert.NotNil(t, volumesToReturn[outerVolumeSpecName2], "key %s", outerVolumeSpecName2)
  63. }
  64. func TestPodVolumesExist(t *testing.T) {
  65. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  66. defer testKubelet.Cleanup()
  67. kubelet := testKubelet.kubelet
  68. pods := []*v1.Pod{
  69. {
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: "pod1",
  72. UID: "pod1uid",
  73. },
  74. Spec: v1.PodSpec{
  75. Volumes: []v1.Volume{
  76. {
  77. Name: "vol1",
  78. VolumeSource: v1.VolumeSource{
  79. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  80. PDName: "fake-device1",
  81. },
  82. },
  83. },
  84. },
  85. },
  86. },
  87. {
  88. ObjectMeta: metav1.ObjectMeta{
  89. Name: "pod2",
  90. UID: "pod2uid",
  91. },
  92. Spec: v1.PodSpec{
  93. Volumes: []v1.Volume{
  94. {
  95. Name: "vol2",
  96. VolumeSource: v1.VolumeSource{
  97. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  98. PDName: "fake-device2",
  99. },
  100. },
  101. },
  102. },
  103. },
  104. },
  105. {
  106. ObjectMeta: metav1.ObjectMeta{
  107. Name: "pod3",
  108. UID: "pod3uid",
  109. },
  110. Spec: v1.PodSpec{
  111. Volumes: []v1.Volume{
  112. {
  113. Name: "vol3",
  114. VolumeSource: v1.VolumeSource{
  115. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  116. PDName: "fake-device3",
  117. },
  118. },
  119. },
  120. },
  121. },
  122. },
  123. }
  124. stopCh := runVolumeManager(kubelet)
  125. defer close(stopCh)
  126. kubelet.podManager.SetPods(pods)
  127. for _, pod := range pods {
  128. err := kubelet.volumeManager.WaitForAttachAndMount(pod)
  129. assert.NoError(t, err)
  130. }
  131. for _, pod := range pods {
  132. podVolumesExist := kubelet.podVolumesExist(pod.UID)
  133. assert.True(t, podVolumesExist, "pod %q", pod.UID)
  134. }
  135. }
  136. func TestVolumeAttachAndMountControllerDisabled(t *testing.T) {
  137. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  138. defer testKubelet.Cleanup()
  139. kubelet := testKubelet.kubelet
  140. pod := podWithUIDNameNsSpec("12345678", "foo", "test", v1.PodSpec{
  141. Volumes: []v1.Volume{
  142. {
  143. Name: "vol1",
  144. VolumeSource: v1.VolumeSource{
  145. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  146. PDName: "fake-device",
  147. },
  148. },
  149. },
  150. },
  151. })
  152. stopCh := runVolumeManager(kubelet)
  153. defer close(stopCh)
  154. kubelet.podManager.SetPods([]*v1.Pod{pod})
  155. err := kubelet.volumeManager.WaitForAttachAndMount(pod)
  156. assert.NoError(t, err)
  157. podVolumes := kubelet.volumeManager.GetMountedVolumesForPod(
  158. util.GetUniquePodName(pod))
  159. expectedPodVolumes := []string{"vol1"}
  160. assert.Len(t, podVolumes, len(expectedPodVolumes), "Volumes for pod %+v", pod)
  161. for _, name := range expectedPodVolumes {
  162. assert.Contains(t, podVolumes, name, "Volumes for pod %+v", pod)
  163. }
  164. assert.True(t, testKubelet.volumePlugin.GetNewAttacherCallCount() >= 1, "Expected plugin NewAttacher to be called at least once")
  165. assert.NoError(t, volumetest.VerifyWaitForAttachCallCount(
  166. 1 /* expectedWaitForAttachCallCount */, testKubelet.volumePlugin))
  167. assert.NoError(t, volumetest.VerifyAttachCallCount(
  168. 1 /* expectedAttachCallCount */, testKubelet.volumePlugin))
  169. assert.NoError(t, volumetest.VerifyMountDeviceCallCount(
  170. 1 /* expectedMountDeviceCallCount */, testKubelet.volumePlugin))
  171. assert.NoError(t, volumetest.VerifySetUpCallCount(
  172. 1 /* expectedSetUpCallCount */, testKubelet.volumePlugin))
  173. }
  174. func TestVolumeUnmountAndDetachControllerDisabled(t *testing.T) {
  175. testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
  176. defer testKubelet.Cleanup()
  177. kubelet := testKubelet.kubelet
  178. pod := podWithUIDNameNsSpec("12345678", "foo", "test", v1.PodSpec{
  179. Volumes: []v1.Volume{
  180. {
  181. Name: "vol1",
  182. VolumeSource: v1.VolumeSource{
  183. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  184. PDName: "fake-device",
  185. },
  186. },
  187. },
  188. },
  189. })
  190. stopCh := runVolumeManager(kubelet)
  191. defer close(stopCh)
  192. // Add pod
  193. kubelet.podManager.SetPods([]*v1.Pod{pod})
  194. // Verify volumes attached
  195. err := kubelet.volumeManager.WaitForAttachAndMount(pod)
  196. assert.NoError(t, err)
  197. podVolumes := kubelet.volumeManager.GetMountedVolumesForPod(
  198. util.GetUniquePodName(pod))
  199. expectedPodVolumes := []string{"vol1"}
  200. assert.Len(t, podVolumes, len(expectedPodVolumes), "Volumes for pod %+v", pod)
  201. for _, name := range expectedPodVolumes {
  202. assert.Contains(t, podVolumes, name, "Volumes for pod %+v", pod)
  203. }
  204. assert.True(t, testKubelet.volumePlugin.GetNewAttacherCallCount() >= 1, "Expected plugin NewAttacher to be called at least once")
  205. assert.NoError(t, volumetest.VerifyWaitForAttachCallCount(
  206. 1 /* expectedWaitForAttachCallCount */, testKubelet.volumePlugin))
  207. assert.NoError(t, volumetest.VerifyAttachCallCount(
  208. 1 /* expectedAttachCallCount */, testKubelet.volumePlugin))
  209. assert.NoError(t, volumetest.VerifyMountDeviceCallCount(
  210. 1 /* expectedMountDeviceCallCount */, testKubelet.volumePlugin))
  211. assert.NoError(t, volumetest.VerifySetUpCallCount(
  212. 1 /* expectedSetUpCallCount */, testKubelet.volumePlugin))
  213. // Remove pod
  214. kubelet.podManager.SetPods([]*v1.Pod{})
  215. assert.NoError(t, waitForVolumeUnmount(kubelet.volumeManager, pod))
  216. // Verify volumes unmounted
  217. podVolumes = kubelet.volumeManager.GetMountedVolumesForPod(
  218. util.GetUniquePodName(pod))
  219. assert.Len(t, podVolumes, 0,
  220. "Expected volumes to be unmounted and detached. But some volumes are still mounted: %#v", podVolumes)
  221. assert.NoError(t, volumetest.VerifyTearDownCallCount(
  222. 1 /* expectedTearDownCallCount */, testKubelet.volumePlugin))
  223. // Verify volumes detached and no longer reported as in use
  224. assert.NoError(t, waitForVolumeDetach(v1.UniqueVolumeName("fake/fake-device"), kubelet.volumeManager))
  225. assert.True(t, testKubelet.volumePlugin.GetNewAttacherCallCount() >= 1, "Expected plugin NewAttacher to be called at least once")
  226. assert.NoError(t, volumetest.VerifyDetachCallCount(
  227. 1 /* expectedDetachCallCount */, testKubelet.volumePlugin))
  228. }
  229. func TestVolumeAttachAndMountControllerEnabled(t *testing.T) {
  230. testKubelet := newTestKubelet(t, true /* controllerAttachDetachEnabled */)
  231. defer testKubelet.Cleanup()
  232. kubelet := testKubelet.kubelet
  233. kubeClient := testKubelet.fakeKubeClient
  234. kubeClient.AddReactor("get", "nodes",
  235. func(action core.Action) (bool, runtime.Object, error) {
  236. return true, &v1.Node{
  237. ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname},
  238. Status: v1.NodeStatus{
  239. VolumesAttached: []v1.AttachedVolume{
  240. {
  241. Name: "fake/fake-device",
  242. DevicePath: "fake/path",
  243. },
  244. }},
  245. }, nil
  246. })
  247. kubeClient.AddReactor("*", "*", func(action core.Action) (bool, runtime.Object, error) {
  248. return true, nil, fmt.Errorf("no reaction implemented for %s", action)
  249. })
  250. pod := podWithUIDNameNsSpec("12345678", "foo", "test", v1.PodSpec{
  251. Volumes: []v1.Volume{
  252. {
  253. Name: "vol1",
  254. VolumeSource: v1.VolumeSource{
  255. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  256. PDName: "fake-device",
  257. },
  258. },
  259. },
  260. },
  261. })
  262. stopCh := runVolumeManager(kubelet)
  263. defer close(stopCh)
  264. kubelet.podManager.SetPods([]*v1.Pod{pod})
  265. // Fake node status update
  266. go simulateVolumeInUseUpdate(
  267. v1.UniqueVolumeName("fake/fake-device"),
  268. stopCh,
  269. kubelet.volumeManager)
  270. assert.NoError(t, kubelet.volumeManager.WaitForAttachAndMount(pod))
  271. podVolumes := kubelet.volumeManager.GetMountedVolumesForPod(
  272. util.GetUniquePodName(pod))
  273. expectedPodVolumes := []string{"vol1"}
  274. assert.Len(t, podVolumes, len(expectedPodVolumes), "Volumes for pod %+v", pod)
  275. for _, name := range expectedPodVolumes {
  276. assert.Contains(t, podVolumes, name, "Volumes for pod %+v", pod)
  277. }
  278. assert.True(t, testKubelet.volumePlugin.GetNewAttacherCallCount() >= 1, "Expected plugin NewAttacher to be called at least once")
  279. assert.NoError(t, volumetest.VerifyWaitForAttachCallCount(
  280. 1 /* expectedWaitForAttachCallCount */, testKubelet.volumePlugin))
  281. assert.NoError(t, volumetest.VerifyZeroAttachCalls(testKubelet.volumePlugin))
  282. assert.NoError(t, volumetest.VerifyMountDeviceCallCount(
  283. 1 /* expectedMountDeviceCallCount */, testKubelet.volumePlugin))
  284. assert.NoError(t, volumetest.VerifySetUpCallCount(
  285. 1 /* expectedSetUpCallCount */, testKubelet.volumePlugin))
  286. }
  287. func TestVolumeUnmountAndDetachControllerEnabled(t *testing.T) {
  288. testKubelet := newTestKubelet(t, true /* controllerAttachDetachEnabled */)
  289. defer testKubelet.Cleanup()
  290. kubelet := testKubelet.kubelet
  291. kubeClient := testKubelet.fakeKubeClient
  292. kubeClient.AddReactor("get", "nodes",
  293. func(action core.Action) (bool, runtime.Object, error) {
  294. return true, &v1.Node{
  295. ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname},
  296. Status: v1.NodeStatus{
  297. VolumesAttached: []v1.AttachedVolume{
  298. {
  299. Name: "fake/fake-device",
  300. DevicePath: "fake/path",
  301. },
  302. }},
  303. }, nil
  304. })
  305. kubeClient.AddReactor("*", "*", func(action core.Action) (bool, runtime.Object, error) {
  306. return true, nil, fmt.Errorf("no reaction implemented for %s", action)
  307. })
  308. pod := podWithUIDNameNsSpec("12345678", "foo", "test", v1.PodSpec{
  309. Volumes: []v1.Volume{
  310. {
  311. Name: "vol1",
  312. VolumeSource: v1.VolumeSource{
  313. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  314. PDName: "fake-device",
  315. },
  316. },
  317. },
  318. },
  319. })
  320. stopCh := runVolumeManager(kubelet)
  321. defer close(stopCh)
  322. // Add pod
  323. kubelet.podManager.SetPods([]*v1.Pod{pod})
  324. // Fake node status update
  325. go simulateVolumeInUseUpdate(
  326. v1.UniqueVolumeName("fake/fake-device"),
  327. stopCh,
  328. kubelet.volumeManager)
  329. // Verify volumes attached
  330. assert.NoError(t, kubelet.volumeManager.WaitForAttachAndMount(pod))
  331. podVolumes := kubelet.volumeManager.GetMountedVolumesForPod(
  332. util.GetUniquePodName(pod))
  333. expectedPodVolumes := []string{"vol1"}
  334. assert.Len(t, podVolumes, len(expectedPodVolumes), "Volumes for pod %+v", pod)
  335. for _, name := range expectedPodVolumes {
  336. assert.Contains(t, podVolumes, name, "Volumes for pod %+v", pod)
  337. }
  338. assert.True(t, testKubelet.volumePlugin.GetNewAttacherCallCount() >= 1, "Expected plugin NewAttacher to be called at least once")
  339. assert.NoError(t, volumetest.VerifyWaitForAttachCallCount(
  340. 1 /* expectedWaitForAttachCallCount */, testKubelet.volumePlugin))
  341. assert.NoError(t, volumetest.VerifyZeroAttachCalls(testKubelet.volumePlugin))
  342. assert.NoError(t, volumetest.VerifyMountDeviceCallCount(
  343. 1 /* expectedMountDeviceCallCount */, testKubelet.volumePlugin))
  344. assert.NoError(t, volumetest.VerifySetUpCallCount(
  345. 1 /* expectedSetUpCallCount */, testKubelet.volumePlugin))
  346. // Remove pod
  347. kubelet.podManager.SetPods([]*v1.Pod{})
  348. assert.NoError(t, waitForVolumeUnmount(kubelet.volumeManager, pod))
  349. // Verify volumes unmounted
  350. podVolumes = kubelet.volumeManager.GetMountedVolumesForPod(
  351. util.GetUniquePodName(pod))
  352. assert.Len(t, podVolumes, 0,
  353. "Expected volumes to be unmounted and detached. But some volumes are still mounted: %#v", podVolumes)
  354. assert.NoError(t, volumetest.VerifyTearDownCallCount(
  355. 1 /* expectedTearDownCallCount */, testKubelet.volumePlugin))
  356. // Verify volumes detached and no longer reported as in use
  357. assert.NoError(t, waitForVolumeDetach(v1.UniqueVolumeName("fake/fake-device"), kubelet.volumeManager))
  358. assert.True(t, testKubelet.volumePlugin.GetNewAttacherCallCount() >= 1, "Expected plugin NewAttacher to be called at least once")
  359. assert.NoError(t, volumetest.VerifyZeroDetachCallCount(testKubelet.volumePlugin))
  360. }
  361. type stubVolume struct {
  362. path string
  363. volume.MetricsNil
  364. }
  365. func (f *stubVolume) GetPath() string {
  366. return f.path
  367. }
  368. func (f *stubVolume) GetAttributes() volume.Attributes {
  369. return volume.Attributes{}
  370. }
  371. func (f *stubVolume) CanMount() error {
  372. return nil
  373. }
  374. func (f *stubVolume) SetUp(mounterArgs volume.MounterArgs) error {
  375. return nil
  376. }
  377. func (f *stubVolume) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  378. return nil
  379. }
  380. type stubBlockVolume struct {
  381. dirPath string
  382. volName string
  383. }
  384. func (f *stubBlockVolume) GetGlobalMapPath(spec *volume.Spec) (string, error) {
  385. return "", nil
  386. }
  387. func (f *stubBlockVolume) GetPodDeviceMapPath() (string, string) {
  388. return f.dirPath, f.volName
  389. }
  390. func (f *stubBlockVolume) SetUpDevice() (string, error) {
  391. return "", nil
  392. }
  393. func (f stubBlockVolume) MapDevice(devicePath, globalMapPath, volumeMapPath, volumeMapName string, podUID types.UID) error {
  394. return nil
  395. }
  396. func (f *stubBlockVolume) TearDownDevice(mapPath string, devicePath string) error {
  397. return nil
  398. }