Skip to content

Commit 3e9b71b

Browse files
committed
Wrap all GRPC errors in status, fix semantics of NotFound errors
1 parent c17d73e commit 3e9b71b

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

pkg/gce-cloud-provider/compute/gce.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ package gcecloudprovider
1717
import (
1818
"context"
1919
"fmt"
20-
"golang.org/x/oauth2/google"
21-
"gopkg.in/gcfg.v1"
2220
"net/http"
2321
"os"
2422
"runtime"
2523
"time"
2624

25+
"golang.org/x/oauth2/google"
26+
"gopkg.in/gcfg.v1"
27+
2728
"cloud.google.com/go/compute/metadata"
2829
"golang.org/x/oauth2"
2930
beta "google.golang.org/api/compute/v0.beta"
@@ -242,3 +243,9 @@ func IsGCEError(err error, reason string) bool {
242243
}
243244
return false
244245
}
246+
247+
// IsGCENotFoundError returns true if the error is a googleapi.Error with
248+
// notFound reason
249+
func IsGCENotFoundError(err error) bool {
250+
return IsGCEError(err, "notFound")
251+
}

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
224224

225225
volKey, err := common.VolumeIDToKey(volumeID)
226226
if err != nil {
227-
// Cannot find volume associated with this ID because can't even get the name or zone
228-
// This is a success according to the spec
227+
klog.Warningf("Treating volume as deleted because volume id %s is invalid: %v", volumeID, err)
229228
return &csi.DeleteVolumeResponse{}, nil
230229
}
231230

232231
volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, volKey)
233232
if err != nil {
234-
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volumeID, err))
233+
klog.Warningf("Treating volume as deleted because cannot find volume %v: %v", volKey.String(), err)
234+
return &csi.DeleteVolumeResponse{}, nil
235235
}
236236

237237
if acquired := gceCS.volumeLocks.TryAcquire(volumeID); !acquired {
@@ -267,7 +267,7 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
267267

268268
volKey, err := common.VolumeIDToKey(volumeID)
269269
if err != nil {
270-
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volumeID, err))
270+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerPublishVolume volume ID is invalid: %v", err))
271271
}
272272

273273
volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, volKey)
@@ -294,7 +294,7 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
294294

