Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
chore(controller): improve idempotency
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Apr 1, 2021
1 parent 8beefd3 commit be99b28
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
hostMapDoesNotExistsErrorCode = -10074
unmapFailedErrorCode = -10509
volumeNotFoundErrorCode = -10075
snapshotNotFoundErrorCode = -10050
snapshotAlreadyExists = -10186
)

var volumeCapabilities = []*csi.VolumeCapability{
Expand Down
17 changes: 14 additions & 3 deletions pkg/controller/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import (
"github.com/enix/dothill-api-go"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"
)

// CreateSnapshot creates a snapshot of the given volume
func (controller *Controller) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequest) (*csi.CreateSnapshotResponse, error) {
name := strings.Replace(req.Name[9:], "-", "", -1)

_, status, err := controller.dothillClient.CreateSnapshot(req.SourceVolumeId, name)
if err != nil && status.ReturnCode != -10186 {
_, respStatus, err := controller.dothillClient.CreateSnapshot(req.SourceVolumeId, name)
if err != nil && respStatus.ReturnCode != snapshotAlreadyExists {
return nil, err
}

Expand All @@ -44,13 +47,21 @@ func (controller *Controller) CreateSnapshot(ctx context.Context, req *csi.Creat
return nil, errors.New("snapshot not found")
}

if snapshot.SourceVolumeId != req.SourceVolumeId {
return nil, status.Error(codes.AlreadyExists, "cannot validate volume with empty ID")
}

return &csi.CreateSnapshotResponse{Snapshot: snapshot}, nil
}

// DeleteSnapshot deletes a snapshot of the given volume
func (controller *Controller) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
_, status, err := controller.dothillClient.DeleteSnapshot(req.SnapshotId)
if err != nil && status.ReturnCode != -10050 {
if err != nil {
if status != nil && status.ReturnCode == snapshotNotFoundErrorCode {
klog.Infof("snapshot %s does not exist, assuming it has already been deleted", req.SnapshotId)
return &csi.DeleteSnapshotResponse{}, nil
}
return nil, err
}
return &csi.DeleteSnapshotResponse{}, nil
Expand Down

0 comments on commit be99b28

Please sign in to comment.