【注】源码分析均以 k8s 的第一个 commit 代码分析;
1)分析 WatchFile() 方法:
func (sl *Kubelet) WatchFile(file string, changeChannel chan<- api.ContainerManifest) {}
每隔一段时间,从监控的文件中读取数据:
data, err := ioutil.ReadFile(file)
将读取的数据解析到 ContainerManifest 的 ymal 文件中:
sl.ExtractYAMLData(data, &manifest)
再通过 byte.Equal 函数进行对比,对应的文件是否有变更:
bytes.Equal(lastData, data)
如果对应的文件有更新,就将更新的数据写入 fileChannel 中;
2)分析 WatchHTTP() 方法:
3)分析 SyncAndSetupEtcdWatch() 方法:
-- 现获取当前 kubelet 运行的主机 hostname:
hostname, err := exec.Command("hostname", "-f").Output()
对应 etcd 中的 key 为:
key := "/registry/hosts/" + strings.TrimSpace(string(hostname))
第一次只读取最初的配置,wanch 只会监控变化:
// First fetch the initial configuration (watch only gives changes...)
for {
err = sl.getKubeletStateFromEtcd(key, changeChannel)
if err == nil {
// We got a successful response, etcd is up, set up the watch.
break
}
time.Sleep(30 * time.Second)
}
第一启动中,获取到 key 报错,则每隔 30s 进行重试;
获取 etcd 中 kubelet 的 state:
func (sl *Kubelet) getKubeletStateFromEtcd(key string, changeChannel chan<- []api.ContainerManifest) error {}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。