pv_reclaimpolicy.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 vsphere
  14. import (
  15. "strconv"
  16. "time"
  17. "github.com/onsi/ginkgo"
  18. "github.com/onsi/gomega"
  19. v1 "k8s.io/api/core/v1"
  20. apierrs "k8s.io/apimachinery/pkg/api/errors"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  25. "k8s.io/kubernetes/test/e2e/storage/utils"
  26. )
  27. var _ = utils.SIGDescribe("PersistentVolumes [Feature:ReclaimPolicy]", func() {
  28. f := framework.NewDefaultFramework("persistentvolumereclaim")
  29. var (
  30. c clientset.Interface
  31. ns string
  32. volumePath string
  33. pv *v1.PersistentVolume
  34. pvc *v1.PersistentVolumeClaim
  35. nodeInfo *NodeInfo
  36. )
  37. ginkgo.BeforeEach(func() {
  38. c = f.ClientSet
  39. ns = f.Namespace.Name
  40. framework.ExpectNoError(framework.WaitForAllNodesSchedulable(c, framework.TestContext.NodeSchedulableTimeout))
  41. })
  42. utils.SIGDescribe("persistentvolumereclaim:vsphere", func() {
  43. ginkgo.BeforeEach(func() {
  44. framework.SkipUnlessProviderIs("vsphere")
  45. Bootstrap(f)
  46. nodeInfo = GetReadySchedulableRandomNodeInfo()
  47. pv = nil
  48. pvc = nil
  49. volumePath = ""
  50. })
  51. ginkgo.AfterEach(func() {
  52. testCleanupVSpherePersistentVolumeReclaim(c, nodeInfo, ns, volumePath, pv, pvc)
  53. })
  54. /*
  55. This test verifies persistent volume should be deleted when reclaimPolicy on the PV is set to delete and
  56. associated claim is deleted
  57. Test Steps:
  58. 1. Create vmdk
  59. 2. Create PV Spec with volume path set to VMDK file created in Step-1, and PersistentVolumeReclaimPolicy is set to Delete
  60. 3. Create PVC with the storage request set to PV's storage capacity.
  61. 4. Wait for PV and PVC to bound.
  62. 5. Delete PVC
  63. 6. Verify PV is deleted automatically.
  64. */
  65. ginkgo.It("should delete persistent volume when reclaimPolicy set to delete and associated claim is deleted", func() {
  66. var err error
  67. volumePath, pv, pvc, err = testSetupVSpherePersistentVolumeReclaim(c, nodeInfo, ns, v1.PersistentVolumeReclaimDelete)
  68. framework.ExpectNoError(err)
  69. deletePVCAfterBind(c, ns, pvc, pv)
  70. pvc = nil
  71. ginkgo.By("verify pv is deleted")
  72. err = framework.WaitForPersistentVolumeDeleted(c, pv.Name, 3*time.Second, 300*time.Second)
  73. framework.ExpectNoError(err)
  74. pv = nil
  75. volumePath = ""
  76. })
  77. /*
  78. Test Steps:
  79. 1. Create vmdk
  80. 2. Create PV Spec with volume path set to VMDK file created in Step-1, and PersistentVolumeReclaimPolicy is set to Delete
  81. 3. Create PVC with the storage request set to PV's storage capacity.
  82. 4. Wait for PV and PVC to bound.
  83. 5. Delete PVC.
  84. 6. Verify volume is attached to the node and volume is accessible in the pod.
  85. 7. Verify PV status should be failed.
  86. 8. Delete the pod.
  87. 9. Verify PV should be detached from the node and automatically deleted.
  88. */
  89. ginkgo.It("should not detach and unmount PV when associated pvc with delete as reclaimPolicy is deleted when it is in use by the pod", func() {
  90. var err error
  91. volumePath, pv, pvc, err = testSetupVSpherePersistentVolumeReclaim(c, nodeInfo, ns, v1.PersistentVolumeReclaimDelete)
  92. framework.ExpectNoError(err)
  93. // Wait for PV and PVC to Bind
  94. framework.ExpectNoError(framework.WaitOnPVandPVC(c, ns, pv, pvc))
  95. ginkgo.By("Creating the Pod")
  96. pod, err := framework.CreateClientPod(c, ns, pvc)
  97. framework.ExpectNoError(err)
  98. ginkgo.By("Deleting the Claim")
  99. framework.ExpectNoError(framework.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Failed to delete PVC ", pvc.Name)
  100. pvc = nil
  101. // Verify PV is Present, after PVC is deleted and PV status should be Failed.
  102. pv, err := c.CoreV1().PersistentVolumes().Get(pv.Name, metav1.GetOptions{})
  103. framework.ExpectNoError(err)
  104. err = framework.WaitForPersistentVolumePhase(v1.VolumeFailed, c, pv.Name, 1*time.Second, 60*time.Second)
  105. framework.ExpectNoError(err)
  106. ginkgo.By("Verify the volume is attached to the node")
  107. isVolumeAttached, verifyDiskAttachedError := diskIsAttached(pv.Spec.VsphereVolume.VolumePath, pod.Spec.NodeName)
  108. framework.ExpectNoError(verifyDiskAttachedError)
  109. gomega.Expect(isVolumeAttached).To(gomega.BeTrue())
  110. ginkgo.By("Verify the volume is accessible and available in the pod")
  111. verifyVSphereVolumesAccessible(c, pod, []*v1.PersistentVolume{pv})
  112. e2elog.Logf("Verified that Volume is accessible in the POD after deleting PV claim")
  113. ginkgo.By("Deleting the Pod")
  114. framework.ExpectNoError(framework.DeletePodWithWait(f, c, pod), "Failed to delete pod ", pod.Name)
  115. ginkgo.By("Verify PV is detached from the node after Pod is deleted")
  116. err = waitForVSphereDiskToDetach(pv.Spec.VsphereVolume.VolumePath, pod.Spec.NodeName)
  117. framework.ExpectNoError(err)
  118. ginkgo.By("Verify PV should be deleted automatically")
  119. framework.ExpectNoError(framework.WaitForPersistentVolumeDeleted(c, pv.Name, 1*time.Second, 30*time.Second))
  120. pv = nil
  121. volumePath = ""
  122. })
  123. /*
  124. This test Verify persistent volume should be retained when reclaimPolicy on the PV is set to retain
  125. and associated claim is deleted
  126. Test Steps:
  127. 1. Create vmdk
  128. 2. Create PV Spec with volume path set to VMDK file created in Step-1, and PersistentVolumeReclaimPolicy is set to Retain
  129. 3. Create PVC with the storage request set to PV's storage capacity.
  130. 4. Wait for PV and PVC to bound.
  131. 5. Write some content in the volume.
  132. 6. Delete PVC
  133. 7. Verify PV is retained.
  134. 8. Delete retained PV.
  135. 9. Create PV Spec with the same volume path used in step 2.
  136. 10. Create PVC with the storage request set to PV's storage capacity.
  137. 11. Created POD using PVC created in Step 10 and verify volume content is matching.
  138. */
  139. ginkgo.It("should retain persistent volume when reclaimPolicy set to retain when associated claim is deleted", func() {
  140. var err error
  141. var volumeFileContent = "hello from vsphere cloud provider, Random Content is :" + strconv.FormatInt(time.Now().UnixNano(), 10)
  142. volumePath, pv, pvc, err = testSetupVSpherePersistentVolumeReclaim(c, nodeInfo, ns, v1.PersistentVolumeReclaimRetain)
  143. framework.ExpectNoError(err)
  144. writeContentToVSpherePV(c, pvc, volumeFileContent)
  145. ginkgo.By("Delete PVC")
  146. framework.ExpectNoError(framework.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Failed to delete PVC ", pvc.Name)
  147. pvc = nil
  148. ginkgo.By("Verify PV is retained")
  149. e2elog.Logf("Waiting for PV %v to become Released", pv.Name)
  150. err = framework.WaitForPersistentVolumePhase(v1.VolumeReleased, c, pv.Name, 3*time.Second, 300*time.Second)
  151. framework.ExpectNoError(err)
  152. framework.ExpectNoError(framework.DeletePersistentVolume(c, pv.Name), "Failed to delete PV ", pv.Name)
  153. ginkgo.By("Creating the PV for same volume path")
  154. pv = getVSpherePersistentVolumeSpec(volumePath, v1.PersistentVolumeReclaimRetain, nil)
  155. pv, err = c.CoreV1().PersistentVolumes().Create(pv)
  156. framework.ExpectNoError(err)
  157. ginkgo.By("creating the pvc")
  158. pvc = getVSpherePersistentVolumeClaimSpec(ns, nil)
  159. pvc, err = c.CoreV1().PersistentVolumeClaims(ns).Create(pvc)
  160. framework.ExpectNoError(err)
  161. ginkgo.By("wait for the pv and pvc to bind")
  162. framework.ExpectNoError(framework.WaitOnPVandPVC(c, ns, pv, pvc))
  163. verifyContentOfVSpherePV(c, pvc, volumeFileContent)
  164. })
  165. })
  166. })
  167. // Test Setup for persistentvolumereclaim tests for vSphere Provider
  168. func testSetupVSpherePersistentVolumeReclaim(c clientset.Interface, nodeInfo *NodeInfo, ns string, persistentVolumeReclaimPolicy v1.PersistentVolumeReclaimPolicy) (volumePath string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim, err error) {
  169. ginkgo.By("running testSetupVSpherePersistentVolumeReclaim")
  170. ginkgo.By("creating vmdk")
  171. volumePath, err = nodeInfo.VSphere.CreateVolume(&VolumeOptions{}, nodeInfo.DataCenterRef)
  172. if err != nil {
  173. return
  174. }
  175. ginkgo.By("creating the pv")
  176. pv = getVSpherePersistentVolumeSpec(volumePath, persistentVolumeReclaimPolicy, nil)
  177. pv, err = c.CoreV1().PersistentVolumes().Create(pv)
  178. if err != nil {
  179. return
  180. }
  181. ginkgo.By("creating the pvc")
  182. pvc = getVSpherePersistentVolumeClaimSpec(ns, nil)
  183. pvc, err = c.CoreV1().PersistentVolumeClaims(ns).Create(pvc)
  184. return
  185. }
  186. // Test Cleanup for persistentvolumereclaim tests for vSphere Provider
  187. func testCleanupVSpherePersistentVolumeReclaim(c clientset.Interface, nodeInfo *NodeInfo, ns string, volumePath string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) {
  188. ginkgo.By("running testCleanupVSpherePersistentVolumeReclaim")
  189. if len(volumePath) > 0 {
  190. err := nodeInfo.VSphere.DeleteVolume(volumePath, nodeInfo.DataCenterRef)
  191. framework.ExpectNoError(err)
  192. }
  193. if pv != nil {
  194. framework.ExpectNoError(framework.DeletePersistentVolume(c, pv.Name), "Failed to delete PV ", pv.Name)
  195. }
  196. if pvc != nil {
  197. framework.ExpectNoError(framework.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Failed to delete PVC ", pvc.Name)
  198. }
  199. }
  200. // func to wait until PV and PVC bind and once bind completes, delete the PVC
  201. func deletePVCAfterBind(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim, pv *v1.PersistentVolume) {
  202. var err error
  203. ginkgo.By("wait for the pv and pvc to bind")
  204. framework.ExpectNoError(framework.WaitOnPVandPVC(c, ns, pv, pvc))
  205. ginkgo.By("delete pvc")
  206. framework.ExpectNoError(framework.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Failed to delete PVC ", pvc.Name)
  207. pvc, err = c.CoreV1().PersistentVolumeClaims(ns).Get(pvc.Name, metav1.GetOptions{})
  208. if !apierrs.IsNotFound(err) {
  209. framework.ExpectNoError(err)
  210. }
  211. }