Skip to content

Commit

Permalink
modifiy network judge logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbeeSo committed Nov 14, 2024
1 parent 3320b7e commit 1a611ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
25 changes: 17 additions & 8 deletions pkg/oss/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,27 @@ func isNotMountPoint(mounter mountutils.Interface, target string, expensive bool

func setNetworkType(originURL, regionID string) (URL string, modified bool) {
URL = originURL
if utils.IsPrivateCloud() || !strings.HasSuffix(strings.TrimRight(URL, "/"), ".aliyuncs.com") {
if utils.IsPrivateCloud() {
return
}
// compatible with the old OSS accelerator endpoint, remove after it is deprecated
endpoint := strings.TrimPrefix(strings.TrimPrefix(originURL, "https://"), "http://")
if strings.HasPrefix(endpoint, "oss-cache-") {
return
var protocol string
if strings.HasPrefix(originURL, "https://") {
protocol = "https://"
} else if strings.HasPrefix(originURL, "http://") {
protocol = "http://"
}
if strings.Contains(originURL, regionID) && !strings.Contains(originURL, "internal") {
URL = strings.ReplaceAll(originURL, regionID, regionID+"-internal")
modified = true
endpoint := strings.TrimPrefix(originURL, protocol)

switch endpoint {
case fmt.Sprintf("oss-%s.aliyuncs.com", regionID):
endpoint = fmt.Sprintf("oss-%s-internal.aliyuncs.com", regionID)
case fmt.Sprintf("%s.oss-data-acc.aliyuncs.com", regionID):
endpoint = fmt.Sprintf("%s-internal.oss-data-acc.aliyuncs.com", regionID)
default:
return
}
URL = protocol + endpoint
modified = true
return
}

Expand Down
20 changes: 16 additions & 4 deletions pkg/oss/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,26 @@ func Test_setNetworkType(t *testing.T) {
"cn-hangzhou-internal.oss-data-acc.aliyuncs.com",
true,
},
{
"vpc100",
"vpc100-oss-cn-beijing.aliyuncs.com",
"cn-beijing",
"vpc100-oss-cn-beijing.aliyuncs.com",
false,
},
{
"vpc100-with-protocol",
"http://vpc100-oss-cn-beijing.aliyuncs.com",
"cn-beijing",
"http://vpc100-oss-cn-beijing.aliyuncs.com",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url, done := setNetworkType(tt.originURL, tt.regionID)
if url != tt.wantURL || done != tt.wantModified {
t.Errorf("setNetworkType(%v, %v) = %v, %v, want %v %v",
tt.originURL, tt.regionID, url, done, tt.wantURL, tt.wantModified)
}
assert.Equal(t, tt.wantURL, url)
assert.Equal(t, tt.wantModified, done)
})
}
}
Expand Down

0 comments on commit 1a611ae

Please sign in to comment.