vsphere_scale.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. "fmt"
  16. "strconv"
  17. "github.com/onsi/ginkgo"
  18. "github.com/onsi/gomega"
  19. "k8s.io/api/core/v1"
  20. storageV1 "k8s.io/api/storage/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. "k8s.io/kubernetes/test/e2e/storage/utils"
  25. )
  26. /*
  27. Perform vsphere volume life cycle management at scale based on user configurable value for number of volumes.
  28. The following actions will be performed as part of this test.
  29. 1. Create Storage Classes of 4 Categories (Default, SC with Non Default Datastore, SC with SPBM Policy, SC with VSAN Storage Capabilities.)
  30. 2. Read VCP_SCALE_VOLUME_COUNT, VCP_SCALE_INSTANCES, VCP_SCALE_VOLUMES_PER_POD, VSPHERE_SPBM_POLICY_NAME, VSPHERE_DATASTORE from System Environment.
  31. 3. Launch VCP_SCALE_INSTANCES goroutine for creating VCP_SCALE_VOLUME_COUNT volumes. Each goroutine is responsible for create/attach of VCP_SCALE_VOLUME_COUNT/VCP_SCALE_INSTANCES volumes.
  32. 4. Read VCP_SCALE_VOLUMES_PER_POD from System Environment. Each pod will be have VCP_SCALE_VOLUMES_PER_POD attached to it.
  33. 5. Once all the go routines are completed, we delete all the pods and volumes.
  34. */
  35. const (
  36. NodeLabelKey = "vsphere_e2e_label"
  37. )
  38. // NodeSelector holds
  39. type NodeSelector struct {
  40. labelKey string
  41. labelValue string
  42. }
  43. var _ = utils.SIGDescribe("vcp at scale [Feature:vsphere] ", func() {
  44. f := framework.NewDefaultFramework("vcp-at-scale")
  45. var (
  46. client clientset.Interface
  47. namespace string
  48. nodeSelectorList []*NodeSelector
  49. volumeCount int
  50. numberOfInstances int
  51. volumesPerPod int
  52. policyName string
  53. datastoreName string
  54. nodeVolumeMapChan chan map[string][]string
  55. nodes *v1.NodeList
  56. scNames = []string{storageclass1, storageclass2, storageclass3, storageclass4}
  57. )
  58. ginkgo.BeforeEach(func() {
  59. framework.SkipUnlessProviderIs("vsphere")
  60. Bootstrap(f)
  61. client = f.ClientSet
  62. namespace = f.Namespace.Name
  63. nodeVolumeMapChan = make(chan map[string][]string)
  64. // Read the environment variables
  65. volumeCount = GetAndExpectIntEnvVar(VCPScaleVolumeCount)
  66. volumesPerPod = GetAndExpectIntEnvVar(VCPScaleVolumesPerPod)
  67. numberOfInstances = GetAndExpectIntEnvVar(VCPScaleInstances)
  68. gomega.Expect(numberOfInstances > 5).NotTo(gomega.BeTrue(), "Maximum allowed instances are 5")
  69. gomega.Expect(numberOfInstances > volumeCount).NotTo(gomega.BeTrue(), "Number of instances should be less than the total volume count")
  70. policyName = GetAndExpectStringEnvVar(SPBMPolicyName)
  71. datastoreName = GetAndExpectStringEnvVar(StorageClassDatastoreName)
  72. nodes = framework.GetReadySchedulableNodesOrDie(client)
  73. if len(nodes.Items) < 2 {
  74. framework.Skipf("Requires at least %d nodes (not %d)", 2, len(nodes.Items))
  75. }
  76. // Verify volume count specified by the user can be satisfied
  77. if volumeCount > volumesPerNode*len(nodes.Items) {
  78. framework.Skipf("Cannot attach %d volumes to %d nodes. Maximum volumes that can be attached on %d nodes is %d", volumeCount, len(nodes.Items), len(nodes.Items), volumesPerNode*len(nodes.Items))
  79. }
  80. nodeSelectorList = createNodeLabels(client, namespace, nodes)
  81. })
  82. /*
  83. Remove labels from all the nodes
  84. */
  85. framework.AddCleanupAction(func() {
  86. // Cleanup actions will be called even when the tests are skipped and leaves namespace unset.
  87. if len(namespace) > 0 {
  88. for _, node := range nodes.Items {
  89. framework.RemoveLabelOffNode(client, node.Name, NodeLabelKey)
  90. }
  91. }
  92. })
  93. ginkgo.It("vsphere scale tests", func() {
  94. var pvcClaimList []string
  95. nodeVolumeMap := make(map[string][]string)
  96. // Volumes will be provisioned with each different types of Storage Class
  97. scArrays := make([]*storageV1.StorageClass, len(scNames))
  98. for index, scname := range scNames {
  99. // Create vSphere Storage Class
  100. ginkgo.By(fmt.Sprintf("Creating Storage Class : %q", scname))
  101. var sc *storageV1.StorageClass
  102. scParams := make(map[string]string)
  103. var err error
  104. switch scname {
  105. case storageclass1:
  106. scParams = nil
  107. case storageclass2:
  108. scParams[Policy_HostFailuresToTolerate] = "1"
  109. case storageclass3:
  110. scParams[SpbmStoragePolicy] = policyName
  111. case storageclass4:
  112. scParams[Datastore] = datastoreName
  113. }
  114. sc, err = client.StorageV1().StorageClasses().Create(getVSphereStorageClassSpec(scname, scParams, nil))
  115. gomega.Expect(sc).NotTo(gomega.BeNil(), "Storage class is empty")
  116. framework.ExpectNoError(err, "Failed to create storage class")
  117. defer client.StorageV1().StorageClasses().Delete(scname, nil)
  118. scArrays[index] = sc
  119. }
  120. volumeCountPerInstance := volumeCount / numberOfInstances
  121. for instanceCount := 0; instanceCount < numberOfInstances; instanceCount++ {
  122. if instanceCount == numberOfInstances-1 {
  123. volumeCountPerInstance = volumeCount
  124. }
  125. volumeCount = volumeCount - volumeCountPerInstance
  126. go VolumeCreateAndAttach(client, namespace, scArrays, volumeCountPerInstance, volumesPerPod, nodeSelectorList, nodeVolumeMapChan)
  127. }
  128. // Get the list of all volumes attached to each node from the go routines by reading the data from the channel
  129. for instanceCount := 0; instanceCount < numberOfInstances; instanceCount++ {
  130. for node, volumeList := range <-nodeVolumeMapChan {
  131. nodeVolumeMap[node] = append(nodeVolumeMap[node], volumeList...)
  132. }
  133. }
  134. podList, err := client.CoreV1().Pods(namespace).List(metav1.ListOptions{})
  135. for _, pod := range podList.Items {
  136. pvcClaimList = append(pvcClaimList, getClaimsForPod(&pod, volumesPerPod)...)
  137. ginkgo.By("Deleting pod")
  138. err = framework.DeletePodWithWait(f, client, &pod)
  139. framework.ExpectNoError(err)
  140. }
  141. ginkgo.By("Waiting for volumes to be detached from the node")
  142. err = waitForVSphereDisksToDetach(nodeVolumeMap)
  143. framework.ExpectNoError(err)
  144. for _, pvcClaim := range pvcClaimList {
  145. err = framework.DeletePersistentVolumeClaim(client, pvcClaim, namespace)
  146. framework.ExpectNoError(err)
  147. }
  148. })
  149. })
  150. // Get PVC claims for the pod
  151. func getClaimsForPod(pod *v1.Pod, volumesPerPod int) []string {
  152. pvcClaimList := make([]string, volumesPerPod)
  153. for i, volumespec := range pod.Spec.Volumes {
  154. if volumespec.PersistentVolumeClaim != nil {
  155. pvcClaimList[i] = volumespec.PersistentVolumeClaim.ClaimName
  156. }
  157. }
  158. return pvcClaimList
  159. }
  160. // VolumeCreateAndAttach peforms create and attach operations of vSphere persistent volumes at scale
  161. func VolumeCreateAndAttach(client clientset.Interface, namespace string, sc []*storageV1.StorageClass, volumeCountPerInstance int, volumesPerPod int, nodeSelectorList []*NodeSelector, nodeVolumeMapChan chan map[string][]string) {
  162. defer ginkgo.GinkgoRecover()
  163. nodeVolumeMap := make(map[string][]string)
  164. nodeSelectorIndex := 0
  165. for index := 0; index < volumeCountPerInstance; index = index + volumesPerPod {
  166. if (volumeCountPerInstance - index) < volumesPerPod {
  167. volumesPerPod = volumeCountPerInstance - index
  168. }
  169. pvclaims := make([]*v1.PersistentVolumeClaim, volumesPerPod)
  170. for i := 0; i < volumesPerPod; i++ {
  171. ginkgo.By("Creating PVC using the Storage Class")
  172. pvclaim, err := framework.CreatePVC(client, namespace, getVSphereClaimSpecWithStorageClass(namespace, "2Gi", sc[index%len(sc)]))
  173. framework.ExpectNoError(err)
  174. pvclaims[i] = pvclaim
  175. }
  176. ginkgo.By("Waiting for claim to be in bound phase")
  177. persistentvolumes, err := framework.WaitForPVClaimBoundPhase(client, pvclaims, framework.ClaimProvisionTimeout)
  178. framework.ExpectNoError(err)
  179. ginkgo.By("Creating pod to attach PV to the node")
  180. nodeSelector := nodeSelectorList[nodeSelectorIndex%len(nodeSelectorList)]
  181. // Create pod to attach Volume to Node
  182. pod, err := framework.CreatePod(client, namespace, map[string]string{nodeSelector.labelKey: nodeSelector.labelValue}, pvclaims, false, "")
  183. framework.ExpectNoError(err)
  184. for _, pv := range persistentvolumes {
  185. nodeVolumeMap[pod.Spec.NodeName] = append(nodeVolumeMap[pod.Spec.NodeName], pv.Spec.VsphereVolume.VolumePath)
  186. }
  187. ginkgo.By("Verify the volume is accessible and available in the pod")
  188. verifyVSphereVolumesAccessible(client, pod, persistentvolumes)
  189. nodeSelectorIndex++
  190. }
  191. nodeVolumeMapChan <- nodeVolumeMap
  192. close(nodeVolumeMapChan)
  193. }
  194. func createNodeLabels(client clientset.Interface, namespace string, nodes *v1.NodeList) []*NodeSelector {
  195. var nodeSelectorList []*NodeSelector
  196. for i, node := range nodes.Items {
  197. labelVal := "vsphere_e2e_" + strconv.Itoa(i)
  198. nodeSelector := &NodeSelector{
  199. labelKey: NodeLabelKey,
  200. labelValue: labelVal,
  201. }
  202. nodeSelectorList = append(nodeSelectorList, nodeSelector)
  203. framework.AddOrUpdateLabelOnNode(client, node.Name, NodeLabelKey, labelVal)
  204. }
  205. return nodeSelectorList
  206. }