@@ -127,23 +127,36 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
127127 // Since err is nil, it means the volume with the same name already exists
128128 // need to check if the size of existing volume is the same as in new
129129 // request
130- if exVol .VolSize >= int64 (req .GetCapacityRange ().GetRequiredBytes ()) {
131- // existing volume is compatible with new request and should be reused.
132- // TODO (sbezverk) Do I need to make sure that volume still exists?
133- return & csi.CreateVolumeResponse {
134- Volume : & csi.Volume {
135- VolumeId : exVol .VolID ,
136- CapacityBytes : int64 (exVol .VolSize ),
137- VolumeContext : req .GetParameters (),
138- ContentSource : req .GetVolumeContentSource (),
139- },
140- }, nil
130+ if exVol .VolSize < capacity {
131+ return nil , status .Errorf (codes .AlreadyExists , "Volume with the same name: %s but with different size already exist" , req .GetName ())
132+ }
133+ if req .GetVolumeContentSource () != nil {
134+ volumeSource := req .VolumeContentSource
135+ switch volumeSource .Type .(type ) {
136+ case * csi.VolumeContentSource_Snapshot :
137+ if volumeSource .GetSnapshot () != nil && exVol .ParentSnapID != volumeSource .GetSnapshot ().GetSnapshotId () {
138+ return nil , status .Error (codes .AlreadyExists , "existing volume source snapshot id not matching" )
139+ }
140+ case * csi.VolumeContentSource_Volume :
141+ if volumeSource .GetVolume () != nil && exVol .ParentVolID != volumeSource .GetVolume ().GetVolumeId () {
142+ return nil , status .Error (codes .AlreadyExists , "existing volume source volume id not matching" )
143+ }
144+ default :
145+ return nil , status .Errorf (codes .InvalidArgument , "%v not a proper volume source" , volumeSource )
146+ }
141147 }
142- return nil , status .Errorf (codes .AlreadyExists , "Volume with the same name: %s but with different size already exist" , req .GetName ())
148+ // TODO (sbezverk) Do I need to make sure that volume still exists?
149+ return & csi.CreateVolumeResponse {
150+ Volume : & csi.Volume {
151+ VolumeId : exVol .VolID ,
152+ CapacityBytes : int64 (exVol .VolSize ),
153+ VolumeContext : req .GetParameters (),
154+ ContentSource : req .GetVolumeContentSource (),
155+ },
156+ }, nil
143157 }
144158
145159 volumeID := uuid .NewUUID ().String ()
146- path := getVolumePath (volumeID )
147160
148161 vol , err := createHostpathVolume (volumeID , req .GetName (), capacity , requestedAccessType , false /* ephemeral */ )
149162 if err != nil {
@@ -152,12 +165,21 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
152165 glog .V (4 ).Infof ("created volume %s at path %s" , vol .VolID , vol .VolPath )
153166
154167 if req .GetVolumeContentSource () != nil {
155- contentSource := req .GetVolumeContentSource ()
156- if snapshot := contentSource .GetSnapshot (); snapshot != nil {
157- err = loadFromSnapshot (capacity , snapshot .GetSnapshotId (), path )
158- }
159- if srcVolume := contentSource .GetVolume (); srcVolume != nil {
160- err = loadFromVolume (capacity , srcVolume .GetVolumeId (), path )
168+ path := getVolumePath (volumeID )
169+ volumeSource := req .VolumeContentSource
170+ switch volumeSource .Type .(type ) {
171+ case * csi.VolumeContentSource_Snapshot :
172+ if snapshot := volumeSource .GetSnapshot (); snapshot != nil {
173+ err = loadFromSnapshot (capacity , snapshot .GetSnapshotId (), path )
174+ vol .ParentSnapID = snapshot .GetSnapshotId ()
175+ }
176+ case * csi.VolumeContentSource_Volume :
177+ if srcVolume := volumeSource .GetVolume (); srcVolume != nil {
178+ err = loadFromVolume (capacity , srcVolume .GetVolumeId (), path )
179+ vol .ParentVolID = srcVolume .GetVolumeId ()
180+ }
181+ default :
182+ err = status .Errorf (codes .InvalidArgument , "%v not a proper volume source" , volumeSource )
161183 }
162184 if err != nil {
163185 if delErr := deleteHostpathVolume (volumeID ); delErr != nil {
0 commit comments