Skip to content

Commit 308018b

Browse files
committed
Fix failing createSnapshot sanity checks
1 parent b448dd0 commit 308018b

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

Diff for: pkg/driver/controller_server.go

+32-27
Original file line numberDiff line numberDiff line change
@@ -647,28 +647,23 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
647647
continue
648648
}
649649
if snapshot.VolumeID == sourceVolID {
650-
creationTime, err := ParseTimeToProtoTimestamp(snapshot.CreationTime)
651-
if err != nil {
652-
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to parse creation time: %v", err))
650+
snap, err := toCSISnapshot(&snapshot)
651+
if err != nil{
652+
log.Error().
653+
Str("snapshot_name", snapshotName).
654+
Str("source_volume_id", sourceVolID).
655+
Err(err).
656+
Msg("filed to convert civo snapshot to csi snapshot")
657+
return nil, status.Errorf(codes.Internal, "filed to convert civo snapshot %s to csi snapshot: %v", snapshot.SnapshotID, err)
653658
}
654-
655-
isReady := isSnapshotReady(snapshot.State)
656-
657659
return &csi.CreateSnapshotResponse{
658-
Snapshot: &csi.Snapshot{
659-
SnapshotId: snapshot.SnapshotID,
660-
SourceVolumeId: snapshot.VolumeID,
661-
CreationTime: creationTime,
662-
SizeBytes: int64(snapshot.RestoreSize),
663-
ReadyToUse: isReady,
664-
},
660+
Snapshot: snap,
665661
}, nil
666662
}
667663
log.Error().
668664
Str("snapshot_name", snapshotName).
669665
Str("requested_source_volume_id", sourceVolID).
670666
Str("actual_source_volume_id", snapshot.VolumeID).
671-
Err(err).
672667
Msg("Snapshot with the same name but with different SourceVolumeId already exist")
673668
return nil, status.Errorf(codes.AlreadyExists, "snapshot with the same name %q but with different SourceVolumeId already exist", snapshotName)
674669
}
@@ -933,22 +928,13 @@ func getVolSizeInBytes(capRange *csi.CapacityRange) (int64, error) {
933928

934929
// convertSnapshot function converts a civogo.Snapshot object(API response) into a CSI ListSnapshotsResponse_Entry
935930
func convertSnapshot(in *civogo.VolumeSnapshot) (*csi.ListSnapshotsResponse_Entry, error) {
936-
creationTime, err := ParseTimeToProtoTimestamp(in.CreationTime)
937-
if err != nil {
938-
return nil, fmt.Errorf("failed to parse creation time for snapshot %s: %w", in.SnapshotID, err)
931+
snap, err := toCSISnapshot(in)
932+
if err != nil{
933+
return nil, fmt.Errorf("filed to convert civo snapshot %s to csi snapshot: %v", in.SnapshotID, err)
939934
}
940935

941-
// Explicitly define which state indicates the snapshot is ready for use
942-
isReady := isSnapshotReady(in.State)
943-
944936
return &csi.ListSnapshotsResponse_Entry{
945-
Snapshot: &csi.Snapshot{
946-
SnapshotId: in.SnapshotID,
947-
SourceVolumeId: in.VolumeID,
948-
CreationTime: creationTime,
949-
SizeBytes: int64(in.RestoreSize),
950-
ReadyToUse: isReady,
951-
},
937+
Snapshot: snap,
952938
}, nil
953939
}
954940

@@ -970,3 +956,22 @@ func isSnapshotReady(state string) bool {
970956
}
971957
return readyStates[state]
972958
}
959+
960+
961+
func toCSISnapshot(snap *civogo.VolumeSnapshot)(*csi.Snapshot, error){
962+
creationTime, err := ParseTimeToProtoTimestamp(snap.CreationTime)
963+
if err != nil {
964+
return nil, fmt.Errorf("failed to parse creation time for snapshot %s: %w", snap.SnapshotID, err)
965+
}
966+
967+
// Explicitly define which state indicates the snapshot is ready for use
968+
isReady := isSnapshotReady(snap.State)
969+
970+
return &csi.Snapshot{
971+
SnapshotId: snap.SnapshotID,
972+
SourceVolumeId: snap.VolumeID,
973+
CreationTime: creationTime,
974+
SizeBytes: int64(snap.RestoreSize),
975+
ReadyToUse: isReady,
976+
}, nil
977+
}

0 commit comments

Comments
 (0)