From 3f28f789635e0997f172b2baec7460d30b6c8b88 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Fri, 13 Dec 2024 11:24:40 +0000 Subject: [PATCH] cleanup: refine Windows Resizevolume func --- pkg/os/volume/volume.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/pkg/os/volume/volume.go b/pkg/os/volume/volume.go index 11a5327d21..5beb90fb5c 100644 --- a/pkg/os/volume/volume.go +++ b/pkg/os/volume/volume.go @@ -118,14 +118,10 @@ func UnmountVolume(volumeID, path string) error { // ResizeVolume - resizes a volume with the given size, if size == 0 then max supported size is used func ResizeVolume(volumeID string, size int64) error { - // If size is 0 then we will resize to the maximum size possible, otherwise just resize to size - var cmd string - var out []byte - var err error + // if size is 0 then we will resize to the maximum size possible, otherwise just resize to size var finalSize int64 - var outString string if size == 0 { - cmd = "Get-Volume -UniqueId \"$Env:volumeID\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" + cmd := "Get-Volume -UniqueId \"$Env:volumeID\" | Get-partition | Get-PartitionSupportedSize | Select SizeMax | ConvertTo-Json" out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID)) if err != nil || len(out) == 0 { @@ -133,15 +129,11 @@ func ResizeVolume(volumeID string, size int64) error { } var getVolumeSizing map[string]int64 - outString = string(out) - err = json.Unmarshal([]byte(outString), &getVolumeSizing) - if err != nil { + outString := string(out) + if err = json.Unmarshal([]byte(outString), &getVolumeSizing); err != nil { return fmt.Errorf("out %v outstring %v err %v", out, outString, err) } - - sizeMax := getVolumeSizing["SizeMax"] - - finalSize = sizeMax + finalSize = getVolumeSizing["SizeMax"] } else { finalSize = size } @@ -151,14 +143,13 @@ func ResizeVolume(volumeID string, size int64) error { return fmt.Errorf("error getting the current size of volume (%s) with error (%v)", volumeID, err) } - //if the partition's size is already the size we want this is a noop, just return if currentSize >= finalSize { klog.V(2).Infof("Attempted to resize volume %s to a lower size, from currentBytes=%d wantedBytes=%d", volumeID, currentSize, finalSize) return nil } - cmd = fmt.Sprintf("Get-Volume -UniqueId \"$Env:volumeID\" | Get-Partition | Resize-Partition -Size %d", finalSize) - out, err = azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID)) + cmd := fmt.Sprintf("Get-Volume -UniqueId \"$Env:volumeID\" | Get-Partition | Resize-Partition -Size %d", finalSize) + out, err := azureutils.RunPowershellCmd(cmd, fmt.Sprintf("volumeID=%s", volumeID)) if err != nil { return fmt.Errorf("error resizing volume. cmd: %s, output: %s size:%v, finalSize %v, error: %v", cmd, string(out), size, finalSize, err) }