Skip to content

Commit 0bf6df4

Browse files
committed
Return success in CreateVolume when disk is READY
1 parent 317f2a6 commit 0bf6df4

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ func (d *CloudDisk) GetKind() string {
9292
}
9393
}
9494

95+
func (d *CloudDisk) GetStatus() string {
96+
switch d.Type() {
97+
case Zonal:
98+
return d.ZonalDisk.Status
99+
case Regional:
100+
return d.RegionalDisk.Status
101+
default:
102+
return "Unknown"
103+
}
104+
}
105+
95106
// GetPDType returns the type of the PD as either 'pd-standard' or 'pd-ssd' The
96107
// "Type" field on the compute disk is stored as a url like
97108
// projects/project/zones/zone/diskTypes/pd-standard

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, volKey *meta.Key
250250
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
251251
SelfLink: fmt.Sprintf("projects/%s/zones/%s/disks/%s", cloud.project, volKey.Zone, volKey.Name),
252252
SourceSnapshotId: snapshotID,
253+
Status: "READY",
253254
}
254255
if params.DiskEncryptionKMSKey != "" {
255256
diskToCreateGA.DiskEncryptionKey = &computev1.CustomerEncryptionKey{
@@ -265,6 +266,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, volKey *meta.Key
265266
Type: cloud.GetDiskTypeURI(volKey, params.DiskType),
266267
SelfLink: fmt.Sprintf("projects/%s/regions/%s/disks/%s", cloud.project, volKey.Region, volKey.Name),
267268
SourceSnapshotId: snapshotID,
269+
Status: "READY",
268270
}
269271
if params.DiskEncryptionKMSKey != "" {
270272
diskToCreateV1.DiskEncryptionKey = &computev1.CustomerEncryptionKey{

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ const (
6262
replicationTypeRegionalPD = "regional-pd"
6363
)
6464

65+
func isDiskReady(disk *gce.CloudDisk) (bool, error) {
66+
status := disk.GetStatus()
67+
switch status {
68+
case "READY":
69+
return true, nil
70+
case "FAILED":
71+
return false, fmt.Errorf("Disk %s status is FAILED", disk.GetName())
72+
case "CREATING":
73+
klog.V(4).Infof("Disk %s status is CREATING", disk.GetName())
74+
return false, nil
75+
case "DELETING":
76+
klog.V(4).Infof("Disk %s status is DELETING", disk.GetName())
77+
return false, nil
78+
case "RESTORING":
79+
klog.V(4).Infof("Disk %s status is RESTORING", disk.GetName())
80+
return false, nil
81+
default:
82+
klog.V(4).Infof("Disk %s status is: %s", disk.GetName(), status)
83+
}
84+
85+
return false, nil
86+
}
87+
6588
func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) (*csi.CreateVolumeResponse, error) {
6689
var err error
6790
// Validate arguments
@@ -143,6 +166,16 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
143166
if err != nil {
144167
return nil, status.Error(codes.AlreadyExists, fmt.Sprintf("CreateVolume disk already exists with same name and is incompatible: %v", err))
145168
}
169+
170+
ready, err := isDiskReady(existingDisk)
171+
if err != nil {
172+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk %v had error checking ready status: %v", volKey, err))
173+
}
174+
175+
if !ready {
176+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk %v is not ready", volKey))
177+
}
178+
146179
// If there is no validation error, immediately return success
147180
klog.V(4).Infof("CreateVolume succeeded for disk %v, it already exists and was compatible", volKey)
148181
return generateCreateVolumeResponse(existingDisk, capBytes, zones), nil
@@ -187,6 +220,15 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
187220
default:
188221
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("CreateVolume replication type '%s' is not supported", params.ReplicationType))
189222
}
223+
224+
ready, err := isDiskReady(disk)
225+
if err != nil {
226+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk %v had error checking ready status: %v", volKey, err))
227+
}
228+
if !ready {
229+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk %v is not ready", volKey))
230+
}
231+
190232
klog.V(4).Infof("CreateVolume succeeded for disk %v", volKey)
191233
return generateCreateVolumeResponse(disk, capBytes, zones), nil
192234

0 commit comments

Comments
 (0)