Skip to content

Commit

Permalink
refactor storage error parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
songjiaxun committed Jul 5, 2024
1 parent 74dc589 commit 0ca5eb9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
8 changes: 6 additions & 2 deletions pkg/cloud_provider/storage/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ func (service *fakeService) SetIAMPolicy(_ context.Context, _ *ServiceBucket, _,
return nil
}

func (service *fakeService) CheckBucketExists(_ context.Context, _ *ServiceBucket) (bool, error) {
return true, nil
func (service *fakeService) CheckBucketExists(_ context.Context, obj *ServiceBucket) (bool, error) {
if _, ok := service.sm.createdBuckets[obj.Name]; ok {
return true, nil
}

return false, storage.ErrBucketNotExist
}

func (service *fakeService) Close() {
Expand Down
23 changes: 21 additions & 2 deletions pkg/cloud_provider/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"golang.org/x/oauth2"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -235,10 +236,28 @@ func IsNotExistErr(err error) bool {
return errors.Is(err, storage.ErrBucketNotExist)
}

func IsPermissionDeniedErr(err error) bool {
func isPermissionDeniedErr(err error) bool {
return strings.Contains(err.Error(), "googleapi: Error 403")
}

func IsCanceledErr(err error) bool {
func isCanceledErr(err error) bool {
return strings.Contains(err.Error(), "context canceled") || strings.Contains(err.Error(), "context deadline exceeded")
}

// ParseErrCode parses error and returns a gRPC code.
func ParseErrCode(err error) codes.Code {
code := codes.Internal
if IsNotExistErr(err) {
code = codes.NotFound
}

if isPermissionDeniedErr(err) {
code = codes.PermissionDenied
}

if isCanceledErr(err) {
code = codes.Aborted
}

return code
}
10 changes: 4 additions & 6 deletions pkg/csi_driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,11 @@ func (s *controllerServer) ValidateVolumeCapabilities(ctx context.Context, req *
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "failed to prepare storage service: %v", err)
}
defer storageService.Close()

// Check that the volume exists
newBucket, err := storageService.GetBucket(ctx, &storage.ServiceBucket{Name: volumeID})
if err != nil && !storage.IsNotExistErr(err) {
return nil, status.Error(codes.Internal, err.Error())
}
if newBucket == nil {
return nil, status.Error(codes.NotFound, fmt.Sprintf("volume %v doesn't exist", volumeID))
if exist, err := storageService.CheckBucketExists(ctx, &storage.ServiceBucket{Name: volumeID}); !exist {
return nil, status.Errorf(storage.ParseErrCode(err), "volume %v doesn't exist: %v", volumeID, err)
}

// Validate that the volume matches the capabilities
Expand Down Expand Up @@ -154,6 +151,7 @@ func (s *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolu
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, "failed to prepare storage service: %v", err)
}
defer storageService.Close()

// Check if the bucket already exists
bucket, err := storageService.GetBucket(ctx, newBucket)
Expand Down
15 changes: 1 addition & 14 deletions pkg/csi_driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,7 @@ func (s *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublish
defer storageService.Close()

if exist, err := storageService.CheckBucketExists(ctx, &storage.ServiceBucket{Name: bucketName}); !exist {
code := codes.Internal
if storage.IsNotExistErr(err) {
code = codes.NotFound
}

if storage.IsPermissionDeniedErr(err) {
code = codes.PermissionDenied
}

if storage.IsCanceledErr(err) {
code = codes.Aborted
}

return nil, status.Errorf(code, "failed to get GCS bucket %q: %v", bucketName, err)
return nil, status.Errorf(storage.ParseErrCode(err), "failed to get GCS bucket %q: %v", bucketName, err)
}
}

Expand Down

0 comments on commit 0ca5eb9

Please sign in to comment.