@@ -99,7 +99,7 @@ type GCECompute interface {
99
99
DetachDisk (ctx context.Context , project , deviceName , instanceZone , instanceName string ) error
100
100
GetDiskSourceURI (project string , volKey * meta.Key ) string
101
101
GetDiskTypeURI (project string , volKey * meta.Key , diskType string ) string
102
- WaitForAttach (ctx context.Context , project string , volKey * meta.Key , instanceZone , instanceName string ) error
102
+ WaitForAttach (ctx context.Context , project string , volKey * meta.Key , diskType , instanceZone , instanceName string ) error
103
103
ResizeDisk (ctx context.Context , project string , volKey * meta.Key , requestBytes int64 ) (int64 , error )
104
104
ListDisks (ctx context.Context ) ([]* computev1.Disk , string , error )
105
105
// Regional Disk Methods
@@ -924,18 +924,46 @@ func (cloud *CloudProvider) waitForGlobalOp(ctx context.Context, project, opName
924
924
})
925
925
}
926
926
927
- func (cloud * CloudProvider ) WaitForAttach (ctx context.Context , project string , volKey * meta.Key , instanceZone , instanceName string ) error {
927
+ func (cloud * CloudProvider ) waitForAttachOnInstance (ctx context.Context , project string , volKey * meta.Key , instanceZone , instanceName string ) error {
928
928
klog .V (5 ).Infof ("Waiting for attach of disk %v to instance %v to complete..." , volKey .Name , instanceName )
929
929
start := time .Now ()
930
930
return wait .ExponentialBackoff (AttachDiskBackoff , func () (bool , error ) {
931
- klog .V (6 ).Infof ("Polling for attach of disk %v to instance %v to complete for %v" , volKey .Name , instanceName , time .Since (start ))
931
+ klog .V (6 ).Infof ("Polling instances.get for attach of disk %v to instance %v to complete for %v" , volKey .Name , instanceName , time .Since (start ))
932
+ instance , err := cloud .GetInstanceOrError (ctx , instanceZone , instanceName )
933
+ if err != nil {
934
+ return false , fmt .Errorf ("GetInstance failed to get instance: %w" , err )
935
+ }
936
+
937
+ if instance == nil {
938
+ return false , fmt .Errorf ("instance %v could not be found" , instanceName )
939
+ }
940
+
941
+ for _ , disk := range instance .Disks {
942
+ deviceName , err := common .GetDeviceName (volKey )
943
+ if err != nil {
944
+ return false , fmt .Errorf ("failed to get disk device name for %s: %w" , volKey , err )
945
+ }
946
+
947
+ if deviceName == disk .DeviceName {
948
+ return true , nil
949
+ }
950
+ }
951
+ return false , nil
952
+ })
953
+ }
954
+
955
+ func (cloud * CloudProvider ) waitForAttachOnDisk (ctx context.Context , project string , volKey * meta.Key , instanceZone , instanceName string ) error {
956
+ klog .V (5 ).Infof ("Waiting for attach of disk %v to instance %v to complete..." , volKey .Name , instanceName )
957
+ start := time .Now ()
958
+ return wait .ExponentialBackoff (AttachDiskBackoff , func () (bool , error ) {
959
+ klog .V (6 ).Infof ("Polling disks.get for attach of disk %v to instance %v to complete for %v" , volKey .Name , instanceName , time .Since (start ))
932
960
disk , err := cloud .GetDisk (ctx , project , volKey , GCEAPIVersionV1 )
933
961
if err != nil {
934
962
return false , fmt .Errorf ("GetDisk failed to get disk: %w" , err )
935
963
}
936
964
937
965
if disk == nil {
938
- return false , fmt .Errorf ("Disk %v could not be found" , volKey .Name )
966
+ return false , fmt .Errorf ("disk %v could not be found" , volKey .Name )
939
967
}
940
968
941
969
for _ , user := range disk .GetUsers () {
@@ -947,6 +975,14 @@ func (cloud *CloudProvider) WaitForAttach(ctx context.Context, project string, v
947
975
})
948
976
}
949
977
978
+ func (cloud * CloudProvider ) WaitForAttach (ctx context.Context , project string , volKey * meta.Key , diskType , instanceZone , instanceName string ) error {
979
+ if cloud .waitForAttachConfig .ShouldUseGetInstanceAPI (diskType ) {
980
+ return cloud .waitForAttachOnInstance (ctx , project , volKey , instanceZone , instanceName )
981
+ } else {
982
+ return cloud .waitForAttachOnDisk (ctx , project , volKey , instanceZone , instanceName )
983
+ }
984
+ }
985
+
950
986
func wrapOpErr (name string , opErr * computev1.OperationErrorErrors ) error {
951
987
if opErr == nil {
952
988
return nil
0 commit comments