forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
176 lines (147 loc) · 5.02 KB
/
factory.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package dialer
import (
"fmt"
"net"
"time"
"net/url"
"strings"
"github.com/rancher/rancher/pkg/encryptedstore"
"github.com/rancher/rancher/pkg/nodeconfig"
"github.com/rancher/rancher/pkg/remotedialer"
"github.com/rancher/rancher/pkg/tunnelserver"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/config/dialer"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
)
func NewFactory(apiContext *config.ScaledContext) (dialer.Factory, error) {
authorizer := tunnelserver.NewAuthorizer(apiContext)
tunneler := tunnelserver.NewTunnelServer(apiContext, authorizer)
secretStore, err := nodeconfig.NewStore(apiContext.Core.Namespaces(""), apiContext.K8sClient.CoreV1())
if err != nil {
return nil, err
}
apiContext.Management.Nodes("local").Controller().Informer().AddIndexers(cache.Indexers{
nodeAccessIndexer: nodeIndexer,
})
return &Factory{
clusterLister: apiContext.Management.Clusters("").Controller().Lister(),
localNodeController: apiContext.Management.Nodes("local").Controller(),
nodeLister: apiContext.Management.Nodes("").Controller().Lister(),
TunnelServer: tunneler,
TunnelAuthorizer: authorizer,
store: secretStore,
}, nil
}
type Factory struct {
localNodeController v3.NodeController
nodeLister v3.NodeLister
clusterLister v3.ClusterLister
TunnelServer *remotedialer.Server
TunnelAuthorizer *tunnelserver.Authorizer
store *encryptedstore.GenericEncryptedStore
}
func (f *Factory) ClusterDialer(clusterName string) (dialer.Dialer, error) {
return func(network, address string) (net.Conn, error) {
d, err := f.clusterDialer(clusterName, address)
if err != nil {
return nil, err
}
return d(network, address)
}, nil
}
func isCloudDriver(cluster *v3.Cluster) bool {
return !cluster.Spec.Internal && cluster.Status.Driver != v3.ClusterDriverImported && cluster.Status.Driver != v3.ClusterDriverRKE
}
func (f *Factory) clusterDialer(clusterName, address string) (dialer.Dialer, error) {
cluster, err := f.clusterLister.Get("", clusterName)
if err != nil {
return nil, err
}
if cluster.Spec.Internal {
// For local (embedded, or import) we just assume we can connect directly
return native()
}
hostPort := hostPort(cluster)
if address == hostPort && isCloudDriver(cluster) {
// For cloud drivers we just connect directly to the k8s API, not through the tunnel. All other go through tunnel
return native()
}
if f.TunnelServer.HasSession(cluster.Name) {
return f.TunnelServer.Dialer(cluster.Name, 15*time.Second), nil
}
if cluster.Status.Driver != v3.ClusterDriverRKE {
return nil, fmt.Errorf("waiting for cluster agent to connect")
}
// Only for RKE will we try to connect to a node for the cluster dialer
nodes, err := f.nodeLister.List(cluster.Name, labels.Everything())
if err != nil {
return nil, err
}
for _, node := range nodes {
if node.DeletionTimestamp == nil && v3.NodeConditionProvisioned.IsTrue(node) {
if nodeDialer, err := f.nodeDialer(clusterName, node.Name); err == nil {
return func(network, address string) (net.Conn, error) {
if address == hostPort {
// The node dialer may not have direct access to kube-api so we hit localhost:6443 instead
address = "127.0.0.1:6443"
}
return nodeDialer(network, address)
}, nil
}
}
}
return nil, fmt.Errorf("waiting for cluster agent to connect")
}
func hostPort(cluster *v3.Cluster) string {
u, err := url.Parse(cluster.Status.APIEndpoint)
if err != nil {
return ""
}
if strings.Contains(u.Host, ":") {
return u.Host
}
return u.Host + ":443"
}
func native() (dialer.Dialer, error) {
return func(network, address string) (net.Conn, error) {
return net.DialTimeout(network, address, 30*time.Second)
}, nil
}
func (f *Factory) DockerDialer(clusterName, machineName string) (dialer.Dialer, error) {
machine, err := f.nodeLister.Get(clusterName, machineName)
if err != nil {
return nil, err
}
if f.TunnelServer.HasSession(machine.Name) {
d := f.TunnelServer.Dialer(machine.Name, 15*time.Second)
return func(string, string) (net.Conn, error) {
return d("unix", "/var/run/docker.sock")
}, nil
}
if machine.Spec.NodeTemplateName != "" {
return f.tlsDialer(machine)
}
return nil, fmt.Errorf("can not build dialer to %s:%s", clusterName, machineName)
}
func (f *Factory) NodeDialer(clusterName, machineName string) (dialer.Dialer, error) {
return func(network, address string) (net.Conn, error) {
d, err := f.nodeDialer(clusterName, machineName)
if err != nil {
return nil, err
}
return d(network, address)
}, nil
}
func (f *Factory) nodeDialer(clusterName, machineName string) (dialer.Dialer, error) {
machine, err := f.nodeLister.Get(clusterName, machineName)
if err != nil {
return nil, err
}
if f.TunnelServer.HasSession(machine.Name) {
d := f.TunnelServer.Dialer(machine.Name, 15*time.Second)
return dialer.Dialer(d), nil
}
return nil, fmt.Errorf("can not build dialer to %s:%s", clusterName, machineName)
}