Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix direct shortpath url error #26

Merged
merged 1 commit into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -262,7 +262,7 @@ explicitly.
Actually what you need is to

1. Append `/apis/proxies.clusternet.io/v1alpha1/sockets/<CLUSTER-ID>/proxy/https/<SERVER-URL>`
or `/apis/proxies.clusternet.io/v1alpha1/sockets/<CLUSTER-ID>/proxy` at the end of original **parent cluster** server
or `/apis/proxies.clusternet.io/v1alpha1/sockets/<CLUSTER-ID>/proxy/direct` at the end of original **parent cluster** server
address

> - `CLUSTER-ID` is a UUID for your child cluster, which is auto-populated by `clusternet-agent`, such as dc91021d-2361-4f6d-a404-7c33b9e01118. You could get this UUID from objects `ClusterRegistrationRequest`,
Expand Down Expand Up @@ -302,9 +302,9 @@ Actually what you need is to
$ # suppose your child cluster running at https://demo1.cluster.net
$ kubectl config set-cluster `kubectl config get-clusters | grep -v NAME` \
--server=https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy/https/demo1.cluster.net
$ # or just use the proxy path
$ # or just use the direct proxy path
$ kubectl config set-cluster `kubectl config get-clusters | grep -v NAME` \
--server=https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy
--server=https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy/direct
```

> :pushpin: :pushpin: Note:
Expand Down Expand Up @@ -361,7 +361,7 @@ Actually what you need is to
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy
server: https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy/direct
name: kubernetes
contexts:
- context:
Expand Down Expand Up @@ -401,7 +401,7 @@ Actually what you need is to
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy
server: https://10.0.0.10:6443/apis/proxies.clusternet.io/v1alpha1/sockets/dc91021d-2361-4f6d-a404-7c33b9e01118/proxy/direct
name: kubernetes
contexts:
- context:
Expand Down
30 changes: 18 additions & 12 deletions pkg/exchanger/exchanger.go
Expand Up @@ -129,7 +129,6 @@ func (e *Exchanger) ProxyConnect(ctx context.Context, id string, opts *proxies.S

location, transport, err := e.ClusterLocation(true, id, opts)
if err != nil {
responder.Error(err)
return nil, err
}

Expand Down Expand Up @@ -198,21 +197,27 @@ func (e *Exchanger) ProxyConnect(ctx context.Context, id string, opts *proxies.S
func (e *Exchanger) ClusterLocation(useSocket bool, id string, opts *proxies.Socket) (*url.URL, http.RoundTripper, error) {
var transport *http.Transport

location := new(url.URL)

reqPath := strings.TrimLeft(opts.Path, "/")
parts := strings.Split(reqPath, "/")
if len(parts) == 0 {
return nil, nil, apierrors.NewBadRequest(fmt.Sprintf("unexpected error: invalid request path %s", reqPath))
}
var isShortPath bool
if len(reqPath) == 0 {
if parts[0] == "direct" {
isShortPath = true
}

location := new(url.URL)
if isShortPath {
mcls, err := e.mcLister.List(labels.SelectorFromSet(labels.Set{
known.ClusterIDLabel: id,
}))
if err != nil {
return nil, nil, apierrors.NewServiceUnavailable(err.Error())
}
if mcls == nil {
return nil, nil, apierrors.NewBadRequest(fmt.Sprintf("no cluster id is %s", id))
}
if len(mcls) > 1 {
klog.Warningf("found multiple ManagedCluster dedicated for cluster %s !!!", id)
}
Expand All @@ -229,12 +234,13 @@ func (e *Exchanger) ClusterLocation(useSocket bool, id string, opts *proxies.Soc

location.Scheme = loc.Scheme
location.Host = loc.Host
location.Path = loc.Path
} else {
parts := strings.Split(reqPath, "/")
if len(parts) == 0 {
return nil, nil, fmt.Errorf("unexpected error: invalid request path %s", reqPath)

paths := []string{loc.Path}
if len(parts) > 1 {
paths = append(paths, parts[1:]...)
}
location.Path = path.Join(paths...)
} else {
location.Scheme = parts[0]
if len(parts) > 1 {
location.Host = parts[1]
Expand All @@ -245,9 +251,9 @@ func (e *Exchanger) ClusterLocation(useSocket bool, id string, opts *proxies.Soc
}

if useSocket {
//if !e.dialerServer.HasSession(id) {
// return nil, nil, apierrors.NewBadRequest(fmt.Sprintf("cannot proxy through cluster %s, whose agent is disconnected", id))
//}
if !e.dialerServer.HasSession(id) {
return nil, nil, apierrors.NewBadRequest(fmt.Sprintf("cannot proxy through cluster %s, whose agent is disconnected", id))
}
transport = e.getClonedTransport(id)
}

Expand Down