pv_protection.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2018 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. "context"
  16. "time"
  17. "github.com/onsi/ginkgo"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/labels"
  21. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  22. clientset "k8s.io/client-go/kubernetes"
  23. "k8s.io/kubernetes/pkg/util/slice"
  24. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  25. "k8s.io/kubernetes/test/e2e/framework"
  26. e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
  27. "k8s.io/kubernetes/test/e2e/storage/utils"
  28. )
  29. var _ = utils.SIGDescribe("PV Protection", func() {
  30. var (
  31. client clientset.Interface
  32. nameSpace string
  33. err error
  34. pvc *v1.PersistentVolumeClaim
  35. pv *v1.PersistentVolume
  36. pvConfig e2epv.PersistentVolumeConfig
  37. pvcConfig e2epv.PersistentVolumeClaimConfig
  38. volLabel labels.Set
  39. selector *metav1.LabelSelector
  40. )
  41. f := framework.NewDefaultFramework("pv-protection")
  42. ginkgo.BeforeEach(func() {
  43. client = f.ClientSet
  44. nameSpace = f.Namespace.Name
  45. framework.ExpectNoError(framework.WaitForAllNodesSchedulable(client, framework.TestContext.NodeSchedulableTimeout))
  46. // Enforce binding only within test space via selector labels
  47. volLabel = labels.Set{e2epv.VolumeSelectorKey: nameSpace}
  48. selector = metav1.SetAsLabelSelector(volLabel)
  49. pvConfig = e2epv.PersistentVolumeConfig{
  50. NamePrefix: "hostpath-",
  51. Labels: volLabel,
  52. PVSource: v1.PersistentVolumeSource{
  53. HostPath: &v1.HostPathVolumeSource{
  54. Path: "/tmp/data",
  55. },
  56. },
  57. }
  58. emptyStorageClass := ""
  59. pvcConfig = e2epv.PersistentVolumeClaimConfig{
  60. Selector: selector,
  61. StorageClassName: &emptyStorageClass,
  62. }
  63. ginkgo.By("Creating a PV")
  64. // make the pv definitions
  65. pv = e2epv.MakePersistentVolume(pvConfig)
  66. // create the PV
  67. pv, err = client.CoreV1().PersistentVolumes().Create(context.TODO(), pv, metav1.CreateOptions{})
  68. framework.ExpectNoError(err, "Error creating PV")
  69. ginkgo.By("Waiting for PV to enter phase Available")
  70. framework.ExpectNoError(e2epv.WaitForPersistentVolumePhase(v1.VolumeAvailable, client, pv.Name, 1*time.Second, 30*time.Second))
  71. ginkgo.By("Checking that PV Protection finalizer is set")
  72. pv, err = client.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{})
  73. framework.ExpectNoError(err, "While getting PV status")
  74. framework.ExpectEqual(slice.ContainsString(pv.ObjectMeta.Finalizers, volumeutil.PVProtectionFinalizer, nil), true, "PV Protection finalizer(%v) is not set in %v", volumeutil.PVProtectionFinalizer, pv.ObjectMeta.Finalizers)
  75. })
  76. ginkgo.AfterEach(func() {
  77. framework.Logf("AfterEach: Cleaning up test resources.")
  78. if errs := e2epv.PVPVCCleanup(client, nameSpace, pv, pvc); len(errs) > 0 {
  79. framework.Failf("AfterEach: Failed to delete PVC and/or PV. Errors: %v", utilerrors.NewAggregate(errs))
  80. }
  81. })
  82. ginkgo.It("Verify \"immediate\" deletion of a PV that is not bound to a PVC", func() {
  83. ginkgo.By("Deleting the PV")
  84. err = client.CoreV1().PersistentVolumes().Delete(context.TODO(), pv.Name, metav1.NewDeleteOptions(0))
  85. framework.ExpectNoError(err, "Error deleting PV")
  86. framework.WaitForPersistentVolumeDeleted(client, pv.Name, framework.Poll, e2epv.PVDeletingTimeout)
  87. })
  88. ginkgo.It("Verify that PV bound to a PVC is not removed immediately", func() {
  89. ginkgo.By("Creating a PVC")
  90. pvc = e2epv.MakePersistentVolumeClaim(pvcConfig, nameSpace)
  91. pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(context.TODO(), pvc, metav1.CreateOptions{})
  92. framework.ExpectNoError(err, "Error creating PVC")
  93. ginkgo.By("Waiting for PVC to become Bound")
  94. err = e2epv.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, nameSpace, pvc.Name, framework.Poll, e2epv.ClaimBindingTimeout)
  95. framework.ExpectNoError(err, "Failed waiting for PVC to be bound %v", err)
  96. ginkgo.By("Deleting the PV, however, the PV must not be removed from the system as it's bound to a PVC")
  97. err = client.CoreV1().PersistentVolumes().Delete(context.TODO(), pv.Name, metav1.NewDeleteOptions(0))
  98. framework.ExpectNoError(err, "Error deleting PV")
  99. ginkgo.By("Checking that the PV status is Terminating")
  100. pv, err = client.CoreV1().PersistentVolumes().Get(context.TODO(), pv.Name, metav1.GetOptions{})
  101. framework.ExpectNoError(err, "While checking PV status")
  102. framework.ExpectNotEqual(pv.ObjectMeta.DeletionTimestamp, nil)
  103. ginkgo.By("Deleting the PVC that is bound to the PV")
  104. err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(context.TODO(), pvc.Name, metav1.NewDeleteOptions(0))
  105. framework.ExpectNoError(err, "Error deleting PVC")
  106. ginkgo.By("Checking that the PV is automatically removed from the system because it's no longer bound to a PVC")
  107. framework.WaitForPersistentVolumeDeleted(client, pv.Name, framework.Poll, e2epv.PVDeletingTimeout)
  108. })
  109. })