protect a map in ssh.Connector struct
the e2e test revealed that there is a data race in ssh.Connector struct
actuall msg was "fatal error: concurrent map writes".
It turns out to be real as write access to the map in the struct was not protected.
@@ -2,12 +2,14 @@ package ssh
import (
"time"
+ "sync"
"github.com/kubermatic/kubeone/pkg/config"
)
// Connector holds a map of Connections
type Connector struct {
+ lock sync.RWMutex
connections map[string]Connection
}
@@ -22,7 +24,9 @@ func NewConnector() *Connector {
func (c *Connector) Connect(node config.HostConfig) (Connection, error) {
var err error
+ c.lock.RLock()
conn, exists := c.connections[node.PublicAddress]
+ c.lock.RUnlock()
if !exists || conn.Closed() {
opts := Opts{
Username: node.SSHUsername,
@@ -38,6 +42,8 @@ func (c *Connector) Connect(node config.HostConfig) (Connection, error) {
return nil, err
}
+ c.lock.Lock()
+ defer c.lock.Unlock()
c.connections[node.PublicAddress] = conn
}
@@ -46,6 +52,8 @@ func (c *Connector) Connect(node config.HostConfig) (Connection, error) {
// CloseAll closes all connections
func (c *Connector) CloseAll() {
+ c.lock.RLock()
+ defer c.lock.RUnlock()
for _, conn := range c.connections {
conn.Close()
}
