forked from rancher/cluster-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
70 lines (55 loc) · 1.45 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package dialer
import (
"crypto/tls"
"net"
"strings"
"fmt"
"github.com/rancher/machine-controller/store"
"github.com/rancher/machine-controller/store/config"
"github.com/rancher/rke/hosts"
"github.com/rancher/types/apis/management.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/typed/core/v1"
)
type TLSDialerFactory struct {
Store *store.GenericEncryptedStore
MachineClient v3.MachineInterface
ConfigMapGetter v1.ConfigMapsGetter
}
func (t *TLSDialerFactory) Build(h *hosts.Host) (func(network, address string) (net.Conn, error), error) {
if h.MachineName == "" {
return hosts.SSHFactory(h)
}
parts := strings.SplitN(h.MachineName, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid name reference %s", h.MachineName)
}
machine, err := t.MachineClient.GetNamespaced(parts[0], parts[1], metav1.GetOptions{})
if err != nil {
return nil, err
}
config, err := config.NewMachineConfig(t.Store, machine)
if err != nil {
return nil, err
}
tlsConfig, err := config.TLSConfig()
if err != nil {
return nil, err
}
realTLSConfig, err := tlsConfig.ToConfig()
if err != nil {
return nil, err
}
d := &TLSDialer{
Config: realTLSConfig,
Address: tlsConfig.Address,
}
return d.Dial, nil
}
type TLSDialer struct {
Config *tls.Config
Address string
}
func (t *TLSDialer) Dial(network, address string) (net.Conn, error) {
return tls.Dial("tcp", t.Address, t.Config)
}