@@ -22,6 +22,7 @@ import (
22
22
23
23
"k8s.io/apimachinery/pkg/util/wait"
24
24
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
25
+ "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/sdktime"
25
26
huaweicloudsdkas "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/services/as/v1"
26
27
huaweicloudsdkasmodel "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/services/as/v1/model"
27
28
huaweicloudsdkecs "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/services/ecs/v2"
@@ -188,9 +189,75 @@ func (csm *cloudServiceManager) GetInstances(groupID string) ([]cloudprovider.In
188
189
return instances , nil
189
190
}
190
191
192
+ // IncreaseSizeInstance increases a scaling group's instance size.
193
+ // The workflow works as follows:
194
+ // 1. create scaling policy with scheduled type.
195
+ // 2. execute the scaling policy immediately(not waiting the policy's launch time).
196
+ // 3. wait for the instance number be increased and remove the scaling policy.
191
197
func (csm * cloudServiceManager ) IncreaseSizeInstance (groupID string , delta int ) error {
192
- // TODO(RainbowMango) finish implementation later
193
- return nil
198
+ originalInstanceSize , err := csm .GetDesireInstanceNumber (groupID )
199
+ if err != nil {
200
+ return err
201
+ }
202
+
203
+ // create a scaling policy
204
+ launchTime := sdktime .SdkTime (time .Now ().Add (time .Hour ))
205
+ addOperation := huaweicloudsdkasmodel .GetScalingPolicyActionOperationEnum ().ADD
206
+ instanceNum := int32 (delta )
207
+ opts := & huaweicloudsdkasmodel.CreateScalingPolicyRequest {
208
+ Body : & huaweicloudsdkasmodel.CreateScalingPolicyRequestBody {
209
+ // It's not mandatory for AS service to set a unique policy name.
210
+ ScalingPolicyName : "huaweicloudautoscaler" ,
211
+ ScalingGroupId : groupID ,
212
+ ScalingPolicyType : huaweicloudsdkasmodel .GetCreateScalingPolicyRequestBodyScalingPolicyTypeEnum ().SCHEDULED ,
213
+ ScheduledPolicy : & huaweicloudsdkasmodel.ScheduledPolicy {
214
+ LaunchTime : & launchTime ,
215
+ },
216
+ ScalingPolicyAction : & huaweicloudsdkasmodel.ScalingPolicyAction {
217
+ Operation : & addOperation ,
218
+ InstanceNumber : & instanceNum ,
219
+ },
220
+ },
221
+ }
222
+
223
+ spID , err := csm .createScalingPolicy (opts )
224
+ if err != nil {
225
+ return err
226
+ }
227
+
228
+ // make sure scaling policy will be cleaned up.
229
+ deletePolicyOps := & huaweicloudsdkasmodel.DeleteScalingPolicyRequest {
230
+ ScalingPolicyId : spID ,
231
+ }
232
+ defer csm .deleteScalingPolicy (deletePolicyOps )
233
+
234
+ // execute policy immediately
235
+ executeAction := huaweicloudsdkasmodel .GetExecuteScalingPolicyRequestBodyActionEnum ()
236
+ executeOpts := & huaweicloudsdkasmodel.ExecuteScalingPolicyRequest {
237
+ ScalingPolicyId : spID ,
238
+ Body : & huaweicloudsdkasmodel.ExecuteScalingPolicyRequestBody {
239
+ Action : & executeAction .EXECUTE ,
240
+ },
241
+ }
242
+ err = csm .executeScalingPolicy (executeOpts )
243
+ if err != nil {
244
+ return err
245
+ }
246
+
247
+ // wait for instance number indeed be increased
248
+ return wait .Poll (5 * time .Second , 300 * time .Second , func () (done bool , err error ) {
249
+ currentInstanceSize , err := csm .GetDesireInstanceNumber (groupID )
250
+ if err != nil {
251
+ return false , err
252
+ }
253
+
254
+ if currentInstanceSize == originalInstanceSize + delta {
255
+ return true , nil
256
+ }
257
+ klog .V (1 ).Infof ("waiting instance increase from %d to %d, now is: %d" , originalInstanceSize , originalInstanceSize + delta , currentInstanceSize )
258
+
259
+ return false , nil
260
+ })
194
261
}
195
262
196
263
func (csm * cloudServiceManager ) ListScalingGroups () ([]AutoScalingGroup , error ) {
@@ -268,3 +335,52 @@ func (csm *cloudServiceManager) transformInstanceState(lifeCycleState huaweiclou
268
335
269
336
return instanceStatus
270
337
}
338
+
339
+ func (csm * cloudServiceManager ) createScalingPolicy (opts * huaweicloudsdkasmodel.CreateScalingPolicyRequest ) (scalingPolicyID string , err error ) {
340
+ asClient := csm .getASClientFunc ()
341
+ if asClient == nil {
342
+ return "" , fmt .Errorf ("failed to get as client" )
343
+ }
344
+
345
+ response , err := asClient .CreateScalingPolicy (opts )
346
+ if err != nil {
347
+ klog .Warningf ("create scaling policy failed. policy: %s, error: %v" , opts .String (), err )
348
+ return "" , err
349
+ }
350
+
351
+ klog .V (1 ).Infof ("create scaling policy succeed. policy id: %s" , * (response .ScalingPolicyId ))
352
+
353
+ return * (response .ScalingPolicyId ), nil
354
+ }
355
+
356
+ func (csm * cloudServiceManager ) executeScalingPolicy (opts * huaweicloudsdkasmodel.ExecuteScalingPolicyRequest ) error {
357
+ asClient := csm .getASClientFunc ()
358
+ if asClient == nil {
359
+ return fmt .Errorf ("failed to get as client" )
360
+ }
361
+
362
+ _ , err := asClient .ExecuteScalingPolicy (opts )
363
+ if err != nil {
364
+ klog .Warningf ("execute scaling policy failed. policy id: %s, error: %v" , opts .ScalingPolicyId , err )
365
+ return err
366
+ }
367
+
368
+ klog .V (1 ).Infof ("execute scaling policy succeed. policy id: %s" , opts .ScalingPolicyId )
369
+ return nil
370
+ }
371
+
372
+ func (csm * cloudServiceManager ) deleteScalingPolicy (opts * huaweicloudsdkasmodel.DeleteScalingPolicyRequest ) error {
373
+ asClient := csm .getASClientFunc ()
374
+ if asClient == nil {
375
+ return fmt .Errorf ("failed to get as client" )
376
+ }
377
+
378
+ _ , err := asClient .DeleteScalingPolicy (opts )
379
+ if err != nil {
380
+ klog .Warningf ("failed to delete scaling policy. policy id: %s, error: %v" , opts .ScalingPolicyId , err )
381
+ return err
382
+ }
383
+
384
+ klog .V (1 ).Infof ("delete scaling policy succeed. policy id: %s" , opts .ScalingPolicyId )
385
+ return nil
386
+ }
0 commit comments