Skip to content

Commit

Permalink
CreateSnapshot changes based on new CSI Spec snapshot.status definition
Browse files Browse the repository at this point in the history
  • Loading branch information
davidz627 committed Nov 14, 2018
1 parent 91ff956 commit 851dde2
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions pkg/gce-pd-csi-driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,23 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volumeID, err))
}

snapshot, err := gceCS.CloudProvider.CreateSnapshot(ctx, volKey, req.Name)
// Check if snapshot already exists
var snapshot *compute.Snapshot
snapshot, err = gceCS.CloudProvider.GetSnapshot(ctx, req.Name)
if err != nil {
if gce.IsGCEError(err, "notFound") {
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volKey.String(), err))
if !gce.IsGCEError(err, "notFound") {
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown get snapshot error: %v", err))
}
// If we could not find the snapshot, we create a new one
snapshot, err = gceCS.CloudProvider.CreateSnapshot(ctx, volKey, req.Name)
if err != nil {
if gce.IsGCEError(err, "notFound") {
return nil, status.Error(codes.NotFound, fmt.Sprintf("Could not find volume with ID %v: %v", volKey.String(), err))
}
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown create snapshot error: %v", err))
}
return nil, status.Error(codes.Internal, fmt.Sprintf("Unknown create snapshot error: %v", err))
}

err = gceCS.validateExistingSnapshot(snapshot, volKey)
if err != nil {
return nil, status.Error(codes.AlreadyExists, fmt.Sprintf("Error in creating snapshot: %v", err))
Expand All @@ -495,13 +505,18 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
return nil, status.Error(codes.Internal, fmt.Sprintf("Failed to covert creation timestamp: %v", err))
}

ready, err := isCSISnapshotReady(snapshot.Status)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("Snapshot had error checking ready status: %v", err))
}

createResp := &csi.CreateSnapshotResponse{
Snapshot: &csi.Snapshot{
SizeBytes: common.GbToBytes(snapshot.DiskSizeGb),
SnapshotId: cleanSelfLink(snapshot.SelfLink),
SourceVolumeId: volumeID,
CreationTime: tp,
ReadyToUse: isCSISnapshotReady(snapshot.Status),
ReadyToUse: ready,
},
}
return createResp, nil
Expand All @@ -525,22 +540,18 @@ func (gceCS *GCEControllerServer) validateExistingSnapshot(snapshot *compute.Sna
return nil
}

func isCSISnapshotReady(status string) bool {
var csiStatus bool
func isCSISnapshotReady(status string) (bool, error) {
switch status {
case "READY":
csiStatus = true
case "UPLOADING":
csiStatus = false
return true, nil
case "FAILED":
csiStatus = false
return false, fmt.Errorf("snapshot status is FAILED")
case "DELETING":
csiStatus = false
glog.V(4).Infof("snapshot is in DELETING")
fallthrough
default:
csiStatus = false
return false, nil
}
return csiStatus
}

func (gceCS *GCEControllerServer) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequest) (*csi.DeleteSnapshotResponse, error) {
Expand Down Expand Up @@ -649,13 +660,18 @@ func generateSnapshotEntry(snapshot *compute.Snapshot) (*csi.ListSnapshotsRespon
return nil, fmt.Errorf("Failed to covert creation timestamp: %v", err)
}

// We ignore the error intentionally here since we are just listing snapshots
// TODO: If the snapshot is in "FAILED" state we need to think through what this
// should actually look like.
ready, _ := isCSISnapshotReady(snapshot.Status)

entry := &csi.ListSnapshotsResponse_Entry{
Snapshot: &csi.Snapshot{
SizeBytes: common.GbToBytes(snapshot.DiskSizeGb),
SnapshotId: cleanSelfLink(snapshot.SelfLink),
SourceVolumeId: cleanSelfLink(snapshot.SourceDisk),
CreationTime: tp,
ReadyToUse: isCSISnapshotReady(snapshot.Status),
ReadyToUse: ready,
},
}
return entry, nil
Expand Down

0 comments on commit 851dde2

Please sign in to comment.