Skip to content

Commit aaebd25

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 b5003f2 commit aaebd25

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
@@ -1480,8 +1480,9 @@ func generateCreateVolumeResponse(disk *gce.CloudDisk, zones []string) *csi.Crea
14801480
},
14811481
}
14821482
snapshotID := disk.GetSnapshotId()
1483+
imageID := disk.GetImageId()
14831484
diskID := disk.GetSourceDiskId()
1484-
if diskID != "" || snapshotID != "" {
1485+
if diskID != "" || snapshotID != "" || imageID != "" {
14851486
contentSource := &csi.VolumeContentSource{}
14861487
if snapshotID != "" {
14871488
contentSource = &csi.VolumeContentSource{
@@ -1501,6 +1502,15 @@ func generateCreateVolumeResponse(disk *gce.CloudDisk, zones []string) *csi.Crea
15011502
},
15021503
}
15031504
}
1505+
if imageID != "" {
1506+
contentSource = &csi.VolumeContentSource{
1507+
Type: &csi.VolumeContentSource_Snapshot{
1508+
Snapshot: &csi.VolumeContentSource_SnapshotSource{
1509+
SnapshotId: imageID,
1510+
},
1511+
},
1512+
}
1513+
}
15041514
createResp.Volume.ContentSource = contentSource
15051515
}
15061516
return createResp

0 commit comments

Comments
 (0)