mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 12:32:03 +00:00
Change SSHTunnelList to struct to make Open() semantics better.
(cherry picked from commit 48f672af92
)
This commit is contained in:
parent
e98f79e4bc
commit
4cd4d363c5
@ -210,7 +210,7 @@ type Master struct {
|
|||||||
InsecureHandler http.Handler
|
InsecureHandler http.Handler
|
||||||
|
|
||||||
// Used for secure proxy
|
// Used for secure proxy
|
||||||
tunnels util.SSHTunnelList
|
tunnels *util.SSHTunnelList
|
||||||
tunnelsLock sync.Mutex
|
tunnelsLock sync.Mutex
|
||||||
installSSHKey InstallSSHKey
|
installSSHKey InstallSSHKey
|
||||||
}
|
}
|
||||||
@ -772,7 +772,7 @@ func (m *Master) Dial(net, addr string) (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Master) needToReplaceTunnels(addrs []string) bool {
|
func (m *Master) needToReplaceTunnels(addrs []string) bool {
|
||||||
if len(m.tunnels) != len(addrs) {
|
if m.tunnels == nil || m.tunnels.Len() != len(addrs) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// TODO (cjcullen): This doesn't need to be n^2
|
// TODO (cjcullen): This doesn't need to be n^2
|
||||||
@ -850,7 +850,7 @@ func (m *Master) setupSecureProxy(user, keyfile string) {
|
|||||||
if err := m.loadTunnels(user, keyfile); err != nil {
|
if err := m.loadTunnels(user, keyfile); err != nil {
|
||||||
glog.Errorf("Failed to load SSH Tunnels: %v", err)
|
glog.Errorf("Failed to load SSH Tunnels: %v", err)
|
||||||
}
|
}
|
||||||
if len(m.tunnels) != 0 {
|
if m.tunnels != nil && m.tunnels.Len() != 0 {
|
||||||
// Sleep for 10 seconds if we have some tunnels.
|
// Sleep for 10 seconds if we have some tunnels.
|
||||||
// TODO (cjcullen): tunnels can lag behind actually existing nodes.
|
// TODO (cjcullen): tunnels can lag behind actually existing nodes.
|
||||||
time.Sleep(9 * time.Second)
|
time.Sleep(9 * time.Second)
|
||||||
|
@ -207,9 +207,11 @@ type SSHTunnelEntry struct {
|
|||||||
Tunnel *SSHTunnel
|
Tunnel *SSHTunnel
|
||||||
}
|
}
|
||||||
|
|
||||||
type SSHTunnelList []SSHTunnelEntry
|
type SSHTunnelList struct {
|
||||||
|
entries []SSHTunnelEntry
|
||||||
|
}
|
||||||
|
|
||||||
func MakeSSHTunnels(user, keyfile string, addresses []string) (SSHTunnelList, error) {
|
func MakeSSHTunnels(user, keyfile string, addresses []string) (*SSHTunnelList, error) {
|
||||||
tunnels := []SSHTunnelEntry{}
|
tunnels := []SSHTunnelEntry{}
|
||||||
for ix := range addresses {
|
for ix := range addresses {
|
||||||
addr := addresses[ix]
|
addr := addresses[ix]
|
||||||
@ -219,19 +221,22 @@ func MakeSSHTunnels(user, keyfile string, addresses []string) (SSHTunnelList, er
|
|||||||
}
|
}
|
||||||
tunnels = append(tunnels, SSHTunnelEntry{addr, tunnel})
|
tunnels = append(tunnels, SSHTunnelEntry{addr, tunnel})
|
||||||
}
|
}
|
||||||
return tunnels, nil
|
return &SSHTunnelList{tunnels}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l SSHTunnelList) Open() error {
|
// Open attempts to open all tunnels in the list, and removes any tunnels that
|
||||||
for ix := 0; ix < len(l); ix++ {
|
// failed to open.
|
||||||
if err := l[ix].Tunnel.Open(); err != nil {
|
func (l *SSHTunnelList) Open() error {
|
||||||
// Remove a failed Open from the list.
|
var openTunnels []SSHTunnelEntry
|
||||||
glog.Errorf("Failed to open tunnel %v: %v", l[ix], err)
|
for ix := range l.entries {
|
||||||
l = append(l[:ix], l[ix+1:]...)
|
if err := l.entries[ix].Tunnel.Open(); err != nil {
|
||||||
ix--
|
glog.Errorf("Failed to open tunnel %v: %v", l.entries[ix], err)
|
||||||
|
} else {
|
||||||
|
openTunnels = append(openTunnels, l.entries[ix])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(l) == 0 {
|
l.entries = openTunnels
|
||||||
|
if len(l.entries) == 0 {
|
||||||
return errors.New("Failed to open any tunnels.")
|
return errors.New("Failed to open any tunnels.")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -240,9 +245,9 @@ func (l SSHTunnelList) Open() error {
|
|||||||
// Close asynchronously closes all tunnels in the list after waiting for 1
|
// Close asynchronously closes all tunnels in the list after waiting for 1
|
||||||
// minute. Tunnels will still be open upon this function's return, but should
|
// minute. Tunnels will still be open upon this function's return, but should
|
||||||
// no longer be used.
|
// no longer be used.
|
||||||
func (l SSHTunnelList) Close() {
|
func (l *SSHTunnelList) Close() {
|
||||||
for ix := range l {
|
for ix := range l.entries {
|
||||||
entry := l[ix]
|
entry := l.entries[ix]
|
||||||
go func() {
|
go func() {
|
||||||
defer HandleCrash()
|
defer HandleCrash()
|
||||||
time.Sleep(1 * time.Minute)
|
time.Sleep(1 * time.Minute)
|
||||||
@ -253,22 +258,26 @@ func (l SSHTunnelList) Close() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l SSHTunnelList) Dial(network, addr string) (net.Conn, error) {
|
func (l *SSHTunnelList) Dial(network, addr string) (net.Conn, error) {
|
||||||
if len(l) == 0 {
|
if len(l.entries) == 0 {
|
||||||
return nil, fmt.Errorf("Empty tunnel list.")
|
return nil, fmt.Errorf("Empty tunnel list.")
|
||||||
}
|
}
|
||||||
return l[mathrand.Int()%len(l)].Tunnel.Dial(network, addr)
|
return l.entries[mathrand.Int()%len(l.entries)].Tunnel.Dial(network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l SSHTunnelList) Has(addr string) bool {
|
func (l *SSHTunnelList) Has(addr string) bool {
|
||||||
for ix := range l {
|
for ix := range l.entries {
|
||||||
if l[ix].Address == addr {
|
if l.entries[ix].Address == addr {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *SSHTunnelList) Len() int {
|
||||||
|
return len(l.entries)
|
||||||
|
}
|
||||||
|
|
||||||
func EncodePrivateKey(private *rsa.PrivateKey) []byte {
|
func EncodePrivateKey(private *rsa.PrivateKey) []byte {
|
||||||
return pem.EncodeToMemory(&pem.Block{
|
return pem.EncodeToMemory(&pem.Block{
|
||||||
Bytes: x509.MarshalPKCS1PrivateKey(private),
|
Bytes: x509.MarshalPKCS1PrivateKey(private),
|
||||||
|
Loading…
Reference in New Issue
Block a user