vsphere_zone_support.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. Copyright 2019 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. "fmt"
  16. "strings"
  17. "time"
  18. "github.com/onsi/ginkgo"
  19. "github.com/onsi/gomega"
  20. v1 "k8s.io/api/core/v1"
  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. /*
  28. Test to verify multi-zone support for dynamic volume provisioning in kubernetes.
  29. The test environment is illustrated below:
  30. datacenter
  31. --->cluster-vsan-1 (zone-a) ____________________ _________________
  32. --->host-1 : master | | | |
  33. --->host-2 : node1 | vsanDatastore | | |
  34. --->host-3 : node2 |____________________| | |
  35. | |
  36. | sharedVmfs-0 |
  37. --->cluster-vsan-2 (zone-b) ____________________ | |
  38. --->host-4 : node3 | | | |
  39. --->host-5 : node4 | vsanDatastore (1) | | |
  40. --->host-6 |____________________| |_________________|
  41. --->cluster-3 (zone-c) ________________
  42. --->host-7 : node5 | |
  43. | localDatastore |
  44. |________________|
  45. ____________________
  46. --->host-8 (zone-c) : node6 | |
  47. | localDatastore (1) |
  48. |____________________|
  49. Testbed description :
  50. 1. cluster-vsan-1 is tagged with zone-a. So, vsanDatastore inherits zone-a since all the hosts under zone-a have vsanDatastore mounted on them.
  51. 2. cluster-vsan-2 is tagged with zone-b. So, vsanDatastore (1) inherits zone-b since all the hosts under zone-b have vsanDatastore (1) mounted on them.
  52. 3. sharedVmfs-0 inherits both zone-a and zone-b since all the hosts in both zone-a and zone-b have this datastore mounted on them.
  53. 4. cluster-3 is tagged with zone-c. cluster-3 only contains host-7.
  54. 5. host-8 is not under any cluster and is tagged with zone-c.
  55. 6. Since there are no shared datastores between host-7 under cluster-3 and host-8, no datastores in the environment inherit zone-c.
  56. 7. The six worker nodes are distributed among the hosts as shown in the above illustration.
  57. 8. Two storage policies are created on VC. One is a VSAN storage policy named as compatpolicy with hostFailuresToTolerate capability set to 1.
  58. Second is a VSAN storage policy named as noncompatpolicy with hostFailuresToTolerate capability set to 4.
  59. Testsuite description :
  60. 1. Tests to verify that zone labels are set correctly on a dynamically created PV.
  61. 2. Tests to verify dynamic pv creation fails if availability zones are not specified or if there are no shared datastores under the specified zones.
  62. 3. Tests to verify dynamic pv creation using availability zones works in combination with other storage class parameters such as storage policy,
  63. datastore and VSAN capabilities.
  64. 4. Tests to verify dynamic pv creation using availability zones fails in combination with other storage class parameters such as storage policy,
  65. datastore and VSAN capabilities specifications when any of the former mentioned parameters are incompatible with the rest.
  66. */
  67. var _ = utils.SIGDescribe("Zone Support", func() {
  68. f := framework.NewDefaultFramework("zone-support")
  69. var (
  70. client clientset.Interface
  71. namespace string
  72. scParameters map[string]string
  73. zones []string
  74. vsanDatastore1 string
  75. vsanDatastore2 string
  76. compatPolicy string
  77. nonCompatPolicy string
  78. zoneA string
  79. zoneB string
  80. zoneC string
  81. zoneD string
  82. )
  83. ginkgo.BeforeEach(func() {
  84. framework.SkipUnlessProviderIs("vsphere")
  85. Bootstrap(f)
  86. client = f.ClientSet
  87. namespace = f.Namespace.Name
  88. vsanDatastore1 = GetAndExpectStringEnvVar(VCPZoneVsanDatastore1)
  89. vsanDatastore2 = GetAndExpectStringEnvVar(VCPZoneVsanDatastore2)
  90. compatPolicy = GetAndExpectStringEnvVar(VCPZoneCompatPolicyName)
  91. nonCompatPolicy = GetAndExpectStringEnvVar(VCPZoneNonCompatPolicyName)
  92. zoneA = GetAndExpectStringEnvVar(VCPZoneA)
  93. zoneB = GetAndExpectStringEnvVar(VCPZoneB)
  94. zoneC = GetAndExpectStringEnvVar(VCPZoneC)
  95. zoneD = GetAndExpectStringEnvVar(VCPZoneD)
  96. scParameters = make(map[string]string)
  97. zones = make([]string, 0)
  98. nodeList := framework.GetReadySchedulableNodesOrDie(f.ClientSet)
  99. if !(len(nodeList.Items) > 0) {
  100. framework.Failf("Unable to find ready and schedulable Node")
  101. }
  102. })
  103. ginkgo.It("Verify dynamically created pv with allowed zones specified in storage class, shows the right zone information on its labels", func() {
  104. ginkgo.By(fmt.Sprintf("Creating storage class with the following zones : %s", zoneA))
  105. zones = append(zones, zoneA)
  106. verifyPVZoneLabels(client, namespace, nil, zones)
  107. })
  108. ginkgo.It("Verify dynamically created pv with multiple zones specified in the storage class, shows both the zones on its labels", func() {
  109. ginkgo.By(fmt.Sprintf("Creating storage class with the following zones : %s, %s", zoneA, zoneB))
  110. zones = append(zones, zoneA)
  111. zones = append(zones, zoneB)
  112. verifyPVZoneLabels(client, namespace, nil, zones)
  113. })
  114. ginkgo.It("Verify PVC creation with invalid zone specified in storage class fails", func() {
  115. ginkgo.By(fmt.Sprintf("Creating storage class with unknown zone : %s", zoneD))
  116. zones = append(zones, zoneD)
  117. err := verifyPVCCreationFails(client, namespace, nil, zones)
  118. framework.ExpectError(err)
  119. errorMsg := "Failed to find a shared datastore matching zone [" + zoneD + "]"
  120. if !strings.Contains(err.Error(), errorMsg) {
  121. framework.ExpectNoError(err, errorMsg)
  122. }
  123. })
  124. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on allowed zones specified in storage class ", func() {
  125. ginkgo.By(fmt.Sprintf("Creating storage class with zones :%s", zoneA))
  126. zones = append(zones, zoneA)
  127. verifyPVCAndPodCreationSucceeds(client, namespace, nil, zones)
  128. })
  129. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on multiple zones specified in storage class ", func() {
  130. ginkgo.By(fmt.Sprintf("Creating storage class with zones :%s, %s", zoneA, zoneB))
  131. zones = append(zones, zoneA)
  132. zones = append(zones, zoneB)
  133. verifyPVCAndPodCreationSucceeds(client, namespace, nil, zones)
  134. })
  135. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on the allowed zones and datastore specified in storage class", func() {
  136. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s and datastore :%s", zoneA, vsanDatastore1))
  137. scParameters[Datastore] = vsanDatastore1
  138. zones = append(zones, zoneA)
  139. verifyPVCAndPodCreationSucceeds(client, namespace, scParameters, zones)
  140. })
  141. ginkgo.It("Verify PVC creation with incompatible datastore and zone combination specified in storage class fails", func() {
  142. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s and datastore :%s", zoneC, vsanDatastore1))
  143. scParameters[Datastore] = vsanDatastore1
  144. zones = append(zones, zoneC)
  145. err := verifyPVCCreationFails(client, namespace, scParameters, zones)
  146. errorMsg := "The specified datastore " + scParameters[Datastore] + " does not match the provided zones : [" + zoneC + "]"
  147. if !strings.Contains(err.Error(), errorMsg) {
  148. framework.ExpectNoError(err, errorMsg)
  149. }
  150. })
  151. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on the allowed zones and storage policy specified in storage class", func() {
  152. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s and storage policy :%s", zoneA, compatPolicy))
  153. scParameters[SpbmStoragePolicy] = compatPolicy
  154. zones = append(zones, zoneA)
  155. verifyPVCAndPodCreationSucceeds(client, namespace, scParameters, zones)
  156. })
  157. ginkgo.It("Verify a pod is created on a non-Workspace zone and attached to a dynamically created PV, based on the allowed zones and storage policy specified in storage class", func() {
  158. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s and storage policy :%s", zoneB, compatPolicy))
  159. scParameters[SpbmStoragePolicy] = compatPolicy
  160. zones = append(zones, zoneB)
  161. verifyPVCAndPodCreationSucceeds(client, namespace, scParameters, zones)
  162. })
  163. ginkgo.It("Verify PVC creation with incompatible storagePolicy and zone combination specified in storage class fails", func() {
  164. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s and storage policy :%s", zoneA, nonCompatPolicy))
  165. scParameters[SpbmStoragePolicy] = nonCompatPolicy
  166. zones = append(zones, zoneA)
  167. err := verifyPVCCreationFails(client, namespace, scParameters, zones)
  168. errorMsg := "No compatible datastores found that satisfy the storage policy requirements"
  169. if !strings.Contains(err.Error(), errorMsg) {
  170. framework.ExpectNoError(err, errorMsg)
  171. }
  172. })
  173. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on the allowed zones, datastore and storage policy specified in storage class", func() {
  174. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s datastore :%s and storagePolicy :%s", zoneA, vsanDatastore1, compatPolicy))
  175. scParameters[SpbmStoragePolicy] = compatPolicy
  176. scParameters[Datastore] = vsanDatastore1
  177. zones = append(zones, zoneA)
  178. verifyPVCAndPodCreationSucceeds(client, namespace, scParameters, zones)
  179. })
  180. ginkgo.It("Verify PVC creation with incompatible storage policy along with compatible zone and datastore combination specified in storage class fails", func() {
  181. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s datastore :%s and storagePolicy :%s", zoneA, vsanDatastore1, nonCompatPolicy))
  182. scParameters[SpbmStoragePolicy] = nonCompatPolicy
  183. scParameters[Datastore] = vsanDatastore1
  184. zones = append(zones, zoneA)
  185. err := verifyPVCCreationFails(client, namespace, scParameters, zones)
  186. errorMsg := "User specified datastore is not compatible with the storagePolicy: \\\"" + nonCompatPolicy + "\\\"."
  187. if !strings.Contains(err.Error(), errorMsg) {
  188. framework.ExpectNoError(err, errorMsg)
  189. }
  190. })
  191. ginkgo.It("Verify PVC creation with incompatible zone along with compatible storagePolicy and datastore combination specified in storage class fails", func() {
  192. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s datastore :%s and storagePolicy :%s", zoneC, vsanDatastore2, compatPolicy))
  193. scParameters[SpbmStoragePolicy] = compatPolicy
  194. scParameters[Datastore] = vsanDatastore2
  195. zones = append(zones, zoneC)
  196. err := verifyPVCCreationFails(client, namespace, scParameters, zones)
  197. errorMsg := "The specified datastore " + scParameters[Datastore] + " does not match the provided zones : [" + zoneC + "]"
  198. if !strings.Contains(err.Error(), errorMsg) {
  199. framework.ExpectNoError(err, errorMsg)
  200. }
  201. })
  202. ginkgo.It("Verify PVC creation fails if no zones are specified in the storage class (No shared datastores exist among all the nodes)", func() {
  203. ginkgo.By(fmt.Sprintf("Creating storage class with no zones"))
  204. err := verifyPVCCreationFails(client, namespace, nil, nil)
  205. errorMsg := "No shared datastores found in the Kubernetes cluster"
  206. if !strings.Contains(err.Error(), errorMsg) {
  207. framework.ExpectNoError(err, errorMsg)
  208. }
  209. })
  210. ginkgo.It("Verify PVC creation fails if only datastore is specified in the storage class (No shared datastores exist among all the nodes)", func() {
  211. ginkgo.By(fmt.Sprintf("Creating storage class with datastore :%s", vsanDatastore1))
  212. scParameters[Datastore] = vsanDatastore1
  213. err := verifyPVCCreationFails(client, namespace, scParameters, nil)
  214. errorMsg := "No shared datastores found in the Kubernetes cluster"
  215. if !strings.Contains(err.Error(), errorMsg) {
  216. framework.ExpectNoError(err, errorMsg)
  217. }
  218. })
  219. ginkgo.It("Verify PVC creation fails if only storage policy is specified in the storage class (No shared datastores exist among all the nodes)", func() {
  220. ginkgo.By(fmt.Sprintf("Creating storage class with storage policy :%s", compatPolicy))
  221. scParameters[SpbmStoragePolicy] = compatPolicy
  222. err := verifyPVCCreationFails(client, namespace, scParameters, nil)
  223. errorMsg := "No shared datastores found in the Kubernetes cluster"
  224. if !strings.Contains(err.Error(), errorMsg) {
  225. framework.ExpectNoError(err, errorMsg)
  226. }
  227. })
  228. ginkgo.It("Verify PVC creation with compatible policy and datastore without any zones specified in the storage class fails (No shared datastores exist among all the nodes)", func() {
  229. ginkgo.By(fmt.Sprintf("Creating storage class with storage policy :%s and datastore :%s", compatPolicy, vsanDatastore1))
  230. scParameters[SpbmStoragePolicy] = compatPolicy
  231. scParameters[Datastore] = vsanDatastore1
  232. err := verifyPVCCreationFails(client, namespace, scParameters, nil)
  233. errorMsg := "No shared datastores found in the Kubernetes cluster"
  234. if !strings.Contains(err.Error(), errorMsg) {
  235. framework.ExpectNoError(err, errorMsg)
  236. }
  237. })
  238. ginkgo.It("Verify PVC creation fails if the availability zone specified in the storage class have no shared datastores under it.", func() {
  239. ginkgo.By(fmt.Sprintf("Creating storage class with zone :%s", zoneC))
  240. zones = append(zones, zoneC)
  241. err := verifyPVCCreationFails(client, namespace, nil, zones)
  242. errorMsg := "Failed to find a shared datastore matching zone [" + zoneC + "]"
  243. if !strings.Contains(err.Error(), errorMsg) {
  244. framework.ExpectNoError(err, errorMsg)
  245. }
  246. })
  247. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on multiple zones specified in the storage class. (No shared datastores exist among both zones)", func() {
  248. ginkgo.By(fmt.Sprintf("Creating storage class with the following zones :%s and %s", zoneA, zoneC))
  249. zones = append(zones, zoneA)
  250. zones = append(zones, zoneC)
  251. err := verifyPVCCreationFails(client, namespace, nil, zones)
  252. errorMsg := "Failed to find a shared datastore matching zone [" + zoneA + " " + zoneC + "]"
  253. if !strings.Contains(err.Error(), errorMsg) {
  254. framework.ExpectNoError(err, errorMsg)
  255. }
  256. })
  257. ginkgo.It("Verify PVC creation with an invalid VSAN capability along with a compatible zone combination specified in storage class fails", func() {
  258. ginkgo.By(fmt.Sprintf("Creating storage class with %s :%s and zone :%s", Policy_HostFailuresToTolerate, HostFailuresToTolerateCapabilityInvalidVal, zoneA))
  259. scParameters[Policy_HostFailuresToTolerate] = HostFailuresToTolerateCapabilityInvalidVal
  260. zones = append(zones, zoneA)
  261. err := verifyPVCCreationFails(client, namespace, scParameters, zones)
  262. errorMsg := "Invalid value for " + Policy_HostFailuresToTolerate + "."
  263. if !strings.Contains(err.Error(), errorMsg) {
  264. framework.ExpectNoError(err, errorMsg)
  265. }
  266. })
  267. ginkgo.It("Verify a pod is created and attached to a dynamically created PV, based on a VSAN capability, datastore and compatible zone specified in storage class", func() {
  268. ginkgo.By(fmt.Sprintf("Creating storage class with %s :%s, %s :%s, datastore :%s and zone :%s", Policy_ObjectSpaceReservation, ObjectSpaceReservationCapabilityVal, Policy_IopsLimit, IopsLimitCapabilityVal, vsanDatastore1, zoneA))
  269. scParameters[Policy_ObjectSpaceReservation] = ObjectSpaceReservationCapabilityVal
  270. scParameters[Policy_IopsLimit] = IopsLimitCapabilityVal
  271. scParameters[Datastore] = vsanDatastore1
  272. zones = append(zones, zoneA)
  273. verifyPVCAndPodCreationSucceeds(client, namespace, scParameters, zones)
  274. })
  275. })
  276. func verifyPVCAndPodCreationSucceeds(client clientset.Interface, namespace string, scParameters map[string]string, zones []string) {
  277. storageclass, err := client.StorageV1().StorageClasses().Create(getVSphereStorageClassSpec("zone-sc", scParameters, zones))
  278. framework.ExpectNoError(err, fmt.Sprintf("Failed to create storage class with err: %v", err))
  279. defer client.StorageV1().StorageClasses().Delete(storageclass.Name, nil)
  280. ginkgo.By("Creating PVC using the Storage Class")
  281. pvclaim, err := framework.CreatePVC(client, namespace, getVSphereClaimSpecWithStorageClass(namespace, "2Gi", storageclass))
  282. framework.ExpectNoError(err)
  283. defer framework.DeletePersistentVolumeClaim(client, pvclaim.Name, namespace)
  284. var pvclaims []*v1.PersistentVolumeClaim
  285. pvclaims = append(pvclaims, pvclaim)
  286. ginkgo.By("Waiting for claim to be in bound phase")
  287. persistentvolumes, err := framework.WaitForPVClaimBoundPhase(client, pvclaims, framework.ClaimProvisionTimeout)
  288. framework.ExpectNoError(err)
  289. ginkgo.By("Creating pod to attach PV to the node")
  290. pod, err := framework.CreatePod(client, namespace, nil, pvclaims, false, "")
  291. framework.ExpectNoError(err)
  292. ginkgo.By("Verify persistent volume was created on the right zone")
  293. verifyVolumeCreationOnRightZone(persistentvolumes, pod.Spec.NodeName, zones)
  294. ginkgo.By("Verify the volume is accessible and available in the pod")
  295. verifyVSphereVolumesAccessible(client, pod, persistentvolumes)
  296. ginkgo.By("Deleting pod")
  297. framework.DeletePodWithWait(f, client, pod)
  298. ginkgo.By("Waiting for volumes to be detached from the node")
  299. waitForVSphereDiskToDetach(persistentvolumes[0].Spec.VsphereVolume.VolumePath, pod.Spec.NodeName)
  300. }
  301. func verifyPVCCreationFails(client clientset.Interface, namespace string, scParameters map[string]string, zones []string) error {
  302. storageclass, err := client.StorageV1().StorageClasses().Create(getVSphereStorageClassSpec("zone-sc", scParameters, zones))
  303. framework.ExpectNoError(err, fmt.Sprintf("Failed to create storage class with err: %v", err))
  304. defer client.StorageV1().StorageClasses().Delete(storageclass.Name, nil)
  305. ginkgo.By("Creating PVC using the Storage Class")
  306. pvclaim, err := framework.CreatePVC(client, namespace, getVSphereClaimSpecWithStorageClass(namespace, "2Gi", storageclass))
  307. framework.ExpectNoError(err)
  308. defer framework.DeletePersistentVolumeClaim(client, pvclaim.Name, namespace)
  309. var pvclaims []*v1.PersistentVolumeClaim
  310. pvclaims = append(pvclaims, pvclaim)
  311. ginkgo.By("Waiting for claim to be in bound phase")
  312. err = framework.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, pvclaim.Namespace, pvclaim.Name, framework.Poll, 2*time.Minute)
  313. framework.ExpectError(err)
  314. eventList, err := client.CoreV1().Events(pvclaim.Namespace).List(metav1.ListOptions{})
  315. e2elog.Logf("Failure message : %+q", eventList.Items[0].Message)
  316. return fmt.Errorf("Failure message: %+q", eventList.Items[0].Message)
  317. }
  318. func verifyPVZoneLabels(client clientset.Interface, namespace string, scParameters map[string]string, zones []string) {
  319. storageclass, err := client.StorageV1().StorageClasses().Create(getVSphereStorageClassSpec("zone-sc", nil, zones))
  320. framework.ExpectNoError(err, fmt.Sprintf("Failed to create storage class with err: %v", err))
  321. defer client.StorageV1().StorageClasses().Delete(storageclass.Name, nil)
  322. ginkgo.By("Creating PVC using the storage class")
  323. pvclaim, err := framework.CreatePVC(client, namespace, getVSphereClaimSpecWithStorageClass(namespace, "2Gi", storageclass))
  324. framework.ExpectNoError(err)
  325. defer framework.DeletePersistentVolumeClaim(client, pvclaim.Name, namespace)
  326. var pvclaims []*v1.PersistentVolumeClaim
  327. pvclaims = append(pvclaims, pvclaim)
  328. ginkgo.By("Waiting for claim to be in bound phase")
  329. persistentvolumes, err := framework.WaitForPVClaimBoundPhase(client, pvclaims, framework.ClaimProvisionTimeout)
  330. framework.ExpectNoError(err)
  331. ginkgo.By("Verify zone information is present in the volume labels")
  332. for _, pv := range persistentvolumes {
  333. // Multiple zones are separated with "__"
  334. pvZoneLabels := strings.Split(pv.ObjectMeta.Labels["failure-domain.beta.kubernetes.io/zone"], "__")
  335. for _, zone := range zones {
  336. gomega.Expect(pvZoneLabels).Should(gomega.ContainElement(zone), "Incorrect or missing zone labels in pv.")
  337. }
  338. }
  339. }