@@ -145,7 +145,15 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
145
145
return nil , status .Error (codes .AlreadyExists , fmt .Sprintf ("CreateVolume disk already exists with same name and is incompatible: %v" , err ))
146
146
}
147
147
// If there is no validation error, immediately return success
148
- return generateCreateVolumeResponse (existingDisk .GetSelfLink (), capBytes , zones ), nil
148
+ return generateCreateVolumeResponse (existingDisk , capBytes , zones ), nil
149
+ }
150
+
151
+ snapshotId := ""
152
+ content := req .GetVolumeContentSource ()
153
+ if content != nil {
154
+ if content .GetSnapshot () != nil {
155
+ snapshotId = content .GetSnapshot ().GetId ()
156
+ }
149
157
}
150
158
151
159
// Create the disk
@@ -155,23 +163,22 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
155
163
if len (zones ) != 1 {
156
164
return nil , status .Errorf (codes .Internal , fmt .Sprintf ("CreateVolume failed to get a single zone for creating zonal disk, instead got: %v" , zones ))
157
165
}
158
- disk , err = createSingleZoneDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes )
166
+ disk , err = createSingleZoneDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes , snapshotId )
159
167
if err != nil {
160
168
return nil , status .Error (codes .Internal , fmt .Sprintf ("CreateVolume failed to create single zonal disk %#v: %v" , name , err ))
161
169
}
162
170
case replicationTypeRegionalPD :
163
171
if len (zones ) != 2 {
164
172
return nil , status .Errorf (codes .Internal , fmt .Sprintf ("CreateVolume failed to get a 2 zones for creating regional disk, instead got: %v" , zones ))
165
173
}
166
- disk , err = createRegionalDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes )
174
+ disk , err = createRegionalDisk (ctx , gceCS .CloudProvider , name , zones , diskType , capacityRange , capBytes , snapshotId )
167
175
if err != nil {
168
176
return nil , status .Error (codes .Internal , fmt .Sprintf ("CreateVolume failed to create regional disk %#v: %v" , name , err ))
169
177
}
170
178
default :
171
179
return nil , status .Error (codes .InvalidArgument , fmt .Sprintf ("CreateVolume replication type '%s' is not supported" , replicationType ))
172
180
}
173
-
174
- return generateCreateVolumeResponse (disk .GetSelfLink (), capBytes , zones ), nil
181
+ return generateCreateVolumeResponse (disk , capBytes , zones ), nil
175
182
176
183
}
177
184
@@ -798,7 +805,7 @@ func getDefaultZonesInRegion(gceCS *GCEControllerServer, existingZones []string,
798
805
return ret , nil
799
806
}
800
807
801
- func generateCreateVolumeResponse (selfLink string , capBytes int64 , zones []string ) * csi.CreateVolumeResponse {
808
+ func generateCreateVolumeResponse (disk * gce. CloudDisk , capBytes int64 , zones []string ) * csi.CreateVolumeResponse {
802
809
tops := []* csi.Topology {}
803
810
for _ , zone := range zones {
804
811
tops = append (tops , & csi.Topology {
@@ -808,11 +815,23 @@ func generateCreateVolumeResponse(selfLink string, capBytes int64, zones []strin
808
815
createResp := & csi.CreateVolumeResponse {
809
816
Volume : & csi.Volume {
810
817
CapacityBytes : capBytes ,
811
- Id : cleanSelfLink (selfLink ),
818
+ Id : cleanSelfLink (disk . GetSelfLink () ),
812
819
Attributes : nil ,
813
820
AccessibleTopology : tops ,
814
821
},
815
822
}
823
+ snapshotId := disk .GetSnapshotId ()
824
+ if snapshotId != "" {
825
+ source := & csi.VolumeContentSource {
826
+ Type : & csi.VolumeContentSource_Snapshot {
827
+ Snapshot : & csi.VolumeContentSource_SnapshotSource {
828
+ Id : snapshotId ,
829
+ },
830
+ },
831
+ }
832
+ createResp .Volume .ContentSource = source
833
+
834
+ }
816
835
return createResp
817
836
}
818
837
@@ -821,7 +840,7 @@ func cleanSelfLink(selfLink string) string {
821
840
return strings .TrimPrefix (temp , gce .GCEComputeBetaAPIEndpoint )
822
841
}
823
842
824
- func createRegionalDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 ) (* gce.CloudDisk , error ) {
843
+ func createRegionalDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 , snapshotId string ) (* gce.CloudDisk , error ) {
825
844
region , err := common .GetRegionFromZones (zones )
826
845
if err != nil {
827
846
return nil , fmt .Errorf ("failed to get region from zones: %v" , err )
@@ -833,7 +852,7 @@ func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name
833
852
fullyQualifiedReplicaZones , cloudProvider .GetReplicaZoneURI (replicaZone ))
834
853
}
835
854
836
- err = cloudProvider .InsertDisk (ctx , meta .RegionalKey (name , region ), diskType , capBytes , capacityRange , fullyQualifiedReplicaZones )
855
+ err = cloudProvider .InsertDisk (ctx , meta .RegionalKey (name , region ), diskType , capBytes , capacityRange , fullyQualifiedReplicaZones , snapshotId )
837
856
if err != nil {
838
857
return nil , fmt .Errorf ("failed to insert regional disk: %v" , err )
839
858
}
@@ -847,12 +866,12 @@ func createRegionalDisk(ctx context.Context, cloudProvider gce.GCECompute, name
847
866
return disk , nil
848
867
}
849
868
850
- func createSingleZoneDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 ) (* gce.CloudDisk , error ) {
869
+ func createSingleZoneDisk (ctx context.Context , cloudProvider gce.GCECompute , name string , zones []string , diskType string , capacityRange * csi.CapacityRange , capBytes int64 , snapshotId string ) (* gce.CloudDisk , error ) {
851
870
if len (zones ) != 1 {
852
871
return nil , fmt .Errorf ("got wrong number of zones for zonal create volume: %v" , len (zones ))
853
872
}
854
873
diskZone := zones [0 ]
855
- err := cloudProvider .InsertDisk (ctx , meta .ZonalKey (name , diskZone ), diskType , capBytes , capacityRange , nil )
874
+ err := cloudProvider .InsertDisk (ctx , meta .ZonalKey (name , diskZone ), diskType , capBytes , capacityRange , nil , snapshotId )
856
875
if err != nil {
857
876
return nil , fmt .Errorf ("failed to insert zonal disk: %v" , err )
858
877
}
0 commit comments