volume_provisioning.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. Copyright 2016 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 e2e
  14. import (
  15. "time"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/resource"
  18. "k8s.io/kubernetes/pkg/api/unversioned"
  19. "k8s.io/kubernetes/pkg/apis/extensions"
  20. client "k8s.io/kubernetes/pkg/client/unversioned"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. )
  25. const (
  26. // Requested size of the volume
  27. requestedSize = "1500Mi"
  28. // Expected size of the volume is 2GiB, because all three supported cloud
  29. // providers allocate volumes in 1GiB chunks.
  30. expectedSize = "2Gi"
  31. )
  32. func testDynamicProvisioning(client *client.Client, claim *api.PersistentVolumeClaim) {
  33. err := framework.WaitForPersistentVolumeClaimPhase(api.ClaimBound, client, claim.Namespace, claim.Name, framework.Poll, framework.ClaimProvisionTimeout)
  34. Expect(err).NotTo(HaveOccurred())
  35. By("checking the claim")
  36. // Get new copy of the claim
  37. claim, err = client.PersistentVolumeClaims(claim.Namespace).Get(claim.Name)
  38. Expect(err).NotTo(HaveOccurred())
  39. // Get the bound PV
  40. pv, err := client.PersistentVolumes().Get(claim.Spec.VolumeName)
  41. Expect(err).NotTo(HaveOccurred())
  42. // Check sizes
  43. expectedCapacity := resource.MustParse(expectedSize)
  44. pvCapacity := pv.Spec.Capacity[api.ResourceName(api.ResourceStorage)]
  45. Expect(pvCapacity.Value()).To(Equal(expectedCapacity.Value()))
  46. requestedCapacity := resource.MustParse(requestedSize)
  47. claimCapacity := claim.Spec.Resources.Requests[api.ResourceName(api.ResourceStorage)]
  48. Expect(claimCapacity.Value()).To(Equal(requestedCapacity.Value()))
  49. // Check PV properties
  50. Expect(pv.Spec.PersistentVolumeReclaimPolicy).To(Equal(api.PersistentVolumeReclaimDelete))
  51. expectedAccessModes := []api.PersistentVolumeAccessMode{api.ReadWriteOnce}
  52. Expect(pv.Spec.AccessModes).To(Equal(expectedAccessModes))
  53. Expect(pv.Spec.ClaimRef.Name).To(Equal(claim.ObjectMeta.Name))
  54. Expect(pv.Spec.ClaimRef.Namespace).To(Equal(claim.ObjectMeta.Namespace))
  55. // We start two pods:
  56. // - The first writes 'hello word' to the /mnt/test (= the volume).
  57. // - The second one runs grep 'hello world' on /mnt/test.
  58. // If both succeed, Kubernetes actually allocated something that is
  59. // persistent across pods.
  60. By("checking the created volume is writable")
  61. runInPodWithVolume(client, claim.Namespace, claim.Name, "echo 'hello world' > /mnt/test/data")
  62. By("checking the created volume is readable and retains data")
  63. runInPodWithVolume(client, claim.Namespace, claim.Name, "grep 'hello world' /mnt/test/data")
  64. // Ugly hack: if we delete the AWS/GCE/OpenStack volume here, it will
  65. // probably collide with destruction of the pods above - the pods
  66. // still have the volume attached (kubelet is slow...) and deletion
  67. // of attached volume is not allowed by AWS/GCE/OpenStack.
  68. // Kubernetes *will* retry deletion several times in
  69. // pvclaimbinder-sync-period.
  70. // So, technically, this sleep is not needed. On the other hand,
  71. // the sync perion is 10 minutes and we really don't want to wait
  72. // 10 minutes here. There is no way how to see if kubelet is
  73. // finished with cleaning volumes. A small sleep here actually
  74. // speeds up the test!
  75. // Three minutes should be enough to clean up the pods properly.
  76. // We've seen GCE PD detach to take more than 1 minute.
  77. By("Sleeping to let kubelet destroy all pods")
  78. time.Sleep(3 * time.Minute)
  79. By("deleting the claim")
  80. framework.ExpectNoError(client.PersistentVolumeClaims(claim.Namespace).Delete(claim.Name))
  81. // Wait for the PV to get deleted too.
  82. framework.ExpectNoError(framework.WaitForPersistentVolumeDeleted(client, pv.Name, 5*time.Second, 20*time.Minute))
  83. }
  84. var _ = framework.KubeDescribe("Dynamic provisioning", func() {
  85. f := framework.NewDefaultFramework("volume-provisioning")
  86. // filled in BeforeEach
  87. var c *client.Client
  88. var ns string
  89. BeforeEach(func() {
  90. c = f.Client
  91. ns = f.Namespace.Name
  92. })
  93. framework.KubeDescribe("DynamicProvisioner", func() {
  94. It("should create and delete persistent volumes [Slow]", func() {
  95. framework.SkipUnlessProviderIs("openstack", "gce", "aws", "gke")
  96. By("creating a StorageClass")
  97. class := newStorageClass()
  98. _, err := c.Extensions().StorageClasses().Create(class)
  99. defer c.Extensions().StorageClasses().Delete(class.Name)
  100. Expect(err).NotTo(HaveOccurred())
  101. By("creating a claim with a dynamic provisioning annotation")
  102. claim := newClaim(ns, false)
  103. defer func() {
  104. c.PersistentVolumeClaims(ns).Delete(claim.Name)
  105. }()
  106. claim, err = c.PersistentVolumeClaims(ns).Create(claim)
  107. Expect(err).NotTo(HaveOccurred())
  108. testDynamicProvisioning(c, claim)
  109. })
  110. })
  111. framework.KubeDescribe("DynamicProvisioner Alpha", func() {
  112. It("should create and delete alpha persistent volumes [Slow]", func() {
  113. framework.SkipUnlessProviderIs("openstack", "gce", "aws", "gke")
  114. By("creating a claim with an alpha dynamic provisioning annotation")
  115. claim := newClaim(ns, true)
  116. defer func() {
  117. c.PersistentVolumeClaims(ns).Delete(claim.Name)
  118. }()
  119. claim, err := c.PersistentVolumeClaims(ns).Create(claim)
  120. Expect(err).NotTo(HaveOccurred())
  121. testDynamicProvisioning(c, claim)
  122. })
  123. })
  124. })
  125. func newClaim(ns string, alpha bool) *api.PersistentVolumeClaim {
  126. claim := api.PersistentVolumeClaim{
  127. ObjectMeta: api.ObjectMeta{
  128. GenerateName: "pvc-",
  129. Namespace: ns,
  130. },
  131. Spec: api.PersistentVolumeClaimSpec{
  132. AccessModes: []api.PersistentVolumeAccessMode{
  133. api.ReadWriteOnce,
  134. },
  135. Resources: api.ResourceRequirements{
  136. Requests: api.ResourceList{
  137. api.ResourceName(api.ResourceStorage): resource.MustParse(requestedSize),
  138. },
  139. },
  140. },
  141. }
  142. if alpha {
  143. claim.Annotations = map[string]string{
  144. "volume.alpha.kubernetes.io/storage-class": "",
  145. }
  146. } else {
  147. claim.Annotations = map[string]string{
  148. "volume.beta.kubernetes.io/storage-class": "fast",
  149. }
  150. }
  151. return &claim
  152. }
  153. // runInPodWithVolume runs a command in a pod with given claim mounted to /mnt directory.
  154. func runInPodWithVolume(c *client.Client, ns, claimName, command string) {
  155. pod := &api.Pod{
  156. TypeMeta: unversioned.TypeMeta{
  157. Kind: "Pod",
  158. APIVersion: "v1",
  159. },
  160. ObjectMeta: api.ObjectMeta{
  161. GenerateName: "pvc-volume-tester-",
  162. },
  163. Spec: api.PodSpec{
  164. Containers: []api.Container{
  165. {
  166. Name: "volume-tester",
  167. Image: "gcr.io/google_containers/busybox:1.24",
  168. Command: []string{"/bin/sh"},
  169. Args: []string{"-c", command},
  170. VolumeMounts: []api.VolumeMount{
  171. {
  172. Name: "my-volume",
  173. MountPath: "/mnt/test",
  174. },
  175. },
  176. },
  177. },
  178. RestartPolicy: api.RestartPolicyNever,
  179. Volumes: []api.Volume{
  180. {
  181. Name: "my-volume",
  182. VolumeSource: api.VolumeSource{
  183. PersistentVolumeClaim: &api.PersistentVolumeClaimVolumeSource{
  184. ClaimName: claimName,
  185. ReadOnly: false,
  186. },
  187. },
  188. },
  189. },
  190. },
  191. }
  192. pod, err := c.Pods(ns).Create(pod)
  193. defer func() {
  194. framework.ExpectNoError(c.Pods(ns).Delete(pod.Name, nil))
  195. }()
  196. framework.ExpectNoError(err, "Failed to create pod: %v", err)
  197. framework.ExpectNoError(framework.WaitForPodSuccessInNamespaceSlow(c, pod.Name, pod.Spec.Containers[0].Name, pod.Namespace))
  198. }
  199. func newStorageClass() *extensions.StorageClass {
  200. var pluginName string
  201. switch {
  202. case framework.ProviderIs("gke"), framework.ProviderIs("gce"):
  203. pluginName = "kubernetes.io/gce-pd"
  204. case framework.ProviderIs("aws"):
  205. pluginName = "kubernetes.io/aws-ebs"
  206. case framework.ProviderIs("openstack"):
  207. pluginName = "kubernetes.io/cinder"
  208. }
  209. return &extensions.StorageClass{
  210. TypeMeta: unversioned.TypeMeta{
  211. Kind: "StorageClass",
  212. },
  213. ObjectMeta: api.ObjectMeta{
  214. Name: "fast",
  215. },
  216. Provisioner: pluginName,
  217. }
  218. }