Skip to content

Commit dbd2883

Browse files
authored
Merge branch 'master' into logs-for-device-mappings
2 parents 9460d9d + 289ae6c commit dbd2883

File tree

8 files changed

+92
-8
lines changed

8 files changed

+92
-8
lines changed

cmd/gce-pd-csi-driver/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ var (
100100

101101
diskCacheSyncPeriod = flag.Duration("disk-cache-sync-period", 10*time.Minute, "Period for the disk cache to check the /dev/disk/by-id/ directory and evaluate the symlinks")
102102

103+
enableDiskSizeValidation = flag.Bool("enable-disk-size-validation", false, "If set to true, the driver will validate that the requested disk size is matches the physical disk size. This flag is disabled by default.")
104+
103105
version string
104106
)
105107

@@ -251,7 +253,8 @@ func handle() {
251253
maxBackoffDuration := time.Duration(*errorBackoffMaxDurationMs) * time.Millisecond
252254
// TODO(2042): Move more of the constructor args into this struct
253255
args := &driver.GCEControllerServerArgs{
254-
EnableDiskTopology: *diskTopology,
256+
EnableDiskTopology: *diskTopology,
257+
EnableDiskSizeValidation: *enableDiskSizeValidation,
255258
}
256259

257260
controllerServer = driver.NewControllerServer(gceDriver, cloudProvider, initialBackoffDuration, maxBackoffDuration, fallbackRequisiteZones, *enableStoragePoolsFlag, *enableDataCacheFlag, multiZoneVolumeHandleConfig, listVolumesConfig, provisionableDisksConfig, *enableHdHAFlag, args)

pkg/common/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848

4949
ContextDataCacheSize = "data-cache-size"
5050
ContextDataCacheMode = "data-cache-mode"
51+
ContextDiskSizeGB = "disk-size"
5152

5253
// Keys in the publish context
5354
ContexLocalSsdCacheSize = "local-ssd-cache-size"

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"math/rand"
2222
neturl "net/url"
2323
"sort"
24+
"strconv"
2425
"strings"
2526
"time"
2627

@@ -121,11 +122,13 @@ type GCEControllerServer struct {
121122
// new RPC methods that might be introduced in future versions of the spec.
122123
csi.UnimplementedControllerServer
123124

124-
EnableDiskTopology bool
125+
EnableDiskTopology bool
126+
EnableDiskSizeValidation bool
125127
}
126128

127129
type GCEControllerServerArgs struct {
128-
EnableDiskTopology bool
130+
EnableDiskTopology bool
131+
EnableDiskSizeValidation bool
129132
}
130133

131134
type MultiZoneVolumeHandleConfig struct {
@@ -1113,7 +1116,7 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
11131116
volumeCapability := req.GetVolumeCapability()
11141117

11151118
pubVolResp := &csi.ControllerPublishVolumeResponse{
1116-
PublishContext: nil,
1119+
PublishContext: map[string]string{},
11171120
}
11181121

11191122
// Set data cache publish context
@@ -1162,6 +1165,9 @@ func (gceCS *GCEControllerServer) executeControllerPublishVolume(ctx context.Con
11621165
}
11631166
return nil, common.LoggedError("Failed to getDisk: ", err), disk
11641167
}
1168+
if gceCS.EnableDiskSizeValidation && pubVolResp.GetPublishContext() != nil {
1169+
pubVolResp.PublishContext[common.ContextDiskSizeGB] = strconv.FormatInt(disk.GetSizeGb(), 10)
1170+
}
11651171
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, project, instanceZone, instanceName)
11661172
if err != nil {
11671173
if gce.IsGCENotFoundError(err) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func NewControllerServer(gceDriver *GCEDriver, cloudProvider gce.GCECompute, err
178178
provisionableDisksConfig: provisionableDisksConfig,
179179
enableHdHA: enableHdHA,
180180
EnableDiskTopology: args.EnableDiskTopology,
181+
EnableDiskSizeValidation: args.EnableDiskSizeValidation,
181182
}
182183
}
183184

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,17 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
449449
klog.V(4).Infof("CSI volume is read-only, mounting with extra option ro")
450450
}
451451

