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"
|
|
|
|
|
|
|
|
. "github.com/onsi/gomega" // nolint
|
|
|
|
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"
|
|
|
|
e2elog "k8s.io/kubernetes/test/e2e/framework/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func expandPVCSize(c kubernetes.Interface, pvc *v1.PersistentVolumeClaim, size string, t int) error {
|
|
|
|
pvcName := pvc.Name
|
|
|
|
|
2021-06-25 12:55:46 +00:00
|
|
|
updatedPVC, err := c.CoreV1().
|
|
|
|
PersistentVolumeClaims(pvc.Namespace).
|
|
|
|
Get(context.TODO(), pvcName, metav1.GetOptions{})
|
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).
|
|
|
|
Update(context.TODO(), updatedPVC, metav1.UpdateOptions{})
|
2019-12-18 10:32:35 +00:00
|
|
|
Expect(err).Should(BeNil())
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
e2elog.Logf("Waiting up to %v to be in Resized state", pvc)
|
|
|
|
return wait.PollImmediate(poll, timeout, func() (bool, error) {
|
|
|
|
e2elog.Logf("waiting for PVC %s (%d seconds elapsed)", updatedPVC.Name, int(time.Since(start).Seconds()))
|
2021-06-25 12:55:46 +00:00
|
|
|
updatedPVC, err = c.CoreV1().
|
|
|
|
PersistentVolumeClaims(updatedPVC.Namespace).
|
|
|
|
Get(context.TODO(), pvcName, metav1.GetOptions{})
|
2019-12-18 10:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
e2elog.Logf("Error getting pvc in namespace: '%s': %v", updatedPVC.Namespace, err)
|
2020-12-17 13:00:02 +00:00
|
|
|
if isRetryableAPIError(err) {
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
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 {
|
|
|
|
e2elog.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)) {
|
2021-06-25 12:55:46 +00:00
|
|
|
e2elog.Logf(
|
|
|
|
"current size in status %v,expected size %v",
|
|
|
|
updatedPVC.Status.Capacity[v1.ResourceStorage],
|
|
|
|
resource.MustParse(size))
|
2019-12-18 10:32:35 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2021-06-25 12:55:46 +00:00
|
|
|
pvc, err = f.ClientSet.CoreV1().
|
|
|
|
PersistentVolumeClaims(pvc.Namespace).
|
|
|
|
Get(context.TODO(), pvc.Name, metav1.GetOptions{})
|
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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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()
|
|
|
|
|
|
|
|
return wait.PollImmediate(poll, timeout, func() (bool, error) {
|
|
|
|
e2elog.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 != "" {
|
|
|
|
e2elog.Logf("failed to execute command in app pod %v", stdErr)
|
|
|
|
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 {
|
|
|
|
e2elog.Logf("expected size %s found %s information", size, output)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
}
|