|
| 1 | +/* |
| 2 | +Copyright 2021 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 | + "context" |
| 21 | + "fmt" |
| 22 | + "path/filepath" |
| 23 | + "time" |
| 24 | + |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + storagev1 "k8s.io/api/storage/v1" |
| 27 | + "k8s.io/apimachinery/pkg/api/resource" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + clientset "k8s.io/client-go/kubernetes" |
| 30 | + |
| 31 | + "github.com/onsi/ginkgo" |
| 32 | + "github.com/onsi/gomega" |
| 33 | + "k8s.io/apimachinery/pkg/util/rand" |
| 34 | + "k8s.io/apimachinery/pkg/util/wait" |
| 35 | + "k8s.io/kubernetes/test/e2e/framework" |
| 36 | + e2enode "k8s.io/kubernetes/test/e2e/framework/node" |
| 37 | + "k8s.io/kubernetes/test/e2e/storage/testsuites" |
| 38 | + "k8s.io/kubernetes/test/e2e/storage/utils" |
| 39 | +) |
| 40 | + |
| 41 | +var _ = utils.SIGDescribe("PersistentVolumes-expansion ", func() { |
| 42 | + f := framework.NewDefaultFramework("persistent-local-volumes-expansion") |
| 43 | + ginkgo.Context("loopback local block volume", func() { |
| 44 | + var ( |
| 45 | + config *localTestConfig |
| 46 | + scName string |
| 47 | + ) |
| 48 | + |
| 49 | + testVolType := BlockFsWithFormatLocalVolumeType |
| 50 | + var testVol *localTestVolume |
| 51 | + testMode := immediateMode |
| 52 | + ginkgo.BeforeEach(func() { |
| 53 | + nodes, err := e2enode.GetBoundedReadySchedulableNodes(f.ClientSet, maxNodes) |
| 54 | + framework.ExpectNoError(err) |
| 55 | + |
| 56 | + scName = fmt.Sprintf("%v-%v", testSCPrefix, f.Namespace.Name) |
| 57 | + // Choose a random node |
| 58 | + randomNode := &nodes.Items[rand.Intn(len(nodes.Items))] |
| 59 | + |
| 60 | + hostExec := utils.NewHostExec(f) |
| 61 | + ltrMgr := utils.NewLocalResourceManager("local-volume-test", hostExec, hostBase) |
| 62 | + config = &localTestConfig{ |
| 63 | + ns: f.Namespace.Name, |
| 64 | + client: f.ClientSet, |
| 65 | + timeouts: f.Timeouts, |
| 66 | + nodes: nodes.Items, |
| 67 | + randomNode: randomNode, |
| 68 | + scName: scName, |
| 69 | + discoveryDir: filepath.Join(hostBase, f.Namespace.Name), |
| 70 | + hostExec: hostExec, |
| 71 | + ltrMgr: ltrMgr, |
| 72 | + } |
| 73 | + |
| 74 | + setupExpandableLocalStorageClass(config, &testMode) |
| 75 | + testVols := setupLocalVolumesPVCsPVs(config, testVolType, config.randomNode, 1, testMode) |
| 76 | + testVol = testVols[0] |
| 77 | + }) |
| 78 | + ginkgo.AfterEach(func() { |
| 79 | + cleanupLocalVolumes(config, []*localTestVolume{testVol}) |
| 80 | + cleanupStorageClass(config) |
| 81 | + }) |
| 82 | + |
| 83 | + ginkgo.It("should support online expansion on node", func() { |
| 84 | + var ( |
| 85 | + pod1 *v1.Pod |
| 86 | + pod1Err error |
| 87 | + ) |
| 88 | + ginkgo.By("Creating pod1") |
| 89 | + pod1, pod1Err = createLocalPod(config, testVol, nil) |
| 90 | + framework.ExpectNoError(pod1Err) |
| 91 | + verifyLocalPod(config, testVol, pod1, config.randomNode.Name) |
| 92 | + |
| 93 | + // We expand the PVC while l.pod is using it for online expansion. |
| 94 | + ginkgo.By("Expanding current pvc") |
| 95 | + currentPvcSize := testVol.pvc.Spec.Resources.Requests[v1.ResourceStorage] |
| 96 | + newSize := currentPvcSize.DeepCopy() |
| 97 | + newSize.Add(resource.MustParse("10Mi")) |
| 98 | + framework.Logf("currentPvcSize %s, newSize %s", currentPvcSize.String(), newSize.String()) |
| 99 | + newPVC, err := testsuites.ExpandPVCSize(testVol.pvc, newSize, f.ClientSet) |
| 100 | + framework.ExpectNoError(err, "While updating pvc for more size") |
| 101 | + testVol.pvc = newPVC |
| 102 | + gomega.Expect(testVol.pvc).NotTo(gomega.BeNil()) |
| 103 | + |
| 104 | + pvcSize := testVol.pvc.Spec.Resources.Requests[v1.ResourceStorage] |
| 105 | + if pvcSize.Cmp(newSize) != 0 { |
| 106 | + framework.Failf("error updating pvc size %q", testVol.pvc.Name) |
| 107 | + } |
| 108 | + |
| 109 | + // Now update the underlying volume manually |
| 110 | + err = config.ltrMgr.ExpandBlockDevice(testVol.ltr, 10 /*number of 1M blocks to add*/) |
| 111 | + framework.ExpectNoError(err, "while expanding loopback device") |
| 112 | + |
| 113 | + // now update PV to matching size |
| 114 | + pv, err := UpdatePVSize(testVol.pv, newSize, f.ClientSet) |
| 115 | + framework.ExpectNoError(err, "while updating pv to more size") |
| 116 | + gomega.Expect(pv).NotTo(gomega.BeNil()) |
| 117 | + testVol.pv = pv |
| 118 | + |
| 119 | + ginkgo.By("Waiting for file system resize to finish") |
| 120 | + testVol.pvc, err = testsuites.WaitForFSResize(testVol.pvc, f.ClientSet) |
| 121 | + framework.ExpectNoError(err, "while waiting for fs resize to finish") |
| 122 | + |
| 123 | + pvcConditions := testVol.pvc.Status.Conditions |
| 124 | + framework.ExpectEqual(len(pvcConditions), 0, "pvc should not have conditions") |
| 125 | + }) |
| 126 | + |
| 127 | + }) |
| 128 | + |
| 129 | +}) |
| 130 | + |
| 131 | +func UpdatePVSize(pv *v1.PersistentVolume, size resource.Quantity, c clientset.Interface) (*v1.PersistentVolume, error) { |
| 132 | + pvName := pv.Name |
| 133 | + pvToUpdate := pv.DeepCopy() |
| 134 | + |
| 135 | + var lastError error |
| 136 | + waitErr := wait.PollImmediate(5*time.Second, csiResizeWaitPeriod, func() (bool, error) { |
| 137 | + var err error |
| 138 | + pvToUpdate, err = c.CoreV1().PersistentVolumes().Get(context.TODO(), pvName, metav1.GetOptions{}) |
| 139 | + if err != nil { |
| 140 | + return false, fmt.Errorf("error fetching pv %s: %v", pvName, err) |
| 141 | + } |
| 142 | + pvToUpdate.Spec.Capacity[v1.ResourceStorage] = size |
| 143 | + pvToUpdate, err = c.CoreV1().PersistentVolumes().Update(context.TODO(), pvToUpdate, metav1.UpdateOptions{}) |
| 144 | + if err != nil { |
| 145 | + framework.Logf("error updating PV %s: %v", pvName, err) |
| 146 | + lastError = err |
| 147 | + return false, nil |
| 148 | + } |
| 149 | + return true, nil |
| 150 | + }) |
| 151 | + if waitErr == wait.ErrWaitTimeout { |
| 152 | + return nil, fmt.Errorf("timed out attempting to update PV size. last update error: %v", lastError) |
| 153 | + } |
| 154 | + if waitErr != nil { |
| 155 | + return nil, fmt.Errorf("failed to expand PV size: %v", waitErr) |
| 156 | + } |
| 157 | + return pvToUpdate, nil |
| 158 | +} |
| 159 | + |
| 160 | +func setupExpandableLocalStorageClass(config *localTestConfig, mode *storagev1.VolumeBindingMode) { |
| 161 | + enableExpansion := true |
| 162 | + sc := &storagev1.StorageClass{ |
| 163 | + ObjectMeta: metav1.ObjectMeta{ |
| 164 | + Name: config.scName, |
| 165 | + }, |
| 166 | + Provisioner: "kubernetes.io/no-provisioner", |
| 167 | + VolumeBindingMode: mode, |
| 168 | + AllowVolumeExpansion: &enableExpansion, |
| 169 | + } |
| 170 | + |
| 171 | + _, err := config.client.StorageV1().StorageClasses().Create(context.TODO(), sc, metav1.CreateOptions{}) |
| 172 | + framework.ExpectNoError(err) |
| 173 | +} |
0 commit comments