From 825d5253c8b3a762ebbf38a050ffef3afa9e4cd7 Mon Sep 17 00:00:00 2001 From: IrisIris Date: Mon, 24 Jun 2024 11:53:40 +0800 Subject: [PATCH] add support to scaling group desired size for alicloud --- .../services/ess/describe_scaling_groups.go | 89 +--------- .../services/ess/scale_with_adjustment.go | 152 ++++++++++++++++++ .../services/ess/struct_scaling_group.go | 80 +++++++++ .../services/ess/struct_scaling_groups.go | 22 +++ .../alicloud/alicloud_auto_scaling.go | 79 +++------ .../alicloud/alicloud_auto_scaling_test.go | 4 + .../alicloud/alicloud_manager.go | 7 +- 7 files changed, 295 insertions(+), 138 deletions(-) create mode 100644 cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/scale_with_adjustment.go create mode 100644 cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_group.go create mode 100644 cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_groups.go diff --git a/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/describe_scaling_groups.go b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/describe_scaling_groups.go index 2c2c92a85144..406561b4df27 100755 --- a/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/describe_scaling_groups.go +++ b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/describe_scaling_groups.go @@ -22,7 +22,6 @@ import ( ) // DescribeScalingGroups invokes the ess.DescribeScalingGroups API synchronously -// api document: https://help.aliyun.com/api/ess/describescalinggroups.html func (client *Client) DescribeScalingGroups(request *DescribeScalingGroupsRequest) (response *DescribeScalingGroupsResponse, err error) { response = CreateDescribeScalingGroupsResponse() err = client.DoAction(request, response) @@ -30,8 +29,6 @@ func (client *Client) DescribeScalingGroups(request *DescribeScalingGroupsReques } // DescribeScalingGroupsWithChan invokes the ess.DescribeScalingGroups API asynchronously -// api document: https://help.aliyun.com/api/ess/describescalinggroups.html -// asynchronous document: https://help.aliyun.com/document_detail/66220.html func (client *Client) DescribeScalingGroupsWithChan(request *DescribeScalingGroupsRequest) (<-chan *DescribeScalingGroupsResponse, <-chan error) { responseChan := make(chan *DescribeScalingGroupsResponse, 1) errChan := make(chan error, 1) @@ -54,8 +51,6 @@ func (client *Client) DescribeScalingGroupsWithChan(request *DescribeScalingGrou } // DescribeScalingGroupsWithCallback invokes the ess.DescribeScalingGroups API asynchronously -// api document: https://help.aliyun.com/api/ess/describescalinggroups.html -// asynchronous document: https://help.aliyun.com/document_detail/66220.html func (client *Client) DescribeScalingGroupsWithCallback(request *DescribeScalingGroupsRequest, callback func(response *DescribeScalingGroupsResponse, err error)) <-chan int { result := make(chan int, 1) err := client.AddAsyncTask(func() { @@ -78,17 +73,14 @@ func (client *Client) DescribeScalingGroupsWithCallback(request *DescribeScaling type DescribeScalingGroupsRequest struct { *requests.RpcRequest ResourceOwnerId requests.Integer `position:"Query" name:"ResourceOwnerId"` - ScalingGroupId10 string `position:"Query" name:"ScalingGroupId.10"` - ScalingGroupId12 string `position:"Query" name:"ScalingGroupId.12"` - ScalingGroupId13 string `position:"Query" name:"ScalingGroupId.13"` - ScalingGroupId14 string `position:"Query" name:"ScalingGroupId.14"` - ScalingGroupId15 string `position:"Query" name:"ScalingGroupId.15"` + ResourceGroupId string `position:"Query" name:"ResourceGroupId"` + GroupType string `position:"Query" name:"GroupType"` OwnerId requests.Integer `position:"Query" name:"OwnerId"` + ScalingGroupId *[]string `position:"Query" name:"ScalingGroupId" type:"Repeated"` PageNumber requests.Integer `position:"Query" name:"PageNumber"` PageSize requests.Integer `position:"Query" name:"PageSize"` ScalingGroupName20 string `position:"Query" name:"ScalingGroupName.20"` ScalingGroupName19 string `position:"Query" name:"ScalingGroupName.19"` - ScalingGroupId20 string `position:"Query" name:"ScalingGroupId.20"` ScalingGroupName18 string `position:"Query" name:"ScalingGroupName.18"` ScalingGroupName17 string `position:"Query" name:"ScalingGroupName.17"` ScalingGroupName16 string `position:"Query" name:"ScalingGroupName.16"` @@ -97,30 +89,17 @@ type DescribeScalingGroupsRequest struct { OwnerAccount string `position:"Query" name:"OwnerAccount"` ScalingGroupName1 string `position:"Query" name:"ScalingGroupName.1"` ScalingGroupName2 string `position:"Query" name:"ScalingGroupName.2"` - ScalingGroupId2 string `position:"Query" name:"ScalingGroupId.2"` - ScalingGroupId1 string `position:"Query" name:"ScalingGroupId.1"` - ScalingGroupId6 string `position:"Query" name:"ScalingGroupId.6"` - ScalingGroupId16 string `position:"Query" name:"ScalingGroupId.16"` ScalingGroupName7 string `position:"Query" name:"ScalingGroupName.7"` ScalingGroupName11 string `position:"Query" name:"ScalingGroupName.11"` - ScalingGroupId5 string `position:"Query" name:"ScalingGroupId.5"` - ScalingGroupId17 string `position:"Query" name:"ScalingGroupId.17"` ScalingGroupName8 string `position:"Query" name:"ScalingGroupName.8"` ScalingGroupName10 string `position:"Query" name:"ScalingGroupName.10"` - ScalingGroupId4 string `position:"Query" name:"ScalingGroupId.4"` - ScalingGroupId18 string `position:"Query" name:"ScalingGroupId.18"` ScalingGroupName9 string `position:"Query" name:"ScalingGroupName.9"` - ScalingGroupId3 string `position:"Query" name:"ScalingGroupId.3"` - ScalingGroupId19 string `position:"Query" name:"ScalingGroupId.19"` ScalingGroupName3 string `position:"Query" name:"ScalingGroupName.3"` ScalingGroupName15 string `position:"Query" name:"ScalingGroupName.15"` - ScalingGroupId9 string `position:"Query" name:"ScalingGroupId.9"` ScalingGroupName4 string `position:"Query" name:"ScalingGroupName.4"` ScalingGroupName14 string `position:"Query" name:"ScalingGroupName.14"` - ScalingGroupId8 string `position:"Query" name:"ScalingGroupId.8"` ScalingGroupName5 string `position:"Query" name:"ScalingGroupName.5"` ScalingGroupName13 string `position:"Query" name:"ScalingGroupName.13"` - ScalingGroupId7 string `position:"Query" name:"ScalingGroupId.7"` ScalingGroupName6 string `position:"Query" name:"ScalingGroupName.6"` ScalingGroupName12 string `position:"Query" name:"ScalingGroupName.12"` } @@ -128,10 +107,10 @@ type DescribeScalingGroupsRequest struct { // DescribeScalingGroupsResponse is the response struct for api DescribeScalingGroups type DescribeScalingGroupsResponse struct { *responses.BaseResponse - TotalCount int `json:"TotalCount" xml:"TotalCount"` + RequestId string `json:"RequestId" xml:"RequestId"` PageNumber int `json:"PageNumber" xml:"PageNumber"` PageSize int `json:"PageSize" xml:"PageSize"` - RequestId string `json:"RequestId" xml:"RequestId"` + TotalCount int `json:"TotalCount" xml:"TotalCount"` ScalingGroups ScalingGroups `json:"ScalingGroups" xml:"ScalingGroups"` } @@ -141,6 +120,7 @@ func CreateDescribeScalingGroupsRequest() (request *DescribeScalingGroupsRequest RpcRequest: &requests.RpcRequest{}, } request.InitWithApiInfo("Ess", "2014-08-28", "DescribeScalingGroups", "ess", "openAPI") + request.Method = requests.POST return } @@ -151,60 +131,3 @@ func CreateDescribeScalingGroupsResponse() (response *DescribeScalingGroupsRespo } return } - -// ScalingGroups is a nested struct in ess response -type ScalingGroups struct { - ScalingGroup []ScalingGroup `json:"ScalingGroup" xml:"ScalingGroup"` -} - -// ScalingGroup is a nested struct in ess response -type ScalingGroup struct { - DefaultCooldown int `json:"DefaultCooldown" xml:"DefaultCooldown"` - MaxSize int `json:"MaxSize" xml:"MaxSize"` - PendingWaitCapacity int `json:"PendingWaitCapacity" xml:"PendingWaitCapacity"` - RemovingWaitCapacity int `json:"RemovingWaitCapacity" xml:"RemovingWaitCapacity"` - PendingCapacity int `json:"PendingCapacity" xml:"PendingCapacity"` - RemovingCapacity int `json:"RemovingCapacity" xml:"RemovingCapacity"` - ScalingGroupName string `json:"ScalingGroupName" xml:"ScalingGroupName"` - ActiveCapacity int `json:"ActiveCapacity" xml:"ActiveCapacity"` - StandbyCapacity int `json:"StandbyCapacity" xml:"StandbyCapacity"` - ProtectedCapacity int `json:"ProtectedCapacity" xml:"ProtectedCapacity"` - ActiveScalingConfigurationId string `json:"ActiveScalingConfigurationId" xml:"ActiveScalingConfigurationId"` - LaunchTemplateId string `json:"LaunchTemplateId" xml:"LaunchTemplateId"` - LaunchTemplateVersion string `json:"LaunchTemplateVersion" xml:"LaunchTemplateVersion"` - ScalingGroupId string `json:"ScalingGroupId" xml:"ScalingGroupId"` - RegionId string `json:"RegionId" xml:"RegionId"` - TotalCapacity int `json:"TotalCapacity" xml:"TotalCapacity"` - MinSize int `json:"MinSize" xml:"MinSize"` - LifecycleState string `json:"LifecycleState" xml:"LifecycleState"` - CreationTime string `json:"CreationTime" xml:"CreationTime"` - ModificationTime string `json:"ModificationTime" xml:"ModificationTime"` - VpcId string `json:"VpcId" xml:"VpcId"` - VSwitchId string `json:"VSwitchId" xml:"VSwitchId"` - MultiAZPolicy string `json:"MultiAZPolicy" xml:"MultiAZPolicy"` - HealthCheckType string `json:"HealthCheckType" xml:"HealthCheckType"` - VSwitchIds VSwitchIds `json:"VSwitchIds" xml:"VSwitchIds"` - RemovalPolicies RemovalPolicies `json:"RemovalPolicies" xml:"RemovalPolicies"` - DBInstanceIds DBInstanceIds `json:"DBInstanceIds" xml:"DBInstanceIds"` - LoadBalancerIds LoadBalancerIds `json:"LoadBalancerIds" xml:"LoadBalancerIds"` -} - -// VSwitchIds is a nested struct in ess response -type VSwitchIds struct { - VSwitchId []string `json:"VSwitchId" xml:"VSwitchId"` -} - -// RemovalPolicies is a nested struct in ess response -type RemovalPolicies struct { - RemovalPolicy []string `json:"RemovalPolicy" xml:"RemovalPolicy"` -} - -// DBInstanceIds is a nested struct in ess response -type DBInstanceIds struct { - DBInstanceId []string `json:"DBInstanceId" xml:"DBInstanceId"` -} - -// LoadBalancerIds is a nested struct in ess response -type LoadBalancerIds struct { - LoadBalancerId []string `json:"LoadBalancerId" xml:"LoadBalancerId"` -} diff --git a/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/scale_with_adjustment.go b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/scale_with_adjustment.go new file mode 100644 index 000000000000..820467c4d645 --- /dev/null +++ b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/scale_with_adjustment.go @@ -0,0 +1,152 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ess + +import ( + "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/requests" + "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/responses" +) + +// ScaleWithAdjustment invokes the ess.ScaleWithAdjustment API synchronously +func (client *Client) ScaleWithAdjustment(request *ScaleWithAdjustmentRequest) (response *ScaleWithAdjustmentResponse, err error) { + response = CreateScaleWithAdjustmentResponse() + err = client.DoAction(request, response) + return +} + +// ScaleWithAdjustmentWithChan invokes the ess.ScaleWithAdjustment API asynchronously +func (client *Client) ScaleWithAdjustmentWithChan(request *ScaleWithAdjustmentRequest) (<-chan *ScaleWithAdjustmentResponse, <-chan error) { + responseChan := make(chan *ScaleWithAdjustmentResponse, 1) + errChan := make(chan error, 1) + err := client.AddAsyncTask(func() { + defer close(responseChan) + defer close(errChan) + response, err := client.ScaleWithAdjustment(request) + if err != nil { + errChan <- err + } else { + responseChan <- response + } + }) + if err != nil { + errChan <- err + close(responseChan) + close(errChan) + } + return responseChan, errChan +} + +// ScaleWithAdjustmentWithCallback invokes the ess.ScaleWithAdjustment API asynchronously +func (client *Client) ScaleWithAdjustmentWithCallback(request *ScaleWithAdjustmentRequest, callback func(response *ScaleWithAdjustmentResponse, err error)) <-chan int { + result := make(chan int, 1) + err := client.AddAsyncTask(func() { + var response *ScaleWithAdjustmentResponse + var err error + defer close(result) + response, err = client.ScaleWithAdjustment(request) + callback(response, err) + result <- 1 + }) + if err != nil { + defer close(result) + callback(nil, err) + result <- 0 + } + return result +} + +// ScaleWithAdjustmentRequest is the request struct for api ScaleWithAdjustment +type ScaleWithAdjustmentRequest struct { + *requests.RpcRequest + ClientToken string `position:"Query" name:"ClientToken"` + ScalingGroupId string `position:"Query" name:"ScalingGroupId"` + LifecycleHookContext ScaleWithAdjustmentLifecycleHookContext `position:"Query" name:"LifecycleHookContext" type:"Struct"` + InstanceType *[]string `position:"Query" name:"InstanceType" type:"Repeated"` + SyncActivity requests.Boolean `position:"Query" name:"SyncActivity"` + Allocation *[]ScaleWithAdjustmentAllocation `position:"Query" name:"Allocation" type:"Repeated"` + AdjustmentValue requests.Integer `position:"Query" name:"AdjustmentValue"` + ResourceOwnerAccount string `position:"Query" name:"ResourceOwnerAccount"` + ActivityMetadata string `position:"Query" name:"ActivityMetadata"` + AdjustmentType string `position:"Query" name:"AdjustmentType"` + ParallelTask requests.Boolean `position:"Query" name:"ParallelTask"` + Overrides ScaleWithAdjustmentOverrides `position:"Query" name:"Overrides" type:"Struct"` + OwnerId requests.Integer `position:"Query" name:"OwnerId"` + SpotStrategy string `position:"Query" name:"SpotStrategy"` + VSwitchId *[]string `position:"Query" name:"VSwitchId" type:"Repeated"` + MinAdjustmentMagnitude requests.Integer `position:"Query" name:"MinAdjustmentMagnitude"` +} + +// ScaleWithAdjustmentLifecycleHookContext is a repeated param struct in ScaleWithAdjustmentRequest +type ScaleWithAdjustmentLifecycleHookContext struct { + DisableLifecycleHook string `name:"DisableLifecycleHook"` + IgnoredLifecycleHookIds *[]string `name:"IgnoredLifecycleHookIds" type:"Repeated"` +} + +// ScaleWithAdjustmentAllocation is a repeated param struct in ScaleWithAdjustmentRequest +type ScaleWithAdjustmentAllocation struct { + VSwitchId *[]string `name:"VSwitchId" type:"Repeated"` + Count string `name:"Count"` +} + +// ScaleWithAdjustmentOverrides is a repeated param struct in ScaleWithAdjustmentRequest +type ScaleWithAdjustmentOverrides struct { + Memory string `name:"Memory"` + ContainerOverride *[]ScaleWithAdjustmentOverridesContainerOverrideItem `name:"ContainerOverride" type:"Repeated"` + Cpu string `name:"Cpu"` +} + +// ScaleWithAdjustmentOverridesContainerOverrideItem is a repeated param struct in ScaleWithAdjustmentRequest +type ScaleWithAdjustmentOverridesContainerOverrideItem struct { + Memory string `name:"Memory"` + Arg *[]string `name:"Arg" type:"Repeated"` + EnvironmentVar *[]ScaleWithAdjustmentOverridesContainerOverrideItemEnvironmentVarItem `name:"EnvironmentVar" type:"Repeated"` + Name string `name:"Name"` + Cpu string `name:"Cpu"` + Command *[]string `name:"Command" type:"Repeated"` +} + +// ScaleWithAdjustmentOverridesContainerOverrideItemEnvironmentVarItem is a repeated param struct in ScaleWithAdjustmentRequest +type ScaleWithAdjustmentOverridesContainerOverrideItemEnvironmentVarItem struct { + Value string `name:"Value"` + Key string `name:"Key"` +} + +// ScaleWithAdjustmentResponse is the response struct for api ScaleWithAdjustment +type ScaleWithAdjustmentResponse struct { + *responses.BaseResponse + ScalingActivityId string `json:"ScalingActivityId" xml:"ScalingActivityId"` + RequestId string `json:"RequestId" xml:"RequestId"` + ActivityType string `json:"ActivityType" xml:"ActivityType"` +} + +// CreateScaleWithAdjustmentRequest creates a request to invoke ScaleWithAdjustment API +func CreateScaleWithAdjustmentRequest() (request *ScaleWithAdjustmentRequest) { + request = &ScaleWithAdjustmentRequest{ + RpcRequest: &requests.RpcRequest{}, + } + request.InitWithApiInfo("Ess", "2014-08-28", "ScaleWithAdjustment", "ess", "openAPI") + request.Method = requests.POST + return +} + +// CreateScaleWithAdjustmentResponse creates a response to parse from ScaleWithAdjustment response +func CreateScaleWithAdjustmentResponse() (response *ScaleWithAdjustmentResponse) { + response = &ScaleWithAdjustmentResponse{ + BaseResponse: &responses.BaseResponse{}, + } + return +} diff --git a/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_group.go b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_group.go new file mode 100644 index 000000000000..fc2c5d04f185 --- /dev/null +++ b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_group.go @@ -0,0 +1,80 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ess + +// ScalingGroup is a nested struct in ess response +type ScalingGroup struct { + VpcId string `json:"VpcId" xml:"VpcId"` + CreationTime string `json:"CreationTime" xml:"CreationTime"` + TotalInstanceCount int `json:"TotalInstanceCount" xml:"TotalInstanceCount"` + ScalingGroupName string `json:"ScalingGroupName" xml:"ScalingGroupName"` + Weighted bool `json:"Weighted" xml:"Weighted"` + SpotInstancePools int `json:"SpotInstancePools" xml:"SpotInstancePools"` + StoppedCapacity int `json:"StoppedCapacity" xml:"StoppedCapacity"` + OnDemandPercentageAboveBaseCapacity int `json:"OnDemandPercentageAboveBaseCapacity" xml:"OnDemandPercentageAboveBaseCapacity"` + ModificationTime string `json:"ModificationTime" xml:"ModificationTime"` + MinSize int `json:"MinSize" xml:"MinSize"` + ScalingGroupId string `json:"ScalingGroupId" xml:"ScalingGroupId"` + CompensateWithOnDemand bool `json:"CompensateWithOnDemand" xml:"CompensateWithOnDemand"` + ScalingPolicy string `json:"ScalingPolicy" xml:"ScalingPolicy"` + RemovingWaitCapacity int `json:"RemovingWaitCapacity" xml:"RemovingWaitCapacity"` + ActiveCapacity int `json:"ActiveCapacity" xml:"ActiveCapacity"` + OnDemandBaseCapacity int `json:"OnDemandBaseCapacity" xml:"OnDemandBaseCapacity"` + ProtectedCapacity int `json:"ProtectedCapacity" xml:"ProtectedCapacity"` + HealthCheckType string `json:"HealthCheckType" xml:"HealthCheckType"` + LifecycleState string `json:"LifecycleState" xml:"LifecycleState"` + GroupDeletionProtection bool `json:"GroupDeletionProtection" xml:"GroupDeletionProtection"` + ActiveScalingConfigurationId string `json:"ActiveScalingConfigurationId" xml:"ActiveScalingConfigurationId"` + GroupType string `json:"GroupType" xml:"GroupType"` + MultiAZPolicy string `json:"MultiAZPolicy" xml:"MultiAZPolicy"` + RemovingCapacity int `json:"RemovingCapacity" xml:"RemovingCapacity"` + PendingWaitCapacity int `json:"PendingWaitCapacity" xml:"PendingWaitCapacity"` + StandbyCapacity int `json:"StandbyCapacity" xml:"StandbyCapacity"` + CurrentHostName string `json:"CurrentHostName" xml:"CurrentHostName"` + PendingCapacity int `json:"PendingCapacity" xml:"PendingCapacity"` + LaunchTemplateId string `json:"LaunchTemplateId" xml:"LaunchTemplateId"` + TotalCapacity int `json:"TotalCapacity" xml:"TotalCapacity"` + DesiredCapacity int `json:"DesiredCapacity" xml:"DesiredCapacity"` + SpotInstanceRemedy bool `json:"SpotInstanceRemedy" xml:"SpotInstanceRemedy"` + LaunchTemplateVersion string `json:"LaunchTemplateVersion" xml:"LaunchTemplateVersion"` + RegionId string `json:"RegionId" xml:"RegionId"` + VSwitchId string `json:"VSwitchId" xml:"VSwitchId"` + MaxSize int `json:"MaxSize" xml:"MaxSize"` + ScaleOutAmountCheck bool `json:"ScaleOutAmountCheck" xml:"ScaleOutAmountCheck"` + DefaultCooldown int `json:"DefaultCooldown" xml:"DefaultCooldown"` + SystemSuspended bool `json:"SystemSuspended" xml:"SystemSuspended"` + IsElasticStrengthInAlarm bool `json:"IsElasticStrengthInAlarm" xml:"IsElasticStrengthInAlarm"` + MonitorGroupId string `json:"MonitorGroupId" xml:"MonitorGroupId"` + AzBalance bool `json:"AzBalance" xml:"AzBalance"` + AllocationStrategy string `json:"AllocationStrategy" xml:"AllocationStrategy"` + SpotAllocationStrategy string `json:"SpotAllocationStrategy" xml:"SpotAllocationStrategy"` + MaxInstanceLifetime int `json:"MaxInstanceLifetime" xml:"MaxInstanceLifetime"` + CustomPolicyARN string `json:"CustomPolicyARN" xml:"CustomPolicyARN"` + InitCapacity int `json:"InitCapacity" xml:"InitCapacity"` + ResourceGroupId string `json:"ResourceGroupId" xml:"ResourceGroupId"` + EnableDesiredCapacity bool `json:"EnableDesiredCapacity" xml:"EnableDesiredCapacity"` + //RemovalPolicies RemovalPolicies `json:"RemovalPolicies" xml:"RemovalPolicies"` + //DBInstanceIds DBInstanceIds `json:"DBInstanceIds" xml:"DBInstanceIds"` + //LoadBalancerIds LoadBalancerIds `json:"LoadBalancerIds" xml:"LoadBalancerIds"` + //VSwitchIds VSwitchIds `json:"VSwitchIds" xml:"VSwitchIds"` + //SuspendedProcesses SuspendedProcesses `json:"SuspendedProcesses" xml:"SuspendedProcesses"` + //VServerGroups VServerGroups `json:"VServerGroups" xml:"VServerGroups"` + //LaunchTemplateOverrides LaunchTemplateOverrides `json:"LaunchTemplateOverrides" xml:"LaunchTemplateOverrides"` + //AlbServerGroups AlbServerGroups `json:"AlbServerGroups" xml:"AlbServerGroups"` + //ServerGroups ServerGroups `json:"ServerGroups" xml:"ServerGroups"` + //LoadBalancerConfigs LoadBalancerConfigs `json:"LoadBalancerConfigs" xml:"LoadBalancerConfigs"` +} diff --git a/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_groups.go b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_groups.go new file mode 100644 index 000000000000..2c5f1500f8ce --- /dev/null +++ b/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess/struct_scaling_groups.go @@ -0,0 +1,22 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package ess + +// ScalingGroups is a nested struct in ess response +type ScalingGroups struct { + ScalingGroup []ScalingGroup `json:"ScalingGroup" xml:"ScalingGroup"` +} diff --git a/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling.go b/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling.go index 780b772eee24..4a16514dab51 100644 --- a/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling.go +++ b/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling.go @@ -17,6 +17,7 @@ limitations under the License. package alicloud import ( + "encoding/json" "fmt" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/requests" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/services/ess" @@ -29,6 +30,7 @@ const ( acsAutogenIncreaseRules = "acs-autogen-increase-rules" defaultAdjustmentType = "TotalCapacity" defaultRequestPageSize = 10 + alicloudManaged = "cluster-autoscaler/alicloud/managed" ) // autoScaling define the interface usage in alibaba-cloud-sdk-go. @@ -41,6 +43,7 @@ type autoScaling interface { ModifyScalingGroup(req *ess.ModifyScalingGroupRequest) (*ess.ModifyScalingGroupResponse, error) RemoveInstances(req *ess.RemoveInstancesRequest) (*ess.RemoveInstancesResponse, error) ExecuteScalingRule(req *ess.ExecuteScalingRuleRequest) (*ess.ExecuteScalingRuleResponse, error) + ScaleWithAdjustment(req *ess.ScaleWithAdjustmentRequest) (*ess.ScaleWithAdjustmentResponse, error) ModifyScalingRule(req *ess.ModifyScalingRuleRequest) (*ess.ModifyScalingRuleResponse, error) DeleteScalingRule(req *ess.DeleteScalingRuleRequest) (*ess.DeleteScalingRuleResponse, error) } @@ -132,7 +135,7 @@ func (m autoScalingWrapper) getScalingGroupConfigurationByID(configID string, as func (m autoScalingWrapper) getScalingGroupByID(groupID string) (*ess.ScalingGroup, error) { params := ess.CreateDescribeScalingGroupsRequest() - params.ScalingGroupId1 = groupID + params.ScalingGroupId = &[]string{groupID} resp, err := m.DescribeScalingGroups(params) if err != nil { @@ -192,65 +195,35 @@ func (m autoScalingWrapper) getScalingInstancesByGroup(asgId string) ([]ess.Scal return instances, nil } -func (m autoScalingWrapper) setCapcityInstanceSize(groupId string, capcityInstanceSize int64) error { - var ( - ruleId string - scalingRuleAri string - ) - req := ess.CreateDescribeScalingRulesRequest() - req.RegionId = m.cfg.getRegion() +func (m autoScalingWrapper) setCapacityInstanceSize(groupId string, capcityInstanceSize int64) error { + req := ess.CreateScaleWithAdjustmentRequest() + //req.RegionId = m.cfg.GetRegion() req.ScalingGroupId = groupId - req.ScalingRuleName1 = acsAutogenIncreaseRules - resp, err := m.DescribeScalingRules(req) + req.AdjustmentType = defaultAdjustmentType + req.AdjustmentValue = requests.NewInteger64(capcityInstanceSize) + req.SyncActivity = requests.NewBoolean(true) + tmpBytes, _ := json.Marshal(map[string]string{alicloudManaged: "true"}) + req.ActivityMetadata = string(tmpBytes) + + resp, err := m.ScaleWithAdjustment(req) if err != nil { - //need to handle return err } - - defer func() { - deleteReq := ess.CreateDeleteScalingRuleRequest() - deleteReq.ScalingRuleId = ruleId - deleteReq.RegionId = m.cfg.getRegion() - _, err := m.DeleteScalingRule(deleteReq) - if err != nil { - klog.Warningf("failed to clean scaling group rules,Because of %s", err.Error()) - } - }() - - if len(resp.ScalingRules.ScalingRule) == 0 { - //found the specific rules - createReq := ess.CreateCreateScalingRuleRequest() - createReq.RegionId = m.cfg.getRegion() - createReq.ScalingGroupId = groupId - createReq.AdjustmentType = defaultAdjustmentType - createReq.AdjustmentValue = requests.NewInteger64(capcityInstanceSize) - resp, err := m.CreateScalingRule(createReq) - if err != nil { - return err - } - ruleId = resp.ScalingRuleId - scalingRuleAri = resp.ScalingRuleAri - } else { - ruleId = resp.ScalingRules.ScalingRule[0].ScalingRuleId - scalingRuleAri = resp.ScalingRules.ScalingRule[0].ScalingRuleAri + if resp == nil { + // not expect go here, in this case error should not be empty + klog.Warningf("scaling group %s scaled to be %d with empty error & empty response", groupId, capcityInstanceSize) + return nil } - - modifyReq := ess.CreateModifyScalingRuleRequest() - modifyReq.RegionId = m.cfg.getRegion() - modifyReq.ScalingRuleId = ruleId - modifyReq.AdjustmentType = defaultAdjustmentType - modifyReq.AdjustmentValue = requests.NewInteger64(capcityInstanceSize) - _, err = m.ModifyScalingRule(modifyReq) - if err != nil { - return err + if resp.ScalingActivityId == "" { + klog.Warningf("scale scaling group %s scaled size to be %d with request id %s but empty scaling activity id", groupId, capcityInstanceSize, resp.RequestId) } - executeReq := ess.CreateExecuteScalingRuleRequest() - executeReq.RegionId = m.cfg.getRegion() - executeReq.ScalingRuleAri = scalingRuleAri - _, err = m.ExecuteScalingRule(executeReq) - if err != nil { - return err + // only desired capacity changed + if resp.ActivityType == "CapacityChange" { + klog.Warningf("scaling group %s scaled to be %d only desired capacity changed with activity id %s with request id %s", groupId, capcityInstanceSize, resp.ScalingActivityId, resp.RequestId) + return nil } + // succeed with activity id + klog.Infof("scaling group %s succeed scaled to be %d with activity id %s with request id %s", groupId, capcityInstanceSize, resp.ScalingActivityId, resp.RequestId) return nil } diff --git a/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling_test.go b/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling_test.go index cb682676c723..4929f71cdfe8 100644 --- a/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling_test.go +++ b/cluster-autoscaler/cloudprovider/alicloud/alicloud_auto_scaling_test.go @@ -51,6 +51,10 @@ type mockAutoScaling struct { mock.Mock } +func (as *mockAutoScaling) ScaleWithAdjustment(req *ess.ScaleWithAdjustmentRequest) (*ess.ScaleWithAdjustmentResponse, error) { + return nil, nil +} + func (as *mockAutoScaling) DescribeScalingGroups(req *ess.DescribeScalingGroupsRequest) (*ess.DescribeScalingGroupsResponse, error) { return nil, nil } diff --git a/cluster-autoscaler/cloudprovider/alicloud/alicloud_manager.go b/cluster-autoscaler/cloudprovider/alicloud/alicloud_manager.go index 429df4e81f9a..fdc9401bada3 100644 --- a/cluster-autoscaler/cloudprovider/alicloud/alicloud_manager.go +++ b/cluster-autoscaler/cloudprovider/alicloud/alicloud_manager.go @@ -107,12 +107,15 @@ func (m *AliCloudManager) GetAsgSize(asgConfig *Asg) (int64, error) { if err != nil { return -1, fmt.Errorf("failed to describe ASG %s,Because of %s", asgConfig.id, err.Error()) } - return int64(sg.ActiveCapacity + sg.PendingCapacity), nil + if sg.EnableDesiredCapacity { + return int64(sg.DesiredCapacity), nil + } + return int64(sg.ActiveCapacity + sg.PendingCapacity + sg.InitCapacity), nil } // SetAsgSize sets ASG size. func (m *AliCloudManager) SetAsgSize(asg *Asg, size int64) error { - return m.aService.setCapcityInstanceSize(asg.id, size) + return m.aService.setCapacityInstanceSize(asg.id, size) } // DeleteInstances deletes the given instances. All instances must be controlled by the same ASG.