Skip to content

Commit

Permalink
Make CreateVolume idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishenzie committed Feb 12, 2021
1 parent 21fe2a8 commit 26113ee
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
csi "github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/protobuf/ptypes"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver/internal"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -56,6 +57,7 @@ var (
// controllerService represents the controller service of CSI driver
type controllerService struct {
cloud cloud.Cloud
inFlight *internal.InFlight
driverOptions *DriverOptions
}

Expand Down Expand Up @@ -87,6 +89,7 @@ func newControllerService(driverOptions *DriverOptions) controllerService {

return controllerService{
cloud: cloud,
inFlight: internal.NewInFlight(),
driverOptions: driverOptions,
}
}
Expand Down Expand Up @@ -201,6 +204,13 @@ func (d *controllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
return newCreateVolumeResponse(disk), nil
}

// check if a request is already in-flight because the CreateVolume API is not idempotent
if ok := d.inFlight.Insert(req); !ok {
msg := fmt.Sprintf("Create volume request for %s is already in progress", volName)
return nil, status.Error(codes.AlreadyExists, msg)
}
defer d.inFlight.Delete(req)

// create a new volume
zone := pickAvailabilityZone(req.GetAccessibilityRequirements())
outpostArn := getOutpostArn(req.GetAccessibilityRequirements())
Expand Down
Loading

0 comments on commit 26113ee

Please sign in to comment.