Skip to content

Commit

Permalink
setNetworkType + setTransmissionProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbeeSo committed Feb 19, 2024
1 parent a2c1e9c commit e091c0b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/oss/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,12 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
regionID, _ := utils.GetRegionID()
if regionID == "" {
log.Warnf("NodePublishVolume:: failed to get region id from both env and metadata, use original URL: %s", opt.URL)
} else if url, done := modifiedURL(opt.URL, regionID); done {
log.Warnf("NodePublishVolume:: modified URL from %s to %s", opt.URL, url)
} else if url, done := setNetworkType(opt.URL, regionID); done {
log.Warnf("NodePublishVolume:: setNetworkType: modified URL from %s to %s", opt.URL, url)
opt.URL = url
}
if url, done := setTransmissionProtocol(opt.URL); done {
log.Warnf("NodePublishVolume:: setTransmissionProtocol: modified URL from %s to %s", opt.URL, url)
opt.URL = url
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/oss/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,17 @@ func doMount(mounter mountutils.Interface, target string, opts Options, mountOpt
return nil
}

func modifiedURL(originURL, regionID string) (URL string, modified bool) {
func setNetworkType(originURL, regionID string) (URL string, modified bool) {
URL = originURL
if strings.Contains(originURL, regionID) && !strings.Contains(originURL, "internal") && !utils.IsPrivateCloud() {
URL = strings.ReplaceAll(originURL, regionID, regionID+"-internal")
modified = true
}
return
}

func setTransmissionProtocol(originURL string) (URL string, modified bool) {
URL = originURL
if strings.HasPrefix(URL, "http://") || strings.HasPrefix(URL, "https://") {
return
}
Expand Down
84 changes: 84 additions & 0 deletions pkg/oss/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,87 @@ func Test_parseOtherOpts(t *testing.T) {
})
}
}

func Test_setNetworkType(t *testing.T) {
tests := []struct {
name string
originURL string
regionID string
wantURL string
wantModified bool
}{
{
"internal-modified",
"http://oss-cn-beijing.aliyuncs.com",
"cn-beijing",
"http://oss-cn-beijing-internal.aliyuncs.com",
true,
},
{
"public-unmodified",
"https://oss-cn-beijing.aliyuncs.com",
"cn-hangzhou",
"https://oss-cn-beijing.aliyuncs.com",
false,
},
{
"internal-unmodified",
"oss-cn-beijing-internal.aliyuncs.com",
"cn-beijing",
"oss-cn-beijing-internal.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)
}
})
}
}

func Test_setTransmissionProtocol(t *testing.T) {
tests := []struct {
name string
originURL string
wantURL string
wantModified bool
}{
{
"http-unmodified",
"http://oss-cn-beijing.aliyuncs.com",
"http://oss-cn-beijing.aliyuncs.com",
false,
},
{
"https-unmodified",
"https://oss-cn-beijing-internal.aliyuncs.com",
"https://oss-cn-beijing-internal.aliyuncs.com",
false,
},
{
"public-modified",
"oss-cn-beijing.aliyuncs.com",
"https://oss-cn-beijing.aliyuncs.com",
true,
},
{
"internal-modified",
"oss-cn-beijing-internal.aliyuncs.com",
"http://oss-cn-beijing-internal.aliyuncs.com",
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
url, done := setTransmissionProtocol(tt.originURL)
if url != tt.wantURL || done != tt.wantModified {
t.Errorf("setTransmissionProtocol(%v) = %v, %v, want %v %v",
tt.originURL, url, done, tt.wantURL, tt.wantModified)
}
})
}
}

0 comments on commit e091c0b

Please sign in to comment.