Skip to content

Commit 6c31130

Browse files
committed
Fix image creation and clone
This patch fixed two issues in image creation and clone: - add forceCreate flag to avoid image creation failures when source disk is attached - fix the missing content source in CreateVolume response when restoring an image
1 parent 09ff4a0 commit 6c31130

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

pkg/gce-cloud-provider/compute/cloud-disk.go

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ func (d *CloudDisk) GetSourceDiskId() string {
188188
}
189189
}
190190

191+
func (d *CloudDisk) GetImageId() string {
192+
switch {
193+
case d.disk != nil:
194+
return d.disk.SourceImageId
195+
case d.betaDisk != nil:
196+
return d.betaDisk.SourceImageId
197+
default:
198+
return ""
199+
}
200+
}
201+
191202
func (d *CloudDisk) GetKMSKeyName() string {
192203
switch {
193204
case d.disk != nil:

pkg/gce-cloud-provider/compute/fake-gce.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,30 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string,
196196
}
197197

198198
computeDisk := &computev1.Disk{
199-
Name: volKey.Name,
200-
SizeGb: common.BytesToGbRoundUp(capBytes),
201-
Description: "Disk created by GCE-PD CSI Driver",
202-
Type: cloud.GetDiskTypeURI(project, volKey, params.DiskType),
203-
SourceSnapshotId: snapshotID,
204-
SourceDiskId: volumeContentSourceVolumeID,
205-
SourceImageId: snapshotID,
206-
Status: cloud.mockDiskStatus,
207-
Labels: params.Labels,
199+
Name: volKey.Name,
200+
SizeGb: common.BytesToGbRoundUp(capBytes),
201+
Description: "Disk created by GCE-PD CSI Driver",
202+
Type: cloud.GetDiskTypeURI(project, volKey, params.DiskType),
203+
SourceDiskId: volumeContentSourceVolumeID,
204+
Status: cloud.mockDiskStatus,
205+
Labels: params.Labels,
208206
}
207+
208+
if snapshotID != "" {
209+
_, snapshotType, _, err := common.SnapshotIDToProjectKey(snapshotID)
210+
if err != nil {
211+
return err
212+
}
213+
switch snapshotType {
214+
case common.DiskSnapshotType:
215+
computeDisk.SourceSnapshotId = snapshotID
216+
case common.DiskImageType:
217+
computeDisk.SourceImageId = snapshotID
218+
default:
219+
return fmt.Errorf("invalid snapshot type in snapshot ID: %s", snapshotType)
220+
}
221+
}
222+
209223
if params.DiskEncryptionKMSKey != "" {
210224
computeDisk.DiskEncryptionKey = &computev1.CustomerEncryptionKey{
211225
KmsKeyName: params.DiskEncryptionKMSKey,

pkg/gce-cloud-provider/compute/gce-compute.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ func (cloud *CloudProvider) CreateImage(ctx context.Context, project string, vol
871871
StorageLocations: snapshotParams.StorageLocations,
872872
}
873873

874-
_, err = cloud.service.Images.Insert(project, image).Context(ctx).Do()
874+
_, err = cloud.service.Images.Insert(project, image).Context(ctx).ForceCreate(true).Do()
875875
if err != nil {
876876
return nil, err
877877
}

pkg/gce-pd-csi-driver/controller.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -1476,8 +1476,9 @@ func generateCreateVolumeResponse(disk *gce.CloudDisk, zones []string) *csi.Crea
14761476
},
14771477
}
14781478
snapshotID := disk.GetSnapshotId()
1479+
imageID := disk.GetImageId()
14791480
diskID := disk.GetSourceDiskId()
1480-
if diskID != "" || snapshotID != "" {
1481+
if diskID != "" || snapshotID != "" || imageID != "" {
14811482
contentSource := &csi.VolumeContentSource{}
14821483
if snapshotID != "" {
14831484
contentSource = &csi.VolumeContentSource{
@@ -1497,6 +1498,15 @@ func generateCreateVolumeResponse(disk *gce.CloudDisk, zones []string) *csi.Crea
14971498
},
14981499
}
14991500
}
1501+
if imageID != "" {
1502+
contentSource = &csi.VolumeContentSource{
1503+
Type: &csi.VolumeContentSource_Snapshot{
1504+
Snapshot: &csi.VolumeContentSource_SnapshotSource{
1505+
SnapshotId: imageID,
1506+
},
1507+
},
1508+
}
1509+
}
15001510
createResp.Volume.ContentSource = contentSource
15011511
}
15021512
return createResp

0 commit comments

Comments
 (0)