persistent_volumes-gce.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 storage
  14. import (
  15. "context"
  16. "github.com/onsi/ginkgo"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/labels"
  20. "k8s.io/apimachinery/pkg/types"
  21. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  25. "k8s.io/kubernetes/test/e2e/framework/providers/gce"
  26. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  27. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  28. "k8s.io/kubernetes/test/e2e/storage/utils"
  29. )
  30. // verifyGCEDiskAttached performs a sanity check to verify the PD attached to the node
  31. func verifyGCEDiskAttached(diskName string, nodeName types.NodeName) bool {
  32. gceCloud, err := gce.GetGCECloud()
  33. framework.ExpectNoError(err)
  34. isAttached, err := gceCloud.DiskIsAttached(diskName, nodeName)
  35. framework.ExpectNoError(err)
  36. return isAttached
  37. }
  38. // initializeGCETestSpec creates a PV, PVC, and ClientPod that will run until killed by test or clean up.
  39. func initializeGCETestSpec(c clientset.Interface, ns string, pvConfig e2epv.PersistentVolumeConfig, pvcConfig e2epv.PersistentVolumeClaimConfig, isPrebound bool) (*v1.Pod, *v1.PersistentVolume, *v1.PersistentVolumeClaim) {
  40. ginkgo.By("Creating the PV and PVC")
  41. pv, pvc, err := e2epv.CreatePVPVC(c, pvConfig, pvcConfig, ns, isPrebound)
  42. framework.ExpectNoError(err)
  43. framework.ExpectNoError(e2epv.WaitOnPVandPVC(c, ns, pv, pvc))
  44. ginkgo.By("Creating the Client Pod")
  45. clientPod, err := e2epod.CreateClientPod(c, ns, pvc)
  46. framework.ExpectNoError(err)
  47. return clientPod, pv, pvc
  48. }
  49. // Testing configurations of single a PV/PVC pair attached to a GCE PD
  50. var _ = utils.SIGDescribe("PersistentVolumes GCEPD", func() {
  51. var (
  52. c clientset.Interface
  53. diskName string
  54. ns string
  55. err error
  56. pv *v1.PersistentVolume
  57. pvc *v1.PersistentVolumeClaim
  58. clientPod *v1.Pod
  59. pvConfig e2epv.PersistentVolumeConfig
  60. pvcConfig e2epv.PersistentVolumeClaimConfig
  61. volLabel labels.Set
  62. selector *metav1.LabelSelector
  63. node types.NodeName
  64. )
  65. f := framework.NewDefaultFramework("pv")
  66. ginkgo.BeforeEach(func() {
  67. c = f.ClientSet
  68. ns = f.Namespace.Name
  69. // Enforce binding only within test space via selector labels
  70. volLabel = labels.Set{e2epv.VolumeSelectorKey: ns}
  71. selector = metav1.SetAsLabelSelector(volLabel)
  72. e2eskipper.SkipUnlessProviderIs("gce", "gke")
  73. ginkgo.By("Initializing Test Spec")
  74. diskName, err = e2epv.CreatePDWithRetry()
  75. framework.ExpectNoError(err)
  76. pvConfig = e2epv.PersistentVolumeConfig{
  77. NamePrefix: "gce-",
  78. Labels: volLabel,
  79. PVSource: v1.PersistentVolumeSource{
  80. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  81. PDName: diskName,
  82. FSType: "ext3",
  83. ReadOnly: false,
  84. },
  85. },
  86. Prebind: nil,
  87. }
  88. emptyStorageClass := ""
  89. pvcConfig = e2epv.PersistentVolumeClaimConfig{
  90. Selector: selector,
  91. StorageClassName: &emptyStorageClass,
  92. }
  93. clientPod, pv, pvc = initializeGCETestSpec(c, ns, pvConfig, pvcConfig, false)
  94. node = types.NodeName(clientPod.Spec.NodeName)
  95. })
  96. ginkgo.AfterEach(func() {
  97. framework.Logf("AfterEach: Cleaning up test resources")
  98. if c != nil {
  99. framework.ExpectNoError(e2epod.DeletePodWithWait(c, clientPod))
  100. if errs := e2epv.PVPVCCleanup(c, ns, pv, pvc); len(errs) > 0 {
  101. framework.Failf("AfterEach: Failed to delete PVC and/or PV. Errors: %v", utilerrors.NewAggregate(errs))
  102. }
  103. clientPod, pv, pvc, node = nil, nil, nil, ""
  104. if diskName != "" {
  105. framework.ExpectNoError(e2epv.DeletePDWithRetry(diskName))
  106. }
  107. }
  108. })
  109. // Attach a persistent disk to a pod using a PVC.
  110. // Delete the PVC and then the pod. Expect the pod to succeed in unmounting and detaching PD on delete.
  111. ginkgo.It("should test that deleting a PVC before the pod does not cause pod deletion to fail on PD detach", func() {
  112. ginkgo.By("Deleting the Claim")
  113. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvc.Name, ns), "Unable to delete PVC ", pvc.Name)
  114. framework.ExpectEqual(verifyGCEDiskAttached(diskName, node), true)
  115. ginkgo.By("Deleting the Pod")
  116. framework.ExpectNoError(e2epod.DeletePodWithWait(c, clientPod), "Failed to delete pod ", clientPod.Name)
  117. ginkgo.By("Verifying Persistent Disk detach")
  118. framework.ExpectNoError(waitForPDDetach(diskName, node), "PD ", diskName, " did not detach")
  119. })
  120. // Attach a persistent disk to a pod using a PVC.
  121. // Delete the PV and then the pod. Expect the pod to succeed in unmounting and detaching PD on delete.
  122. ginkgo.It("should test that deleting the PV before the pod does not cause pod deletion to fail on PD detach", func() {
  123. ginkgo.By("Deleting the Persistent Volume")
  124. framework.ExpectNoError(e2epv.DeletePersistentVolume(c, pv.Name), "Failed to delete PV ", pv.Name)
  125. framework.ExpectEqual(verifyGCEDiskAttached(diskName, node), true)
  126. ginkgo.By("Deleting the client pod")
  127. framework.ExpectNoError(e2epod.DeletePodWithWait(c, clientPod), "Failed to delete pod ", clientPod.Name)
  128. ginkgo.By("Verifying Persistent Disk detaches")
  129. framework.ExpectNoError(waitForPDDetach(diskName, node), "PD ", diskName, " did not detach")
  130. })
  131. // Test that a Pod and PVC attached to a GCEPD successfully unmounts and detaches when the encompassing Namespace is deleted.
  132. ginkgo.It("should test that deleting the Namespace of a PVC and Pod causes the successful detach of Persistent Disk [Flaky]", func() {
  133. ginkgo.By("Deleting the Namespace")
  134. err := c.CoreV1().Namespaces().Delete(context.TODO(), ns, nil)
  135. framework.ExpectNoError(err)
  136. err = framework.WaitForNamespacesDeleted(c, []string{ns}, framework.DefaultNamespaceDeletionTimeout)
  137. framework.ExpectNoError(err)
  138. ginkgo.By("Verifying Persistent Disk detaches")
  139. framework.ExpectNoError(waitForPDDetach(diskName, node), "PD ", diskName, " did not detach")
  140. })
  141. })