452+
// If a disk size is provided in the publish context, ensure it matches the actual device size.
453+
if expectedSize := req.GetPublishContext()[common.ContextDiskSizeGB]; expectedSize != "" {
454+
actualSize, err := getBlockSizeBytes(devicePath, ns.Mounter)
455+
if err != nil {
456+
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get block size for '%s': %v", devicePath, err.Error()))
457+
}
458+
if expectedSize != strconv.FormatInt(actualSize, 10) {
459+
return nil, status.Error(codes.Internal, fmt.Sprintf("expected block size %q, got %q", expectedSize, strconv.FormatInt(actualSize, 10)))
460+
}
461+
}
462+
452463
err = ns.formatAndMount(devicePath, stagingTargetPath, fstype, options, ns.Mounter)
453464
if err != nil {
454465
// If a volume is created from a content source like snapshot or cloning, the filesystem might get marked

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"google.golang.org/grpc/codes"
3434
"google.golang.org/grpc/status"
3535
"k8s.io/mount-utils"
36+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/common"
3637
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/deviceutils"
3738
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
3839
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/linkcache"
@@ -1123,6 +1124,41 @@ func TestNodeStageVolume(t *testing.T) {
11231124
},
11241125
},
11251126
},
1127+
{
1128+
name: "Valid request with disk size check",
1129+
req: &csi.NodeStageVolumeRequest{
1130+
VolumeId: volumeID,
1131+
StagingTargetPath: stagingPath,
1132+
VolumeCapability: stdVolCap,
1133+
PublishContext: map[string]string{common.ContextDiskSizeGB: "1"},
1134+
},
1135+
deviceSize: 1,
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+
},
11261162
{
11271163
name: "Invalid request (Bad Access Mode)",
11281164
req: &csi.NodeStageVolumeRequest{
@@ -1202,6 +1238,24 @@ func TestNodeStageVolume(t *testing.T) {
12021238
},
12031239
expErrCode: codes.InvalidArgument,
12041240
},
1241+
{
1242+
name: "Invalid request, block size mismatch",
1243+
req: &csi.NodeStageVolumeRequest{
1244+
VolumeId: volumeID,
1245+
StagingTargetPath: stagingPath,
1246+
VolumeCapability: stdVolCap,
1247+
PublishContext: map[string]string{common.ContextDiskSizeGB: "10"},
1248+
},
1249+
deviceSize: 5,
1250+
expErrCode: codes.Internal,
1251+
expCommandList: []fakeCmd{
1252+
{
1253+
cmd: "blockdev",
1254+
args: "--getsize64 /dev/disk/fake-path",
1255+
stdout: "%v",
1256+
},
1257+
},
1258+
},
12051259
}
12061260
for _, tc := range testCases {
12071261
t.Run(tc.name, func(t *testing.T) {

test/remote/instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ func (i *InstanceInfo) CreateOrGetInstance(localSSDCount int) error {
248248
i.externalIP = externalIP
249249
}
250250

251-
if sshOut, err := i.SSHCheckAlive(); err != nil {
251+
if err := i.SSHCheckAlive(); err != nil {
252252
err = fmt.Errorf("Instance %v in state RUNNING but not available by SSH: %v", i.cfg.Name, err.Error())
253-
klog.Warningf("SSH encountered an error: %v, output: %v", err, sshOut)
253+
klog.Warningf("SSH encountered an error: %v", err)
254254
return false, nil
255255
}
256256
klog.V(4).Infof("Instance %v in state RUNNING and available by SSH", i.cfg.Name)

test/remote/ssh.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"os/exec"
2323
"os/user"
2424
"strings"
25+
"time"
2526

27+
"k8s.io/apimachinery/pkg/util/wait"
2628
"k8s.io/klog/v2"
2729
)
2830

@@ -88,8 +90,14 @@ func (i *InstanceInfo) SSHNoSudo(cmd ...string) (string, error) {
8890
}
8991

9092
// SSHCheckAlive just pings the server quickly to check whether it is reachable by SSH
91-
func (i *InstanceInfo) SSHCheckAlive() (string, error) {
92-
return runSSHCommand("ssh", []string{i.GetSSHTarget(), "-o", "ConnectTimeout=10", "--", "echo"}...)
93+
func (i *InstanceInfo) SSHCheckAlive() error {
94+
return wait.Poll(5*time.Second, time.Minute, func() (bool, error) {
95+
out, err := runSSHCommand("ssh", []string{i.GetSSHTarget(), "-o", "ConnectTimeout=10", "--", "echo"}...)
96+
if err != nil {
97+
klog.V(2).Infof("ssh error, retrying: %v, %s", err, out)
98+
}
99+
return err == nil, nil
100+
})
93101
}
94102

95103
// runSSHCommand executes the ssh or scp command, adding the flag provided --ssh-options

0 commit comments

Comments
 (0)