Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
add default scheme if endpoint no scheme
Browse files Browse the repository at this point in the history
Signed-off-by: yang yang <yang8518296@163.com>
  • Loading branch information
yylt committed Apr 17, 2020
1 parent 61b7af7 commit d07f7f1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/server/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -370,6 +371,19 @@ func defaultScheme(host string) string {
return "https"
}

// addDefaultScheme returns the endpoint with default scheme
func addDefaultScheme(endpoint string) (string, error) {
if strings.Contains(endpoint, "://") {
return endpoint, nil
}
ue := "dummy://" + endpoint
u, err := url.Parse(ue)
if err != nil {
return "", err
}
return fmt.Sprintf("%s://%s", defaultScheme(u.Host), endpoint), nil
}

// registryEndpoints returns endpoints for a given host.
// It adds default registry endpoint if it does not exist in the passed-in endpoint list.
// It also supports wildcard host matching with `*`.
Expand All @@ -385,6 +399,13 @@ func (c *criService) registryEndpoints(host string) ([]string, error) {
if err != nil {
return nil, errors.Wrap(err, "get default host")
}
for i := range endpoints {
en, err := addDefaultScheme(endpoints[i])
if err != nil {
return nil, errors.Wrap(err, "parse endpoint url")
}
endpoints[i] = en
}
for _, e := range endpoints {
u, err := url.Parse(e)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/server/image_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ func TestRegistryEndpoints(t *testing.T) {
"https://registry-3.io/path",
},
},
"miss scheme endpoint in list with path": {
mirrors: map[string]criconfig.Mirror{
"registry-3.io": {
Endpoints: []string{
"https://registry-3.io",
"registry-1.io",
"127.0.0.1:1234",
},
},
},
host: "registry-3.io",
expected: []string{
"https://registry-3.io",
"https://registry-1.io",
"http://127.0.0.1:1234",
},
},
} {
t.Logf("TestCase %q", desc)
c := newTestCRIService()
Expand Down

0 comments on commit d07f7f1

Please sign in to comment.