csi_volumes.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2018 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. v1 "k8s.io/api/core/v1"
  16. storagev1 "k8s.io/api/storage/v1"
  17. clientset "k8s.io/client-go/kubernetes"
  18. "k8s.io/kubernetes/test/e2e/framework"
  19. "k8s.io/kubernetes/test/e2e/storage/drivers"
  20. "k8s.io/kubernetes/test/e2e/storage/testpatterns"
  21. "k8s.io/kubernetes/test/e2e/storage/testsuites"
  22. "k8s.io/kubernetes/test/e2e/storage/utils"
  23. "github.com/onsi/ginkgo"
  24. "github.com/onsi/gomega"
  25. "k8s.io/apimachinery/pkg/util/rand"
  26. )
  27. // List of testDrivers to be executed in below loop
  28. var csiTestDrivers = []func() testsuites.TestDriver{
  29. drivers.InitHostPathCSIDriver,
  30. drivers.InitGcePDCSIDriver,
  31. drivers.InitHostPathV0CSIDriver,
  32. // Don't run tests with mock driver (drivers.InitMockCSIDriver), it does not provide persistent storage.
  33. }
  34. // List of testSuites to be executed in below loop
  35. var csiTestSuites = []func() testsuites.TestSuite{
  36. testsuites.InitVolumesTestSuite,
  37. testsuites.InitVolumeIOTestSuite,
  38. testsuites.InitVolumeModeTestSuite,
  39. testsuites.InitSubPathTestSuite,
  40. testsuites.InitProvisioningTestSuite,
  41. testsuites.InitSnapshottableTestSuite,
  42. testsuites.InitMultiVolumeTestSuite,
  43. }
  44. // This executes testSuites for csi volumes.
  45. var _ = utils.SIGDescribe("CSI Volumes", func() {
  46. for _, initDriver := range csiTestDrivers {
  47. curDriver := initDriver()
  48. ginkgo.Context(testsuites.GetDriverNameWithFeatureTags(curDriver), func() {
  49. testsuites.DefineTestSuite(curDriver, csiTestSuites)
  50. })
  51. }
  52. // TODO: PD CSI driver needs to be serial because it uses a fixed name. Address as part of #71289
  53. ginkgo.Context("CSI Topology test using GCE PD driver [Serial]", func() {
  54. f := framework.NewDefaultFramework("csitopology")
  55. driver := drivers.InitGcePDCSIDriver().(testsuites.DynamicPVTestDriver) // TODO (#71289) eliminate by moving this test to common test suite.
  56. var (
  57. config *testsuites.PerTestConfig
  58. testCleanup func()
  59. )
  60. ginkgo.BeforeEach(func() {
  61. driver.SkipUnsupportedTest(testpatterns.TestPattern{})
  62. config, testCleanup = driver.PrepareTest(f)
  63. })
  64. ginkgo.AfterEach(func() {
  65. if testCleanup != nil {
  66. testCleanup()
  67. }
  68. })
  69. ginkgo.It("should provision zonal PD with immediate volume binding and AllowedTopologies set and mount the volume to a pod", func() {
  70. suffix := "topology-positive"
  71. testTopologyPositive(config.Framework.ClientSet, suffix, config.Framework.Namespace.GetName(), false /* delayBinding */, true /* allowedTopologies */)
  72. })
  73. ginkgo.It("should provision zonal PD with delayed volume binding and mount the volume to a pod", func() {
  74. suffix := "delayed"
  75. testTopologyPositive(config.Framework.ClientSet, suffix, config.Framework.Namespace.GetName(), true /* delayBinding */, false /* allowedTopologies */)
  76. })
  77. ginkgo.It("should provision zonal PD with delayed volume binding and AllowedTopologies set and mount the volume to a pod", func() {
  78. suffix := "delayed-topology-positive"
  79. testTopologyPositive(config.Framework.ClientSet, suffix, config.Framework.Namespace.GetName(), true /* delayBinding */, true /* allowedTopologies */)
  80. })
  81. ginkgo.It("should fail to schedule a pod with a zone missing from AllowedTopologies; PD is provisioned with immediate volume binding", func() {
  82. framework.SkipUnlessMultizone(config.Framework.ClientSet)
  83. suffix := "topology-negative"
  84. testTopologyNegative(config.Framework.ClientSet, suffix, config.Framework.Namespace.GetName(), false /* delayBinding */)
  85. })
  86. ginkgo.It("should fail to schedule a pod with a zone missing from AllowedTopologies; PD is provisioned with delayed volume binding", func() {
  87. framework.SkipUnlessMultizone(config.Framework.ClientSet)
  88. suffix := "delayed-topology-negative"
  89. testTopologyNegative(config.Framework.ClientSet, suffix, config.Framework.Namespace.GetName(), true /* delayBinding */)
  90. })
  91. })
  92. })
  93. func testTopologyPositive(cs clientset.Interface, suffix, namespace string, delayBinding, allowedTopologies bool) {
  94. test := createGCEPDStorageClassTest()
  95. test.DelayBinding = delayBinding
  96. class := newStorageClass(test, namespace, suffix)
  97. if allowedTopologies {
  98. topoZone := getRandomClusterZone(cs)
  99. addSingleCSIZoneAllowedTopologyToStorageClass(cs, class, topoZone)
  100. }
  101. test.Client = cs
  102. test.Claim = newClaim(test, namespace, suffix)
  103. test.Claim.Spec.StorageClassName = &class.Name
  104. test.Class = class
  105. if delayBinding {
  106. _, node := test.TestBindingWaitForFirstConsumer(nil /* node selector */, false /* expect unschedulable */)
  107. gomega.Expect(node).ToNot(gomega.BeNil(), "Unexpected nil node found")
  108. } else {
  109. test.TestDynamicProvisioning()
  110. }
  111. }
  112. func testTopologyNegative(cs clientset.Interface, suffix, namespace string, delayBinding bool) {
  113. framework.SkipUnlessMultizone(cs)
  114. // Use different zones for pod and PV
  115. zones, err := framework.GetClusterZones(cs)
  116. framework.ExpectNoError(err)
  117. gomega.Expect(zones.Len()).To(gomega.BeNumerically(">=", 2))
  118. zonesList := zones.UnsortedList()
  119. podZoneIndex := rand.Intn(zones.Len())
  120. podZone := zonesList[podZoneIndex]
  121. pvZone := zonesList[(podZoneIndex+1)%zones.Len()]
  122. test := createGCEPDStorageClassTest()
  123. test.DelayBinding = delayBinding
  124. nodeSelector := map[string]string{v1.LabelZoneFailureDomain: podZone}
  125. test.Client = cs
  126. test.Class = newStorageClass(test, namespace, suffix)
  127. addSingleCSIZoneAllowedTopologyToStorageClass(cs, test.Class, pvZone)
  128. test.Claim = newClaim(test, namespace, suffix)
  129. test.Claim.Spec.StorageClassName = &test.Class.Name
  130. if delayBinding {
  131. test.TestBindingWaitForFirstConsumer(nodeSelector, true /* expect unschedulable */)
  132. } else {
  133. test.PvCheck = func(claim *v1.PersistentVolumeClaim) {
  134. // Ensure that a pod cannot be scheduled in an unsuitable zone.
  135. pod := testsuites.StartInPodWithVolume(cs, namespace, claim.Name, "pvc-tester-unschedulable", "sleep 100000",
  136. framework.NodeSelection{Selector: nodeSelector})
  137. defer testsuites.StopPod(cs, pod)
  138. framework.ExpectNoError(framework.WaitForPodNameUnschedulableInNamespace(cs, pod.Name, pod.Namespace), "pod should be unschedulable")
  139. }
  140. test.TestDynamicProvisioning()
  141. }
  142. }
  143. func addSingleCSIZoneAllowedTopologyToStorageClass(c clientset.Interface, sc *storagev1.StorageClass, zone string) {
  144. term := v1.TopologySelectorTerm{
  145. MatchLabelExpressions: []v1.TopologySelectorLabelRequirement{
  146. {
  147. Key: drivers.GCEPDCSIZoneTopologyKey,
  148. Values: []string{zone},
  149. },
  150. },
  151. }
  152. sc.AllowedTopologies = append(sc.AllowedTopologies, term)
  153. }
  154. func createGCEPDStorageClassTest() testsuites.StorageClassTest {
  155. return testsuites.StorageClassTest{
  156. Name: drivers.GCEPDCSIProvisionerName,
  157. Provisioner: drivers.GCEPDCSIProvisionerName,
  158. Parameters: map[string]string{"type": "pd-standard"},
  159. ClaimSize: "5Gi",
  160. ExpectedSize: "5Gi",
  161. }
  162. }