37 Star 396 Fork 71

GVPrancher / rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
rke_dialer.go 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
Daishan Peng 提交于 2018-12-06 16:48 . always use private ip when dialing
package rkedialerfactory
import (
"fmt"
"net"
"strings"
"net/http"
"github.com/rancher/norman/types/slice"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/k8s"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
)
type RKEDialerFactory struct {
Factory dialer.Factory
Docker bool
}
func (t *RKEDialerFactory) Build(h *hosts.Host) (func(network, address string) (net.Conn, error), error) {
if h.NodeName == "" {
return hosts.SSHFactory(h)
}
parts := strings.SplitN(h.NodeName, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid name reference %s", h.NodeName)
}
if t.Docker {
return t.Factory.DockerDialer(parts[0], parts[1])
}
return t.Factory.NodeDialer(parts[0], parts[1])
}
func (t *RKEDialerFactory) WrapTransport(config *v3.RancherKubernetesEngineConfig) k8s.WrapTransport {
translateAddress := map[string]string{}
for _, node := range config.Nodes {
if !slice.ContainsString(node.Role, "controlplane") {
continue
}
if node.InternalAddress != "" && node.Address != "" {
translateAddress[node.Address] = node.InternalAddress
}
}
for _, node := range config.Nodes {
if !slice.ContainsString(node.Role, "controlplane") {
continue
}
ns, n := ref.Parse(node.NodeName)
dialer, err := t.Factory.NodeDialer(ns, n)
if dialer == nil || err != nil {
continue
}
return func(rt http.RoundTripper) http.RoundTripper {
if ht, ok := rt.(*http.Transport); ok {
ht.DialContext = nil
ht.DialTLS = nil
ht.Dial = func(network, address string) (net.Conn, error) {
ip, _, _ := net.SplitHostPort(address)
if privateIP, ok := translateAddress[ip]; ok {
address = strings.Replace(address, ip, privateIP, 1)
}
return dialer(network, address)
}
}
return rt
}
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.3.0-alpha2

搜索帮助

344bd9b3 5694891 D2dac590 5694891