ubernetes_lite_volumes.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 scheduling
  14. import (
  15. "context"
  16. "fmt"
  17. "strconv"
  18. "github.com/onsi/ginkgo"
  19. compute "google.golang.org/api/compute/v1"
  20. v1 "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/util/sets"
  24. "k8s.io/apimachinery/pkg/util/uuid"
  25. "k8s.io/kubernetes/test/e2e/framework"
  26. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  27. "k8s.io/kubernetes/test/e2e/framework/providers/gce"
  28. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  29. e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
  30. )
  31. var _ = SIGDescribe("Multi-AZ Cluster Volumes [sig-storage]", func() {
  32. f := framework.NewDefaultFramework("multi-az")
  33. var zoneCount int
  34. var err error
  35. image := framework.ServeHostnameImage
  36. ginkgo.BeforeEach(func() {
  37. e2eskipper.SkipUnlessProviderIs("gce", "gke")
  38. if zoneCount <= 0 {
  39. zoneCount, err = getZoneCount(f.ClientSet)
  40. framework.ExpectNoError(err)
  41. }
  42. ginkgo.By(fmt.Sprintf("Checking for multi-zone cluster. Zone count = %d", zoneCount))
  43. msg := fmt.Sprintf("Zone count is %d, only run for multi-zone clusters, skipping test", zoneCount)
  44. e2eskipper.SkipUnlessAtLeast(zoneCount, 2, msg)
  45. // TODO: SkipUnlessDefaultScheduler() // Non-default schedulers might not spread
  46. })
  47. ginkgo.It("should schedule pods in the same zones as statically provisioned PVs", func() {
  48. PodsUseStaticPVsOrFail(f, (2*zoneCount)+1, image)
  49. })
  50. ginkgo.It("should only be allowed to provision PDs in zones where nodes exist", func() {
  51. OnlyAllowNodeZones(f, zoneCount, image)
  52. })
  53. })
  54. // OnlyAllowNodeZones tests that GetAllCurrentZones returns only zones with Nodes
  55. func OnlyAllowNodeZones(f *framework.Framework, zoneCount int, image string) {
  56. gceCloud, err := gce.GetGCECloud()
  57. framework.ExpectNoError(err)
  58. // Get all the zones that the nodes are in
  59. expectedZones, err := gceCloud.GetAllZonesFromCloudProvider()
  60. framework.ExpectNoError(err)
  61. framework.Logf("Expected zones: %v", expectedZones)
  62. // Get all the zones in this current region
  63. region := gceCloud.Region()
  64. allZonesInRegion, err := gceCloud.ListZonesInRegion(region)
  65. framework.ExpectNoError(err)
  66. var extraZone string
  67. for _, zone := range allZonesInRegion {
  68. if !expectedZones.Has(zone.Name) {
  69. extraZone = zone.Name
  70. break
  71. }
  72. }
  73. framework.ExpectNotEqual(extraZone, "", fmt.Sprintf("No extra zones available in region %s", region))
  74. ginkgo.By(fmt.Sprintf("starting a compute instance in unused zone: %v\n", extraZone))
  75. project := framework.TestContext.CloudConfig.ProjectID
  76. zone := extraZone
  77. myuuid := string(uuid.NewUUID())
  78. name := "compute-" + myuuid
  79. imageURL := "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140606"
  80. rb := &compute.Instance{
  81. MachineType: "zones/" + zone + "/machineTypes/f1-micro",
  82. Disks: []*compute.AttachedDisk{
  83. {
  84. AutoDelete: true,
  85. Boot: true,
  86. Type: "PERSISTENT",
  87. InitializeParams: &compute.AttachedDiskInitializeParams{
  88. DiskName: "my-root-pd-" + myuuid,
  89. SourceImage: imageURL,
  90. },
  91. },
  92. },
  93. NetworkInterfaces: []*compute.NetworkInterface{
  94. {
  95. AccessConfigs: []*compute.AccessConfig{
  96. {
  97. Type: "ONE_TO_ONE_NAT",
  98. Name: "External NAT",
  99. },
  100. },
  101. Network: "/global/networks/default",
  102. },
  103. },
  104. Name: name,
  105. }
  106. err = gceCloud.InsertInstance(project, zone, rb)
  107. framework.ExpectNoError(err)
  108. defer func() {
  109. // Teardown of the compute instance
  110. framework.Logf("Deleting compute resource: %v", name)
  111. err := gceCloud.DeleteInstance(project, zone, name)
  112. framework.ExpectNoError(err)
  113. }()
  114. ginkgo.By("Creating zoneCount+1 PVCs and making sure PDs are only provisioned in zones with nodes")
  115. // Create some (zoneCount+1) PVCs with names of form "pvc-x" where x is 1...zoneCount+1
  116. // This will exploit ChooseZoneForVolume in pkg/volume/util.go to provision them in all the zones it "sees"
  117. var pvcList []*v1.PersistentVolumeClaim
  118. c := f.ClientSet
  119. ns := f.Namespace.Name
  120. for index := 1; index <= zoneCount+1; index++ {
  121. pvc := newNamedDefaultClaim(ns, index)
  122. pvc, err = e2epv.CreatePVC(c, ns, pvc)
  123. framework.ExpectNoError(err)
  124. pvcList = append(pvcList, pvc)
  125. // Defer the cleanup
  126. defer func() {
  127. framework.Logf("deleting claim %q/%q", pvc.Namespace, pvc.Name)
  128. err = c.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(context.TODO(), pvc.Name, nil)
  129. if err != nil {
  130. framework.Failf("Error deleting claim %q. Error: %v", pvc.Name, err)
  131. }
  132. }()
  133. }
  134. // Wait for all claims bound
  135. for _, claim := range pvcList {
  136. err = e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, c, claim.Namespace, claim.Name, framework.Poll, framework.ClaimProvisionTimeout)
  137. framework.ExpectNoError(err)
  138. }
  139. pvZones := sets.NewString()
  140. ginkgo.By("Checking that PDs have been provisioned in only the expected zones")
  141. for _, claim := range pvcList {
  142. // Get a new copy of the claim to have all fields populated
  143. claim, err = c.CoreV1().PersistentVolumeClaims(claim.Namespace).Get(context.TODO(), claim.Name, metav1.GetOptions{})
  144. framework.ExpectNoError(err)
  145. // Get the related PV
  146. pv, err := c.CoreV1().PersistentVolumes().Get(context.TODO(), claim.Spec.VolumeName, metav1.GetOptions{})
  147. framework.ExpectNoError(err)
  148. pvZone, ok := pv.ObjectMeta.Labels[v1.LabelZoneFailureDomain]
  149. framework.ExpectEqual(ok, true, "PV has no LabelZone to be found")
  150. pvZones.Insert(pvZone)
  151. }
  152. framework.ExpectEqual(pvZones.Equal(expectedZones), true, fmt.Sprintf("PDs provisioned in unwanted zones. We want zones: %v, got: %v", expectedZones, pvZones))
  153. }
  154. type staticPVTestConfig struct {
  155. pvSource *v1.PersistentVolumeSource
  156. pv *v1.PersistentVolume
  157. pvc *v1.PersistentVolumeClaim
  158. pod *v1.Pod
  159. }
  160. // PodsUseStaticPVsOrFail Check that the pods using statically
  161. // created PVs get scheduled to the same zone that the PV is in.
  162. func PodsUseStaticPVsOrFail(f *framework.Framework, podCount int, image string) {
  163. var err error
  164. c := f.ClientSet
  165. ns := f.Namespace.Name
  166. zones, err := framework.GetClusterZones(c)
  167. framework.ExpectNoError(err)
  168. zonelist := zones.List()
  169. ginkgo.By("Creating static PVs across zones")
  170. configs := make([]*staticPVTestConfig, podCount)
  171. for i := range configs {
  172. configs[i] = &staticPVTestConfig{}
  173. }
  174. defer func() {
  175. ginkgo.By("Cleaning up pods and PVs")
  176. for _, config := range configs {
  177. e2epod.DeletePodOrFail(c, ns, config.pod.Name)
  178. }
  179. for _, config := range configs {
  180. e2epod.WaitForPodNoLongerRunningInNamespace(c, config.pod.Name, ns)
  181. e2epv.PVPVCCleanup(c, ns, config.pv, config.pvc)
  182. err = e2epv.DeletePVSource(config.pvSource)
  183. framework.ExpectNoError(err)
  184. }
  185. }()
  186. for i, config := range configs {
  187. zone := zonelist[i%len(zones)]
  188. config.pvSource, err = e2epv.CreatePVSource(zone)
  189. framework.ExpectNoError(err)
  190. pvConfig := e2epv.PersistentVolumeConfig{
  191. NamePrefix: "multizone-pv",
  192. PVSource: *config.pvSource,
  193. Prebind: nil,
  194. }
  195. className := ""
  196. pvcConfig := e2epv.PersistentVolumeClaimConfig{StorageClassName: &className}
  197. config.pv, config.pvc, err = e2epv.CreatePVPVC(c, pvConfig, pvcConfig, ns, true)
  198. framework.ExpectNoError(err)
  199. }
  200. ginkgo.By("Waiting for all PVCs to be bound")
  201. for _, config := range configs {
  202. e2epv.WaitOnPVandPVC(c, ns, config.pv, config.pvc)
  203. }
  204. ginkgo.By("Creating pods for each static PV")
  205. for _, config := range configs {
  206. podConfig := e2epod.MakePod(ns, nil, []*v1.PersistentVolumeClaim{config.pvc}, false, "")
  207. config.pod, err = c.CoreV1().Pods(ns).Create(context.TODO(), podConfig, metav1.CreateOptions{})
  208. framework.ExpectNoError(err)
  209. }
  210. ginkgo.By("Waiting for all pods to be running")
  211. for _, config := range configs {
  212. err = e2epod.WaitForPodRunningInNamespace(c, config.pod)
  213. framework.ExpectNoError(err)
  214. }
  215. }
  216. func newNamedDefaultClaim(ns string, index int) *v1.PersistentVolumeClaim {
  217. claim := v1.PersistentVolumeClaim{
  218. ObjectMeta: metav1.ObjectMeta{
  219. Name: "pvc-" + strconv.Itoa(index),
  220. Namespace: ns,
  221. },
  222. Spec: v1.PersistentVolumeClaimSpec{
  223. AccessModes: []v1.PersistentVolumeAccessMode{
  224. v1.ReadWriteOnce,
  225. },
  226. Resources: v1.ResourceRequirements{
  227. Requests: v1.ResourceList{
  228. v1.ResourceName(v1.ResourceStorage): resource.MustParse("1Gi"),
  229. },
  230. },
  231. },
  232. }
  233. return &claim
  234. }