295295
_, err = gceCS.CloudProvider.GetDisk(ctx, volKey)
296296
if err != nil {
297-
if gce.IsGCEError(err, "notFound") {
297+
if gce.IsGCENotFoundError(err) {
298298
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find disk %v: %v", volKey.String(), err))
299299
}
300300
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get disk error: %v", err))
@@ -305,7 +305,7 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
305305
}
306306
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
307307
if err != nil {
308-
if gce.IsGCEError(err, "notFound") {
308+
if gce.IsGCENotFoundError(err) {
309309
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find instance %v: %v", nodeID, err))
310310
}
311311
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get instance error: %v", err))
@@ -365,7 +365,7 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
365365

366366
volKey, err := common.VolumeIDToKey(volumeID)
367367
if err != nil {
368-
return nil, err
368+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerUnpublishVolume Volume ID is invalid: %v", err))
369369
}
370370

371371
// Acquires the lock for the volume on that node only, because we need to support the ability
@@ -382,7 +382,12 @@ func (gceCS *GCEControllerServer) ControllerUnpublishVolume(ctx context.Context,
382382
}
383383
instance, err := gceCS.CloudProvider.GetInstanceOrError(ctx, instanceZone, instanceName)
384384
if err != nil {
385-
return nil, err
385+
if gce.IsGCENotFoundError(err) {
386+
// Node not existing on GCE means that disk has been detached
387+
klog.Warningf("Treating volume %v as unpublished because node %v could not be found", volKey.String(), instanceName)
388+
return &csi.ControllerUnpublishVolumeResponse{}, nil
389+
}
390+
return nil, status.Error(codes.Internal, fmt.Sprintf("error getting instance: %v", err))
386391
}
387392

388393
deviceName, err := common.GetDeviceName(volKey)
@@ -420,7 +425,7 @@ func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context
420425
}
421426
volKey, err := common.VolumeIDToKey(volumeID)
422427
if err != nil {
423-
return nil, status.Error(codes.NotFound, fmt.Sprintf("Volume ID is of improper format, got %v", volumeID))
428+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ValidateVolumeCapabilities Volume ID is invalid: %v", err))
424429
}
425430

426431
if acquired := gceCS.volumeLocks.TryAcquire(volumeID); !acquired {
@@ -430,7 +435,7 @@ func (gceCS *GCEControllerServer) ValidateVolumeCapabilities(ctx context.Context
430435

431436
_, err = gceCS.CloudProvider.GetDisk(ctx, volKey)
432437
if err != nil {
433-
if gce.IsGCEError(err, "notFound") {
438+
if gce.IsGCENotFoundError(err) {
434439
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find disk %v: %v", volKey.Name, err))
435440
}
436441
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get disk error: %v", err))
@@ -532,7 +537,7 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
532537
}
533538
volKey, err := common.VolumeIDToKey(volumeID)
534539
if err != nil {
535-
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volumeID, err))
540+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("CreateSnapshot Volume ID is invalid: %v", err))
536541
}
537542

538543
if acquired := gceCS.volumeLocks.TryAcquire(volumeID); !acquired {
@@ -670,7 +675,7 @@ func (gceCS *GCEControllerServer) ControllerExpandVolume(ctx context.Context, re
670675

671676
volKey, err := common.VolumeIDToKey(volumeID)
672677
if err != nil {
673-
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerExpandVolume volume ID is invalid: %v", err))
678+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerExpandVolume Volume ID is invalid: %v", err))
674679
}
675680

676681
resizedGb, err := gceCS.CloudProvider.ResizeDisk(ctx, volKey, reqBytes)

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
9090

9191
notMnt, err := ns.Mounter.Interface.IsLikelyNotMountPoint(targetPath)
9292
if err != nil && !os.IsNotExist(err) {
93-
klog.Errorf("cannot validate mount point: %s %v", targetPath, err)
94-
return nil, err
93+
return nil, status.Error(codes.Internal, fmt.Sprintf("cannot validate mount point: %s %v", targetPath, err))
9594
}
9695
if !notMnt {
9796
// TODO(#95): check if mount is compatible. Return OK if it is, or appropriate error.
@@ -129,8 +128,7 @@ func (ns *GCENodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePub
129128
sourcePath = stagingTargetPath
130129

131130
if err := ns.Mounter.Interface.MakeDir(targetPath); err != nil {
132-
klog.Errorf("mkdir failed on disk %s (%v)", targetPath, err)
133-
return nil, err
131+
return nil, status.Error(codes.Internal, fmt.Sprintf("mkdir failed on disk %s (%v)", targetPath, err))
134132
}
135133
} else if blk := volumeCapability.GetBlock(); blk != nil {
136134
klog.V(4).Infof("NodePublishVolume with block volume mode")
@@ -245,7 +243,7 @@ func (ns *GCENodeServer) NodeStageVolume(ctx context.Context, req *csi.NodeStage
245243

246244
volumeKey, err := common.VolumeIDToKey(volumeID)
247245
if err != nil {
248-
return nil, err
246+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("NodeStageVolume Volume ID is invalid: %v", err))
249247
}
250248

251249
// Part 1: Get device path of attached device
@@ -387,7 +385,7 @@ func (ns *GCENodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpa
387385

388386
volKey, err := common.VolumeIDToKey(volumeID)
389387
if err != nil {
390-
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerExpandVolume volume ID is invalid: %v", err))
388+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("ControllerExpandVolume Volume ID is invalid: %v", err))
391389
}
392390

393391
devicePath, err := ns.getDevicePath(volumeID, "")

0 commit comments

Comments
 (0)