@@ -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
@@ -283,11 +285,7 @@ func (ad *DiskAttachDetach) attachDisk(ctx context.Context, diskID, nodeID strin
283285 for key , value := range GlobalConfigVar .RequestBaseInfo {
284286 attachRequest .AppendUserAgent (key , value )
285287 }
286- var response * ecs.AttachDiskResponse
287- err = ad .attachThrottler .Throttle (ctx , func () error {
288- response , err = ecsClient .AttachDisk (attachRequest )
289- return err
290- })
288+ response , err := throttle .Throttled (ad .attachThrottler , ecsClient .AttachDisk )(ctx , attachRequest )
291289 if err != nil {
292290 var aliErr * alicloudErr.ServerError
293291 if errors .As (err , & aliErr ) {
@@ -325,7 +323,7 @@ func (ad *DiskAttachDetach) attachDisk(ctx context.Context, diskID, nodeID strin
325323}
326324
327325// Only called by controller
328- func (ad * DiskAttachDetach ) attachMultiAttachDisk (ctx context.Context , diskID , nodeID string ) (string , error ) {
326+ func (ad * DiskCloud ) attachMultiAttachDisk (ctx context.Context , diskID , nodeID string ) (string , error ) {
329327 klog .Infof ("AttachDisk: Starting Do AttachMultiAttachDisk: DiskId: %s, InstanceId: %s, Region: %v" , diskID , nodeID , GlobalConfigVar .Region )
330328
331329 ecsClient := updateEcsClient (GlobalConfigVar .EcsClient )
@@ -372,7 +370,7 @@ func (ad *DiskAttachDetach) attachMultiAttachDisk(ctx context.Context, diskID, n
372370 return "" , nil
373371}
374372
375- func (ad * DiskAttachDetach ) detachMultiAttachDisk (ctx context.Context , ecsClient * ecs.Client , diskID , nodeID string ) (isMultiAttach bool , err error ) {
373+ func (ad * DiskCloud ) detachMultiAttachDisk (ctx context.Context , ecsClient * ecs.Client , diskID , nodeID string ) (isMultiAttach bool , err error ) {
376374 disk , err := ad .findDiskByID (ctx , diskID )
377375 if err != nil {
378376 klog .Errorf ("DetachMultiAttachDisk: Describe volume: %s from node: %s, with error: %s" , diskID , nodeID , err .Error ())
@@ -409,7 +407,7 @@ func (ad *DiskAttachDetach) detachMultiAttachDisk(ctx context.Context, ecsClient
409407 return true , nil
410408}
411409
412- func (ad * DiskAttachDetach ) detachDisk (ctx context.Context , ecsClient * ecs.Client , diskID , nodeID string , fromNode bool ) (err error ) {
410+ func (ad * DiskCloud ) detachDisk (ctx context.Context , ecsClient * ecs.Client , diskID , nodeID string , fromNode bool ) (err error ) {
413411 disk , err := ad .findDiskByID (ctx , diskID )
414412 if err != nil {
415413 klog .Errorf ("DetachDisk: Describe volume: %s from node: %s, with error: %s" , diskID , nodeID , err .Error ())
@@ -458,11 +456,7 @@ func (ad *DiskAttachDetach) detachDisk(ctx context.Context, ecsClient *ecs.Clien
458456 for key , value := range GlobalConfigVar .RequestBaseInfo {
459457 detachDiskRequest .AppendUserAgent (key , value )
460458 }
461- var response * ecs.DetachDiskResponse
462- err = ad .detachThrottler .Throttle (ctx , func () error {
463- response , err = ecsClient .DetachDisk (detachDiskRequest )
464- return err
465- })
459+ response , err := throttle .Throttled (ad .detachThrottler , ecsClient .DetachDisk )(ctx , detachDiskRequest )
466460 if err != nil {
467461 return status .Errorf (codes .Aborted , "DetachDisk: Fail to detach %s: from Instance: %s with error: %v" , disk .DiskId , disk .InstanceId , err )
468462 }
@@ -565,7 +559,7 @@ func tagDiskAsK8sAttached(diskID string, ecsClient *ecs.Client) {
565559 klog .Infof ("tagDiskAsK8sAttached:: add tag to disk: %s" , diskID )
566560}
567561
568- func (ad * DiskAttachDetach ) waitForDiskAttached (ctx context.Context , diskID , nodeID string ) error {
562+ func (ad * DiskCloud ) waitForDiskAttached (ctx context.Context , diskID , nodeID string ) error {
569563 disk , err := ad .waiter .WaitFor (ctx , diskID , func (disk * ecs.Disk ) bool {
570564 return waitstatus .IsInstanceAttached (disk , nodeID )
571565 })
@@ -578,7 +572,7 @@ func (ad *DiskAttachDetach) waitForDiskAttached(ctx context.Context, diskID, nod
578572 return nil
579573}
580574
581- func (ad * DiskAttachDetach ) waitForDiskDetached (ctx context.Context , diskID , nodeID string ) error {
575+ func (ad * DiskCloud ) waitForDiskDetached (ctx context.Context , diskID , nodeID string ) error {
582576 disk , err := ad .waiter .WaitFor (ctx , diskID , func (disk * ecs.Disk ) bool {
583577 return ! waitstatus .IsInstanceAttached (disk , nodeID )
584578 })
@@ -592,7 +586,7 @@ func (ad *DiskAttachDetach) waitForDiskDetached(ctx context.Context, diskID, nod
592586 return nil
593587}
594588
595- func (ad * DiskAttachDetach ) findDiskByID (ctx context.Context , diskID string ) (* ecs.Disk , error ) {
589+ func (ad * DiskCloud ) findDiskByID (ctx context.Context , diskID string ) (* ecs.Disk , error ) {
596590 return ad .batcher .Describe (ctx , diskID )
597591}
598592
@@ -920,7 +914,7 @@ func isValidSnapshotName(name string) bool {
920914 return validDiskNameRegexp .MatchString (name )
921915}
922916
923- func createDisk (ecsClient cloud.ECSInterface , diskName , snapshotID string , diskVol * diskVolumeArgs , supportedTypes sets.Set [Category ], selectedInstance string , isVirtualNode bool ) (string , createAttempt , error ) {
917+ 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 ) {
924918 // 需要配置external-provisioner启动参数--extra-create-metadata=true,然后ACK的external-provisioner才会将PVC的Annotations传过来
925919 createDiskRequest := buildCreateDiskRequest (diskVol )
926920 if isValidDiskName (diskName ) {
@@ -955,7 +949,7 @@ func createDisk(ecsClient cloud.ECSInterface, diskName, snapshotID string, diskV
955949 attempt .Instance = selectedInstance
956950 }
957951 retry:
958- diskID , final , err := createDiskAttempt (createDiskRequest , attempt , ecsClient )
952+ diskID , final , err := c . createDiskAttempt (ctx , createDiskRequest , attempt , ecsClient )
959953 if err != nil {
960954 if final {
961955 return "" , attempt , err
@@ -1044,11 +1038,11 @@ func finalizeCreateDiskRequest(template *ecs.CreateDiskRequest, attempt createAt
10441038
10451039var ErrParameterMismatch = errors .New ("parameter mismatch" )
10461040
1047- func createDiskAttempt (req * ecs.CreateDiskRequest , attempt createAttempt , ecsClient cloud.ECSInterface ) (diskId string , final bool , err error ) {
1041+ func ( c * DiskCloud ) createDiskAttempt (ctx context. Context , req * ecs.CreateDiskRequest , attempt createAttempt , ecsClient cloud.ECSInterface ) (diskId string , final bool , err error ) {
10481042 req = finalizeCreateDiskRequest (req , attempt )
10491043 klog .Infof ("request: request content: %++v" , req )
10501044
1051- volumeRes , err := ecsClient .CreateDisk ( req )
1045+ volumeRes , err := throttle . Throttled ( c . createThrottler , ecsClient .CreateDisk )( ctx , req )
10521046 if err == nil {
10531047 klog .Infof ("request: diskId: %s, reqId: %s" , volumeRes .DiskId , volumeRes .RequestId )
10541048 return volumeRes .DiskId , true , nil
@@ -1150,14 +1144,14 @@ func getDefaultDiskTags(diskVol *diskVolumeArgs) []ecs.CreateDiskTag {
11501144 return diskTags
11511145}
11521146
1153- func deleteDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskId string ) (* ecs.DeleteDiskResponse , error ) {
1147+ func ( c * DiskCloud ) deleteDisk (ctx context.Context , ecsClient cloud.ECSInterface , diskId string ) (* ecs.DeleteDiskResponse , error ) {
11541148 deleteDiskRequest := ecs .CreateDeleteDiskRequest ()
11551149 deleteDiskRequest .DiskId = diskId
11561150
11571151 var resp * ecs.DeleteDiskResponse
11581152 err := wait .PollUntilContextTimeout (ctx , time .Second * 5 , DISK_DELETE_INIT_TIMEOUT , true , func (ctx context.Context ) (bool , error ) {
11591153 var err error
1160- resp , err = ecsClient .DeleteDisk ( deleteDiskRequest )
1154+ resp , err = throttle . Throttled ( c . deleteThrottler , ecsClient .DeleteDisk )( ctx , deleteDiskRequest )
11611155 if err == nil {
11621156 klog .Infof ("DeleteVolume: Successfully deleted volume: %s, with RequestId: %s" , diskId , resp .RequestId )
11631157 return true , nil
0 commit comments