Skip to content

Add Volume Resize support for Windows #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 4 additions & 23 deletions pkg/gce-pd-csi-driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"strings"

"context"

Expand Down Expand Up @@ -398,7 +396,7 @@ func (ns *GCENodeServer) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGe
return nil, status.Errorf(codes.Internal, "failed to determine whether %s is block device: %v", req.VolumePath, err)
}
if isBlock {
bcap, err := ns.getBlockSizeBytes(req.VolumePath)
bcap, err := getBlockSizeBytes(req.VolumePath, ns.Mounter)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get block capacity on path %s: %v", req.VolumePath, err)
}
Expand Down Expand Up @@ -482,14 +480,10 @@ func (ns *GCENodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpa

}

// Check the block size
gotBlockSizeBytes, err := ns.getBlockSizeBytes(devicePath)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("error when getting size of block volume at path %s: %v", devicePath, err))
}
if gotBlockSizeBytes < reqBytes {
diskSizeBytes, err := getBlockSizeBytes(devicePath, ns.Mounter)
if diskSizeBytes < reqBytes {
// It's possible that the somewhere the volume size was rounded up, getting more size than requested is a success :)
return nil, status.Errorf(codes.Internal, "resize requested for %v but after resize volume was size %v", reqBytes, gotBlockSizeBytes)
return nil, status.Errorf(codes.Internal, "resize requested for %v but after resize volume was size %v", reqBytes, diskSizeBytes)
}

// TODO(dyzz) Some sort of formatted volume could also check the fs size.
Expand Down Expand Up @@ -531,16 +525,3 @@ func (ns *GCENodeServer) GetVolumeLimits() (int64, error) {
}
return volumeLimitBig, nil
}

func (ns *GCENodeServer) getBlockSizeBytes(devicePath string) (int64, error) {
output, err := ns.Mounter.Exec.Command("blockdev", "--getsize64", devicePath).CombinedOutput()
if err != nil {
return -1, fmt.Errorf("error when getting size of block volume at path %s: output: %s, err: %v", devicePath, string(output), err)
}
strOut := strings.TrimSpace(string(output))
gotSizeBytes, err := strconv.ParseInt(strOut, 10, 64)
if err != nil {
return -1, fmt.Errorf("failed to parse %s into an int size", strOut)
}
return gotSizeBytes, nil
}
15 changes: 15 additions & 0 deletions pkg/gce-pd-csi-driver/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package gceGCEDriver
import (
"fmt"
"os"
"strconv"
"strings"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -64,3 +66,16 @@ func cleanupPublishPath(path string, m *mount.SafeFormatAndMount) error {
func cleanupStagePath(path string, m *mount.SafeFormatAndMount) error {
return cleanupPublishPath(path, m)
}

func getBlockSizeBytes(devicePath string, m *mount.SafeFormatAndMount) (int64, error) {
output, err := m.Exec.Command("blockdev", "--getsize64", devicePath).CombinedOutput()
if err != nil {
return -1, fmt.Errorf("error when getting size of block volume at path %s: output: %s, err: %v", devicePath, string(output), err)
}
strOut := strings.TrimSpace(string(output))
gotSizeBytes, err := strconv.ParseInt(strOut, 10, 64)
if err != nil {
return -1, fmt.Errorf("failed to parse %s into an int size", strOut)
}
return gotSizeBytes, nil
}
9 changes: 9 additions & 0 deletions pkg/gce-pd-csi-driver/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,12 @@ func getDevicePath(ns *GCENodeServer, volumeID, partition string) (string, error
}
return proxy.GetDevicePath(deviceName, partition, volumeKey.Name)
}

func getBlockSizeBytes(devicePath string, m *mount.SafeFormatAndMount) (int64, error) {
proxy, ok := m.Interface.(*mounter.CSIProxyMounter)
if !ok {
return 0, fmt.Errorf("could not cast to csi proxy class")
}

return proxy.GetBlockSizeBytes(devicePath)
}
8 changes: 8 additions & 0 deletions pkg/mount-manager/safe-mounter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ func (mounter *CSIProxyMounter) ExistsPath(path string) (bool, error) {
})
return isExistsResponse.Exists, err
}

func (mounter *CSIProxyMounter) GetBlockSizeBytes(diskId string) (int64, error) {
DiskStatsResponse, err := mounter.DiskClient.DiskStats(context.Background(),
&diskapi.DiskStatsRequest{
DiskID: diskId,
})
return DiskStatsResponse.DiskSize, err
}
19 changes: 19 additions & 0 deletions pkg/resizefs/resizefs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2020 The Kubernetes 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.
*/

