Skip to content

Commit e57880a

Browse files
committed
utils: ECS SDK v2: use VPC endpoints for cn-hangzhou, etc.
1 parent 35dbd59 commit e57880a

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

pkg/utils/openapi.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
7+
"k8s.io/utils/ptr"
78
)
89

910
func getOpenAPIConfig(regionID string) *openapi.Config {
@@ -29,6 +30,20 @@ func GetEcsConfig(regionID string) *openapi.Config {
2930
config := getOpenAPIConfig(regionID)
3031
if e := os.Getenv("ECS_ENDPOINT"); e != "" {
3132
config.Endpoint = &e
33+
} else {
34+
// ECS default endpoint logic maps cn-hangzhou, etc to public network,
35+
// but we may want to use VPC.
36+
// This aligns with GO SDK v1
37+
// cn-hangzhou-finance is the only region in public cloud that don't have VPC endpoint
38+
// see https://api.aliyun.com/api/Ecs/2014-05-26 (Regions at top-left)
39+
if network := ptr.Deref(config.Network, ""); network != "" && regionID != "cn-hangzhou-finance" {
40+
if network == "public" {
41+
network = ""
42+
} else {
43+
network = "-" + network
44+
}
45+
config.Endpoint = ptr.To("ecs" + network + "." + regionID + ".aliyuncs.com")
46+
}
3247
}
3348
return config
3449
}

pkg/utils/openapi_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
ecs20140526 "github.com/alibabacloud-go/ecs-20140526/v7/client"
7+
"github.com/stretchr/testify/assert"
8+
"k8s.io/utils/ptr"
9+
)
10+
11+
func TestEcsEndpoint(t *testing.T) {
12+
// All regions that has VPC endpoint from https://api.aliyun.com/api/Ecs/2014-05-26 (Regions at top-left)
13+
vpcRegions := []string{
14+
"cn-qingdao",
15+
"cn-beijing-finance-1",
16+
"cn-beijing",
17+
"cn-zhangjiakou",
18+
"cn-huhehaote",
19+
"cn-wulanchabu",
20+
"cn-hangzhou",
21+
"cn-shanghai-finance-1",
22+
"cn-shanghai",
23+
"cn-nanjing",
24+
"cn-fuzhou",
25+
"cn-shenzhen-finance-1",
26+
"cn-shenzhen",
27+
"cn-heyuan",
28+
"cn-guangzhou",
29+
"cn-wuhan-lr",
30+
"me-east-1",
31+
"ap-southeast-2",
32+
"eu-central-1",
33+
"ap-southeast-6",
34+
"ap-northeast-2",
35+
"cn-heyuan-acdr-1",
36+
"ap-southeast-3",
37+
"us-east-1",
38+
"us-west-1",
39+
"us-southeast-1",
40+
"na-south-1",
41+
"ap-northeast-1",
42+
"me-central-1",
43+
"ap-southeast-7",
44+
"cn-zhongwei",
45+
"cn-chengdu",
46+
"ap-southeast-1",
47+
"ap-south-1",
48+
"ap-southeast-5",
49+
"eu-west-1",
50+
"cn-zhengzhou-jva",
51+
"cn-hongkong",
52+
}
53+
testEp := func(t *testing.T, region, ep string) {
54+
t.Run(region, func(t *testing.T) {
55+
cfg := GetEcsConfig(region)
56+
cfg.AccessKeyId = ptr.To("foo")
57+
cfg.AccessKeySecret = ptr.To("bar")
58+
59+
client, err := ecs20140526.NewClient(cfg)
60+
assert.NoError(t, err)
61+
assert.Equal(t, ep, *client.Endpoint)
62+
})
63+
}
64+
testEp(t, "cn-hangzhou", "ecs-cn-hangzhou.aliyuncs.com") // use SDK defaults by default
65+
66+
t.Run("vpc", func(t *testing.T) {
67+
t.Setenv("ALIBABA_CLOUD_NETWORK_TYPE", "vpc")
68+
for _, region := range vpcRegions {
69+
testEp(t, region, "ecs-vpc."+region+".aliyuncs.com")
70+
}
71+
// The only region in public cloud that don't have VPC endpoint
72+
testEp(t, "cn-hangzhou-finance", "ecs.aliyuncs.com")
73+
})
74+
75+
t.Run("public", func(t *testing.T) {
76+
t.Setenv("ALIBABA_CLOUD_NETWORK_TYPE", "public")
77+
for _, region := range vpcRegions {
78+
testEp(t, region, "ecs."+region+".aliyuncs.com")
79+
}
80+
// Also don't have regional public endpoint
81+
testEp(t, "cn-hangzhou-finance", "ecs.aliyuncs.com")
82+
})
83+
}

0 commit comments

Comments
 (0)