Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disk: use DescribeEndpoints for internal mode #1163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/cloud/commoninterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cloud

import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
)

type Common interface {
ProcessCommonRequest(request *requests.CommonRequest) (response *responses.CommonResponse, err error)
}
1 change: 0 additions & 1 deletion pkg/cloud/ecsinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type ECSInterface interface {
CreateDisk(request *ecs.CreateDiskRequest) (response *ecs.CreateDiskResponse, err error)
DescribeAvailableResource(request *ecs.DescribeAvailableResourceRequest) (response *ecs.DescribeAvailableResourceResponse, err error)
DeleteDisk(request *ecs.DeleteDiskRequest) (response *ecs.DeleteDiskResponse, err error)
DescribeRegions(request *ecs.DescribeRegionsRequest) (response *ecs.DescribeRegionsResponse, err error)
DescribeInstances(request *ecs.DescribeInstancesRequest) (response *ecs.DescribeInstancesResponse, err error)
DescribeInstanceTypes(request *ecs.DescribeInstanceTypesRequest) (response *ecs.DescribeInstanceTypesResponse, err error)
DescribeDisks(request *ecs.DescribeDisksRequest) (response *ecs.DescribeDisksResponse, err error)
Expand Down
15 changes: 0 additions & 15 deletions pkg/cloud/ecsmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 18 additions & 15 deletions pkg/cloud/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package cloud

import (
"bytes"
"fmt"
"errors"
"io"
"net/http"

aliyunep "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/sirupsen/logrus"
)

// Create a response object from json, useful for testing.
Expand All @@ -31,19 +29,24 @@ func UnmarshalAcsResponse(jsonBytes []byte, res responses.AcsResponse) {
}
}

