pvc_protection.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 storage
  14. import (
  15. "github.com/onsi/ginkgo"
  16. "github.com/onsi/gomega"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. clientset "k8s.io/client-go/kubernetes"
  20. "k8s.io/kubernetes/pkg/util/slice"
  21. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  22. "k8s.io/kubernetes/test/e2e/framework"
  23. "k8s.io/kubernetes/test/e2e/storage/testsuites"
  24. "k8s.io/kubernetes/test/e2e/storage/utils"
  25. )
  26. var _ = utils.SIGDescribe("PVC Protection", func() {
  27. var (
  28. client clientset.Interface
  29. nameSpace string
  30. err error
  31. pvc *v1.PersistentVolumeClaim
  32. pvcCreatedAndNotDeleted bool
  33. pod *v1.Pod
  34. )
  35. f := framework.NewDefaultFramework("pvc-protection")
  36. ginkgo.BeforeEach(func() {
  37. client = f.ClientSet
  38. nameSpace = f.Namespace.Name
  39. framework.ExpectNoError(framework.WaitForAllNodesSchedulable(client, framework.TestContext.NodeSchedulableTimeout))
  40. ginkgo.By("Creating a PVC")
  41. suffix := "pvc-protection"
  42. framework.SkipIfNoDefaultStorageClass(client)
  43. testStorageClass := testsuites.StorageClassTest{
  44. ClaimSize: "1Gi",
  45. }
  46. pvc = newClaim(testStorageClass, nameSpace, suffix)
  47. pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(pvc)
  48. framework.ExpectNoError(err, "Error creating PVC")
  49. pvcCreatedAndNotDeleted = true
  50. ginkgo.By("Creating a Pod that becomes Running and therefore is actively using the PVC")
  51. pvcClaims := []*v1.PersistentVolumeClaim{pvc}
  52. pod, err = framework.CreatePod(client, nameSpace, nil, pvcClaims, false, "")
  53. framework.ExpectNoError(err, "While creating pod that uses the PVC or waiting for the Pod to become Running")
  54. ginkgo.By("Waiting for PVC to become Bound")
  55. err = framework.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, nameSpace, pvc.Name, framework.Poll, framework.ClaimBindingTimeout)
  56. framework.ExpectNoError(err, "Failed waiting for PVC to be bound %v", err)
  57. ginkgo.By("Checking that PVC Protection finalizer is set")
  58. pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})
  59. framework.ExpectNoError(err, "While getting PVC status")
  60. gomega.Expect(slice.ContainsString(pvc.ObjectMeta.Finalizers, volumeutil.PVCProtectionFinalizer, nil)).To(gomega.BeTrue(), "PVC Protection finalizer(%v) is not set in %v", volumeutil.PVCProtectionFinalizer, pvc.ObjectMeta.Finalizers)
  61. })
  62. ginkgo.AfterEach(func() {
  63. if pvcCreatedAndNotDeleted {
  64. framework.DeletePersistentVolumeClaim(client, pvc.Name, nameSpace)
  65. }
  66. })
  67. ginkgo.It("Verify \"immediate\" deletion of a PVC that is not in active use by a pod", func() {
  68. ginkgo.By("Deleting the pod using the PVC")
  69. err = framework.DeletePodWithWait(f, client, pod)
  70. framework.ExpectNoError(err, "Error terminating and deleting pod")
  71. ginkgo.By("Deleting the PVC")
  72. err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(pvc.Name, metav1.NewDeleteOptions(0))
  73. framework.ExpectNoError(err, "Error deleting PVC")
  74. framework.WaitForPersistentVolumeClaimDeleted(client, pvc.Namespace, pvc.Name, framework.Poll, framework.ClaimDeletingTimeout)
  75. pvcCreatedAndNotDeleted = false
  76. })
  77. ginkgo.It("Verify that PVC in active use by a pod is not removed immediately", func() {
  78. ginkgo.By("Deleting the PVC, however, the PVC must not be removed from the system as it's in active use by a pod")
  79. err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(pvc.Name, metav1.NewDeleteOptions(0))
  80. framework.ExpectNoError(err, "Error deleting PVC")
  81. ginkgo.By("Checking that the PVC status is Terminating")
  82. pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})
  83. framework.ExpectNoError(err, "While checking PVC status")
  84. gomega.Expect(pvc.ObjectMeta.DeletionTimestamp).NotTo(gomega.Equal(nil))
  85. ginkgo.By("Deleting the pod that uses the PVC")
  86. err = framework.DeletePodWithWait(f, client, pod)
  87. framework.ExpectNoError(err, "Error terminating and deleting pod")
  88. ginkgo.By("Checking that the PVC is automatically removed from the system because it's no longer in active use by a pod")
  89. framework.WaitForPersistentVolumeClaimDeleted(client, pvc.Namespace, pvc.Name, framework.Poll, framework.ClaimDeletingTimeout)
  90. pvcCreatedAndNotDeleted = false
  91. })
  92. ginkgo.It("Verify that scheduling of a pod that uses PVC that is being deleted fails and the pod becomes Unschedulable", func() {
  93. ginkgo.By("Deleting the PVC, however, the PVC must not be removed from the system as it's in active use by a pod")
  94. err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(pvc.Name, metav1.NewDeleteOptions(0))
  95. framework.ExpectNoError(err, "Error deleting PVC")
  96. ginkgo.By("Checking that the PVC status is Terminating")
  97. pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})
  98. framework.ExpectNoError(err, "While checking PVC status")
  99. gomega.Expect(pvc.ObjectMeta.DeletionTimestamp).NotTo(gomega.Equal(nil))
  100. ginkgo.By("Creating second Pod whose scheduling fails because it uses a PVC that is being deleted")
  101. secondPod, err2 := framework.CreateUnschedulablePod(client, nameSpace, nil, []*v1.PersistentVolumeClaim{pvc}, false, "")
  102. framework.ExpectNoError(err2, "While creating second pod that uses a PVC that is being deleted and that is Unschedulable")
  103. ginkgo.By("Deleting the second pod that uses the PVC that is being deleted")
  104. err = framework.DeletePodWithWait(f, client, secondPod)
  105. framework.ExpectNoError(err, "Error terminating and deleting pod")
  106. ginkgo.By("Checking again that the PVC status is Terminating")
  107. pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})
  108. framework.ExpectNoError(err, "While checking PVC status")
  109. gomega.Expect(pvc.ObjectMeta.DeletionTimestamp).NotTo(gomega.Equal(nil))
  110. ginkgo.By("Deleting the first pod that uses the PVC")
  111. err = framework.DeletePodWithWait(f, client, pod)
  112. framework.ExpectNoError(err, "Error terminating and deleting pod")
  113. ginkgo.By("Checking that the PVC is automatically removed from the system because it's no longer in active use by a pod")
  114. framework.WaitForPersistentVolumeClaimDeleted(client, pvc.Namespace, pvc.Name, framework.Poll, framework.ClaimDeletingTimeout)
  115. pvcCreatedAndNotDeleted = false
  116. })
  117. })