Kubernetes client-go 源码分析 - ListWatcher
概述ListWatch 对象的创建GetterListWatchList() & Watch()
概述
源码版本信息
- Project: kubernetes
- Branch: master
- Last commit id: d25d741c
- Date: 2021-09-26
ListWatcher 是 Reflector 的一个主要能力提供者,今天我们具体看下 ListWatcher 是如何实现 List() 和 Watch() 过程的。这里我们只跟到 RESTClient 到调用层,不深入 RESTClient 本身的实现;后面有机会再单独结合 apiserver、etcd 等整体串在一起讲 k8s 里的 list-watch 机制底层原理。
ListWatch 对象的创建
ListWatcher 对应的新建实例函数如下:
- client-go/tools/cache/listwatch.go:70
1// 这里 Getter 类型的 c 对应一个 RESTClient
2func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
3 optionsModifier := func(options *metav1.ListOptions) {
4 options.FieldSelector = fieldSelector.String() // 序列化成字符串
5 }
6 // 调用下面这个 NewFilteredListWatchFromClient() 函数
7 return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
8}
主要逻辑在下面,list 和 watch 能力都是通过 RESTClient 提供:
1func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
2 // list 某个 namespace 下的某个 resource
3 listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
4 optionsModifier(&options)
5 return c.Get(). // RESTClient.Get() -> *request.Request
6 Namespace(namespace).
7 Resource(resource).
8 VersionedParams(&options, metav1.ParameterCodec).
9 Do(context.TODO()).
10 Get()
11 }
12 // watch 某个 namespace 下的某个 resource
13 watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
14 options.Watch = true
15 optionsModifier(&options)
16 return c.Get().
17 Namespace(namespace).
18 Resource(resource).
19 VersionedParams(&options, metav1.ParameterCodec).
20 Watch(context.TODO())
21 }
22 return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
23}
Getter
上面有一个 Getter 接口,看下定义:
- client-go/tools/cache/listwatch.go:65
1type Getter interface {
2 Get() *restclient.Request
3}
这里需要一个能够获得 *restclient.Request 的方式
我们实际使用的时候,会用 rest.Interface 接口类型的实例,这是一个相对底层的工具,封装的是 Kubernetes REST apis 相应动作:
- client-go/rest/client.go:41
1type Interface interface {
2 GetRateLimiter() flowcontrol.RateLimiter
3 Verb(verb string) *Request
4 Post() *Request
5 Put() *Request
6 Patch(pt types.PatchType) *Request
7 Get() *Request
8 Delete() *Request
9 APIVersion() schema.GroupVersion
10}
对应实现是:
- client-go/rest/client.go:81
1type RESTClient struct {
2 base *url.URL
3 versionedAPIPath string
4 content ClientContentConfig
5 createBackoffMgr func() BackoffManager
6 rateLimiter flowcontrol.RateLimiter
7 warningHandler WarningHandler
8 Client *http.Client
9}
10
Getter 接口的 Get() 方法返回的是一个 *restclient.Request 类型,Request 的用法我们直接看 ListWatch 的 New 函数里已经看到是怎么玩的了。
至于这里的 RESTClient 和我们代码里常用的 Clientset 的关系,这里先简单举个例子介绍一下:我们在用 clientset 去 Get 一个指定名字的 DaemonSet 的时候,调用过程类似这样:
1r.AppsV1().DaemonSets("default").Get(ctx, "test-ds", getOpt)
这里的 Get 其实就是利用了 RESTClient 提供的能力,方法实现对应如下:
1func (c *daemonSets) Get(ctx context.Context, name string, options v1.GetOptions) (result *v1beta1.DaemonSet, err error) {
2 result = &v1beta1.DaemonSet{}
3 err = c.client.Get(). // 其实就是 RESTClient.Get(),返回的是 *rest.Request 对象
4 Namespace(c.ns).
5 Resource("daemonsets").
6 Name(name).
7 VersionedParams(&options, scheme.ParameterCodec).
8 Do(ctx).
9 Into(result)
10 return
11}
ListWatch
上面 NewFilteredListWatchFromClient() 函数里实现了 ListFunc 和 WatchFunc 属性的初始化,我们接着看下 ListWatch 结构体定义:
- client-go/tools/cache/listwatch.go:57
1type ListWatch struct {
2 ListFunc ListFunc
3 WatchFunc WatchFunc
4 // DisableChunking requests no chunking for this list watcher.
5 DisableChunking bool
6}
实现的接口叫做 ListWatcher
1type ListerWatcher interface {
2 Lister
3 Watcher
4}
这里的 Lister 是
1type Lister interface {
2 // List 的返回值应该是一个 list 类型对象,也就是里面有 Items 字段,里面的 ResourceVersion 可以用来 watch
3 List(options metav1.ListOptions) (runtime.Object, error)
4}
这里的 Watcher 是
1type Watcher interface {
2 // 从指定的资源版本开始 watch
3 Watch(options metav1.ListOptions) (watch.Interface, error)
4}
List() & Watch()
最后 ListWatch 对象的 List() 和 Watch() 的实现就没有太多新内容了:
- client-go/tools/cache/listwatch.go:103
1func (lw *ListWatch) List(options metav1.ListOptions) (runtime.Object, error) { // ListWatch 在 Reflector 中使用,在 Reflector 中已经有了分页逻辑,所以这里不能再添加分页相关代码 return lw.ListFunc(options)}func (lw *ListWatch) Watch(options metav1.ListOptions) (watch.Interface, error) { return lw.WatchFunc(options)}
(转载请保留本文原始链接 https://www.danielhu.cn)

Kubernetes client-go 源码分析 - ListWatcher的更多相关文章
- Kubernetes client-go Informer 源码分析
概述ControllerController 的初始化Controller 的启动processLoopHandleDeltas()SharedIndexInformersharedIndexerIn ...
- Kubernetes client-go DeltaFIFO 源码分析
概述Queue 接口DeltaFIFO元素增删改 - queueActionLocked()Pop()Replace() 概述 源码版本信息 Project: kubernetes Branch: m ...
- Kubernetes client-go workqueue 源码分析
概述Queue接口和结构体setAdd()Get()Done()DelayingQueue接口和结构体waitForNewDelayingQueuewaitingLoop()AddAfter()Rat ...
- Kubernetes client-go 源码分析 - Reflector
概述入口 - Reflector.Run()核心 - Reflector.ListAndWatch()Reflector.watchHandler()NewReflector()小结 概述 源码版本: ...
- Kubernetes Deployment 源码分析(二)
概述startDeploymentController 入口逻辑DeploymentController 对象DeploymentController 类型定义DeploymentController ...
- Eureka 源码分析之 Eureka Client
文章首发于微信公众号<程序员果果> 地址:https://mp.weixin.qq.com/s/47TUd96NMz67_PCDyvyInQ 简介 Eureka是一种基于REST(Repr ...
- SSO单点登录系列1:cas客户端源码分析cas-client-java-2.1.1.jar
落雨 cas 单点登录 希望能给以后来研究cas的兄弟留下一点思路,也算是研究了两天的成果,外国人的代码写的很晦涩,翻译下来也没有时间继续跟进,所以有错误的还请大家跟帖和我讨论,qq 39426378 ...
- client-go客户端自定义开发Kubernetes及源码分析
介绍 client-go 是一种能够与 Kubernetes 集群通信的客户端,通过它可以对 Kubernetes 集群中各资源类型进行 CRUD 操作,它有三大 client 类,分别为:Clien ...
- 【原】Spark中Client源码分析(二)
继续前一篇的内容.前一篇内容为: Spark中Client源码分析(一)http://www.cnblogs.com/yourarebest/p/5313006.html DriverClient中的 ...
随机推荐
- 微信小程序支付框样式以及功能
1.页面代码 <view catchtap='showInputLayer' class="btn_pay">立即支付</view> <!-- 密码输 ...
- js手机号隐藏中间四位
var tel = "13122223333"; var reg = /^(\d{3})\d{4}(\d{4})$/; tel = tel.replace(reg, "$ ...
- 为Anaconda python3安装gi模块
项目开发中需要使用到gi模块,Ubuntu自带的Python3.5可以正常使用gi.项目解释环境是Anaconda python3.5,提示ImportError: No module named ' ...
- npm使用淘宝镜像源
npm使用淘宝镜像源 单次使用 npm install koa --registry=https://registry.npm.taobao.org 永久使用 配置淘宝镜像源 npm config s ...
- ansible模块解析及使用
模块一:setup(收集远程主机信息) [root@zabbix30 /]# ansible test -m setup 模块二:ping(测试主机是否在线) [root@zabbix30 /]# a ...
- 学习打卡——Linux下安装Redis
http://download.redis.io/releases/,这是查看当前Redis可供下载的Linux系统版本 第一步:启动系统 启动Linux,我这里是CentOS 7 第二步:下载 打开 ...
- Spring Authorization Server授权服务器入门
11月8日Spring官方已经强烈建议使用Spring Authorization Server替换已经过时的Spring Security OAuth2.0,距离Spring Security OA ...
- [AcWing 798] 差分矩阵
点击查看代码 #include<iostream> using namespace std; const int N = 1e3 + 10; int a[N][N], b[N][N]; v ...
- 【深度学习 论文篇 03-2】Pytorch搭建SSD模型踩坑集锦
论文地址:https://arxiv.org/abs/1512.02325 源码地址:http://github.com/amdegroot/ssd.pytorch 环境1:torch1.9.0+CP ...
- 2003031121-浦娟-python数据分析第三周作业-第一次作业
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/pexy/20sj 作业链接 https://edu.cnblogs.com/campus/pexy/20s ...