Skip to content

Commit c8e6c0e

Browse files
committed
Simplify the ssh.Conn parameter passing
1 parent f9d1a2b commit c8e6c0e

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

pkg/proxy/http_proxy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"fmt"
66
"net/http"
77

8+
"golang.org/x/crypto/ssh"
89
"k8s.io/klog"
10+
911
"kubesphere.io/tower/pkg/agent"
10-
"kubesphere.io/tower/pkg/utils"
1112
)
1213

1314
type HTTPProxy struct {
@@ -22,8 +23,8 @@ type HTTPProxy struct {
2223
kubesphereAPIServerProxy *Server
2324
}
2425

25-
func NewHTTPProxy(ssh utils.GetSSHConn, kubernetesPort uint16, kubespherePort uint16, config *agent.Config, ca, serverCert, serverKey []byte) (*HTTPProxy, *http.Transport, *http.Transport, error) {
26-
k8stransPort, useBearerToken, servertlsConfig, err := buildServerData(ssh, config.KubernetesSvcHost, config.CAData, config.CertData, config.KeyData, ca, serverCert, serverKey)
26+
func NewHTTPProxy(sshConn ssh.Conn, kubernetesPort uint16, kubespherePort uint16, config *agent.Config, ca, serverCert, serverKey []byte) (*HTTPProxy, *http.Transport, *http.Transport, error) {
27+
k8stransPort, useBearerToken, servertlsConfig, err := buildServerData(sshConn, config.KubernetesSvcHost, config.CAData, config.CertData, config.KeyData, ca, serverCert, serverKey)
2728
if err != nil {
2829
return nil, nil, nil, err
2930
}
@@ -33,7 +34,7 @@ func NewHTTPProxy(ssh utils.GetSSHConn, kubernetesPort uint16, kubespherePort ui
3334
return nil, nil, nil, err
3435
}
3536

36-
kstransPort, useBearerToken, _, err := buildServerData(ssh, config.KubeSphereSvcHost, nil, nil, nil, nil, nil, nil)
37+
kstransPort, useBearerToken, _, err := buildServerData(sshConn, config.KubeSphereSvcHost, nil, nil, nil, nil, nil, nil)
3738
if err != nil {
3839
return nil, nil, nil, err
3940
}

pkg/proxy/proxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (s *Proxy) handleWebsocket(w http.ResponseWriter, req *http.Request) {
216216
// if the agent has connected the server with the same cluster name, we don't need to create HttpProxy anymore
217217
// we only create two new httpTransport objects, then put them into the server's httpClient set.
218218
if proxy, ok = s.sessions[c.Name]; !ok {
219-
proxy, k8sTransport, ksTransport, err = NewHTTPProxy(func() ssh.Conn { return sshConn }, client.Spec.Connection.KubernetesAPIServerPort, client.Spec.Connection.KubeSphereAPIServerPort, c, s.caCert, cert, key)
219+
proxy, k8sTransport, ksTransport, err = NewHTTPProxy(sshConn, client.Spec.Connection.KubernetesAPIServerPort, client.Spec.Connection.KubeSphereAPIServerPort, c, s.caCert, cert, key)
220220
if err != nil {
221221
failed(err)
222222
return
@@ -229,13 +229,13 @@ func (s *Proxy) handleWebsocket(w http.ResponseWriter, req *http.Request) {
229229

230230
s.sessions[c.Name] = proxy
231231
} else {
232-
k8sTransport, _, _, err = buildServerData(func() ssh.Conn { return sshConn }, c.KubernetesSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
232+
k8sTransport, _, _, err = buildServerData(sshConn, c.KubernetesSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
233233
if err != nil {
234234
failed(err)
235235
return
236236
}
237237

238-
ksTransport, _, _, err = buildServerData(func() ssh.Conn { return sshConn }, c.KubeSphereSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
238+
ksTransport, _, _, err = buildServerData(sshConn, c.KubeSphereSvcHost, c.CAData, c.CertData, c.KeyData, s.caCert, cert, key)
239239
if err != nil {
240240
failed(err)
241241
return

pkg/proxy/proxy_server.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"sync"
1212
"time"
1313

14+
"golang.org/x/crypto/ssh"
1415
utilnet "k8s.io/apimachinery/pkg/util/net"
1516
k8sproxy "k8s.io/apimachinery/pkg/util/proxy"
1617
"k8s.io/klog"
@@ -68,15 +69,11 @@ func newProxyServer(name, host, scheme string, port uint16, useBearerToken bool,
6869
}
6970

7071
// buildServerData returns http.Transport and tlsConfig, which are necessary for creating proxy server.
71-
func buildServerData(sshConn utils.GetSSHConn, host string, ca, cert, key, serverCa, serverCert, serverKey []byte) (*http.Transport, bool, *tls.Config, error) {
72+
func buildServerData(sshConn ssh.Conn, host string, ca, cert, key, serverCa, serverCert, serverKey []byte) (*http.Transport, bool, *tls.Config, error) {
7273
useBearerToken := true
7374

7475
transport := &http.Transport{
7576
DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, err error) {
76-
c := sshConn()
77-
if c == nil {
78-
return nil, fmt.Errorf("no remote connetion available")
79-
}
8077
return utils.NewSshConn(sshConn, host)
8178
},
8279
}

pkg/utils/ssh_conn.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,25 @@ package utils
22

33
import (
44
"errors"
5-
"golang.org/x/crypto/ssh"
65
"io"
76
"net"
87
"time"
8+
9+
"golang.org/x/crypto/ssh"
910
)
1011

1112
var ErrorInvalidConnection = errors.New("invalid connection")
1213

13-
//ErrorNoAvailableConn means there haven't available shh connection.
14-
var ErrorNoAvailableConn = errors.New("no available ssh connection")
15-
16-
type GetSSHConn func() ssh.Conn
17-
1814
type SshConn struct {
1915
dst io.ReadWriteCloser
2016
}
2117

22-
func NewSshConn(conn GetSSHConn, remote string) (net.Conn, error) {
23-
c := conn()
24-
if c == nil {
25-
return nil, ErrorNoAvailableConn
18+
func NewSshConn(conn ssh.Conn, remote string) (net.Conn, error) {
19+
if conn == nil {
20+
return nil, errors.New("the ssh connection is nil")
2621
}
2722

28-
dst, reqs, err := c.OpenChannel("kubesphere", []byte(remote))
23+
dst, reqs, err := conn.OpenChannel("kubesphere", []byte(remote))
2924
if err != nil {
3025
return nil, err
3126
}

0 commit comments

Comments
 (0)