Merge pull request #8436 from kofj/improve_awsecr

Improve awsecr parseRegion method.
This commit is contained in:
Wenkai Yin(尹文开) 2019-07-29 11:23:09 +08:00 committed by GitHub
commit 829d9a56b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -31,6 +31,14 @@ import (
"github.com/goharbor/harbor/src/replication/model"
)
const (
regionPattern = "https://(?:api|\\d+\\.dkr)\\.ecr\\.([\\w\\-]+)\\.amazonaws\\.com"
)
var (
regionRegexp = regexp.MustCompile(regionPattern)
)
func init() {
if err := adp.RegisterFactory(model.RegistryTypeAwsEcr, func(registry *model.Registry) (adp.Adapter, error) {
return newAdapter(registry)
@ -59,8 +67,7 @@ func newAdapter(registry *model.Registry) (*adapter, error) {
}
func parseRegion(url string) (string, error) {
pattern := "https://(?:api|\\d+\\.dkr)\\.ecr\\.([\\w\\-]+)\\.amazonaws\\.com"
rs := regexp.MustCompile(pattern).FindStringSubmatch(url)
rs := regionRegexp.FindStringSubmatch(url)
if rs == nil {
return "", errors.New("Bad aws url")
}

View File

@ -1,11 +1,13 @@
package awsecr
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"
@ -251,3 +253,33 @@ func TestAwsAuthCredential_Modify(t *testing.T) {
err = a.Modify(req)
assert.Nil(t, err)
}
var urlForBenchmark = []string{
"https://1234.dkr.ecr.test-region.amazonaws.com/v2/",
"https://api.ecr.test-region.amazonaws.com",
"https://test-region.amazonaws.com",
}
func compileRegexpEveryTime(url string) (string, error) {
rs := regexp.MustCompile(regionPattern).FindStringSubmatch(url)
if rs == nil {
return "", errors.New("Bad aws url")
}
return rs[1], nil
}
func BenchmarkGetRegion(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, url := range urlForBenchmark {
parseRegion(url)
}
}
}
func BenchmarkCompileRegexpEveryTime(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, url := range urlForBenchmark {
compileRegexpEveryTime(url)
}
}
}