Skip to content

Commit

Permalink
ResourceExhausted error code mapping introduced for Alicloud (#57)
Browse files Browse the repository at this point in the history
* added IT log file to gitignore

* mapped RunInstances error codes to MCM ResourceExhausted error code if possible

* added unit tests and license headers

* lint errors addressed

* addressed review comments
  • Loading branch information
himanshu-kun committed Sep 22, 2023
1 parent d6dc30a commit bf32415
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/tmp/*
/dev
/logs
.ci/controllers-test/logs

.vscode
.idea
Expand Down
45 changes: 45 additions & 0 deletions pkg/alicloud/errors/codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright (c) 2023 SAP SE or an SAP affiliate company. All rights reserved.
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 errors contains the error codes returned by Alicloud APIs
package errors

// constants for alicloud `RunInstances()` response error code which map to MCM `ResourceExhausted` code
const (
// QuotaExceededDiskCapacity: The used capacity of disk type has exceeded the quota in the zone
QuotaExceededDiskCapacity = "QuotaExceed.DiskCapacity"
// QuotaExceededElasticQuota: The number of vCPUs assigned to the ECS instances has exceeded the quota in the zone. Please sign-on to Alibaba Cloud Console, and submit a quota increase application.
QuotaExceededElasticQuota = "QuotaExceed.ElasticQuota"
// OperationDeniedZoneSystemCategoryNotMatch: The specified Zone or cluster does not offer the specified disk category or the specified zone and cluster do not match.
OperationDeniedZoneSystemCategoryNotMatch = "OperationDenied.ZoneSystemCategoryNotMatch"
// OperationDeniedCloudSSDNotSupported: The specified available zone does not offer the cloud_ssd disk, use cloud_essd instead.
OperationDeniedCloudSSDNotSupported = "OperationDenied.CloudSSDNotSupported"
// OperationDeniedZoneNotAllowed: The creation of Instance to the specified Zone is not allowed.
OperationDeniedZoneNotAllowed = "OperationDenied.ZoneNotAllowed"
// OperationDeniedNoStock: The requested resource is sold out in the specified zone; try other types of resources or other regions and zones.
OperationDeniedNoStock = "OperationDenied.NoStock"
// ZoneNotOnSale: The resource in the specified zone is no longer available for sale. Please try other regions and zones.
ZoneNotOnSale = "Zone.NotOnSale"
// ZoneNotOpen: The specified zone is not granted to you to buy resources yet.
ZoneNotOpen = "Zone.NotOpen"
// InvalidVpcZone.NotSupported: The specified operation is not allowed in the zone to which your VPC belongs, please try in other zones.
InvalidVpcZoneNotSupported = "InvalidVpcZone.NotSupported"
// InvalidZoneIDNotSupportShareEncryptedImage: Creating instances by shared encrypted images is not supported in this zone.
InvalidZoneIDNotSupportShareEncryptedImage = "InvalidZoneId.NotSupportShareEncryptedImage"
// InvalidInstanceType.ZoneNotSupported: The specified zone does not support this instancetype.
InvalidInstanceTypeZoneNotSupported = "InvalidInstanceType.ZoneNotSupported"
// ResourceNotAvailable: Resource you requested is not available in this region or zone.
ResourceNotAvailable = "ResourceNotAvailable"
)
36 changes: 36 additions & 0 deletions pkg/alicloud/errors/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright (c) 2023 SAP SE or an SAP affiliate company. All rights reserved.
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 errors contains the error codes returned by Alicloud APIs
package errors

import (
alierr "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes"
)

// GetMCMErrorCodeForCreateMachine takes the error returned from the EC2API during the CreateMachine call and returns the corresponding MCM error code.
func GetMCMErrorCodeForCreateMachine(err error) codes.Code {
aliErr, ok := err.(*alierr.ServerError)
if ok {
switch aliErr.ErrorCode() {
case QuotaExceededDiskCapacity, QuotaExceededElasticQuota, OperationDeniedCloudSSDNotSupported, OperationDeniedNoStock, OperationDeniedZoneNotAllowed, OperationDeniedZoneSystemCategoryNotMatch, ZoneNotOnSale, ZoneNotOpen, InvalidVpcZoneNotSupported, InvalidInstanceTypeZoneNotSupported, InvalidZoneIDNotSupportShareEncryptedImage, ResourceNotAvailable:
return codes.ResourceExhausted
default:
return codes.Internal
}
}
return codes.Internal
}
48 changes: 48 additions & 0 deletions pkg/alicloud/errors/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright (c) 2023 SAP SE or an SAP affiliate company. All rights reserved.
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 errors

import (
"encoding/json"
"testing"

alierr "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes"
. "github.com/onsi/gomega"
)

type input struct {
inputAliErrorCode string
expectedCode codes.Code
}

type responseContent struct {
Code string
}

func TestCreateMachineErrorToMCMErrorCode(t *testing.T) {
table := []input{
{inputAliErrorCode: OperationDeniedNoStock, expectedCode: codes.ResourceExhausted},
// InvalidImageId can't be resolved by trying another zone, so not treated as ResourceExhausted
{inputAliErrorCode: "InvalidImageId.NotFound", expectedCode: codes.Internal},
}
g := NewWithT(t)
for _, entry := range table {
jsonResponse, err := json.Marshal(responseContent{entry.inputAliErrorCode})
g.Expect(err).To(BeNil())
inputError := alierr.NewServerError(403, string(jsonResponse), "some error happened on the server side")
g.Expect(GetMCMErrorCodeForCreateMachine(inputError)).To(Equal(entry.expectedCode))
}
}
3 changes: 2 additions & 1 deletion pkg/alicloud/machine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"fmt"

maperror "github.com/gardener/machine-controller-manager-provider-alicloud/pkg/alicloud/errors"
"github.com/gardener/machine-controller-manager-provider-alicloud/pkg/spi"
"github.com/gardener/machine-controller-manager/pkg/util/provider/driver"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (plugin *MachinePlugin) CreateMachine(ctx context.Context, req *driver.Crea

response, err := client.RunInstances(request)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
return nil, status.Error(maperror.GetMCMErrorCodeForCreateMachine(err), err.Error())
}

instanceID := response.InstanceIdSets.InstanceIdSet[0]
Expand Down

0 comments on commit bf32415

Please sign in to comment.