-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
64 lines (52 loc) · 1.32 KB
/
local.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
package dialer
import (
"net"
"fmt"
"time"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
node2 "k8s.io/kubernetes/pkg/util/node"
)
const (
nodeAccessIndexer = "nodeAccess"
)
var (
preferredNodeAddress = []v1.NodeAddressType{
v1.NodeInternalIP,
v1.NodeExternalIP,
v1.NodeHostName,
}
)
func nodeIndexer(obj interface{}) ([]string, error) {
if node, ok := obj.(*v3.Node); ok {
addr, err := node2.GetPreferredNodeAddress(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Labels: node.Status.NodeLabels,
},
Status: node.Status.InternalNodeStatus,
}, preferredNodeAddress)
return []string{addr}, err
}
return nil, nil
}
func (f *Factory) LocalClusterDialer() dialer.Dialer {
return f.dialLocal
}
func (f *Factory) dialLocal(network, address string) (net.Conn, error) {
host, _, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
objs, err := f.localNodeController.Informer().GetIndexer().ByIndex(nodeAccessIndexer, host)
if err != nil {
return nil, err
}
for _, obj := range objs {
if node, ok := obj.(*v3.Node); ok {
return f.TunnelServer.Dial(node.Name, 15*time.Second, network, address)
}
}
return nil, fmt.Errorf("failed to find tunnel for: %v", address)
}