pvc_label_selector.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "time"
  17. "github.com/onsi/ginkgo"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. clientset "k8s.io/client-go/kubernetes"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  23. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  24. "k8s.io/kubernetes/test/e2e/storage/utils"
  25. )
  26. /*
  27. This is a function test for Selector-Label Volume Binding Feature
  28. Test verifies volume with the matching label is bounded with the PVC.
  29. Test Steps
  30. ----------
  31. 1. Create VMDK.
  32. 2. Create pv with label volume-type:ssd, volume path set to vmdk created in previous step, and PersistentVolumeReclaimPolicy is set to Delete.
  33. 3. Create PVC (pvcVvol) with label selector to match with volume-type:vvol
  34. 4. Create PVC (pvcSsd) with label selector to match with volume-type:ssd
  35. 5. Wait and verify pvSsd is bound with PV.
  36. 6. Verify Status of pvcVvol is still pending.
  37. 7. Delete pvcSsd.
  38. 8. verify associated pv is also deleted.
  39. 9. delete pvcVvol
  40. */
  41. var _ = utils.SIGDescribe("PersistentVolumes [Feature:vsphere][Feature:LabelSelector]", func() {
  42. f := framework.NewDefaultFramework("pvclabelselector")
  43. var (
  44. c clientset.Interface
  45. ns string
  46. pvSsd *v1.PersistentVolume
  47. pvcSsd *v1.PersistentVolumeClaim
  48. pvcVvol *v1.PersistentVolumeClaim
  49. volumePath string
  50. ssdlabels map[string]string
  51. vvollabels map[string]string
  52. err error
  53. nodeInfo *NodeInfo
  54. )
  55. ginkgo.BeforeEach(func() {
  56. e2eskipper.SkipUnlessProviderIs("vsphere")
  57. c = f.ClientSet
  58. ns = f.Namespace.Name
  59. Bootstrap(f)
  60. nodeInfo = GetReadySchedulableRandomNodeInfo()
  61. framework.ExpectNoError(framework.WaitForAllNodesSchedulable(c, framework.TestContext.NodeSchedulableTimeout))
  62. ssdlabels = make(map[string]string)
  63. ssdlabels["volume-type"] = "ssd"
  64. vvollabels = make(map[string]string)
  65. vvollabels["volume-type"] = "vvol"
  66. })
  67. utils.SIGDescribe("Selector-Label Volume Binding:vsphere [Feature:vsphere]", func() {
  68. ginkgo.AfterEach(func() {
  69. ginkgo.By("Running clean up actions")
  70. if framework.ProviderIs("vsphere") {
  71. testCleanupVSpherePVClabelselector(c, ns, nodeInfo, volumePath, pvSsd, pvcSsd, pvcVvol)
  72. }
  73. })
  74. ginkgo.It("should bind volume with claim for given label", func() {
  75. volumePath, pvSsd, pvcSsd, pvcVvol, err = testSetupVSpherePVClabelselector(c, nodeInfo, ns, ssdlabels, vvollabels)
  76. framework.ExpectNoError(err)
  77. ginkgo.By("wait for the pvcSsd to bind with pvSsd")
  78. framework.ExpectNoError(e2epv.WaitOnPVandPVC(c, ns, pvSsd, pvcSsd))
  79. ginkgo.By("Verify status of pvcVvol is pending")
  80. err = e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimPending, c, ns, pvcVvol.Name, 3*time.Second, 300*time.Second)
  81. framework.ExpectNoError(err)
  82. ginkgo.By("delete pvcSsd")
  83. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvcSsd.Name, ns), "Failed to delete PVC ", pvcSsd.Name)
  84. ginkgo.By("verify pvSsd is deleted")
  85. err = framework.WaitForPersistentVolumeDeleted(c, pvSsd.Name, 3*time.Second, 300*time.Second)
  86. framework.ExpectNoError(err)
  87. volumePath = ""
  88. ginkgo.By("delete pvcVvol")
  89. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvcVvol.Name, ns), "Failed to delete PVC ", pvcVvol.Name)
  90. })
  91. })
  92. })
  93. func testSetupVSpherePVClabelselector(c clientset.Interface, nodeInfo *NodeInfo, ns string, ssdlabels map[string]string, vvollabels map[string]string) (volumePath string, pvSsd *v1.PersistentVolume, pvcSsd *v1.PersistentVolumeClaim, pvcVvol *v1.PersistentVolumeClaim, err error) {
  94. ginkgo.By("creating vmdk")
  95. volumePath = ""
  96. volumePath, err = nodeInfo.VSphere.CreateVolume(&VolumeOptions{}, nodeInfo.DataCenterRef)
  97. if err != nil {
  98. return
  99. }
  100. ginkgo.By("creating the pv with label volume-type:ssd")
  101. pvSsd = getVSpherePersistentVolumeSpec(volumePath, v1.PersistentVolumeReclaimDelete, ssdlabels)
  102. pvSsd, err = c.CoreV1().PersistentVolumes().Create(context.TODO(), pvSsd, metav1.CreateOptions{})
  103. if err != nil {
  104. return
  105. }
  106. ginkgo.By("creating pvc with label selector to match with volume-type:vvol")
  107. pvcVvol = getVSpherePersistentVolumeClaimSpec(ns, vvollabels)
  108. pvcVvol, err = c.CoreV1().PersistentVolumeClaims(ns).Create(context.TODO(), pvcVvol, metav1.CreateOptions{})
  109. if err != nil {
  110. return
  111. }
  112. ginkgo.By("creating pvc with label selector to match with volume-type:ssd")
  113. pvcSsd = getVSpherePersistentVolumeClaimSpec(ns, ssdlabels)
  114. pvcSsd, err = c.CoreV1().PersistentVolumeClaims(ns).Create(context.TODO(), pvcSsd, metav1.CreateOptions{})
  115. return
  116. }
  117. func testCleanupVSpherePVClabelselector(c clientset.Interface, ns string, nodeInfo *NodeInfo, volumePath string, pvSsd *v1.PersistentVolume, pvcSsd *v1.PersistentVolumeClaim, pvcVvol *v1.PersistentVolumeClaim) {
  118. ginkgo.By("running testCleanupVSpherePVClabelselector")
  119. if len(volumePath) > 0 {
  120. nodeInfo.VSphere.DeleteVolume(volumePath, nodeInfo.DataCenterRef)
  121. }
  122. if pvcSsd != nil {
  123. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvcSsd.Name, ns), "Failed to delete PVC ", pvcSsd.Name)
  124. }
  125. if pvcVvol != nil {
  126. framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(c, pvcVvol.Name, ns), "Failed to delete PVC ", pvcVvol.Name)
  127. }
  128. if pvSsd != nil {
  129. framework.ExpectNoError(e2epv.DeletePersistentVolume(c, pvSsd.Name), "Failed to delete PV ", pvSsd.Name)
  130. }
  131. }