【注】源码分析均以 k8s 的第一个 commit 代码分析;
上一节主要讲解了 kube-proxy 的 main() 函数流程,这节来具体分析其中的 config.NewServiceConfig() 函数;
函数具体实现的文件:
pkg/proxy/config/config.go
1)func NewServiceConfig() ServiceConfig {}
-- 生成 ServiceConfig 结构的实例
首先来看下 ServiceConfig{} 都有哪些成员变量:
type ServiceConfig struct {
// Configuration sources and their lock.
configSourceLock sync.RWMutex
serviceConfigSources map[string]chan ServiceUpdate
endpointsConfigSources map[string]chan EndpointsUpdate
// Handlers for changes to services and endpoints and their lock.
handlerLock sync.RWMutex
serviceHandlers []ServiceConfigHandler
endpointHandlers []EndpointsConfigHandler
// Last known configuration for union of the sources and the locks. Map goes
// from each source to array of services/endpoints that have been configured
// through that channel.
configLock sync.RWMutex
serviceConfig map[string]map[string]api.Service
endpointConfig map[string]map[string]api.Endpoints
// Channel that service configuration source listeners use to signal of new
// configurations.
// Value written is the source of the change.
serviceNotifyChannel chan string
// Channel that endpoint configuration source listeners use to signal of new
// configurations.
// Value written is the source of the change.
endpointsNotifyChannel chan string
}
可以看到,成员变量还是比较多,包括 Service 和 Endpoint 的更新;
同时,ServiceConfig{} 实现的方法:
看起来还是比较复杂的,主要都是包含相关 service 和 endpoint 相关的变动,service 关联后端 endpoint 的变动;
2)go config.Run()
-- 在初始化完相关 ServiceConfig{} 结构参数后,启动 Run() 函数,来监控相关配置的变更;
func (impl *ServiceConfig) Run() {}
在 Run() 函数中,通过 for 循环,监听 channel 中的消息:
for {
select {
case source := <-impl.serviceNotifyChannel:
log.Printf("Got new service configuration from source %s", source)
impl.NotifyServiceUpdate()
case source := <-impl.endpointsNotifyChannel:
log.Printf("Got new endpoint configuration from source %s", source)
impl.NotifyEndpointsUpdate()
case <-time.After(1 * time.Second):
}
}
这里是每 1s 进行一次轮询,同步 service 和 endpoint 中的消息;
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。