package resizefs

type Resizefs interface {
Resize(devicePath, deviceMountPath string) (bool, error)
}
14 changes: 8 additions & 6 deletions pkg/resizefs/resizefs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ import (
"k8s.io/utils/mount"
)

var _ Resizefs = &resizeFs{}

// ResizeFs Provides support for resizing file systems
type ResizeFs struct {
type resizeFs struct {
mounter *mount.SafeFormatAndMount
}

// NewResizeFs returns new instance of resizer
func NewResizeFs(mounter *mount.SafeFormatAndMount) *ResizeFs {
return &ResizeFs{mounter: mounter}
func NewResizeFs(mounter *mount.SafeFormatAndMount) *resizeFs {
return &resizeFs{mounter: mounter}
}

// Resize perform resize of file system
func (resizefs *ResizeFs) Resize(devicePath string, deviceMountPath string) (bool, error) {
func (resizefs *resizeFs) Resize(devicePath, deviceMountPath string) (bool, error) {
format, err := resizefs.mounter.GetDiskFormat(devicePath)

if err != nil {
Expand All @@ -60,7 +62,7 @@ func (resizefs *ResizeFs) Resize(devicePath string, deviceMountPath string) (boo
return false, fmt.Errorf("ResizeFS.Resize - resize of format %s is not supported for device %s mounted at %s", format, devicePath, deviceMountPath)
}

func (resizefs *ResizeFs) extResize(devicePath string) (bool, error) {
func (resizefs *resizeFs) extResize(devicePath string) (bool, error) {
output, err := resizefs.mounter.Exec.Command("resize2fs", devicePath).CombinedOutput()
if err == nil {
klog.V(2).Infof("Device %s resized successfully", devicePath)
Expand All @@ -72,7 +74,7 @@ func (resizefs *ResizeFs) extResize(devicePath string) (bool, error) {

}

func (resizefs *ResizeFs) xfsResize(deviceMountPath string) (bool, error) {
func (resizefs *resizeFs) xfsResize(deviceMountPath string) (bool, error) {
args := []string{"-d", deviceMountPath}
output, err := resizefs.mounter.Exec.Command("xfs_growfs", args...).CombinedOutput()

Expand Down
40 changes: 0 additions & 40 deletions pkg/resizefs/resizefs_unsupported.go

This file was deleted.

69 changes: 69 additions & 0 deletions pkg/resizefs/resizefs_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// +build windows

/*
Copyright 2020 The Kubernetes 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.
*/

package resizefs

import (
"context"
"fmt"

volumeapi "github.com/kubernetes-csi/csi-proxy/client/api/volume/v1beta1"
"k8s.io/klog"
"k8s.io/utils/mount"
mounter "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
)

var _ Resizefs = &resizeFs{}

// ResizeFs Provides support for resizing file systems
type resizeFs struct {
mounter *mount.SafeFormatAndMount
}

// NewResizeFs returns new instance of resizer
func NewResizeFs(mounter *mount.SafeFormatAndMount) *resizeFs {
return &resizeFs{mounter: mounter}
}

// resize perform resize of file system
func (resizefs *resizeFs) Resize(devicePath string, deviceMountPath string) (bool, error) {
klog.V(3).Infof("resizeFS.Resize - Expanding mounted volume %s", deviceMountPath)

proxy, ok := resizefs.mounter.Interface.(*mounter.CSIProxyMounter)
if !ok {
return false, fmt.Errorf("could not cast to csi proxy class")
}

idRequest := &volumeapi.VolumeIDFromMountRequest{
Mount: deviceMountPath,
}
idResponse, err := proxy.VolumeClient.GetVolumeIDFromMount(context.Background(), idRequest)
if err != nil {
return false, err
}
volumeId := idResponse.GetVolumeId()

request := &volumeapi.ResizeVolumeRequest{
VolumeId: volumeId,
}
_, err = proxy.VolumeClient.ResizeVolume(context.Background(), request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to check filesystem type or does this imply ntfs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It implies ntfs, no need to check fs type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there ever is a need for multiple fs support, will the csi proxy be able to automatically figure out what to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this volumeclient API, it means ntfs volume, and all functions through volume API client assumes that.
So far there is no other type to support resize. In case, for example, smb support resize, then the API will be in smb API group.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking something like ext4 in the future, where it also belongs in the volume client. Anyway, I posed the question in slack. For this pr, this is fine.

if err != nil {
return false, err
}
return true, nil
}