@@ -65,19 +65,21 @@ const (
6565 DiskMultiAttachEnabled = "Enabled"
6666)
6767
68- type DiskAttachDetach struct {
68+ type DiskCloud struct {
6969 slots AttachDetachSlots
7070 waiter waitstatus.StatusWaiter [ecs.Disk ]
7171 batcher batcher.Batcher [ecs.Disk ]
7272
7373 attachThrottler * throttle.Throttler
7474 detachThrottler * throttle.Throttler
75+ createThrottler * throttle.Throttler
76+ deleteThrottler * throttle.Throttler
7577 detaching sync.Map
7678
7779 dev * DeviceManager
7880}
7981
80- func (ad * DiskAttachDetach ) possibleDisks (before sets.Set [string ]) ([]string , error ) {
82+ func (ad * DiskCloud ) possibleDisks (before sets.Set [string ]) ([]string , error ) {
8183 after , err := ad .dev .ListBlocks ()
8284 if err != nil {
8385 return nil , fmt .Errorf ("cannot list devices after attach: %w" , err )
@@ -96,7 +98,7 @@ func (ad *DiskAttachDetach) possibleDisks(before sets.Set[string]) ([]string, er
9698 return disks , nil
9799}
98100
99- func (ad * DiskAttachDetach ) findDevice (ctx context.Context , diskID , serial string , before sets.Set [string ]) (string , error ) {
101+ func (ad * DiskCloud ) findDevice (ctx context.Context , diskID , serial string , before sets.Set [string ]) (string , error ) {
100102 logger := klog .FromContext (ctx )
101103 var bdf , device string
102104 var err error
@@ -160,7 +162,7 @@ func (ad *DiskAttachDetach) findDevice(ctx context.Context, diskID, serial strin
160162
161163// Attach Alibaba Cloud disk.
162164// Returns device path if fromNode, disk serial number otherwise.
163- func (ad * DiskAttachDetach ) attachDisk (ctx context.Context , diskID , nodeID string , fromNode bool ) (string , error ) {
165+ func (ad * DiskCloud ) attachDisk (ctx context.Context , diskID , nodeID string , fromNode bool ) (string , error ) {
164166 logger := klog .FromContext (ctx )
165167 logger .V (2 ).Info ("Starting Do AttachDisk" , "instanceID" , nodeID , "region" , GlobalConfigVar .Region )
166168
@@ -294,11 +296,7 @@ func (ad *DiskAttachDetach) attachDisk(ctx context.Context, diskID, nodeID strin
294296 for key , value := range GlobalConfigVar .RequestBaseInfo {
295297 attachRequest .AppendUserAgent (key , value )
296298 }
297- var response * ecs.AttachDiskResponse
298- err = ad .attachThrottler .Throttle (ctx , func () error {
299- response , err = ecsClient .AttachDisk (attachRequest )
300- return err
301- })
299+ response , err := throttle .Throttled (ad .attachThrottler , ecsClient .AttachDisk )(ctx , attachRequest )
302300 if err != nil {
303301 var aliErr * alicloudErr.ServerError
304302 if errors .As (err , & aliErr ) {
@@ -336,7 +334,7 @@ func (ad *DiskAttachDetach) attachDisk(ctx context.Context, diskID, nodeID strin
336334}
337335
338336// Only called by controller
339- func (ad * DiskAttachDetach ) attachMultiAttachDisk (ctx context.Context , diskID , nodeID string ) (string , error ) {
337+ func (ad * DiskCloud ) attachMultiAttachDisk (ctx context.Context , diskID , nodeID string ) (string , error ) {
340338 klog .Infof ("AttachDisk: Starting Do AttachMultiAttachDisk: DiskId: %s, InstanceId: %s, Region: %v" , diskID , nodeID , GlobalConfigVar .Region )
341339
342340 ecsClient := GlobalConfigVar .EcsClient
@@ -383,7 +381,7 @@ func (ad *DiskAttachDetach) attachMultiAttachDisk(ctx context.Context, diskID, n
383381 return "" , nil
384382}
385383
386- func (ad * DiskAttachDetach ) detachMultiAttachDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskID , nodeID string ) (isMultiAttach bool , err error ) {
384+ func (ad * DiskCloud ) detachMultiAttachDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskID , nodeID string ) (isMultiAttach bool , err error ) {
387385 disk , err := ad .findDiskByID (ctx , diskID )
388386 if err != nil {
389387 klog .Errorf ("DetachMultiAttachDisk: Describe volume: %s from node: %s, with error: %s" , diskID , nodeID , err .Error ())
@@ -420,7 +418,7 @@ func (ad *DiskAttachDetach) detachMultiAttachDisk(ctx context.Context, ecsClient
420418 return true , nil
421419}
422420
423- func (ad * DiskAttachDetach ) detachDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskID , nodeID string , fromNode bool ) (err error ) {
421+ func (ad * DiskCloud ) detachDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskID , nodeID string , fromNode bool ) (err error ) {
424422 disk , err := ad .findDiskByID (ctx , diskID )
425423 if err != nil {
426424 klog .Errorf ("DetachDisk: Describe volume: %s from node: %s, with error: %s" , diskID , nodeID , err .Error ())
@@ -469,11 +467,7 @@ func (ad *DiskAttachDetach) detachDisk(ctx context.Context, ecsClient cloud.ECSI
469467 for key , value := range GlobalConfigVar .RequestBaseInfo {
470468 detachDiskRequest .AppendUserAgent (key , value )
471469 }
472- var response * ecs.DetachDiskResponse
473- err = ad .detachThrottler .Throttle (ctx , func () error {
474- response , err = ecsClient .DetachDisk (detachDiskRequest )
475- return err
476- })
470+ response , err := throttle .Throttled (ad .detachThrottler , ecsClient .DetachDisk )(ctx , detachDiskRequest )
477471 if err != nil {
478472 return status .Errorf (codes .Aborted , "DetachDisk: Fail to detach %s: from Instance: %s with error: %v" , disk .DiskId , disk .InstanceId , err )
479473 }
@@ -576,7 +570,7 @@ func tagDiskAsK8sAttached(diskID string, ecsClient *ecs.Client) {
576570 klog .Infof ("tagDiskAsK8sAttached:: add tag to disk: %s" , diskID )
577571}
578572
579- func (ad * DiskAttachDetach ) waitForDiskAttached (ctx context.Context , diskID , nodeID string ) error {
573+ func (ad * DiskCloud ) waitForDiskAttached (ctx context.Context , diskID , nodeID string ) error {
580574 disk , err := ad .waiter .WaitFor (ctx , diskID , func (disk * ecs.Disk ) bool {
581575 return waitstatus .IsInstanceAttached (disk , nodeID )
582576 })
@@ -589,7 +583,7 @@ func (ad *DiskAttachDetach) waitForDiskAttached(ctx context.Context, diskID, nod
589583 return nil
590584}
591585
592- func (ad * DiskAttachDetach ) waitForDiskDetached (ctx context.Context , diskID , nodeID string ) error {
586+ func (ad * DiskCloud ) waitForDiskDetached (ctx context.Context , diskID , nodeID string ) error {
593587 disk , err := ad .waiter .WaitFor (ctx , diskID , func (disk * ecs.Disk ) bool {
594588 return ! waitstatus .IsInstanceAttached (disk , nodeID )
595589 })
@@ -603,7 +597,7 @@ func (ad *DiskAttachDetach) waitForDiskDetached(ctx context.Context, diskID, nod
603597 return nil
604598}
605599
606- func (ad * DiskAttachDetach ) findDiskByID (ctx context.Context , diskID string ) (* ecs.Disk , error ) {
600+ func (ad * DiskCloud ) findDiskByID (ctx context.Context , diskID string ) (* ecs.Disk , error ) {
607601 return ad .batcher .Describe (ctx , diskID )
608602}
609603
@@ -931,7 +925,7 @@ func isValidSnapshotName(name string) bool {
931925 return validDiskNameRegexp .MatchString (name )
932926}
933927
934- func createDisk (ecsClient cloud.ECSInterface , diskName , snapshotID string , diskVol * diskVolumeArgs , supportedTypes sets.Set [Category ], selectedInstance string , isVirtualNode bool ) (string , createAttempt , error ) {
928+ func ( c * DiskCloud ) createDisk (ctx context. Context , ecsClient cloud.ECSInterface , diskName , snapshotID string , diskVol * diskVolumeArgs , supportedTypes sets.Set [Category ], selectedInstance string , isVirtualNode bool ) (string , createAttempt , error ) {
935929 // 需要配置external-provisioner启动参数--extra-create-metadata=true,然后ACK的external-provisioner才会将PVC的Annotations传过来
936930 createDiskRequest := buildCreateDiskRequest (diskVol )
937931 if isValidDiskName (diskName ) {
@@ -966,7 +960,7 @@ func createDisk(ecsClient cloud.ECSInterface, diskName, snapshotID string, diskV
966960 attempt .Instance = selectedInstance
967961 }
968962 retry:
969- diskID , final , err := createDiskAttempt (createDiskRequest , attempt , ecsClient )
963+ diskID , final , err := c . createDiskAttempt (ctx , createDiskRequest , attempt , ecsClient )
970964 if err != nil {
971965 if final {
972966 return "" , attempt , err
@@ -1055,11 +1049,11 @@ func finalizeCreateDiskRequest(template *ecs.CreateDiskRequest, attempt createAt
10551049
10561050var ErrParameterMismatch = errors .New ("parameter mismatch" )
10571051
1058- func createDiskAttempt (req * ecs.CreateDiskRequest , attempt createAttempt , ecsClient cloud.ECSInterface ) (diskId string , final bool , err error ) {
1052+ func ( c * DiskCloud ) createDiskAttempt (ctx context. Context , req * ecs.CreateDiskRequest , attempt createAttempt , ecsClient cloud.ECSInterface ) (diskId string , final bool , err error ) {
10591053 req = finalizeCreateDiskRequest (req , attempt )
10601054 klog .Infof ("request: request content: %++v" , req )
10611055
1062- volumeRes , err := ecsClient .CreateDisk ( req )
1056+ volumeRes , err := throttle . Throttled ( c . createThrottler , ecsClient .CreateDisk )( ctx , req )
10631057 if err == nil {
10641058 klog .Infof ("request: diskId: %s, reqId: %s" , volumeRes .DiskId , volumeRes .RequestId )
10651059 return volumeRes .DiskId , true , nil
@@ -1161,14 +1155,14 @@ func getDefaultDiskTags(diskVol *diskVolumeArgs) []ecs.CreateDiskTag {
11611155 return diskTags
11621156}
11631157
1164- func deleteDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskId string ) (* ecs.DeleteDiskResponse , error ) {
1158+ func ( c * DiskCloud ) deleteDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskId string ) (* ecs.DeleteDiskResponse , error ) {
11651159 deleteDiskRequest := ecs .CreateDeleteDiskRequest ()
11661160 deleteDiskRequest .DiskId = diskId
11671161
11681162 var resp * ecs.DeleteDiskResponse
11691163 err := wait .PollUntilContextTimeout (ctx , time .Second * 5 , DISK_DELETE_INIT_TIMEOUT , true , func (ctx context.Context ) (bool , error ) {
11701164 var err error
1171- resp , err = ecsClient .DeleteDisk ( deleteDiskRequest )
1165+ resp , err = throttle . Throttled ( c . deleteThrottler , ecsClient .DeleteDisk )( ctx , deleteDiskRequest )
11721166 if err == nil {
11731167 klog .Infof ("DeleteVolume: Successfully deleted volume: %s, with RequestId: %s" , diskId , resp .RequestId )
11741168 return true , nil
0 commit comments