前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【K8s】kube-proxy 源码分析 01-02

【K8s】kube-proxy 源码分析 01-02

原创
作者头像
Librant
发布2022-07-06 00:04:18
2290
发布2022-07-06 00:04:18
举报
文章被收录于专栏:跟我一起学 K8s跟我一起学 K8s

【注】源码分析均以 k8s 的第一个 commit 代码分析;

上一节主要讲解了 kube-proxy 的 main() 函数流程,这节来具体分析其中的 config.NewServiceConfig() 函数;


函数具体实现的文件:

代码语言:go
复制
pkg/proxy/config/config.go

1)func NewServiceConfig() ServiceConfig {}

-- 生成 ServiceConfig 结构的实例

首先来看下 ServiceConfig{} 都有哪些成员变量:

代码语言:go
复制
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{} 实现的方法:

  • Run()
  • ServiceChannelListener(source string, listenChannel chan ServiceUpdate)
  • EndpointsChannelListener(source string, listenChannel chan EndpointsUpdate)
  • GetServiceConfigurationChannel(source string) chan ServiceUpdate
  • GetEndpointsConfigurationChannel(source string) chan EndpointsUpdate
  • RegisterServiceHandler(handler ServiceConfigHandler)
  • RegisterEndpointsHandler(handler EndpointsConfigHandler)
  • NotifyServiceUpdate()
  • NotifyEndpointsUpdate()

看起来还是比较复杂的,主要都是包含相关 service 和 endpoint 相关的变动,service 关联后端 endpoint 的变动;

2)go config.Run()

-- 在初始化完相关 ServiceConfig{} 结构参数后,启动 Run() 函数,来监控相关配置的变更;

代码语言:go
复制
func (impl *ServiceConfig) Run() {}

在 Run() 函数中,通过 for 循环,监听 channel 中的消息:

代码语言:go
复制
	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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档