Skip to content

Commit 0de0aa5

Browse files
authored
Merge pull request #2233 from k8s-infra-cherrypick-robot/cherry-pick-2229-to-release-1.22
[release-1.22] Fix disk size validation branch
2 parents 4d3052b + ce43698 commit 0de0aa5

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

pkg/gce-pd-csi-driver/node.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232

3333
csi "github.com/container-storage-interface/spec/lib/go/csi"
3434

35+
"k8s.io/apimachinery/pkg/api/resource"
36+
volumehelpers "k8s.io/cloud-provider/volume/helpers"
3537
"k8s.io/klog/v2"
3638
"k8s.io/mount-utils"
3739

@@ -451,14 +453,22 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
451453
}
452454

453455
// If a disk size is provided in the publish context, ensure it matches the actual device size.
454-
if expectedSize := req.GetPublishContext()[constants.ContextDiskSizeGB]; expectedSize != "" {
455-
actualSize, err := getBlockSizeBytes(devicePath, ns.Mounter)
456+
expectedDiskSizeStr := req.GetPublishContext()[constants.ContextDiskSizeGB]
457+
if expectedSizeGib, err := strconv.ParseInt(expectedDiskSizeStr, 10, 64); err == nil {
458+
actualSizeBytes, err := getBlockSizeBytes(devicePath, ns.Mounter)
456459
if err != nil {
457460
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get block size for '%s': %v", devicePath, err.Error()))
458461
}
459-
if expectedSize != strconv.FormatInt(actualSize, 10) {
460-
return nil, status.Error(codes.Internal, fmt.Sprintf("expected block size %q, got %q", expectedSize, strconv.FormatInt(actualSize, 10)))
462+
actualSizeGib, err := volumehelpers.RoundUpToGiB(*resource.NewQuantity(actualSizeBytes, resource.DecimalSI))
463+
if err != nil {
464+
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to convert block size '%d' to GiB: %v", actualSizeBytes, err.Error()))
465+
}
466+
467+
if expectedSizeGib > actualSizeGib {
468+
return nil, status.Error(codes.Internal, fmt.Sprintf("expected block size %q, got %q", expectedSizeGib, actualSizeGib))
461469
}
470+
} else {
471+
klog.V(4).Infof("skipping disk size validation due to invalid expected size: %v", err)
462472
}
463473

464474
err = ns.formatAndMount(devicePath, stagingTargetPath, fstype, options, ns.Mounter)

pkg/gce-pd-csi-driver/node_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,14 +1125,49 @@ func TestNodeStageVolume(t *testing.T) {
11251125
},
11261126
},
11271127
{
1128-
name: "Valid request with disk size check",
1128+
name: "Valid request disk size matches expected size",
1129+
req: &csi.NodeStageVolumeRequest{
1130+
VolumeId: volumeID,
1131+
StagingTargetPath: stagingPath,
1132+
VolumeCapability: stdVolCap,
1133+
PublishContext: map[string]string{constants.ContextDiskSizeGB: "6"},
1134+
},
1135+
deviceSize: 6442450944,
1136+
blockExtSize: 1,
1137+
readonlyBit: "1",
1138+
expResize: false,
1139+
expCommandList: []fakeCmd{
1140+
{
1141+
cmd: "blockdev",
1142+
args: "--getsize64 /dev/disk/fake-path",
1143+
stdout: "%v",
1144+
},
1145+
{
1146+
cmd: "blkid",
1147+
args: "-p -s TYPE -s PTTYPE -o export /dev/disk/fake-path",
1148+
stdout: "DEVNAME=/dev/sdb\nTYPE=%v",
1149+
},
1150+
{
1151+
cmd: "fsck",
1152+
args: "-a /dev/disk/fake-path",
1153+
stdout: "",
1154+
},
1155+
{
1156+
cmd: "blockdev",
1157+
args: "--getro /dev/disk/fake-path",
1158+
stdout: "%v",
1159+
},
1160+
},
1161+
},
1162+
{
1163+
name: "Valid request disk size exceeds expected size",
11291164
req: &csi.NodeStageVolumeRequest{
11301165
VolumeId: volumeID,
11311166
StagingTargetPath: stagingPath,
11321167
VolumeCapability: stdVolCap,
11331168
PublishContext: map[string]string{constants.ContextDiskSizeGB: "1"},
11341169
},
1135-
deviceSize: 1,
1170+
deviceSize: 6442450944,
11361171
blockExtSize: 1,
11371172
readonlyBit: "1",
11381173
expResize: false,
@@ -1246,7 +1281,7 @@ func TestNodeStageVolume(t *testing.T) {
12461281
VolumeCapability: stdVolCap,
12471282
PublishContext: map[string]string{constants.ContextDiskSizeGB: "10"},
12481283
},
1249-
deviceSize: 5,
1284+
deviceSize: 6442450944,
12501285
expErrCode: codes.Internal,
12511286
expCommandList: []fakeCmd{
12521287
{

0 commit comments

Comments
 (0)