2021-12-21 14:23:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
2020-04-14 06:59:04 +00:00
|
|
|
"context"
|
2019-12-18 10:32:35 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-06-02 09:41:39 +00:00
|
|
|
. "github.com/onsi/gomega" //nolint:golint // e2e uses Expect() and other Gomega functions
|
2019-12-18 10:32:35 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/cloud-provider/volume/helpers"
|
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func expandPVCSize(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, size string, t int) error {
|
2023-06-05 14:41:04 +00:00
|
|
|
ctx := context.TODO()
|
2019-12-18 10:32:35 +00:00
|
|
|
pvcName := pvc.Name
|
2021-08-03 15:03:13 +00:00
|
|
|
pvcNamespace := pvc.Namespace
|
2022-05-06 06:59:50 +00:00
|
|
|
updatedPVC, err := getPersistentVolumeClaim(c, pvcNamespace, pvcName)
|
2019-12-18 10:32:35 +00:00
|
|
|
if err != nil {
|
2020-07-13 03:56:51 +00:00
|
|
|
return fmt.Errorf("error fetching pvc %q with %w", pvcName, err)
|
2019-12-18 10:32:35 +00:00
|
|
|
}
|
|
|
|
timeout := time.Duration(t) * time.Minute
|
|
|
|
|
|
|
|
updatedPVC.Spec.Resources.Requests[v1.ResourceStorage] = resource.MustParse(size)
|
2021-06-25 12:55:46 +00:00
|
|
|
_, err = c.CoreV1().
|
|
|
|
PersistentVolumeClaims(updatedPVC.Namespace).
|
2023-06-05 14:41:04 +00:00
|
|
|
Update(ctx, updatedPVC, metav1.UpdateOptions{})
|
2023-06-02 09:54:25 +00:00
|
|
|
Expect(err).ShouldNot(HaveOccurred())
|
2019-12-18 10:32:35 +00:00
|
|
|
|
|
|
|
start := time.Now()
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Waiting up to %v to be in Resized state", pvc)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-06-05 14:41:04 +00:00
|
|
|
return wait.PollUntilContextTimeout(ctx, poll, timeout, true, func(ctx context.Context) (bool, error) {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("waiting for PVC %s (%d seconds elapsed)", pvcName, int(time.Since(start).Seconds()))
|
2021-06-25 12:55:46 +00:00
|
|
|
updatedPVC, err = c.CoreV1().
|
2021-08-03 15:03:13 +00:00
|
|
|
PersistentVolumeClaims(pvcNamespace).
|
2023-06-05 14:41:04 +00:00
|
|
|
Get(ctx, pvcName, metav1.GetOptions{})
|
2019-12-18 10:32:35 +00:00
|
|
|
if err != nil {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("Error getting pvc in namespace: '%s': %v", pvcNamespace, err)
|
2020-12-17 13:00:02 +00:00
|
|
|
if isRetryableAPIError(err) {
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-05-11 09:28:56 +00:00
|
|
|
return false, fmt.Errorf("failed to get pvc: %w", err)
|
2019-12-18 10:32:35 +00:00
|
|
|
}
|
|
|
|
pvcConditions := updatedPVC.Status.Conditions
|
|
|
|
if len(pvcConditions) > 0 {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("pvc state %v", pvcConditions[0].Type)
|
2021-06-25 12:55:46 +00:00
|
|
|
if pvcConditions[0].Type == v1.PersistentVolumeClaimResizing ||
|
|
|
|
pvcConditions[0].Type == v1.PersistentVolumeClaimFileSystemResizePending {
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:25:44 +00:00
|
|
|
if !updatedPVC.Status.Capacity[v1.ResourceStorage].Equal(resource.MustParse(size)) {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf(
|
2021-06-25 12:55:46 +00:00
|
|
|
"current size in status %v,expected size %v",
|
|
|
|
updatedPVC.Status.Capacity[v1.ResourceStorage],
|
|
|
|
resource.MustParse(size))
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func resizePVCAndValidateSize(pvcPath, appPath string, f *framework.Framework) error {
|
|
|
|
size := "1Gi"
|
|
|
|
expandSize := "10Gi"
|
|
|
|
pvc, err := loadPVC(pvcPath)
|
2020-10-20 09:55:09 +00:00
|
|
|
if err != nil {
|
2019-12-18 10:32:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
pvc.Namespace = f.UniqueName
|
|
|
|
|
|
|
|
resizePvc, err := loadPVC(pvcPath)
|
2020-10-20 09:55:09 +00:00
|
|
|
if err != nil {
|
2019-12-18 10:32:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
resizePvc.Namespace = f.UniqueName
|
|
|
|
|
|
|
|
app, err := loadApp(appPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pvc.Spec.Resources.Requests[v1.ResourceStorage] = resource.MustParse(size)
|
|
|
|
app.Labels = map[string]string{"app": "resize-pvc"}
|
|
|
|
app.Namespace = f.UniqueName
|
|
|
|
|
2020-03-27 18:21:18 +00:00
|
|
|
err = createPVCAndApp("", f, pvc, app, deployTimeout)
|
2019-12-18 10:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := metav1.ListOptions{
|
|
|
|
LabelSelector: "app=resize-pvc",
|
|
|
|
}
|
2022-05-06 06:59:50 +00:00
|
|
|
pvc, err = getPersistentVolumeClaim(f.ClientSet, pvc.Namespace, pvc.Name)
|
2019-12-18 11:46:17 +00:00
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to get pvc: %w", err)
|
2019-12-18 11:46:17 +00:00
|
|
|
}
|
2019-12-18 10:32:35 +00:00
|
|
|
if *pvc.Spec.VolumeMode == v1.PersistentVolumeFilesystem {
|
|
|
|
err = checkDirSize(app, f, &opt, size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *pvc.Spec.VolumeMode == v1.PersistentVolumeBlock {
|
|
|
|
err = checkDeviceSize(app, f, &opt, size)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// resize PVC
|
|
|
|
err = expandPVCSize(f.ClientSet, resizePvc, expandSize, deployTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// wait for application pod to come up after resize
|
2021-06-15 10:08:51 +00:00
|
|
|
err = waitForPodInRunningState(app.Name, app.Namespace, f.ClientSet, deployTimeout, noError)
|
2019-12-18 10:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *pvc.Spec.VolumeMode == v1.PersistentVolumeFilesystem {
|
|
|
|
err = checkDirSize(app, f, &opt, expandSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *pvc.Spec.VolumeMode == v1.PersistentVolumeBlock {
|
|
|
|
err = checkDeviceSize(app, f, &opt, expandSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = deletePVCAndApp("", f, resizePvc, app)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDirSize(app *v1.Pod, f *framework.Framework, opt *metav1.ListOptions, size string) error {
|
|
|
|
cmd := getDirSizeCheckCmd(app.Spec.Containers[0].VolumeMounts[0].MountPath)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return checkAppMntSize(f, opt, size, cmd, app.Namespace, deployTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDeviceSize(app *v1.Pod, f *framework.Framework, opt *metav1.ListOptions, size string) error {
|
|
|
|
cmd := getDeviceSizeCheckCmd(app.Spec.Containers[0].VolumeDevices[0].DevicePath)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return checkAppMntSize(f, opt, size, cmd, app.Namespace, deployTimeout)
|
|
|
|
}
|
2019-12-18 11:46:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
func getDirSizeCheckCmd(dirPath string) string {
|
|
|
|
return fmt.Sprintf("df -h|grep %s |awk '{print $2}'", dirPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeviceSizeCheckCmd(devPath string) string {
|
|
|
|
return fmt.Sprintf("blockdev --getsize64 %s", devPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAppMntSize(f *framework.Framework, opt *metav1.ListOptions, size, cmd, ns string, t int) error {
|
|
|
|
timeout := time.Duration(t) * time.Minute
|
|
|
|
start := time.Now()
|
|
|
|
|
2023-06-05 12:01:31 +00:00
|
|
|
return wait.PollUntilContextTimeout(context.TODO(), poll, timeout, true, func(_ context.Context) (bool, error) {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("executing cmd %s (%d seconds elapsed)", cmd, int(time.Since(start).Seconds()))
|
2020-09-03 09:34:29 +00:00
|
|
|
output, stdErr, err := execCommandInPod(f, cmd, ns, opt)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-12-18 10:32:35 +00:00
|
|
|
if stdErr != "" {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("failed to execute command in app pod %v", stdErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
s := resource.MustParse(strings.TrimSpace(output))
|
2020-12-17 13:00:02 +00:00
|
|
|
actualSize, err := helpers.RoundUpToGiB(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-12-18 10:32:35 +00:00
|
|
|
s = resource.MustParse(size)
|
2020-12-17 13:00:02 +00:00
|
|
|
expectedSize, err := helpers.RoundUpToGiB(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-12-18 10:32:35 +00:00
|
|
|
if actualSize != expectedSize {
|
2023-02-01 17:06:36 +00:00
|
|
|
framework.Logf("expected size %s found %s information", size, output)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-12-18 10:32:35 +00:00
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|