func ECSQueryEndpoint(regionId string, ecsClient ECSInterface) error {
request := ecs.CreateDescribeRegionsRequest()
request.RegionId = "cn-hangzhou"
regions, err := ecsClient.DescribeRegions(request)
func ECSQueryLocalEndpoint(regionId string, client Common) (string, error) {
return QueryLocalEndpoint("Ecs", "ecs", regionId, client)
}

func QueryLocalEndpoint(product, serviceCode, regionId string, client Common) (string, error) {
resolver := aliyunep.LocationResolver{} // only this resolver supports localAPI
ep, support, err := resolver.TryResolve(&aliyunep.ResolveParam{
RegionId: regionId,
Product: product,
LocationProduct: serviceCode,
LocationEndpointType: "localAPI",
CommonApi: client.ProcessCommonRequest,
})
if err != nil {
return err
return "", err
}
for _, region := range regions.Regions.Region {
if regionId == region.RegionId {
aliyunep.AddEndpointMapping(regionId, "Ecs", region.RegionEndpoint)
logrus.Infof("from DescribeRegions: region %s (%s) endpoint %s", regionId, region.LocalName, region.RegionEndpoint)
return nil
}
if !support {
return "", errors.New("resolve unsupported")
}
return fmt.Errorf("region %s not found", regionId)
return ep, nil
}
93 changes: 42 additions & 51 deletions pkg/cloud/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,60 @@
package cloud_test

import (
"fmt"
"testing"

aliyunep "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/golang/mock/gomock"
aliErrors "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/kubernetes-sigs/alibaba-cloud-csi-driver/pkg/cloud"
"github.com/stretchr/testify/assert"
)

var describeRegionsResponse = ecs.CreateDescribeRegionsResponse()
type endpointMockClient struct {
}

func init() {
cloud.UnmarshalAcsResponse([]byte(`{
"Regions": {
"Region": [
{
"LocalName": "华北2(北京)",
"RegionEndpoint": "ecs-for-ut.cn-beijing.aliyuncs.com",
"RegionId": "cn-beijing"
},
{
"LocalName": "华东1(杭州)",
"RegionEndpoint": "ecs.aliyuncs.com",
"RegionId": "cn-hangzhou"
}
]
},
"RequestId": "CFEB1202-E13C-5421-9A42-6CF5C9706433"
}`), describeRegionsResponse)
func (endpointMockClient) ProcessCommonRequest(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
if request.QueryParams["Id"] == "cn-region-for-ut" {
resp := responses.NewCommonResponse()
cloud.UnmarshalAcsResponse([]byte(`{
"Endpoints": {
"Endpoint": [
{
"Type": "localAPI",
"Protocols": {
"Protocols": [
"HTTP",
"HTTPS"
]
},
"Endpoint": "ecs-openapi-share.cn-region-for-ut.aliyuncs.com",
"Id": "cn-region-for-ut",
"SerivceCode": "ecs",
"Namespace": ""
}
]
},
"RequestId": "4F2F0CEF-74C9-525C-B869-4389B9D39A5E",
"Success": true
}`), resp)
return resp, nil
}
return nil, aliErrors.NewServerError(404, `{
"RequestId": "8B48FA66-7B9B-5C17-9298-65D036A80702",
"HostId": "location-readonly.aliyuncs.com",
"Code": "InvalidRegionId",
"Message": "The specified region does not exist.",
"Recommend": "https://api.aliyun.com/troubleshoot?q=InvalidRegionId&product=Location&requestId=8B48FA66-7B9B-5C17-9298-65D036A80702"
}`, "")
}

func TestECSQueryEndpoint(t *testing.T) {
ctrl := gomock.NewController(t)
ecsClient := cloud.NewMockECSInterface(ctrl)

ecsClient.EXPECT().DescribeRegions(gomock.Any()).Do(func(request *ecs.DescribeRegionsRequest) {
assert.Equal(t, "cn-hangzhou", request.RegionId) // always query hangzhou for endpoint
}).Return(describeRegionsResponse, nil)

err := cloud.ECSQueryEndpoint("cn-beijing", ecsClient)
ep, err := cloud.ECSQueryLocalEndpoint("cn-region-for-ut", endpointMockClient{})
assert.Nil(t, err)
assert.Equal(t, "ecs-for-ut.cn-beijing.aliyuncs.com", aliyunep.GetEndpointFromMap("cn-beijing", "Ecs"))
assert.Equal(t, "ecs-openapi-share.cn-region-for-ut.aliyuncs.com", ep)
}

func TestECSQueryEndpointNotFound(t *testing.T) {
ctrl := gomock.NewController(t)
ecsClient := cloud.NewMockECSInterface(ctrl)

ecsClient.EXPECT().DescribeRegions(gomock.Any()).Return(describeRegionsResponse, nil)

err := cloud.ECSQueryEndpoint("cn-not-exist", ecsClient)
assert.NotNil(t, err)
assert.Equal(t, "region cn-not-exist not found", err.Error())
}

func TestECSQueryEndpointError(t *testing.T) {
ctrl := gomock.NewController(t)
ecsClient := cloud.NewMockECSInterface(ctrl)

ecsClient.EXPECT().DescribeRegions(gomock.Any()).Return(describeRegionsResponse, fmt.Errorf("error"))

err := cloud.ECSQueryEndpoint("does-not-matter", ecsClient)
assert.NotNil(t, err)
assert.Equal(t, "error", err.Error())
ep, err := cloud.ECSQueryLocalEndpoint("cn-not-exist", endpointMockClient{})
assert.ErrorContains(t, err, "InvalidRegionId", ep)
}
12 changes: 8 additions & 4 deletions pkg/disk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ func newEcsClient(ac utils.AccessControl) (ecsClient *ecs.Client) {
}

if os.Getenv("INTERNAL_MODE") == "true" {
if ep := os.Getenv("ECS_ENDPOINT"); ep != "" {
aliyunep.AddEndpointMapping(regionID, "Ecs", ep)
ep := os.Getenv("ECS_ENDPOINT")
if ep != "" {
log.Infof("Use ECS_ENDPOINT: %s", ep)
} else {
err := cloud.ECSQueryEndpoint(regionID, ecsClient)
var err error
ep, err = cloud.ECSQueryLocalEndpoint(regionID, ecsClient)
if err != nil {
log.Fatalf("Internal mode, but query for ECS endpoint failed: %v", err)
log.Fatalf("Internal mode, but resolve ECS endpoint failed: %v", err)
}
log.Infof("Resolved ECS localAPI endpoint: %s", ep)
}
aliyunep.AddEndpointMapping(regionID, "Ecs", ep)
} else {
// Set Unitized Endpoint for hangzhou region
SetEcsEndPoint(regionID)
Expand Down