diff --git a/pkg/models/registries/image_type.go b/pkg/models/registries/image_type.go index f7646d95d..7c6f4e9c1 100644 --- a/pkg/models/registries/image_type.go +++ b/pkg/models/registries/image_type.go @@ -5,6 +5,7 @@ import ( "time" ) +// TODO: deprecated, use github.com/docker/docker/api/types.AuthConfig instead type AuthInfo struct { Username string `json:"username" description:"username"` Password string `json:"password" description:"password"` diff --git a/pkg/models/registries/registries.go b/pkg/models/registries/registries.go index 6fbf5a041..692b2e6b9 100644 --- a/pkg/models/registries/registries.go +++ b/pkg/models/registries/registries.go @@ -28,6 +28,8 @@ import ( "k8s.io/klog" log "k8s.io/klog" "kubesphere.io/kubesphere/pkg/informers" + "net/http" + "strings" ) const ( @@ -68,6 +70,23 @@ func RegistryVerify(authInfo AuthInfo) error { ServerAddress: authInfo.ServerHost, } + // Sometimes request DockerHub V2 API will timeout in China. Skipped. + if authInfo.ServerHost != DefaultDockerHub { + var checkAPIVersionUrl string + + if strings.Contains(authInfo.ServerHost, "://") { + checkAPIVersionUrl = authInfo.ServerHost + "/v2/" + } else { + // default use HTTPS protocol + checkAPIVersionUrl = "https://" + authInfo.ServerHost + "/v2/" + } + _, err := http.Get(checkAPIVersionUrl) + if err != nil { + return err + } + } + + // TODO: deprecated, use native Docker Registry v2 authentication. resp, err := cli.RegistryLogin(ctx, config) cli.Close() diff --git a/pkg/models/registries/registries_test.go b/pkg/models/registries/registries_test.go new file mode 100644 index 000000000..558d1ab48 --- /dev/null +++ b/pkg/models/registries/registries_test.go @@ -0,0 +1,32 @@ +package registries + +import ( + "testing" +) + +func TestRegistryVerify(t *testing.T) { + type testRegistry struct { + Auth AuthInfo + Result bool + } + + // some registry can not login with guest. + registries := []testRegistry{ + {Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "docker.io"}, Result: true}, + {Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "dockerhub.qingcloud.com"}, Result: true}, + {Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "https://dockerhub.qingcloud.com"}, Result: true}, + {Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "http://dockerhub.qingcloud.com"}, Result: false}, + {Auth: AuthInfo{Username: "guest", Password: "guest", ServerHost: "registry.cn-hangzhou.aliyuncs.com"}, Result: false}, + } + + for _, registry := range registries { + err := RegistryVerify(registry.Auth) + if registry.Result == true && err != nil { + t.Fatalf("Get err %s", err) + } + + if registry.Result == false && err == nil { + t.Fatalf("Input Wrong data but without any error.") + } + } +}