diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 47adfe285..9963f8c0b 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -1090,13 +1090,12 @@ func (cs *ControllerServer) ControllerExpandVolume(ctx context.Context, req *csi nodeExpansion := false if rbdVol.VolSize < volSize { util.DebugLog(ctx, "rbd volume %s size is %v,resizing to %v", rbdVol, rbdVol.VolSize, volSize) - rbdVol.VolSize = volSize - nodeExpansion = true - err = rbdVol.resize(ctx, cr) + err = rbdVol.resize(volSize) if err != nil { klog.Errorf(util.Log(ctx, "failed to resize rbd image: %s with error: %v"), rbdVol, err) return nil, status.Error(codes.Internal, err.Error()) } + nodeExpansion = true } return &csi.ControllerExpandVolumeResponse{ diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index 432c50b6a..b1932bb8c 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -1043,17 +1043,21 @@ func cleanupRBDImageMetadataStash(path string) error { } // resize the given volume to new size. -func (rv *rbdVolume) resize(ctx context.Context, cr *util.Credentials) error { - mon := rv.Monitors - volSzMiB := fmt.Sprintf("%dM", util.RoundOffVolSize(rv.VolSize)) - - args := []string{"resize", rv.String(), "--size", volSzMiB, "--id", cr.ID, "-m", mon, "--keyfile=" + cr.KeyFile} - _, stderr, err := util.ExecCommand(ctx, "rbd", args...) - +// updates Volsize of rbdVolume object to newSize in case of success. +func (rv *rbdVolume) resize(newSize int64) error { + image, err := rv.open() if err != nil { - return fmt.Errorf("failed to resize rbd image (%w), command output: %s", err, stderr) + return err + } + defer image.Close() + + err = image.Resize(uint64(util.RoundOffVolSize(newSize) * helpers.MiB)) + if err != nil { + return err } + // update Volsize of rbdVolume object to newSize. + rv.VolSize = newSize return nil }