Skip to content

Commit cf2f3c4

Browse files
committed
Added a waitforattach verification at the end of ControllerPublishVolume
1 parent 9af6079 commit cf2f3c4

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type FakeCloudProvider struct {
3434
instances map[string]*compute.Instance
3535
}
3636

37+
var _ GCECompute = &FakeCloudProvider{}
38+
3739
func FakeCreateCloudProvider(project, zone string) (*FakeCloudProvider, error) {
3840
return &FakeCloudProvider{
3941
project: project,
@@ -156,6 +158,10 @@ func (cloud *FakeCloudProvider) GetDiskTypeURI(zone, diskType string) string {
156158
return ""
157159
}
158160

161+
func (cloud *FakeCloudProvider) WaitForAttach(ctx context.Context, zone, diskName, instanceName string) error {
162+
return nil
163+
}
164+
159165
// Instance Methods
160166
func (cloud *FakeCloudProvider) InsertInstance(instance *compute.Instance, instanceName string) {
161167
cloud.instances[instanceName] = instance

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

+22
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type GCECompute interface {
4141
DetachDisk(ctx context.Context, volumeZone, instanceName, volumeName string) (*compute.Operation, error)
4242
GetDiskSourceURI(disk *compute.Disk, zone string) string
4343
GetDiskTypeURI(zone, diskType string) string
44+
WaitForAttach(ctx context.Context, zone, diskName, instanceName string) error
4445
// Instance Methods
4546
GetInstanceOrError(ctx context.Context, instanceZone, instanceName string) (*compute.Instance, error)
4647
// Operation Methods
@@ -158,6 +159,27 @@ func (cloud *CloudProvider) WaitForOp(ctx context.Context, op *compute.Operation
158159
})
159160
}
160161

162+
func (cloud *CloudProvider) WaitForAttach(ctx context.Context, zone, diskName, instanceName string) error {
163+
return wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
164+
disk, err := cloud.GetDiskOrError(ctx, zone, diskName)
165+
if err != nil {
166+
glog.Errorf("GetDiskOrError failed to get disk: %v", err)
167+
return false, err
168+
}
169+
170+
if disk == nil {
171+
return false, fmt.Errorf("Disk %v could not be found in zone %v", diskName, zone)
172+
}
173+
174+
for _, user := range disk.Users {
175+
if strings.Contains(user, instanceName) && strings.Contains(user, zone) {
176+
return true, nil
177+
}
178+
}
179+
return false, nil
180+
})
181+
}
182+
161183
func opIsDone(op *compute.Operation) bool {
162184
return op != nil && op.Status == "DONE"
163185
}

pkg/gce-cloud-provider/gce.go

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type CloudProvider struct {
4444
zone string
4545
}
4646

47+
var _ GCECompute = &CloudProvider{}
48+
4749
func CreateCloudProvider() (*CloudProvider, error) {
4850
svc, err := createCloudService()
4951
if err != nil {

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

+5
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ func (gceCS *GCEControllerServer) ControllerPublishVolume(ctx context.Context, r
296296
return nil, status.Error(codes.Internal, fmt.Sprintf("unknown Attach operation error: %v", err))
297297
}
298298

299+
err = gceCS.CloudProvider.WaitForAttach(ctx, volumeZone, disk.Name, nodeID)
300+
if err != nil {
301+
return nil, status.Error(codes.Internal, fmt.Sprintf("unknown WaitForAttach error: %v", err))
302+
}
303+
299304
glog.Infof("Disk %v attached to instance %v successfully", disk.Name, nodeID)
300305
return pubVolResp, nil
301306
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ func TestCreateVolumeArguments(t *testing.T) {
255255
}
256256

257257
// Test volume already exists
258+
258259
// Test volume with op pending
259260

260261
// Test DeleteVolume

0 commit comments

Comments
 (0)