|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package storage |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | + |
| 23 | + . "github.com/onsi/ginkgo" |
| 24 | + . "github.com/onsi/gomega" |
| 25 | + |
| 26 | + "k8s.io/api/core/v1" |
| 27 | + apierrs "k8s.io/apimachinery/pkg/api/errors" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + clientset "k8s.io/client-go/kubernetes" |
| 30 | + "k8s.io/kubernetes/pkg/util/slice" |
| 31 | + volumeutil "k8s.io/kubernetes/pkg/volume/util" |
| 32 | + "k8s.io/kubernetes/test/e2e/framework" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = SIGDescribe("PVC Protection [Feature:PVCProtection]", func() { |
| 36 | + var ( |
| 37 | + client clientset.Interface |
| 38 | + nameSpace string |
| 39 | + err error |
| 40 | + pvc *v1.PersistentVolumeClaim |
| 41 | + pvcCreatedAndNotDeleted bool |
| 42 | + ) |
| 43 | + |
| 44 | + f := framework.NewDefaultFramework("pvc-protection") |
| 45 | + BeforeEach(func() { |
| 46 | + client = f.ClientSet |
| 47 | + nameSpace = f.Namespace.Name |
| 48 | + framework.ExpectNoError(framework.WaitForAllNodesSchedulable(client, framework.TestContext.NodeSchedulableTimeout)) |
| 49 | + |
| 50 | + By("Creating a PVC") |
| 51 | + suffix := "pvc-protection" |
| 52 | + defaultSC := getDefaultStorageClassName(client) |
| 53 | + testStorageClass := storageClassTest{ |
| 54 | + claimSize: "1Gi", |
| 55 | + } |
| 56 | + pvc = newClaim(testStorageClass, nameSpace, suffix) |
| 57 | + pvc.Spec.StorageClassName = &defaultSC |
| 58 | + pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Create(pvc) |
| 59 | + Expect(err).NotTo(HaveOccurred(), "Error creating PVC") |
| 60 | + pvcCreatedAndNotDeleted = true |
| 61 | + |
| 62 | + By("Waiting for PVC to become Bound") |
| 63 | + err = framework.WaitForPersistentVolumeClaimPhase(v1.ClaimBound, client, nameSpace, pvc.Name, framework.Poll, framework.ClaimProvisionTimeout) |
| 64 | + Expect(err).NotTo(HaveOccurred(), "Failed waiting for PVC to be bound %v", err) |
| 65 | + |
| 66 | + By("Checking that PVC Protection finalizer is set") |
| 67 | + pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{}) |
| 68 | + Expect(err).NotTo(HaveOccurred(), "While getting PVC status") |
| 69 | + Expect(slice.ContainsString(pvc.ObjectMeta.Finalizers, volumeutil.PVCProtectionFinalizer, nil)).To(BeTrue()) |
| 70 | + }) |
| 71 | + |
| 72 | + AfterEach(func() { |
| 73 | + if pvcCreatedAndNotDeleted { |
| 74 | + framework.DeletePersistentVolumeClaim(client, pvc.Name, nameSpace) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + It("Verify \"immediate\" deletion of a PVC that is not in active use by a pod", func() { |
| 79 | + By("Deleting the PVC") |
| 80 | + err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(pvc.Name, metav1.NewDeleteOptions(0)) |
| 81 | + Expect(err).NotTo(HaveOccurred(), "Error deleting PVC") |
| 82 | + waitForPersistentVolumeClaimBeRemoved(client, pvc.Namespace, pvc.Name, framework.Poll, framework.ClaimProvisionTimeout) |
| 83 | + pvcCreatedAndNotDeleted = false |
| 84 | + }) |
| 85 | + |
| 86 | + It("Verify that PVC in active use by a pod is not removed immediatelly", func() { |
| 87 | + By("Creating a Pod that becomes Running and therefore is actively using the PVC") |
| 88 | + pvcClaims := []*v1.PersistentVolumeClaim{pvc} |
| 89 | + pod, err := framework.CreatePod(client, nameSpace, nil, pvcClaims, false, "") |
| 90 | + Expect(err).NotTo(HaveOccurred(), "While creating pod that uses the PVC or waiting for the Pod to become Running") |
| 91 | + |
| 92 | + By("Deleting the PVC, however, the PVC must not be removed from the system as it's in active use by a pod") |
| 93 | + err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(pvc.Name, metav1.NewDeleteOptions(0)) |
| 94 | + Expect(err).NotTo(HaveOccurred(), "Error deleting PVC") |
| 95 | + |
| 96 | + By("Checking that the PVC status is Terminating") |
| 97 | + pvc, err = client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{}) |
| 98 | + Expect(err).NotTo(HaveOccurred(), "While checking PVC status") |
| 99 | + Expect(pvc.ObjectMeta.DeletionTimestamp).NotTo(Equal(nil)) |
| 100 | + |
| 101 | + By("Deleting the pod that uses the PVC") |
| 102 | + err = framework.DeletePodWithWait(f, client, pod) |
| 103 | + Expect(err).NotTo(HaveOccurred(), "Error terminating and deleting pod") |
| 104 | + |
| 105 | + By("Checking that the PVC is automatically removed from the system because it's no longer in active use by a pod") |
| 106 | + waitForPersistentVolumeClaimBeRemoved(client, pvc.Namespace, pvc.Name, framework.Poll, framework.ClaimProvisionTimeout) |
| 107 | + pvcCreatedAndNotDeleted = false |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +// waitForPersistentVolumeClaimBeRemoved waits for a PersistentVolumeClaim to be removed from the system until timeout occurs, whichever comes first. |
| 112 | +func waitForPersistentVolumeClaimBeRemoved(c clientset.Interface, ns string, pvcName string, Poll, timeout time.Duration) error { |
| 113 | + framework.Logf("Waiting up to %v for PersistentVolumeClaim %s to be removed", timeout, pvcName) |
| 114 | + for start := time.Now(); time.Since(start) < timeout; time.Sleep(Poll) { |
| 115 | + _, err := c.CoreV1().PersistentVolumeClaims(ns).Get(pvcName, metav1.GetOptions{}) |
| 116 | + if err != nil { |
| 117 | + if apierrs.IsNotFound(err) { |
| 118 | + framework.Logf("Claim %q in namespace %q doesn't exist in the system", pvcName, ns) |
| 119 | + return nil |
| 120 | + } |
| 121 | + framework.Logf("Failed to get claim %q in namespace %q, retrying in %v. Error: %v", pvcName, ns, Poll, err) |
| 122 | + } |
| 123 | + } |
| 124 | + return fmt.Errorf("PersistentVolumeClaim %s is not removed from the system within %v", pvcName, timeout) |
| 125 | +} |
0 commit comments