@@ -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 )
0 commit comments