pv_reclaimpolicy.go 10 KB

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