Skip to content

Commit

Permalink
add DiskSize log formatter
Browse files Browse the repository at this point in the history
for both readability and accuracy.
  • Loading branch information
huww98 committed Apr 9, 2024
1 parent d01b77c commit 735dd38
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/disk/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,8 @@ func (ns *nodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandV
if bytes.Contains(output, []byte("it cannot be grown")) || bytes.Contains(output, []byte("could only be grown by")) {
deviceCapacity := getBlockDeviceCapacity(devicePath)
rootCapacity := getBlockDeviceCapacity(rootPath)
log.Infof("NodeExpandVolume: Volume %s with Device Partition %s no need to grown, with requestSize: %d, rootBlockSize: %d, partition BlockDevice size: %d",
diskID, devicePath, requestBytes, rootCapacity, deviceCapacity)
log.Infof("NodeExpandVolume: Volume %s with Device Partition %s no need to grown, with request: %v, root: %v, partition: %v",
diskID, devicePath, DiskSize{requestBytes}, DiskSize{rootCapacity}, DiskSize{deviceCapacity})
return &csi.NodeExpandVolumeResponse{}, nil
}
}
Expand Down Expand Up @@ -999,7 +999,7 @@ func (ns *nodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandV
return nil, status.Errorf(codes.Aborted, "requested %v, but actual block size %v is smaller. Not updated yet?",
resource.NewQuantity(requestBytes, resource.BinarySI), resource.NewQuantity(deviceCapacity, resource.BinarySI))
}
log.Infof("NodeExpandVolume:: Expand %s to %d bytes successful", diskID, deviceCapacity)
log.Infof("NodeExpandVolume:: Expand %s to %v successful", diskID, DiskSize{deviceCapacity})
return &csi.NodeExpandVolumeResponse{
CapacityBytes: deviceCapacity,
}, nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/disk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,3 +1684,16 @@ func createStaticSnap(volumeID, snapshotID string, snapClient snapClientset.Inte
}
return nil
}

// Logging disk block device size
type DiskSize struct {
Bytes int64
}

func (d DiskSize) String() string {
// Alibaba cloud disks are at least in the order of GiB
if d.Bytes%GBSIZE == 0 {
return fmt.Sprintf("%d GiB", d.Bytes/GBSIZE)
}
return fmt.Sprintf("%.3f GiB (0x%X)", float64(d.Bytes)/GBSIZE, d.Bytes)
}
10 changes: 10 additions & 0 deletions pkg/disk/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,13 @@ func TestCheckDeviceAvailableError(t *testing.T) {
t.Errorf("expected os.ErrNotExist, got %v", err)
}
}

func TestDiskSize(t *testing.T) {
size := DiskSize{22014345216}
assert.Equal(t, "20.502 GiB (0x520284000)", size.String())
}

func TestDiskSizeGiOnly(t *testing.T) {
size := DiskSize{20 * GBSIZE}
assert.Equal(t, "20 GiB", size.String())
}

0 comments on commit 735dd38

Please sign in to comment.