Skip to content

Commit

Permalink
Merge pull request #2290 from andyzhangx/refine-getfreespace-1.29
Browse files Browse the repository at this point in the history
[release-1.29] fix: refine GetFreeSpace call on Windows
  • Loading branch information
andyzhangx authored Apr 15, 2024
2 parents 8a2a7ca + f23e8d4 commit 6f1172d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0
go.opentelemetry.io/otel/sdk v1.20.0
golang.org/x/net v0.18.0
golang.org/x/sys v0.15.0
google.golang.org/grpc v1.59.0
google.golang.org/protobuf v1.33.0
k8s.io/api v0.28.4
Expand Down Expand Up @@ -126,7 +127,6 @@ require (
golang.org/x/mod v0.13.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions pkg/azuredisk/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ func (d *Driver) NodeExpandVolume(_ context.Context, req *csi.NodeExpandVolumeRe
klog.Errorf("%v, will continue checking whether the volume has been resized", retErr)
}

if runtime.GOOS == "windows" && d.enableWindowsHostProcess {
// in windows host process mode, this driver could get the volume size from the volume path
devicePath = volumePath
}
gotBlockSizeBytes, err := getBlockSizeBytes(devicePath, d.mounter)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("could not get size of block volume at path %s: %v", devicePath, err))
Expand Down
33 changes: 20 additions & 13 deletions pkg/mounter/safe_mounter_host_process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/sys/windows"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
mount "k8s.io/mount-utils"

Expand Down Expand Up @@ -224,32 +227,36 @@ func (mounter *winMounter) GetDeviceNameFromMount(mountPath, pluginMountDir stri
}

// GetVolumeSizeInBytes returns the size of the volume in bytes.
func (mounter *winMounter) GetVolumeSizeInBytes(devicePath string) (int64, error) {
volumeSize, _, err := volume.GetVolumeStats(devicePath)
return volumeSize, err
func (mounter *winMounter) GetVolumeSizeInBytes(volumePath string) (int64, error) {
_, totalBytes, _, err := GetFreeSpace(volumePath)
return totalBytes, err
}

// ResizeVolume resizes the volume to the maximum available size.
func (mounter *winMounter) ResizeVolume(devicePath string) error {
return volume.ResizeVolume(devicePath, 0)
}

// GetFreeSpace returns the free space of the volume in bytes, total size of the volume in bytes and the used space of the volume in bytes
func GetFreeSpace(path string) (int64, int64, int64, error) {
var totalNumberOfBytes, totalNumberOfFreeBytes uint64
dirName := windows.StringToUTF16Ptr(path)
err := windows.GetDiskFreeSpaceEx(dirName, nil, &totalNumberOfBytes, &totalNumberOfFreeBytes)
return int64(totalNumberOfFreeBytes), int64(totalNumberOfBytes), int64(totalNumberOfBytes - totalNumberOfFreeBytes), err
}

// GetVolumeStats get volume usage
func (mounter *winMounter) GetVolumeStats(ctx context.Context, path string) (*csi.VolumeUsage, error) {
volumeID, err := volume.GetVolumeIDFromTargetPath(path)
freeBytesAvailable, totalBytes, totalBytesUsed, err := GetFreeSpace(path)
if err != nil {
return nil, fmt.Errorf("GetVolumeIDFromMount(%s) failed with error: %v", path, err)
}
klog.V(6).Infof("GetVolumeStats(%s) returned volumeID(%s)", path, volumeID)
volumeSize, volumeUsedSize, err := volume.GetVolumeStats(volumeID)
if err != nil {
return nil, fmt.Errorf("GetVolumeStats(%s) failed with error: %v", volumeID, err)
return nil, status.Errorf(codes.Internal, "failed to get free space on path %s: %v", path, err)
}

volUsage := &csi.VolumeUsage{
Unit: csi.VolumeUsage_BYTES,
Available: volumeSize - volumeUsedSize,
Total: volumeSize,
Used: volumeUsedSize,
Available: freeBytesAvailable,
Total: totalBytes,
Used: totalBytesUsed,
}
return volUsage, nil
}
24 changes: 0 additions & 24 deletions pkg/os/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,6 @@ func ResizeVolume(volumeID string, size int64) error {
return nil
}

// GetVolumeStats - retrieves the volume stats for a given volume
func GetVolumeStats(volumeID string) (int64, int64, error) {
// get the size and sizeRemaining for the volume
cmd := "(Get-Volume -UniqueId \"$Env:volumeID\" | Select SizeRemaining,Size) | ConvertTo-Json"
out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID))

if err != nil {
return -1, -1, fmt.Errorf("error getting capacity and used size of volume. cmd: %s, output: %s, error: %v", cmd, string(out), err)
}

var getVolume map[string]int64
outString := string(out)
err = json.Unmarshal([]byte(outString), &getVolume)
if err != nil {
return -1, -1, fmt.Errorf("out %v outstring %v err %v", out, outString, err)
}

volumeSize := getVolume["Size"]
volumeSizeRemaining := getVolume["SizeRemaining"]

volumeUsedSize := volumeSize - volumeSizeRemaining
return volumeSize, volumeUsedSize, nil
}

// GetDiskNumberFromVolumeID - gets the disk number where the volume is.
func GetDiskNumberFromVolumeID(volumeID string) (uint32, error) {
// get the size and sizeRemaining for the volume
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f1172d

Please sign in to comment.