Skip to content

Commit 8cfe048

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

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

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

+7
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,11 @@ 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+
// TODO: Implement
163+
return nil
164+
}
165+
159166
// Instance Methods
160167
func (cloud *FakeCloudProvider) InsertInstance(instance *compute.Instance, instanceName string) {
161168
cloud.instances[instanceName] = instance

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

+18
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,23 @@ 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+
for _, user := range disk.Users {
171+
if strings.Contains(user, instanceName) && strings.Contains(user, zone) {
172+
return true, nil
173+
}
174+
}
175+
return false, nil
176+
})
177+
}
178+
161179
func opIsDone(op *compute.Operation) bool {
162180
return op != nil && op.Status == "DONE"
163181
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ 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+
299301
glog.Infof("Disk %v attached to instance %v successfully", disk.Name, nodeID)
300302
return pubVolResp, nil
301303
}

0 commit comments

Comments
 (0)