37 Star 407 Fork 75

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
factory.go 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
Darren Shepherd 提交于 2018-02-22 22:10 . Local cluster support
package clusterrouter
import (
"net/http"
"sync"
"github.com/docker/docker/pkg/locker"
"github.com/rancher/rancher/pkg/clusterrouter/proxy"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config/dialer"
"k8s.io/client-go/rest"
)
type factory struct {
dialerFactory dialer.Factory
clusterLookup ClusterLookup
clusters sync.Map
serverLock *locker.Locker
servers sync.Map
localConfig *rest.Config
}
func newFactory(localConfig *rest.Config, dialer dialer.Factory, lookup ClusterLookup) *factory {
return &factory{
dialerFactory: dialer,
serverLock: locker.New(),
clusterLookup: lookup,
localConfig: localConfig,
}
}
func (s *factory) lookupCluster(clusterID string) (*v3.Cluster, http.Handler) {
srv, ok := s.servers.Load(clusterID)
if ok {
if cluster, ok := s.clusters.Load(clusterID); ok {
return cluster.(*v3.Cluster), srv.(server).Handler()
}
}
return nil, nil
}
func (s *factory) get(req *http.Request) (*v3.Cluster, http.Handler, error) {
cluster, err := s.clusterLookup.Lookup(req)
if err != nil || cluster == nil {
return nil, nil, err
}
clusterID := cluster.Name
if newCluster, handler := s.lookupCluster(clusterID); newCluster != nil {
return newCluster, handler, nil
}
s.serverLock.Lock("cluster." + clusterID)
defer s.serverLock.Unlock("cluster." + clusterID)
if newCluster, handler := s.lookupCluster(clusterID); newCluster != nil {
return newCluster, handler, nil
}
var srv interface{}
srv, err = s.newServer(cluster)
if err != nil || srv == nil {
return nil, nil, err
}
srv, _ = s.servers.LoadOrStore(cluster.Name, srv)
s.clusters.LoadOrStore(cluster.Name, cluster)
return cluster, srv.(server).Handler(), nil
}
func (s *factory) newServer(c *v3.Cluster) (server, error) {
return proxy.New(s.localConfig, c, s.dialerFactory)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-alpha27

搜索帮助