监听底层平台的服务注册中心,缓存注册中心Istio服务模型,触发更新事件回调方法
# pilot/pkg/serviceregistry/aggregate/controller.go
// Controller aggregates data across different registries and monitors for changes
type Controller struct {
registries []Registry
storeLock sync.RWMutex
}
// Registry specifies the collection of service registry related interfaces
type Registry struct {
// Name is the type of the registry - Kubernetes, Consul, etc.
Name serviceregistry.ServiceRegistry
// ClusterID is used when multiple registries of the same type are used,
// for example in the case of K8S multicluster.
ClusterID string
model.Controller
model.ServiceDiscovery
}
type Controller struct {
domainSuffix string
client kubernetes.Interface
queue kube.Queue
services cacheHandler
endpoints cacheHandler
nodes cacheHandler
pods *PodCache
// Env is set by server to point to the environment, to allow the controller to
// use env data and push status. It may be null in tests.
Env *model.Environment
// ClusterID identifies the remote cluster in a multicluster env.
ClusterID string
// XDSUpdater will push EDS changes to the ADS model.
XDSUpdater model.XDSUpdater
stop chan struct{}
sync.RWMutex
// servicesMap stores hostname ==> service, it is used to reduce convertService calls.
servicesMap map[host.Name]*model.Service
// externalNameSvcInstanceMap stores hostname ==> instance, is used to store instances for ExternalName k8s services
externalNameSvcInstanceMap map[host.Name][]*model.ServiceInstance
// CIDR ranger based on path-compressed prefix trie
ranger cidranger.Ranger
// Network name for the registry as specified by the MeshNetworks configmap
networkForRegistry string
}
pilot/pkg/model/controller.go
pilot/pkg/model/service.go
核心逻辑
pilot/pkg/serviceregistry/kube/controller/controller.go
NewController
1,istio支持多集群,即pilot/pkg/serviceregistry/aggregate/controller.go下的Controller为一个总ServiceController,主要功能就是存放Registry对象
2,一个pilot/pkg/serviceregistry/aggregate/controller.go下的Registry对应一个集群注册中心,包含对应集群的Name和ClusterID,model.Controller和 model.ServiceDiscovery是监听与感知注册中心变化的操作类
3,Registry下的model.Controller和 model.ServiceDiscovery对象指向的都是pilot/pkg/serviceregistry/kube/controller/controller.go下的Controller
4,pilot/pkg/serviceregistry/kube/controller/controller.go下的Controller对象主要作用是利用cacheHandler对象监听注册中心中services,endpoints,nodes等对象的变化并缓存在本地
5,cacheHandler对象中的informer负责监听注册中心的变化,监听到变化之后,调用handler中的方法,执行预设的逻辑操作