2021-07-01 10:02:38 +08:00
<!DOCTYPE HTML>
< html lang = "zh-hans" >
< head >
< meta charset = "UTF-8" >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< title > client-go 中的 informer 源码分析 · Kubernetes Handbook - Kubernetes 中文指南/云原生应用架构实践手册 · Jimmy Song< / title >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" / >
< meta name = "description" content = "" >
< meta name = "generator" content = "GitBook 3.2.3" >
< meta name = "author" content = "Jimmy Song( 宋净超) " >
< link rel = "stylesheet" href = "../gitbook/style.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-splitter/splitter.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-page-toc-button/plugin.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-image-captions/image-captions.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-back-to-top-button/plugin.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-search-plus/search.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-prism/prism-ghcolors.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css" >
< link rel = "stylesheet" href = "../gitbook/gitbook-plugin-fontsettings/website.css" >
< meta name = "HandheldFriendly" content = "true" / >
< meta name = "viewport" content = "width=device-width, initial-scale=1, user-scalable=no" >
< meta name = "apple-mobile-web-app-capable" content = "yes" >
< meta name = "apple-mobile-web-app-status-bar-style" content = "black" >
< link rel = "apple-touch-icon-precomposed" sizes = "152x152" href = "../gitbook/images/apple-touch-icon-precomposed-152.png" >
< link rel = "shortcut icon" href = "../gitbook/images/favicon.ico" type = "image/x-icon" >
< link rel = "next" href = "operator.html" / >
< link rel = "prev" href = "client-go-sample.html" / >
< link rel = "shortcut icon" href = '../favicon.ico' type = "image/x-icon" >
< link rel = "bookmark" href = '../favicon.ico' type = "image/x-icon" >
< style >
@media only screen and (max-width: 640px) {
.book-header .hidden-mobile {
display: none;
}
}
< / style >
< script >
window["gitbook-plugin-github-buttons"] = {"repo":"rootsongjc/kubernetes-handbook","types":["star"],"size":"small"};
< / script >
< / head >
< body >
< div class = "book" >
< div class = "book-summary" >
< div id = "book-search-input" role = "search" >
< input type = "text" placeholder = "输入并搜索" / >
< / div >
< nav role = "navigation" >
< ul class = "summary" >
< li >
< a href = "https://jimmysong.io" target = "_blank" class = "custom-link" > 回到主页< / a >
< / li >
< li >
< a href = "https://jimmysong.io/awesome-cloud-native" target = "_blank" class = "custom-link" > 云原生开源项目大全< / a >
< / li >
< li >
< a href = "https://cloudnative.to" target = "_blank" class = "custom-link" > 云原生社区< / a >
< / li >
< li class = "divider" > < / li >
< li class = "header" > 前言< / li >
< li class = "chapter " data-level = "1.1" data-path = "../" >
< a href = "../" >
< b > 1.1.< / b >
序言
< / a >
< / li >
< li class = "header" > 云原生< / li >
< li class = "chapter " data-level = "2.1" data-path = "../cloud-native/cloud-native-definition.html" >
< a href = "../cloud-native/cloud-native-definition.html" >
< b > 2.1.< / b >
云原生( Cloud Native) 的定义
< / a >
< / li >
< li class = "chapter " data-level = "2.2" data-path = "../cloud-native/cloud-native-philosophy.html" >
< a href = "../cloud-native/cloud-native-philosophy.html" >
< b > 2.2.< / b >
云原生的设计哲学
< / a >
< / li >
< li class = "chapter " data-level = "2.3" data-path = "../cloud-native/kubernetes-history.html" >
< a href = "../cloud-native/kubernetes-history.html" >
< b > 2.3.< / b >
Kubernetes 的诞生
< / a >
< / li >
< li class = "chapter " data-level = "2.4" data-path = "../cloud-native/kubernetes-and-cloud-native-app-overview.html" >
< a href = "../cloud-native/kubernetes-and-cloud-native-app-overview.html" >
< b > 2.4.< / b >
Kubernetes 与云原生应用概览
< / a >
< / li >
< li class = "chapter " data-level = "2.5" data-path = "../cloud-native/from-kubernetes-to-cloud-native.html" >
< a href = "../cloud-native/from-kubernetes-to-cloud-native.html" >
< b > 2.5.< / b >
2021-07-14 09:23:37 +08:00
云原生应用之路 —— 从 Kubernetes 到云原生
2021-07-01 10:02:38 +08:00
< / a >
< / li >
< li class = "chapter " data-level = "2.6" data-path = "../cloud-native/define-cloud-native-app.html" >
< a href = "../cloud-native/define-cloud-native-app.html" >
< b > 2.6.< / b >
定义云原生应用
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.6.1" data-path = "../cloud-native/oam.html" >
< a href = "../cloud-native/oam.html" >
< b > 2.6.1.< / b >
OAM
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.6.1.1" data-path = "../cloud-native/workload.html" >
< a href = "../cloud-native/workload.html" >
< b > 2.6.1.1.< / b >
Workload
< / a >
< / li >
< li class = "chapter " data-level = "2.6.1.2" data-path = "../cloud-native/component.html" >
< a href = "../cloud-native/component.html" >
< b > 2.6.1.2.< / b >
Component
< / a >
< / li >
< li class = "chapter " data-level = "2.6.1.3" data-path = "../cloud-native/trait.html" >
< a href = "../cloud-native/trait.html" >
< b > 2.6.1.3.< / b >
Trait
< / a >
< / li >
< li class = "chapter " data-level = "2.6.1.4" data-path = "../cloud-native/application-scope.html" >
< a href = "../cloud-native/application-scope.html" >
< b > 2.6.1.4.< / b >
Application Scope
< / a >
< / li >
< li class = "chapter " data-level = "2.6.1.5" data-path = "../cloud-native/application-configuration.html" >
< a href = "../cloud-native/application-configuration.html" >
< b > 2.6.1.5.< / b >
Application Configuration
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2.6.2" data-path = "../cloud-native/crossplane.html" >
< a href = "../cloud-native/crossplane.html" >
< b > 2.6.2.< / b >
Crossplane
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2.7" data-path = "../cloud-native/cloud-native-programming-languages.html" >
< a href = "../cloud-native/cloud-native-programming-languages.html" >
< b > 2.7.< / b >
云原生编程语言
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "2.7.1" data-path = "../cloud-native/cloud-native-programming-language-ballerina.html" >
< a href = "../cloud-native/cloud-native-programming-language-ballerina.html" >
< b > 2.7.1.< / b >
云原生编程语言 Ballerina
< / a >
< / li >
< li class = "chapter " data-level = "2.7.2" data-path = "../cloud-native/cloud-native-programming-language-pulumi.html" >
< a href = "../cloud-native/cloud-native-programming-language-pulumi.html" >
< b > 2.7.2.< / b >
云原生编程语言 Pulumi
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "2.8" data-path = "../cloud-native/the-future-of-cloud-native.html" >
< a href = "../cloud-native/the-future-of-cloud-native.html" >
< b > 2.8.< / b >
云原生的未来
< / a >
< / li >
< li class = "header" > 快速入门< / li >
< li class = "chapter " data-level = "3.1" data-path = "../cloud-native/quick-start.html" >
< a href = "../cloud-native/quick-start.html" >
< b > 3.1.< / b >
云原生新手入门指南
< / a >
< / li >
< li class = "chapter " data-level = "3.2" data-path = "../cloud-native/play-with-kubernetes.html" >
< a href = "../cloud-native/play-with-kubernetes.html" >
< b > 3.2.< / b >
Play with Kubernetes
< / a >
< / li >
< li class = "chapter " data-level = "3.3" data-path = "../cloud-native/cloud-native-local-quick-start.html" >
< a href = "../cloud-native/cloud-native-local-quick-start.html" >
< b > 3.3.< / b >
快速部署一个云原生本地实验环境
< / a >
< / li >
< li class = "chapter " data-level = "3.4" data-path = "../cloud-native/setup-kubernetes-with-rancher-and-aliyun.html" >
< a href = "../cloud-native/setup-kubernetes-with-rancher-and-aliyun.html" >
< b > 3.4.< / b >
使用 Rancher 在阿里云上部署 Kubenretes 集群
< / a >
< / li >
< li class = "header" > 概念与原理< / li >
< li class = "chapter " data-level = "4.1" data-path = "../concepts/" >
< a href = "../concepts/" >
< b > 4.1.< / b >
Kubernetes 架构
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.1.1" data-path = "../concepts/concepts.html" >
< a href = "../concepts/concepts.html" >
< b > 4.1.1.< / b >
设计理念
< / a >
< / li >
< li class = "chapter " data-level = "4.1.2" data-path = "../concepts/etcd.html" >
< a href = "../concepts/etcd.html" >
< b > 4.1.2.< / b >
Etcd 解析
< / a >
< / li >
< li class = "chapter " data-level = "4.1.3" data-path = "../concepts/open-interfaces.html" >
< a href = "../concepts/open-interfaces.html" >
< b > 4.1.3.< / b >
开放接口
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.1.3.1" data-path = "../concepts/cri.html" >
< a href = "../concepts/cri.html" >
< b > 4.1.3.1.< / b >
CRI - Container Runtime Interface( 容器运行时接口)
< / a >
< / li >
< li class = "chapter " data-level = "4.1.3.2" data-path = "../concepts/cni.html" >
< a href = "../concepts/cni.html" >
< b > 4.1.3.2.< / b >
CNI - Container Network Interface( 容器网络接口)
< / a >
< / li >
< li class = "chapter " data-level = "4.1.3.3" data-path = "../concepts/csi.html" >
< a href = "../concepts/csi.html" >
< b > 4.1.3.3.< / b >
CSI - Container Storage Interface( 容器存储接口)
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.1.4" data-path = "../concepts/objects.html" >
2021-07-01 10:02:38 +08:00
2021-07-09 16:57:54 +08:00
< a href = "../concepts/objects.html" >
2021-07-01 10:02:38 +08:00
2021-07-09 16:57:54 +08:00
< b > 4.1.4.< / b >
2021-07-01 10:02:38 +08:00
2021-07-09 16:57:54 +08:00
资源对象与基本概念解析
2021-07-01 10:02:38 +08:00
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2" data-path = "../concepts/pod-state-and-lifecycle.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-state-and-lifecycle.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.< / b >
2021-07-01 10:02:38 +08:00
Pod 状态与生命周期管理
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.1" data-path = "../concepts/pod-overview.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-overview.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.1.< / b >
2021-07-01 10:02:38 +08:00
Pod 概览
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.2" data-path = "../concepts/pod.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.2.< / b >
2021-07-01 10:02:38 +08:00
Pod 解析
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.3" data-path = "../concepts/init-containers.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/init-containers.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.3.< / b >
2021-07-01 10:02:38 +08:00
Init 容器
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.4" data-path = "../concepts/pause-container.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pause-container.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.4.< / b >
2021-07-01 10:02:38 +08:00
Pause 容器
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.5" data-path = "../concepts/pod-security-policy.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-security-policy.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.5.< / b >
2021-07-01 10:02:38 +08:00
Pod 安全策略
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.6" data-path = "../concepts/pod-lifecycle.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-lifecycle.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.6.< / b >
2021-07-01 10:02:38 +08:00
Pod 的生命周期
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.7" data-path = "../concepts/pod-hook.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-hook.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.7.< / b >
2021-07-01 10:02:38 +08:00
Pod Hook
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.8" data-path = "../concepts/pod-preset.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-preset.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.8.< / b >
2021-07-01 10:02:38 +08:00
Pod Preset
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.2.9" data-path = "../concepts/pod-disruption-budget.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/pod-disruption-budget.html" >
2021-07-09 16:57:54 +08:00
< b > 4.2.9.< / b >
2021-07-01 10:02:38 +08:00
Pod 中断与 PDB( Pod 中断预算)
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3" data-path = "../concepts/cluster.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/cluster.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.< / b >
2021-07-01 10:02:38 +08:00
集群资源管理
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3.1" data-path = "../concepts/node.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/node.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.1.< / b >
2021-07-01 10:02:38 +08:00
Node
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3.2" data-path = "../concepts/namespace.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/namespace.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.2.< / b >
2021-07-01 10:02:38 +08:00
Namespace
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3.3" data-path = "../concepts/label.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/label.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.3.< / b >
2021-07-01 10:02:38 +08:00
Label
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3.4" data-path = "../concepts/annotation.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/annotation.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.4.< / b >
2021-07-01 10:02:38 +08:00
Annotation
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3.5" data-path = "../concepts/taint-and-toleration.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/taint-and-toleration.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.5.< / b >
2021-07-01 10:02:38 +08:00
Taint 和 Toleration( 污点和容忍)
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.3.6" data-path = "../concepts/garbage-collection.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/garbage-collection.html" >
2021-07-09 16:57:54 +08:00
< b > 4.3.6.< / b >
2021-07-01 10:02:38 +08:00
垃圾收集
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4" data-path = "../concepts/controllers.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/controllers.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.< / b >
2021-07-01 10:02:38 +08:00
控制器
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.1" data-path = "../concepts/deployment.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/deployment.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.1.< / b >
2021-07-01 10:02:38 +08:00
Deployment
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.2" data-path = "../concepts/statefulset.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/statefulset.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.2.< / b >
2021-07-01 10:02:38 +08:00
StatefulSet
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.3" data-path = "../concepts/daemonset.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/daemonset.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.3.< / b >
2021-07-01 10:02:38 +08:00
DaemonSet
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.4" data-path = "../concepts/replicaset.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/replicaset.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.4.< / b >
2021-07-01 10:02:38 +08:00
ReplicationController 和 ReplicaSet
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.5" data-path = "../concepts/job.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/job.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.5.< / b >
2021-07-01 10:02:38 +08:00
Job
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.6" data-path = "../concepts/cronjob.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/cronjob.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.6.< / b >
2021-07-01 10:02:38 +08:00
CronJob
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.7" data-path = "../concepts/horizontal-pod-autoscaling.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/horizontal-pod-autoscaling.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.7.< / b >
2021-07-01 10:02:38 +08:00
Horizontal Pod Autoscaling
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.7.1" data-path = "../concepts/custom-metrics-hpa.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/custom-metrics-hpa.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.7.1.< / b >
2021-07-01 10:02:38 +08:00
自定义指标 HPA
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.4.8" data-path = "../concepts/admission-controller.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/admission-controller.html" >
2021-07-09 16:57:54 +08:00
< b > 4.4.8.< / b >
2021-07-01 10:02:38 +08:00
准入控制器( Admission Controller)
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.5" data-path = "../concepts/service-discovery.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/service-discovery.html" >
2021-07-09 16:57:54 +08:00
< b > 4.5.< / b >
2021-07-01 10:02:38 +08:00
2021-07-09 16:57:54 +08:00
服务发现与路由
2021-07-01 10:02:38 +08:00
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.5.1" data-path = "../concepts/service.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/service.html" >
2021-07-09 16:57:54 +08:00
< b > 4.5.1.< / b >
2021-07-01 10:02:38 +08:00
Service
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.5.2" data-path = "../concepts/topology-aware-routing.html" >
< a href = "../concepts/topology-aware-routing.html" >
< b > 4.5.2.< / b >
拓扑感知路由
< / a >
< / li >
< li class = "chapter " data-level = "4.5.3" data-path = "../concepts/ingress.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/ingress.html" >
2021-07-09 16:57:54 +08:00
< b > 4.5.3.< / b >
2021-07-01 10:02:38 +08:00
Ingress
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.5.3.1" data-path = "../concepts/traefik-ingress-controller.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/traefik-ingress-controller.html" >
2021-07-09 16:57:54 +08:00
< b > 4.5.3.1.< / b >
2021-07-01 10:02:38 +08:00
Traefik Ingress Controller
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.5.4" data-path = "../concepts/kubernetes-service-api.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/kubernetes-service-api.html" >
2021-07-09 16:57:54 +08:00
< b > 4.5.4.< / b >
2021-07-01 10:02:38 +08:00
Kubernetes Service API
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.5.4.1" data-path = "../concepts/service-api-overview.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/service-api-overview.html" >
2021-07-09 16:57:54 +08:00
< b > 4.5.4.1.< / b >
2021-07-01 10:02:38 +08:00
Service API 简介
< / a >
< / li >
< / ul >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.6" data-path = "../concepts/authentication-and-permission.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/authentication-and-permission.html" >
2021-07-09 16:57:54 +08:00
< b > 4.6.< / b >
2021-07-01 10:02:38 +08:00
身份与权限控制
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.6.1" data-path = "../concepts/serviceaccount.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/serviceaccount.html" >
2021-07-09 16:57:54 +08:00
< b > 4.6.1.< / b >
2021-07-01 10:02:38 +08:00
ServiceAccount
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.6.2" data-path = "../concepts/rbac.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/rbac.html" >
2021-07-09 16:57:54 +08:00
< b > 4.6.2.< / b >
2021-07-01 10:02:38 +08:00
RBAC—— 基于角色的访问控制
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.6.3" data-path = "../concepts/network-policy.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/network-policy.html" >
2021-07-09 16:57:54 +08:00
< b > 4.6.3.< / b >
2021-07-01 10:02:38 +08:00
NetworkPolicy
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.7" data-path = "../concepts/networking.html" >
< a href = "../concepts/networking.html" >
< b > 4.7.< / b >
网络
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.7.1" data-path = "../concepts/flannel.html" >
< a href = "../concepts/flannel.html" >
< b > 4.7.1.< / b >
Kubernetes 中的网络解析 —— 以 flannel 为例
< / a >
< / li >
< li class = "chapter " data-level = "4.7.2" data-path = "../concepts/calico.html" >
< a href = "../concepts/calico.html" >
< b > 4.7.2.< / b >
Kubernetes 中的网络解析 —— 以 calico 为例
< / a >
< / li >
< li class = "chapter " data-level = "4.7.3" data-path = "../concepts/cilium.html" >
< a href = "../concepts/cilium.html" >
< b > 4.7.3.< / b >
具备 API 感知的网络和安全性管理开源软件 Cilium
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "4.7.3.1" data-path = "../concepts/cilium-concepts.html" >
< a href = "../concepts/cilium-concepts.html" >
< b > 4.7.3.1.< / b >
Cilium 架构设计与概念解析
< / a >
< / li >
< / ul >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "4.8" data-path = "../concepts/storage.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/storage.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.< / b >
2021-07-01 10:02:38 +08:00
存储
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.1" data-path = "../concepts/secret.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/secret.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.1.< / b >
2021-07-01 10:02:38 +08:00
Secret
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.2" data-path = "../concepts/configmap.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/configmap.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.2.< / b >
2021-07-01 10:02:38 +08:00
ConfigMap
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.2.1" data-path = "../concepts/configmap-hot-update.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/configmap-hot-update.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.2.1.< / b >
2021-07-01 10:02:38 +08:00
ConfigMap 的热更新
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.3" data-path = "../concepts/volume.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/volume.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.3.< / b >
2021-07-01 10:02:38 +08:00
Volume
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.4" data-path = "../concepts/persistent-volume.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/persistent-volume.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.4.< / b >
2021-07-01 10:02:38 +08:00
Persistent Volume( 持久化卷)
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.5" data-path = "../concepts/storageclass.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/storageclass.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.5.< / b >
2021-07-01 10:02:38 +08:00
Storage Class
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.8.6" data-path = "../concepts/local-persistent-storage.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/local-persistent-storage.html" >
2021-07-09 16:57:54 +08:00
< b > 4.8.6.< / b >
2021-07-01 10:02:38 +08:00
本地持久化存储
< / a >
< / li >
< / ul >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.9" data-path = "../concepts/extension.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/extension.html" >
2021-07-09 16:57:54 +08:00
< b > 4.9.< / b >
2021-07-01 10:02:38 +08:00
集群扩展
< / a >
< ul class = "articles" >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.9.1" data-path = "../concepts/custom-resource.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/custom-resource.html" >
2021-07-09 16:57:54 +08:00
< b > 4.9.1.< / b >
2021-07-01 10:02:38 +08:00
使用自定义资源扩展 API
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.9.2" data-path = "../concepts/crd.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/crd.html" >
2021-07-09 16:57:54 +08:00
< b > 4.9.2.< / b >
2021-07-01 10:02:38 +08:00
使用 CRD 扩展 Kubernetes API
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.9.3" data-path = "../concepts/aggregated-api-server.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/aggregated-api-server.html" >
2021-07-09 16:57:54 +08:00
< b > 4.9.3.< / b >
2021-07-01 10:02:38 +08:00
Aggregated API Server
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.9.4" data-path = "../concepts/apiservice.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/apiservice.html" >
2021-07-09 16:57:54 +08:00
< b > 4.9.4.< / b >
2021-07-01 10:02:38 +08:00
APIService
< / a >
< / li >
2021-07-09 16:57:54 +08:00
< li class = "chapter " data-level = "4.9.5" data-path = "../concepts/service-catalog.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/service-catalog.html" >
2021-07-09 16:57:54 +08:00
< b > 4.9.5.< / b >
2021-07-01 10:02:38 +08:00
Service Catalog
< / a >
< / li >
2021-10-15 13:19:39 +08:00
< / ul >
< / li >
< li class = "chapter " data-level = "4.10" data-path = "../concepts/multicluster.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/multicluster.html" >
2021-10-15 13:19:39 +08:00
< b > 4.10.< / b >
2021-07-01 10:02:38 +08:00
多集群管理
< / a >
2021-10-15 13:19:39 +08:00
< ul class = "articles" >
< li class = "chapter " data-level = "4.10.1" data-path = "../concepts/multi-cluster-services-api.html" >
< a href = "../concepts/multi-cluster-services-api.html" >
< b > 4.10.1.< / b >
Multi-Cluster Services API( 多集群服务 API )
< / a >
2021-07-01 10:02:38 +08:00
< / li >
< / ul >
< / li >
2021-10-15 13:19:39 +08:00
< li class = "chapter " data-level = "4.11" data-path = "../concepts/scheduling.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/scheduling.html" >
2021-10-15 13:19:39 +08:00
< b > 4.11.< / b >
2021-07-01 10:02:38 +08:00
资源调度
< / a >
< ul class = "articles" >
2021-10-15 13:19:39 +08:00
< li class = "chapter " data-level = "4.11.1" data-path = "../concepts/qos.html" >
2021-07-01 10:02:38 +08:00
< a href = "../concepts/qos.html" >
2021-10-15 13:19:39 +08:00
< b > 4.11.1.< / b >
2021-07-01 10:02:38 +08:00
QoS( 服务质量等级)
< / a >
< / li >
< / ul >
< / li >
< li class = "header" > 用户指南< / li >
< li class = "chapter " data-level = "5.1" data-path = "../guide/" >
< a href = "../guide/" >
< b > 5.1.< / b >
用户指南概览
< / a >
< / li >
< li class = "chapter " data-level = "5.2" data-path = "../guide/resource-configuration.html" >
< a href = "../guide/resource-configuration.html" >
< b > 5.2.< / b >
资源对象配置
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.2.1" data-path = "../guide/configure-liveness-readiness-probes.html" >
< a href = "../guide/configure-liveness-readiness-probes.html" >
< b > 5.2.1.< / b >
配置 Pod 的 liveness 和 readiness 探针
< / a >
< / li >
< li class = "chapter " data-level = "5.2.2" data-path = "../guide/configure-pod-service-account.html" >
< a href = "../guide/configure-pod-service-account.html" >
< b > 5.2.2.< / b >
配置 Pod 的 Service Account
< / a >
< / li >
< li class = "chapter " data-level = "5.2.3" data-path = "../guide/secret-configuration.html" >
< a href = "../guide/secret-configuration.html" >
< b > 5.2.3.< / b >
Secret 配置
< / a >
< / li >
< li class = "chapter " data-level = "5.2.4" data-path = "../guide/resource-quota-management.html" >
< a href = "../guide/resource-quota-management.html" >
< b > 5.2.4.< / b >
管理 namespace 中的资源配额
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5.3" data-path = "../guide/command-usage.html" >
< a href = "../guide/command-usage.html" >
< b > 5.3.< / b >
命令使用
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.3.1" data-path = "../guide/docker-cli-to-kubectl.html" >
< a href = "../guide/docker-cli-to-kubectl.html" >
< b > 5.3.1.< / b >
Docker 用户过渡到 kubectl 命令行指南
< / a >
< / li >
< li class = "chapter " data-level = "5.3.2" data-path = "../guide/using-kubectl.html" >
< a href = "../guide/using-kubectl.html" >
< b > 5.3.2.< / b >
kubectl 命令概览
< / a >
< / li >
< li class = "chapter " data-level = "5.3.3" data-path = "../guide/kubectl-cheatsheet.html" >
< a href = "../guide/kubectl-cheatsheet.html" >
< b > 5.3.3.< / b >
kubectl 命令技巧大全
< / a >
< / li >
< li class = "chapter " data-level = "5.3.4" data-path = "../guide/using-etcdctl-to-access-kubernetes-data.html" >
< a href = "../guide/using-etcdctl-to-access-kubernetes-data.html" >
< b > 5.3.4.< / b >
2021-08-04 16:20:42 +08:00
使用 etcdctl 访问 Kubernetes 数据
2021-07-01 10:02:38 +08:00
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5.4" data-path = "../guide/cluster-security-management.html" >
< a href = "../guide/cluster-security-management.html" >
< b > 5.4.< / b >
集群安全性管理
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.4.1" data-path = "../guide/managing-tls-in-a-cluster.html" >
< a href = "../guide/managing-tls-in-a-cluster.html" >
< b > 5.4.1.< / b >
管理集群中的 TLS
< / a >
< / li >
< li class = "chapter " data-level = "5.4.2" data-path = "../guide/kubelet-authentication-authorization.html" >
< a href = "../guide/kubelet-authentication-authorization.html" >
< b > 5.4.2.< / b >
kubelet 的认证授权
< / a >
< / li >
< li class = "chapter " data-level = "5.4.3" data-path = "../guide/tls-bootstrapping.html" >
< a href = "../guide/tls-bootstrapping.html" >
< b > 5.4.3.< / b >
TLS Bootstrap
< / a >
< / li >
< li class = "chapter " data-level = "5.4.4" data-path = "../guide/kubectl-user-authentication-authorization.html" >
< a href = "../guide/kubectl-user-authentication-authorization.html" >
< b > 5.4.4.< / b >
创建用户认证授权的 kubeconfig 文件
< / a >
< / li >
< li class = "chapter " data-level = "5.4.5" data-path = "../guide/ip-masq-agent.html" >
< a href = "../guide/ip-masq-agent.html" >
< b > 5.4.5.< / b >
IP 伪装代理
< / a >
< / li >
< li class = "chapter " data-level = "5.4.6" data-path = "../guide/auth-with-kubeconfig-or-token.html" >
< a href = "../guide/auth-with-kubeconfig-or-token.html" >
< b > 5.4.6.< / b >
使用 kubeconfig 或 token 进行用户身份认证
< / a >
< / li >
< li class = "chapter " data-level = "5.4.7" data-path = "../guide/authentication.html" >
< a href = "../guide/authentication.html" >
< b > 5.4.7.< / b >
Kubernetes 中的用户与身份认证授权
< / a >
< / li >
< li class = "chapter " data-level = "5.4.8" data-path = "../guide/kubernetes-security-best-practice.html" >
< a href = "../guide/kubernetes-security-best-practice.html" >
< b > 5.4.8.< / b >
Kubernetes 集群安全性配置最佳实践
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5.5" data-path = "../guide/access-kubernetes-cluster.html" >
< a href = "../guide/access-kubernetes-cluster.html" >
< b > 5.5.< / b >
访问 Kubernetes 集群
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.5.1" data-path = "../guide/access-cluster.html" >
< a href = "../guide/access-cluster.html" >
< b > 5.5.1.< / b >
访问集群
< / a >
< / li >
< li class = "chapter " data-level = "5.5.2" data-path = "../guide/authenticate-across-clusters-kubeconfig.html" >
< a href = "../guide/authenticate-across-clusters-kubeconfig.html" >
< b > 5.5.2.< / b >
使用 kubeconfig 文件配置跨集群认证
< / a >
< / li >
< li class = "chapter " data-level = "5.5.3" data-path = "../guide/connecting-to-applications-port-forward.html" >
< a href = "../guide/connecting-to-applications-port-forward.html" >
< b > 5.5.3.< / b >
通过端口转发访问集群中的应用程序
< / a >
< / li >
< li class = "chapter " data-level = "5.5.4" data-path = "../guide/service-access-application-cluster.html" >
< a href = "../guide/service-access-application-cluster.html" >
< b > 5.5.4.< / b >
使用 service 访问群集中的应用程序
< / a >
< / li >
< li class = "chapter " data-level = "5.5.5" data-path = "../guide/accessing-kubernetes-pods-from-outside-of-the-cluster.html" >
< a href = "../guide/accessing-kubernetes-pods-from-outside-of-the-cluster.html" >
< b > 5.5.5.< / b >
从外部访问 Kubernetes 中的 Pod
< / a >
< / li >
< li class = "chapter " data-level = "5.5.6" data-path = "../guide/cabin-mobile-dashboard-for-kubernetes.html" >
< a href = "../guide/cabin-mobile-dashboard-for-kubernetes.html" >
< b > 5.5.6.< / b >
Cabin - Kubernetes 手机客户端
< / a >
< / li >
< li class = "chapter " data-level = "5.5.7" data-path = "../guide/kubernetes-desktop-client.html" >
< a href = "../guide/kubernetes-desktop-client.html" >
< b > 5.5.7.< / b >
Lens - Kubernetes IDE/桌面客户端
< / a >
< / li >
< li class = "chapter " data-level = "5.5.8" data-path = "../guide/kubernator-kubernetes-ui.html" >
< a href = "../guide/kubernator-kubernetes-ui.html" >
< b > 5.5.8.< / b >
Kubernator - 更底层的 Kubernetes UI
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "5.6" data-path = "../guide/application-development-deployment-flow.html" >
< a href = "../guide/application-development-deployment-flow.html" >
< b > 5.6.< / b >
在 Kubernetes 中开发部署应用
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "5.6.1" data-path = "../guide/deploy-applications-in-kubernetes.html" >
< a href = "../guide/deploy-applications-in-kubernetes.html" >
< b > 5.6.1.< / b >
2021-08-04 16:20:42 +08:00
适用于 Kubernetes 的应用开发部署流程
2021-07-01 10:02:38 +08:00
< / a >
< / li >
< li class = "chapter " data-level = "5.6.2" data-path = "../guide/migrating-hadoop-yarn-to-kubernetes.html" >
< a href = "../guide/migrating-hadoop-yarn-to-kubernetes.html" >
< b > 5.6.2.< / b >
迁移传统应用到 Kubernetes 中 —— 以 Hadoop YARN 为例
< / a >
< / li >
< li class = "chapter " data-level = "5.6.3" data-path = "../guide/using-statefulset.html" >
< a href = "../guide/using-statefulset.html" >
< b > 5.6.3.< / b >
使用 StatefulSet 部署用状态应用
< / a >
< / li >
< / ul >
< / li >
< li class = "header" > 最佳实践< / li >
< li class = "chapter " data-level = "6.1" data-path = "../practice/" >
< a href = "../practice/" >
< b > 6.1.< / b >
最佳实践概览
< / a >
< / li >
< li class = "chapter " data-level = "6.2" data-path = "../practice/install-kubernetes-on-centos.html" >
< a href = "../practice/install-kubernetes-on-centos.html" >
< b > 6.2.< / b >
在 CentOS 上部署 Kubernetes 集群
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.2.1" data-path = "../practice/create-tls-and-secret-key.html" >
< a href = "../practice/create-tls-and-secret-key.html" >
< b > 6.2.1.< / b >
创建 TLS 证书和秘钥
< / a >
< / li >
< li class = "chapter " data-level = "6.2.2" data-path = "../practice/create-kubeconfig.html" >
< a href = "../practice/create-kubeconfig.html" >
< b > 6.2.2.< / b >
创建 kubeconfig 文件
< / a >
< / li >
< li class = "chapter " data-level = "6.2.3" data-path = "../practice/etcd-cluster-installation.html" >
< a href = "../practice/etcd-cluster-installation.html" >
< b > 6.2.3.< / b >
创建高可用 etcd 集群
< / a >
< / li >
< li class = "chapter " data-level = "6.2.4" data-path = "../practice/kubectl-installation.html" >
< a href = "../practice/kubectl-installation.html" >
< b > 6.2.4.< / b >
安装 kubectl 命令行工具
< / a >
< / li >
< li class = "chapter " data-level = "6.2.5" data-path = "../practice/master-installation.html" >
< a href = "../practice/master-installation.html" >
< b > 6.2.5.< / b >
部署 master 节点
< / a >
< / li >
< li class = "chapter " data-level = "6.2.6" data-path = "../practice/flannel-installation.html" >
< a href = "../practice/flannel-installation.html" >
< b > 6.2.6.< / b >
安装 flannel 网络插件
< / a >
< / li >
< li class = "chapter " data-level = "6.2.7" data-path = "../practice/node-installation.html" >
< a href = "../practice/node-installation.html" >
< b > 6.2.7.< / b >
部署 node 节点
< / a >
< / li >
< li class = "chapter " data-level = "6.2.8" data-path = "../practice/kubedns-addon-installation.html" >
< a href = "../practice/kubedns-addon-installation.html" >
< b > 6.2.8.< / b >
安装 kubedns 插件
< / a >
< / li >
< li class = "chapter " data-level = "6.2.9" data-path = "../practice/dashboard-addon-installation.html" >
< a href = "../practice/dashboard-addon-installation.html" >
< b > 6.2.9.< / b >
安装 dashboard 插件
< / a >
< / li >
< li class = "chapter " data-level = "6.2.10" data-path = "../practice/heapster-addon-installation.html" >
< a href = "../practice/heapster-addon-installation.html" >
< b > 6.2.10.< / b >
安装 heapster 插件
< / a >
< / li >
< li class = "chapter " data-level = "6.2.11" data-path = "../practice/efk-addon-installation.html" >
< a href = "../practice/efk-addon-installation.html" >
< b > 6.2.11.< / b >
安装 EFK 插件
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.3" data-path = "../practice/install-kubernetes-with-kubeadm.html" >
< a href = "../practice/install-kubernetes-with-kubeadm.html" >
< b > 6.3.< / b >
生产级的 Kubernetes 简化管理工具 kubeadm
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.3.1" data-path = "../practice/install-kubernetes-on-ubuntu-server-16.04-with-kubeadm.html" >
< a href = "../practice/install-kubernetes-on-ubuntu-server-16.04-with-kubeadm.html" >
< b > 6.3.1.< / b >
使用 kubeadm 在 Ubuntu Server 16.04 上快速构建测试集群
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.4" data-path = "../practice/service-discovery-and-loadbalancing.html" >
< a href = "../practice/service-discovery-and-loadbalancing.html" >
< b > 6.4.< / b >
服务发现与负载均衡
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.4.1" data-path = "../practice/traefik-ingress-installation.html" >
< a href = "../practice/traefik-ingress-installation.html" >
< b > 6.4.1.< / b >
安装 Traefik ingress
< / a >
< / li >
< li class = "chapter " data-level = "6.4.2" data-path = "../practice/distributed-load-test.html" >
< a href = "../practice/distributed-load-test.html" >
< b > 6.4.2.< / b >
分布式负载测试
< / a >
< / li >
< li class = "chapter " data-level = "6.4.3" data-path = "../practice/network-and-cluster-perfermance-test.html" >
< a href = "../practice/network-and-cluster-perfermance-test.html" >
< b > 6.4.3.< / b >
网络和集群性能测试
< / a >
< / li >
< li class = "chapter " data-level = "6.4.4" data-path = "../practice/edge-node-configuration.html" >
< a href = "../practice/edge-node-configuration.html" >
< b > 6.4.4.< / b >
边缘节点配置
< / a >
< / li >
< li class = "chapter " data-level = "6.4.5" data-path = "../practice/nginx-ingress-installation.html" >
< a href = "../practice/nginx-ingress-installation.html" >
< b > 6.4.5.< / b >
安装 Nginx ingress
< / a >
< / li >
< li class = "chapter " data-level = "6.4.6" data-path = "../practice/dns-installation.html" >
< a href = "../practice/dns-installation.html" >
< b > 6.4.6.< / b >
安装配置 DNS
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.4.6.1" data-path = "../practice/configuring-dns.html" >
< a href = "../practice/configuring-dns.html" >
< b > 6.4.6.1.< / b >
安装配置 Kube-dns
< / a >
< / li >
< li class = "chapter " data-level = "6.4.6.2" data-path = "../practice/coredns.html" >
< a href = "../practice/coredns.html" >
< b > 6.4.6.2.< / b >
安装配置 CoreDNS
< / a >
< / li >
< / ul >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.5" data-path = "../practice/operation.html" >
< a href = "../practice/operation.html" >
< b > 6.5.< / b >
运维管理
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.5.1" data-path = "../practice/master-ha.html" >
< a href = "../practice/master-ha.html" >
< b > 6.5.1.< / b >
Master 节点高可用
< / a >
< / li >
< li class = "chapter " data-level = "6.5.2" data-path = "../practice/service-rolling-update.html" >
< a href = "../practice/service-rolling-update.html" >
< b > 6.5.2.< / b >
服务滚动升级
< / a >
< / li >
< li class = "chapter " data-level = "6.5.3" data-path = "../practice/app-log-collection.html" >
< a href = "../practice/app-log-collection.html" >
< b > 6.5.3.< / b >
应用日志收集
< / a >
< / li >
< li class = "chapter " data-level = "6.5.4" data-path = "../practice/configuration-best-practice.html" >
< a href = "../practice/configuration-best-practice.html" >
< b > 6.5.4.< / b >
配置最佳实践
< / a >
< / li >
< li class = "chapter " data-level = "6.5.5" data-path = "../practice/monitor.html" >
< a href = "../practice/monitor.html" >
< b > 6.5.5.< / b >
集群及应用监控
< / a >
< / li >
< li class = "chapter " data-level = "6.5.6" data-path = "../practice/data-persistence-problem.html" >
< a href = "../practice/data-persistence-problem.html" >
< b > 6.5.6.< / b >
数据持久化问题
< / a >
< / li >
< li class = "chapter " data-level = "6.5.7" data-path = "../practice/manage-compute-resources-container.html" >
< a href = "../practice/manage-compute-resources-container.html" >
< b > 6.5.7.< / b >
管理容器的计算资源
< / a >
< / li >
< li class = "chapter " data-level = "6.5.8" data-path = "../practice/federation.html" >
< a href = "../practice/federation.html" >
< b > 6.5.8.< / b >
集群联邦( Cluster Federation)
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.6" data-path = "../practice/storage.html" >
< a href = "../practice/storage.html" >
< b > 6.6.< / b >
存储管理
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.6.1" data-path = "../practice/glusterfs.html" >
< a href = "../practice/glusterfs.html" >
< b > 6.6.1.< / b >
GlusterFS
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.6.1.1" data-path = "../practice/using-glusterfs-for-persistent-storage.html" >
< a href = "../practice/using-glusterfs-for-persistent-storage.html" >
< b > 6.6.1.1.< / b >
使用 GlusterFS 做持久化存储
< / a >
< / li >
< li class = "chapter " data-level = "6.6.1.2" data-path = "../practice/using-heketi-gluster-for-persistent-storage.html" >
< a href = "../practice/using-heketi-gluster-for-persistent-storage.html" >
< b > 6.6.1.2.< / b >
使用 Heketi 作为 Kubernetes 的持久存储 GlusterFS 的 external provisioner
< / a >
< / li >
< li class = "chapter " data-level = "6.6.1.3" data-path = "../practice/storage-for-containers-using-glusterfs-with-openshift.html" >
< a href = "../practice/storage-for-containers-using-glusterfs-with-openshift.html" >
< b > 6.6.1.3.< / b >
在 OpenShift 中使用 GlusterFS 做持久化存储
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.6.2" data-path = "../practice/glusterd-2.0.html" >
< a href = "../practice/glusterd-2.0.html" >
< b > 6.6.2.< / b >
GlusterD-2.0
< / a >
< / li >
< li class = "chapter " data-level = "6.6.3" data-path = "../practice/ceph.html" >
< a href = "../practice/ceph.html" >
< b > 6.6.3.< / b >
Ceph
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.6.3.1" data-path = "../practice/ceph-helm-install-guide-zh.html" >
< a href = "../practice/ceph-helm-install-guide-zh.html" >
< b > 6.6.3.1.< / b >
用 Helm 托管安装 Ceph 集群并提供后端存储
< / a >
< / li >
< li class = "chapter " data-level = "6.6.3.2" data-path = "../practice/using-ceph-for-persistent-storage.html" >
< a href = "../practice/using-ceph-for-persistent-storage.html" >
< b > 6.6.3.2.< / b >
使用 Ceph 做持久化存储
< / a >
< / li >
< li class = "chapter " data-level = "6.6.3.3" data-path = "../practice/rbd-provisioner.html" >
< a href = "../practice/rbd-provisioner.html" >
< b > 6.6.3.3.< / b >
使用 rbd-provisioner 提供 rbd 持久化存储
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.6.4" data-path = "../practice/openebs.html" >
< a href = "../practice/openebs.html" >
< b > 6.6.4.< / b >
OpenEBS
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.6.4.1" data-path = "../practice/using-openebs-for-persistent-storage.html" >
< a href = "../practice/using-openebs-for-persistent-storage.html" >
< b > 6.6.4.1.< / b >
使用 OpenEBS 做持久化存储
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.6.5" data-path = "../practice/rook.html" >
< a href = "../practice/rook.html" >
< b > 6.6.5.< / b >
Rook
< / a >
< / li >
< li class = "chapter " data-level = "6.6.6" data-path = "../practice/nfs.html" >
< a href = "../practice/nfs.html" >
< b > 6.6.6.< / b >
NFS
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.6.6.1" data-path = "../practice/using-nfs-for-persistent-storage.html" >
< a href = "../practice/using-nfs-for-persistent-storage.html" >
< b > 6.6.6.1.< / b >
利用 NFS 动态提供 Kubernetes 后端存储卷
< / a >
< / li >
< / ul >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.7" data-path = "../practice/monitoring.html" >
< a href = "../practice/monitoring.html" >
< b > 6.7.< / b >
集群与应用监控
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.7.1" data-path = "../practice/heapster.html" >
< a href = "../practice/heapster.html" >
< b > 6.7.1.< / b >
Heapster
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.7.1.1" data-path = "../practice/using-heapster-to-get-object-metrics.html" >
< a href = "../practice/using-heapster-to-get-object-metrics.html" >
< b > 6.7.1.1.< / b >
使用 Heapster 获取集群和对象的 metric 数据
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.7.2" data-path = "../practice/prometheus.html" >
< a href = "../practice/prometheus.html" >
< b > 6.7.2.< / b >
Prometheus
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.7.2.1" data-path = "../practice/using-prometheus-to-monitor-kuberentes-cluster.html" >
< a href = "../practice/using-prometheus-to-monitor-kuberentes-cluster.html" >
< b > 6.7.2.1.< / b >
使用 Prometheus 监控 kubernetes 集群
< / a >
< / li >
< li class = "chapter " data-level = "6.7.2.2" data-path = "../practice/promql.html" >
< a href = "../practice/promql.html" >
< b > 6.7.2.2.< / b >
Prometheus 查询语言 PromQL 使用说明
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.7.3" data-path = "../practice/vistio-visualize-your-istio-mesh.html" >
< a href = "../practice/vistio-visualize-your-istio-mesh.html" >
< b > 6.7.3.< / b >
使用 Vistio 监控 Istio 服务网格中的流量
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.8" data-path = "../practice/distributed-tracing.html" >
< a href = "../practice/distributed-tracing.html" >
< b > 6.8.< / b >
分布式追踪
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.8.1" data-path = "../practice/opentracing.html" >
< a href = "../practice/opentracing.html" >
< b > 6.8.1.< / b >
OpenTracing
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.9" data-path = "../practice/services-management-tool.html" >
< a href = "../practice/services-management-tool.html" >
< b > 6.9.< / b >
服务编排管理
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.9.1" data-path = "../practice/helm.html" >
< a href = "../practice/helm.html" >
< b > 6.9.1.< / b >
使用 Helm 管理 Kubernetes 应用
< / a >
< / li >
< li class = "chapter " data-level = "6.9.2" data-path = "../practice/create-private-charts-repo.html" >
< a href = "../practice/create-private-charts-repo.html" >
< b > 6.9.2.< / b >
构建私有 Chart 仓库
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.10" data-path = "../practice/ci-cd.html" >
< a href = "../practice/ci-cd.html" >
< b > 6.10.< / b >
持续集成与发布
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.10.1" data-path = "../practice/jenkins-ci-cd.html" >
< a href = "../practice/jenkins-ci-cd.html" >
< b > 6.10.1.< / b >
使用 Jenkins 进行持续集成与发布
< / a >
< / li >
< li class = "chapter " data-level = "6.10.2" data-path = "../practice/drone-ci-cd.html" >
< a href = "../practice/drone-ci-cd.html" >
< b > 6.10.2.< / b >
使用 Drone 进行持续集成与发布
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.11" data-path = "../practice/update-and-upgrade.html" >
< a href = "../practice/update-and-upgrade.html" >
< b > 6.11.< / b >
更新与升级
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.11.1" data-path = "../practice/manually-upgrade.html" >
< a href = "../practice/manually-upgrade.html" >
< b > 6.11.1.< / b >
手动升级 Kubernetes 集群
< / a >
< / li >
< li class = "chapter " data-level = "6.11.2" data-path = "../practice/dashboard-upgrade.html" >
< a href = "../practice/dashboard-upgrade.html" >
< b > 6.11.2.< / b >
升级 dashboard
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.12" data-path = "../practice/controller-extended.html" >
< a href = "../practice/controller-extended.html" >
< b > 6.12.< / b >
扩展控制器
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.12.1" data-path = "../practice/openkruise.html" >
< a href = "../practice/openkruise.html" >
< b > 6.12.1.< / b >
OpenKruise
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.12.1.1" data-path = "../practice/in-place-update.html" >
< a href = "../practice/in-place-update.html" >
< b > 6.12.1.1.< / b >
原地升级
< / a >
< / li >
< / ul >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "6.13" data-path = "../practice/security-policy.html" >
< a href = "../practice/security-policy.html" >
< b > 6.13.< / b >
安全策略
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "6.13.1" data-path = "../practice/open-policy-agent.html" >
< a href = "../practice/open-policy-agent.html" >
< b > 6.13.1.< / b >
OPA( 开放策略代理)
< / a >
2021-08-06 14:42:13 +08:00
< / li >
< li class = "chapter " data-level = "6.13.2" data-path = "../practice/cloud-native-security.html" >
< a href = "../practice/cloud-native-security.html" >
< b > 6.13.2.< / b >
云原生安全
< / a >
2021-07-01 10:02:38 +08:00
< / li >
< / ul >
< / li >
< li class = "header" > 服务网格< / li >
< li class = "chapter " data-level = "7.1" data-path = "../usecases/service-mesh.html" >
< a href = "../usecases/service-mesh.html" >
< b > 7.1.< / b >
服务网格( Service Mesh)
< / a >
< / li >
< li class = "chapter " data-level = "7.2" data-path = "../usecases/the-enterprise-path-to-service-mesh-architectures.html" >
< a href = "../usecases/the-enterprise-path-to-service-mesh-architectures.html" >
< b > 7.2.< / b >
企业级服务网格架构
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "7.2.1" data-path = "../usecases/service-mesh-fundamental.html" >
< a href = "../usecases/service-mesh-fundamental.html" >
< b > 7.2.1.< / b >
服务网格基础
< / a >
< / li >
< li class = "chapter " data-level = "7.2.2" data-path = "../usecases/comparing-service-mesh-technologies.html" >
< a href = "../usecases/comparing-service-mesh-technologies.html" >
< b > 7.2.2.< / b >
服务网格技术对比
< / a >
< / li >
< li class = "chapter " data-level = "7.2.3" data-path = "../usecases/service-mesh-vs-api-gateway.html" >
< a href = "../usecases/service-mesh-vs-api-gateway.html" >
< b > 7.2.3.< / b >
服务网格对比 API 网关
< / a >
< / li >
< li class = "chapter " data-level = "7.2.4" data-path = "../usecases/service-mesh-adoption-and-evolution.html" >
< a href = "../usecases/service-mesh-adoption-and-evolution.html" >
< b > 7.2.4.< / b >
采纳和演进
< / a >
< / li >
< li class = "chapter " data-level = "7.2.5" data-path = "../usecases/service-mesh-customization-and-integration.html" >
< a href = "../usecases/service-mesh-customization-and-integration.html" >
< b > 7.2.5.< / b >
定制和集成
< / a >
< / li >
< li class = "chapter " data-level = "7.2.6" data-path = "../usecases/service-mesh-conclusion.html" >
< a href = "../usecases/service-mesh-conclusion.html" >
< b > 7.2.6.< / b >
总结
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "7.3" data-path = "../usecases/istio.html" >
< a href = "../usecases/istio.html" >
< b > 7.3.< / b >
Istio
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "7.3.1" data-path = "../usecases/before-using-istio.html" >
< a href = "../usecases/before-using-istio.html" >
< b > 7.3.1.< / b >
使用 Istio 前需要考虑的问题
< / a >
< / li >
< li class = "chapter " data-level = "7.3.2" data-path = "../usecases/istio-installation.html" >
< a href = "../usecases/istio-installation.html" >
< b > 7.3.2.< / b >
安装并试用 Istio
< / a >
< / li >
< li class = "chapter " data-level = "7.3.3" data-path = "../usecases/sidecar-spec-in-istio.html" >
< a href = "../usecases/sidecar-spec-in-istio.html" >
< b > 7.3.3.< / b >
Istio 中 sidecar 的注入规范及示例
< / a >
< / li >
< li class = "chapter " data-level = "7.3.4" data-path = "../usecases/istio-community-tips.html" >
< a href = "../usecases/istio-community-tips.html" >
< b > 7.3.4.< / b >
如何参与 Istio 社区及注意事项
< / a >
< / li >
< li class = "chapter " data-level = "7.3.5" data-path = "../usecases/istio-tutorials-collection.html" >
< a href = "../usecases/istio-tutorials-collection.html" >
< b > 7.3.5.< / b >
Istio 免费学习资源汇总
< / a >
< / li >
< li class = "chapter " data-level = "7.3.6" data-path = "../usecases/understand-sidecar-injection-and-traffic-hijack-in-istio-service-mesh.html" >
< a href = "../usecases/understand-sidecar-injection-and-traffic-hijack-in-istio-service-mesh.html" >
< b > 7.3.6.< / b >
Sidecar 的注入与流量劫持
< / a >
< / li >
< li class = "chapter " data-level = "7.3.7" data-path = "../usecases/envoy-sidecar-routing-of-istio-service-mesh-deep-dive.html" >
< a href = "../usecases/envoy-sidecar-routing-of-istio-service-mesh-deep-dive.html" >
< b > 7.3.7.< / b >
Envoy Sidecar 代理的路由转发
< / a >
< / li >
< li class = "chapter " data-level = "7.3.8" data-path = "../usecases/how-to-integrate-istio-with-vm.html" >
< a href = "../usecases/how-to-integrate-istio-with-vm.html" >
< b > 7.3.8.< / b >
Istio 如何支持虚拟机
< / a >
< / li >
< li class = "chapter " data-level = "7.3.9" data-path = "../usecases/istio-vm-support.html" >
< a href = "../usecases/istio-vm-support.html" >
< b > 7.3.9.< / b >
Istio 支持虚拟机的历史
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "7.4" data-path = "../usecases/envoy.html" >
< a href = "../usecases/envoy.html" >
< b > 7.4.< / b >
Envoy
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "7.4.1" data-path = "../usecases/envoy-terminology.html" >
< a href = "../usecases/envoy-terminology.html" >
< b > 7.4.1.< / b >
Envoy 的架构与基本术语
< / a >
< / li >
< li class = "chapter " data-level = "7.4.2" data-path = "../usecases/envoy-front-proxy.html" >
< a href = "../usecases/envoy-front-proxy.html" >
< b > 7.4.2.< / b >
Envoy 作为前端代理
< / a >
< / li >
< li class = "chapter " data-level = "7.4.3" data-path = "../usecases/envoy-mesh-in-kubernetes-tutorial.html" >
< a href = "../usecases/envoy-mesh-in-kubernetes-tutorial.html" >
< b > 7.4.3.< / b >
Envoy mesh 教程
< / a >
< / li >
< / ul >
< / li >
< li class = "header" > 领域应用< / li >
< li class = "chapter " data-level = "8.1" data-path = "../usecases/" >
< a href = "../usecases/" >
< b > 8.1.< / b >
领域应用概览
< / a >
< / li >
< li class = "chapter " data-level = "8.2" data-path = "../usecases/microservices.html" >
< a href = "../usecases/microservices.html" >
< b > 8.2.< / b >
微服务架构
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.2.1" data-path = "../usecases/service-discovery-in-microservices.html" >
< a href = "../usecases/service-discovery-in-microservices.html" >
< b > 8.2.1.< / b >
微服务中的服务发现
< / a >
< / li >
< li class = "chapter " data-level = "8.2.2" data-path = "../usecases/microservices-for-java-developers.html" >
< a href = "../usecases/microservices-for-java-developers.html" >
< b > 8.2.2.< / b >
使用 Java 构建微服务并发布到 Kubernetes 平台
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.2.2.1" data-path = "../usecases/spring-boot-quick-start-guide.html" >
< a href = "../usecases/spring-boot-quick-start-guide.html" >
< b > 8.2.2.1.< / b >
Spring Boot 快速开始指南
< / a >
< / li >
< / ul >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8.3" data-path = "../usecases/big-data.html" >
< a href = "../usecases/big-data.html" >
< b > 8.3.< / b >
大数据
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.3.1" data-path = "../usecases/spark-standalone-on-kubernetes.html" >
< a href = "../usecases/spark-standalone-on-kubernetes.html" >
< b > 8.3.1.< / b >
Spark standalone on Kubernetes
< / a >
< / li >
< li class = "chapter " data-level = "8.3.2" data-path = "../usecases/running-spark-with-kubernetes-native-scheduler.html" >
< a href = "../usecases/running-spark-with-kubernetes-native-scheduler.html" >
< b > 8.3.2.< / b >
运行支持 Kubernetes 原生调度的 Spark 程序
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8.4" data-path = "../usecases/serverless.html" >
< a href = "../usecases/serverless.html" >
< b > 8.4.< / b >
Serverless 架构
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.4.1" data-path = "../usecases/understanding-serverless.html" >
< a href = "../usecases/understanding-serverless.html" >
< b > 8.4.1.< / b >
理解 Serverless
< / a >
< / li >
< li class = "chapter " data-level = "8.4.2" data-path = "../usecases/faas.html" >
< a href = "../usecases/faas.html" >
< b > 8.4.2.< / b >
FaaS( 函数即服务)
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "8.4.2.1" data-path = "../usecases/openfaas-quick-start.html" >
< a href = "../usecases/openfaas-quick-start.html" >
< b > 8.4.2.1.< / b >
OpenFaaS 快速入门指南
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8.4.3" data-path = "../usecases/knative.html" >
< a href = "../usecases/knative.html" >
< b > 8.4.3.< / b >
Knative
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "8.5" data-path = "../usecases/edge-computing.html" >
< a href = "../usecases/edge-computing.html" >
< b > 8.5.< / b >
边缘计算
< / a >
< / li >
< li class = "chapter " data-level = "8.6" data-path = "../usecases/ai.html" >
< a href = "../usecases/ai.html" >
< b > 8.6.< / b >
人工智能
< / a >
< / li >
< li class = "chapter " data-level = "8.7" data-path = "../usecases/observability.html" >
< a href = "../usecases/observability.html" >
< b > 8.7.< / b >
可观察性
< / a >
< / li >
< li class = "header" > 开发指南< / li >
< li class = "chapter " data-level = "9.1" data-path = "./" >
< a href = "./" >
< b > 9.1.< / b >
开发指南概览
< / a >
< / li >
< li class = "chapter " data-level = "9.2" data-path = "sigs-and-working-group.html" >
< a href = "sigs-and-working-group.html" >
< b > 9.2.< / b >
SIG 和工作组
< / a >
< / li >
< li class = "chapter " data-level = "9.3" data-path = "developing-environment.html" >
< a href = "developing-environment.html" >
< b > 9.3.< / b >
开发环境搭建
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "9.3.1" data-path = "using-vagrant-and-virtualbox-for-development.html" >
< a href = "using-vagrant-and-virtualbox-for-development.html" >
< b > 9.3.1.< / b >
本地分布式开发环境搭建(使用 Vagrant 和 Virtualbox)
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9.4" data-path = "testing.html" >
< a href = "testing.html" >
< b > 9.4.< / b >
单元测试和集成测试
< / a >
< / li >
< li class = "chapter " data-level = "9.5" data-path = "client-go-sample.html" >
< a href = "client-go-sample.html" >
< b > 9.5.< / b >
client-go 示例
< / a >
< ul class = "articles" >
< li class = "chapter active" data-level = "9.5.1" data-path = "client-go-informer-sourcecode-analyse.html" >
< a href = "client-go-informer-sourcecode-analyse.html" >
< b > 9.5.1.< / b >
client-go 中的 informer 源码分析
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9.6" data-path = "operator.html" >
< a href = "operator.html" >
< b > 9.6.< / b >
Operator
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "9.6.1" data-path = "operator-sdk.html" >
< a href = "operator-sdk.html" >
< b > 9.6.1.< / b >
operator-sdk
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9.7" data-path = "kubebuilder.html" >
< a href = "kubebuilder.html" >
< b > 9.7.< / b >
kubebuilder
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "9.7.1" data-path = "kubebuilder-example.html" >
< a href = "kubebuilder-example.html" >
< b > 9.7.1.< / b >
使用 kubebuilder 创建 operator 示例
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "9.8" data-path = "advance-developer.html" >
< a href = "advance-developer.html" >
< b > 9.8.< / b >
高级开发指南
< / a >
< / li >
< li class = "chapter " data-level = "9.9" data-path = "contribute.html" >
< a href = "contribute.html" >
< b > 9.9.< / b >
社区贡献
< / a >
< / li >
< li class = "chapter " data-level = "9.10" data-path = "minikube.html" >
< a href = "minikube.html" >
< b > 9.10.< / b >
Minikube
< / a >
< / li >
< li class = "header" > 社区及生态< / li >
< li class = "chapter " data-level = "10.1" data-path = "../cloud-native/cncf.html" >
< a href = "../cloud-native/cncf.html" >
< b > 10.1.< / b >
云原生计算基金会( CNCF)
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "10.1.1" data-path = "../cloud-native/cncf-charter.html" >
< a href = "../cloud-native/cncf-charter.html" >
< b > 10.1.1.< / b >
CNCF 章程
< / a >
< / li >
< li class = "chapter " data-level = "10.1.2" data-path = "../cloud-native/cncf-sig.html" >
< a href = "../cloud-native/cncf-sig.html" >
< b > 10.1.2.< / b >
CNCF 特别兴趣小组( SIG) 说明
< / a >
< / li >
< li class = "chapter " data-level = "10.1.3" data-path = "../cloud-native/cncf-sandbox-criteria.html" >
< a href = "../cloud-native/cncf-sandbox-criteria.html" >
< b > 10.1.3.< / b >
开源项目加入 CNCF Sandbox 的要求
< / a >
< / li >
< li class = "chapter " data-level = "10.1.4" data-path = "../cloud-native/cncf-project-governing.html" >
< a href = "../cloud-native/cncf-project-governing.html" >
< b > 10.1.4.< / b >
CNCF 中的项目治理
< / a >
< / li >
< li class = "chapter " data-level = "10.1.5" data-path = "../cloud-native/cncf-ambassador.html" >
< a href = "../cloud-native/cncf-ambassador.html" >
< b > 10.1.5.< / b >
CNCF Ambassador
< / a >
< / li >
< / ul >
< / li >
< li class = "chapter " data-level = "10.2" data-path = "../cloud-native/cnc.html" >
< a href = "../cloud-native/cnc.html" >
< b > 10.2.< / b >
云原生社区
< / a >
< / li >
< li class = "chapter " data-level = "10.3" data-path = "../cloud-native/certification.html" >
< a href = "../cloud-native/certification.html" >
< b > 10.3.< / b >
认证及培训
< / a >
< ul class = "articles" >
< li class = "chapter " data-level = "10.3.1" data-path = "../appendix/about-kcsp.html" >
< a href = "../appendix/about-kcsp.html" >
< b > 10.3.1.< / b >
认证 Kubernetes 服务提供商( KCSP) 说明
< / a >
< / li >
< li class = "chapter " data-level = "10.3.2" data-path = "../appendix/about-cka-candidate.html" >
< a href = "../appendix/about-cka-candidate.html" >
< b > 10.3.2.< / b >
认证 Kubernetes 管理员( CKA) 说明
< / a >
< / li >
< / ul >
< / li >
< li class = "header" > 附录< / li >
< li class = "chapter " data-level = "11.1" data-path = "../appendix/" >
< a href = "../appendix/" >
< b > 11.1.< / b >
附录说明
< / a >
< / li >
< li class = "chapter " data-level = "11.2" data-path = "../appendix/debug-kubernetes-services.html" >
< a href = "../appendix/debug-kubernetes-services.html" >
< b > 11.2.< / b >
Kubernetes 中的应用故障排查
< / a >
< / li >
< li class = "chapter " data-level = "11.3" data-path = "../appendix/material-share.html" >
< a href = "../appendix/material-share.html" >
< b > 11.3.< / b >
Kubernetes 相关资讯和情报链接
< / a >
< / li >
< li class = "chapter " data-level = "11.4" data-path = "../appendix/docker-best-practice.html" >
< a href = "../appendix/docker-best-practice.html" >
< b > 11.4.< / b >
Docker 最佳实践
< / a >
< / li >
< li class = "chapter " data-level = "11.5" data-path = "../appendix/tricks.html" >
< a href = "../appendix/tricks.html" >
< b > 11.5.< / b >
2021-10-20 17:17:43 +08:00
Kubernetes 使用技巧
2021-07-01 10:02:38 +08:00
< / a >
< / li >
< li class = "chapter " data-level = "11.6" data-path = "../appendix/issues.html" >
< a href = "../appendix/issues.html" >
< b > 11.6.< / b >
2021-10-20 17:17:43 +08:00
Kubernetes 相关问题记录
2021-07-01 10:02:38 +08:00
< / a >
< / li >
2021-10-20 17:17:43 +08:00
< li class = "chapter " data-level = "11.7" data-path = "../appendix/summary-and-outlook.html" >
2021-07-01 10:02:38 +08:00
< a href = "../appendix/summary-and-outlook.html" >
2021-10-20 17:17:43 +08:00
< b > 11.7.< / b >
2021-07-01 10:02:38 +08:00
Kubernetes 及云原生年度总结及展望
< / a >
< ul class = "articles" >
2021-10-20 17:17:43 +08:00
< li class = "chapter " data-level = "11.7.1" data-path = "../appendix/kubernetes-and-cloud-native-summary-in-2017-and-outlook-for-2018.html" >
2021-07-01 10:02:38 +08:00
< a href = "../appendix/kubernetes-and-cloud-native-summary-in-2017-and-outlook-for-2018.html" >
2021-10-20 17:17:43 +08:00
< b > 11.7.1.< / b >
2021-07-01 10:02:38 +08:00
Kubernetes 与云原生 2017 年年终总结及 2018 年展望
< / a >
< / li >
2021-10-20 17:17:43 +08:00
< li class = "chapter " data-level = "11.7.2" data-path = "../appendix/kubernetes-and-cloud-native-summary-in-2018-and-outlook-for-2019.html" >
2021-07-01 10:02:38 +08:00
< a href = "../appendix/kubernetes-and-cloud-native-summary-in-2018-and-outlook-for-2019.html" >
2021-10-20 17:17:43 +08:00
< b > 11.7.2.< / b >
2021-07-01 10:02:38 +08:00
Kubernetes 与云原生 2018 年年终总结及 2019 年展望
< / a >
< / li >
< / ul >
< / li >
2021-10-20 17:17:43 +08:00
< li class = "chapter " data-level = "11.8" data-path = "../appendix/cncf-annual-report.html" >
2021-07-01 10:02:38 +08:00
< a href = "../appendix/cncf-annual-report.html" >
2021-10-20 17:17:43 +08:00
< b > 11.8.< / b >
2021-07-01 10:02:38 +08:00
CNCF 年度报告解读
< / a >
< ul class = "articles" >
2021-10-20 17:17:43 +08:00
< li class = "chapter " data-level = "11.8.1" data-path = "../appendix/cncf-annual-report-2018.html" >
2021-07-01 10:02:38 +08:00
< a href = "../appendix/cncf-annual-report-2018.html" >
2021-10-20 17:17:43 +08:00
< b > 11.8.1.< / b >
2021-07-01 10:02:38 +08:00
CNCF 2018 年年度报告解读
< / a >
< / li >
2021-10-20 17:17:43 +08:00
< li class = "chapter " data-level = "11.8.2" data-path = "../appendix/cncf-annual-report-2020.html" >
2021-07-01 10:02:38 +08:00
< a href = "../appendix/cncf-annual-report-2020.html" >
2021-10-20 17:17:43 +08:00
< b > 11.8.2.< / b >
2021-07-01 10:02:38 +08:00
CNCF 2020 年年度报告解读
< / a >
< / li >
< / ul >
< / li >
< li class = "divider" > < / li >
< li >
< a href = "https://www.gitbook.com" target = "blank" class = "gitbook-link" >
本书使用 GitBook 发布
< / a >
< / li >
< / ul >
< / nav >
< / div >
< div class = "book-body" >
< div class = "body-inner" >
< div class = "book-header" role = "navigation" >
<!-- Title -->
< h1 >
< i class = "fa fa-circle-o-notch fa-spin" > < / i >
< a href = ".." > client-go 中的 informer 源码分析< / a >
< / h1 >
< / div >
< div class = "page-wrapper" tabindex = "-1" role = "main" >
< div class = "page-inner" >
< div class = "search-plus" id = "book-search-results" >
< div class = "search-noresults" >
< section class = "normal markdown-section" >
< h1 id = "client-go-中的-informer-源码分析" > client-go 中 的 informer 源 码 分 析 < / h1 >
2021-07-01 10:09:26 +08:00
< p > 本 文 将 以 图 文 并 茂 的 方 式 对 client-go 中 的 informer 的 源 码 分 析 , 其 整 体 流 程 图 如 下 所 示 。 < / p >
2021-10-20 18:07:35 +08:00
< figure id = "fig9.5.1.1" > < a href = "../images/client-go-informer.png" data-lightbox = "46ed17ac-7136-4e52-b8fe-37711d4bf03c" data-title = "client-go informer" > < img src = "../images/client-go-informer.png" alt = "client-go informer" > < / a > < figcaption > 图 9.5.1.1: client-go informer< / figcaption > < / figure >
2021-07-07 17:39:02 +08:00
< h2 id = "前言" > 前 言 < / h2 >
< p > Kubernetes作 为 新 一 代 的 基 础 设 施 系 统 , 其 重 要 性 已 经 不 言 而 喻 了 。 基 于 控 制 器 模 型 实 现 的 声 明 式 API支 持 着 集 群 中 各 类 型 的 工 作 负 载 稳 定 高 效 的 按 照 期 望 状 态 运 转 , 随 着 越 来 越 多 的 用 户 选 择 kubernetes, 无 论 是 为 了 深 入 了 解 kubernetes这 一 云 原 生 操 作 系 统 的 工 作 逻 辑 , 还 是 期 待 能 够 根 据 自 己 的 特 定 业 务 需 求 对 kubernetes进 行 二 次 开 发 , 了 解 控 制 器 模 型 的 实 现 机 制 都 是 非 常 重 要 的 。 kubernetes提 供 了 client-go以 方 便 使 用 go语 言 进 行 二 次 快 发 , 本 文 试 图 讲 述 client-go各 模 块 如 informer、 reflector、 cache等 实 现 细 节 。 < / p >
< p > 当 我 们 需 要 利 用 client-go来 实 现 自 定 义 控 制 器 时 , 通 常 会 使 用 informerFactory来 管 理 控 制 器 需 要 的 多 个 资 源 对 象 的 informer实 例 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // 创 建 一 个 informer factory< / span >
kubeInformerFactory < span class = "token operator" > :=< / span > kubeinformers< span class = "token punctuation" > .< / span > < span class = "token function" > NewSharedInformerFactory< / span > < span class = "token punctuation" > (< / span > kubeClient< span class = "token punctuation" > ,< / span > time< span class = "token punctuation" > .< / span > Second< span class = "token operator" > *< / span > < span class = "token number" > 30< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // factory已 经 为 所 有 k8s的 内 置 资 源 对 象 提 供 了 创 建 对 应 informer实 例 的 方 法 , 调 用 具 体 informer实 例 的 Lister或 Informer方 法 < / span >
< span class = "token comment" > // 就 完 成 了 将 informer注 册 到 factory的 过 程 < / span >
deploymentLister < span class = "token operator" > :=< / span > kubeInformerFactory< span class = "token punctuation" > .< / span > < span class = "token function" > Apps< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > V1< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Deployments< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Lister< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // 启 动 注 册 到 factory的 所 有 informer< / span >
2021-07-01 10:02:38 +08:00
kubeInformerFactory< span class = "token punctuation" > .< / span > < span class = "token function" > Start< / span > < span class = "token punctuation" > (< / span > stopCh< span class = "token punctuation" > )< / span >
< / code > < / pre >
< h3 id = "sharedinformerfactory结构" > SharedInformerFactory结 构 < / h3 >
2021-07-07 17:39:02 +08:00
< p > 使 用 sharedInformerFactory可 以 统 一 管 理 控 制 器 中 需 要 的 各 资 源 对 象 的 informer实 例 , 避 免 同 一 个 资 源 创 建 多 个 实 例 , 这 里 的 informer实 现 是 shareIndexInformer
NewSharedInformerFactory调 用 了 NewSharedInformerFactoryWithOptions, 将 返 回 一 个 sharedInformerFactory对 象 < / p >
2021-07-01 10:02:38 +08:00
< blockquote >
2021-07-07 17:39:02 +08:00
< p > client: clientset, 支 持 直 接 请 求 api中 各 内 置 资 源 对 象 的 restful group客 户 端 集 合
namespace: factory关 注 的 namespace( 默 认 All Namespace) , informer中 的 reflector将 只 会 listAndWatch指 定 namespace的 资 源
defaultResync: 用 于 初 始 化 持 有 的 shareIndexInformer的 resyncCheckPeriod和 defaultEventHandlerResyncPeriod字 段 , 用 于 定 时 的 将 local store同 步 到 deltaFIFO
customResync: 支 持 针 对 每 一 个 informer来 配 置 resync时 间 , 通 过 WithCustomResyncConfig这 个 Option配 置 , 否 则 就 用 指 定 的 defaultResync
informers: factory管 理 的 informer集 合
startedInformers: 记 录 已 经 启 动 的 informer集 合 < / p >
2021-07-01 10:02:38 +08:00
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > type< / span > sharedInformerFactory < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
client kubernetes< span class = "token punctuation" > .< / span > Interface < span class = "token comment" > //clientset< / span >
namespace < span class = "token builtin" > string< / span > < span class = "token comment" > //关 注 的 namepace, 可 以 通 过 WithNamespace Option配 置 < / span >
tweakListOptions internalinterfaces< span class = "token punctuation" > .< / span > TweakListOptionsFunc
lock sync< span class = "token punctuation" > .< / span > Mutex
defaultResync time< span class = "token punctuation" > .< / span > Duration < span class = "token comment" > //前 面 传 过 来 的 时 间 , 如 30s< / span >
2021-07-07 17:39:02 +08:00
customResync < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > time< span class = "token punctuation" > .< / span > Duration < span class = "token comment" > //自 定 义 resync时 间 < / span >
2021-07-01 10:02:38 +08:00
informers < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer < span class = "token comment" > //针 对 每 种 类 型 资 源 存 储 一 个 informer, informer的 类 型 是 ShareIndexInformer< / span >
startedInformers < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > < span class = "token builtin" > bool< / span > < span class = "token comment" > //每 个 informer是 否 都 启 动 了 < / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< p > sharedInformerFactory对 象 的 关 键 方 法 : < / p >
< h4 id = "创建一个sharedinformerfactory" > 创 建 一 个 sharedInformerFactory< / h4 >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token function" > NewSharedInformerFactoryWithOptions< / span > < span class = "token punctuation" > (< / span > client kubernetes< span class = "token punctuation" > .< / span > Interface< span class = "token punctuation" > ,< / span > defaultResync time< span class = "token punctuation" > .< / span > Duration< span class = "token punctuation" > ,< / span > options < span class = "token operator" > ...< / span > SharedInformerOption< span class = "token punctuation" > )< / span > SharedInformerFactory < span class = "token punctuation" > {< / span >
factory < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > sharedInformerFactory< span class = "token punctuation" > {< / span >
2021-07-07 17:39:02 +08:00
client< span class = "token punctuation" > :< / span > client< span class = "token punctuation" > ,< / span > < span class = "token comment" > //clientset, 对 原 生 资 源 来 说 , 这 里 可 以 直 接 使 用 kube clientset< / span >
2021-07-01 10:02:38 +08:00
namespace< span class = "token punctuation" > :< / span > v1< span class = "token punctuation" > .< / span > NamespaceAll< span class = "token punctuation" > ,< / span > < span class = "token comment" > //可 以 看 到 默 认 是 监 听 所 有 ns下 的 指 定 资 源 < / span >
defaultResync< span class = "token punctuation" > :< / span > defaultResync< span class = "token punctuation" > ,< / span > < span class = "token comment" > //30s< / span >
< span class = "token comment" > //以 下 初 始 化 map结 构 < / span >
informers< span class = "token punctuation" > :< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ,< / span >
startedInformers< span class = "token punctuation" > :< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > < span class = "token builtin" > bool< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ,< / span >
customResync< span class = "token punctuation" > :< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > time< span class = "token punctuation" > .< / span > Duration< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > factory
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "启动factory下的所有informer" > 启 动 factory下 的 所 有 informer< / h4 >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > sharedInformerFactory< span class = "token punctuation" > )< / span > < span class = "token function" > Start< / span > < span class = "token punctuation" > (< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > informerType< span class = "token punctuation" > ,< / span > informer < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > f< span class = "token punctuation" > .< / span > informers < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > f< span class = "token punctuation" > .< / span > startedInformers< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > //直 接 起 gorouting调 用 informer的 Run方 法 , 并 且 标 记 对 应 的 informer已 经 启 动 < / span >
< span class = "token keyword" > go< / span > informer< span class = "token punctuation" > .< / span > < span class = "token function" > Run< / span > < span class = "token punctuation" > (< / span > stopCh< span class = "token punctuation" > )< / span >
f< span class = "token punctuation" > .< / span > startedInformers< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > < span class = "token boolean" > true< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "等待informer的cache被同步" > 等 待 informer的 cache被 同 步 < / h4 >
< p > 等 待 每 一 个 ShareIndexInformer的 cache被 同 步 , 具 体 怎 么 算 同 步 完 成 ? < / p >
2021-07-01 10:02:38 +08:00
< ul >
< li > < p > sharedInformerFactory的 WaitForCacheSync将 会 不 断 调 用 factory持 有 的 所 有 informer的 HasSynced方 法 , 直 到 返 回 true< / p >
< / li >
2021-07-07 17:39:02 +08:00
< li > < p > 而 informer的 HasSynced方 法 调 用 的 自 己 持 有 的 controller的 HasSynced方 法 ( informer结 构 持 有 controller对 象 , 下 文 会 分 析 informer的 结 构 ) < / p >
2021-07-01 10:02:38 +08:00
< / li >
< li > < p > informer中 的 controller的 HasSynced方 法 则 调 用 的 是 controller持 有 的 deltaFIFO对 象 的 HasSynced方 法 < / p >
< / li >
< / ul >
2021-07-07 17:39:02 +08:00
< p > 也 就 说 sharedInformerFactory的 WaitForCacheSync方 法 判 断 informer的 cache是 否 同 步 , 最 终 看 的 是 informer中 的 deltaFIFO是 否 同 步 了 , deltaFIFO的 结 构 下 文 将 会 分 析 < / p >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > sharedInformerFactory< span class = "token punctuation" > )< / span > < span class = "token function" > WaitForCacheSync< / span > < span class = "token punctuation" > (< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > < span class = "token builtin" > bool< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > //获 取 每 一 个 已 经 启 动 的 informer< / span >
informers < span class = "token operator" > :=< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
informers < span class = "token operator" > :=< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token keyword" > for< / span > informerType< span class = "token punctuation" > ,< / span > informer < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > f< span class = "token punctuation" > .< / span > informers < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > f< span class = "token punctuation" > .< / span > startedInformers< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > {< / span >
informers< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > informer
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > informers
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
res < span class = "token operator" > :=< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > reflect< span class = "token punctuation" > .< / span > Type< span class = "token punctuation" > ]< / span > < span class = "token builtin" > bool< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token comment" > // 等 待 他 们 的 cache被 同 步 , 调 用 的 是 informer的 HasSynced方 法 < / span >
< span class = "token keyword" > for< / span > informType< span class = "token punctuation" > ,< / span > informer < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > informers < span class = "token punctuation" > {< / span >
res< span class = "token punctuation" > [< / span > informType< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > cache< span class = "token punctuation" > .< / span > < span class = "token function" > WaitForCacheSync< / span > < span class = "token punctuation" > (< / span > stopCh< span class = "token punctuation" > ,< / span > informer< span class = "token punctuation" > .< / span > HasSynced< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > res
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "factory为自己添加informer" > factory为 自 己 添 加 informer< / h4 >
< p > 只 有 向 factory中 添 加 informer, factory才 有 意 义 , 添 加 完 成 之 后 , 上 面 factory的 start方 法 就 可 以 启 动 了 < / p >
< blockquote >
2021-07-07 17:39:02 +08:00
< p > obj: informer关 注 的 资 源 如 deployment{}
newFunc: 一 个 知 道 如 何 创 建 指 定 informer的 方 法 , k8s为 每 一 个 内 置 的 对 象 都 实 现 了 这 个 方 法 , 比 如 创 建 deployment的 ShareIndexInformer的 方 法 < / p >
2021-07-01 10:02:38 +08:00
< / blockquote >
2021-07-07 17:39:02 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // 向 factory中 注 册 指 定 的 informer< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > sharedInformerFactory< span class = "token punctuation" > )< / span > < span class = "token function" > InformerFor< / span > < span class = "token punctuation" > (< / span > obj runtime< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > ,< / span > newFunc internalinterfaces< span class = "token punctuation" > .< / span > NewInformerFunc< span class = "token punctuation" > )< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer < span class = "token punctuation" > {< / span >
2021-07-01 10:02:38 +08:00
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > //根 据 对 象 类 型 判 断 factory中 是 否 已 经 有 对 应 informer< / span >
informerType < span class = "token operator" > :=< / span > reflect< span class = "token punctuation" > .< / span > < span class = "token function" > TypeOf< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
informer< span class = "token punctuation" > ,< / span > exists < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > informers< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > exists < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > informer
< span class = "token punctuation" > }< / span >
< span class = "token comment" > //如 果 factory中 已 经 有 这 个 对 象 类 型 的 informer, 就 不 创 建 了 < / span >
resyncPeriod< span class = "token punctuation" > ,< / span > exists < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > customResync< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > exists < span class = "token punctuation" > {< / span >
resyncPeriod < span class = "token operator" > =< / span > f< span class = "token punctuation" > .< / span > defaultResync
< span class = "token punctuation" > }< / span >
< span class = "token comment" > //没 有 就 根 据 newFunc创 建 一 个 , 并 存 在 map中 < / span >
informer < span class = "token operator" > =< / span > < span class = "token function" > newFunc< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > client< span class = "token punctuation" > ,< / span > resyncPeriod< span class = "token punctuation" > )< / span >
f< span class = "token punctuation" > .< / span > informers< span class = "token punctuation" > [< / span > informerType< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > informer
< span class = "token keyword" > return< / span > informer
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h5 id = "shareindexinformer对应的newfunc的实现" > shareIndexInformer对 应 的 newFunc的 实 现 < / h5 >
2021-07-01 10:02:38 +08:00
< p > client-go中 已 经 为 所 有 内 置 对 象 都 提 供 了 NewInformerFunc< / p >
< p > 以 deployment为 例 , 通 过 调 用 factory.Apps().V1().Deployments()即 可 为 factory添 加 一 个 deployment对 应 的 shareIndexInformer的 实 现 , 具 体 过 程 如 下 : < / p >
< ul >
< li > 调 用 factory.Apps().V1().Deployments()即 会 调 用 以 下 Deployments方 法 创 建 deploymentInformer对 象 < / li >
< / ul >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > v < span class = "token operator" > *< / span > version< span class = "token punctuation" > )< / span > < span class = "token function" > Deployments< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > DeploymentInformer < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token operator" > & < / span > deploymentInformer< span class = "token punctuation" > {< / span > factory< span class = "token punctuation" > :< / span > v< span class = "token punctuation" > .< / span > factory< span class = "token punctuation" > ,< / span > namespace< span class = "token punctuation" > :< / span > v< span class = "token punctuation" > .< / span > namespace< span class = "token punctuation" > ,< / span > tweakListOptions< span class = "token punctuation" > :< / span > v< span class = "token punctuation" > .< / span > tweakListOptions< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< ul >
< li > 只 要 调 用 了 factory.Apps().V1().Deployments()返 回 的 deploymentInformer的 Informer或 Lister方 法 , 就 完 成 了 向 factory中 添 加 deployment informer< / li >
< / ul >
2021-07-07 17:39:02 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // deploymentInformer对 象 具 有 defaultInformer、 Informer、 Lister方 法 < / span >
< span class = "token comment" > // 可 以 看 到 创 建 deploymentInformer时 传 递 了 一 个 带 索 引 的 缓 存 , 附 带 了 一 个 namespace索 引 , 后 面 可 以 了 解 带 索 引 的 缓 存 实 现 , 比 如 可 以 支 持 查 询 : 某 个 namespace下 的 所 有 pod< / span >
< span class = "token comment" > // 用 于 创 建 对 应 的 shareIndexInformer, 该 方 法 提 供 给 factory的 InformerFor方 法 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > deploymentInformer< span class = "token punctuation" > )< / span > < span class = "token function" > defaultInformer< / span > < span class = "token punctuation" > (< / span > client kubernetes< span class = "token punctuation" > .< / span > Interface< span class = "token punctuation" > ,< / span > resyncPeriod time< span class = "token punctuation" > .< / span > Duration< span class = "token punctuation" > )< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token function" > NewFilteredDeploymentInformer< / span > < span class = "token punctuation" > (< / span > client< span class = "token punctuation" > ,< / span > f< span class = "token punctuation" > .< / span > namespace< span class = "token punctuation" > ,< / span > resyncPeriod< span class = "token punctuation" > ,< / span > cache< span class = "token punctuation" > .< / span > Indexers< span class = "token punctuation" > {< / span > cache< span class = "token punctuation" > .< / span > NamespaceIndex< span class = "token punctuation" > :< / span > cache< span class = "token punctuation" > .< / span > MetaNamespaceIndexFunc< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > f< span class = "token punctuation" > .< / span > tweakListOptions< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 向 factor中 添 加 dpeloyment的 shareIndexInformer并 返 回 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > deploymentInformer< span class = "token punctuation" > )< / span > < span class = "token function" > Informer< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > f< span class = "token punctuation" > .< / span > factory< span class = "token punctuation" > .< / span > < span class = "token function" > InformerFor< / span > < span class = "token punctuation" > (< / span > < span class = "token operator" > & < / span > appsv1< span class = "token punctuation" > .< / span > Deployment< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > f< span class = "token punctuation" > .< / span > defaultInformer< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 返 回 dpeloyment的 lister对 象 , 该 lister中 持 有 上 面 创 建 出 的 shareIndexInformer的 cache的 引 用 , 方 便 通 过 缓 存 获 取 对 象 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > deploymentInformer< span class = "token punctuation" > )< / span > < span class = "token function" > Lister< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > v1< span class = "token punctuation" > .< / span > DeploymentLister < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > v1< span class = "token punctuation" > .< / span > < span class = "token function" > NewDeploymentLister< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > Informer< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > GetIndexer< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< ul >
< li > deploymentInformer的 defaultInformer方 法 将 会 创 建 出 一 个 shareIndexInformer< / li >
< / ul >
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // 可 先 看 看 下 面 的 shareIndexInformer结 构 < / span >
< span class = "token keyword" > func< / span > < span class = "token function" > NewFilteredDeploymentInformer< / span > < span class = "token punctuation" > (< / span > client kubernetes< span class = "token punctuation" > .< / span > Interface< span class = "token punctuation" > ,< / span > namespace < span class = "token builtin" > string< / span > < span class = "token punctuation" > ,< / span > resyncPeriod time< span class = "token punctuation" > .< / span > Duration< span class = "token punctuation" > ,< / span > indexers cache< span class = "token punctuation" > .< / span > Indexers< span class = "token punctuation" > ,< / span > tweakListOptions internalinterfaces< span class = "token punctuation" > .< / span > TweakListOptionsFunc< span class = "token punctuation" > )< / span > cache< span class = "token punctuation" > .< / span > SharedIndexInformer < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > cache< span class = "token punctuation" > .< / span > < span class = "token function" > NewSharedIndexInformer< / span > < span class = "token punctuation" > (< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 定 义 对 象 的 ListWatch方 法 , 这 里 直 接 用 的 是 clientset中 的 方 法 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token operator" > & < / span > cache< span class = "token punctuation" > .< / span > ListWatch< span class = "token punctuation" > {< / span >
ListFunc< span class = "token punctuation" > :< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > options v1< span class = "token punctuation" > .< / span > ListOptions< span class = "token punctuation" > )< / span > < span class = "token punctuation" > (< / span > runtime< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > ,< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > tweakListOptions < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token function" > tweakListOptions< / span > < span class = "token punctuation" > (< / span > < span class = "token operator" > & < / span > options< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > client< span class = "token punctuation" > .< / span > < span class = "token function" > AppsV1beta1< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Deployments< / span > < span class = "token punctuation" > (< / span > namespace< span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > List< / span > < span class = "token punctuation" > (< / span > options< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
WatchFunc< span class = "token punctuation" > :< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > options v1< span class = "token punctuation" > .< / span > ListOptions< span class = "token punctuation" > )< / span > < span class = "token punctuation" > (< / span > watch< span class = "token punctuation" > .< / span > Interface< span class = "token punctuation" > ,< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > tweakListOptions < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token function" > tweakListOptions< / span > < span class = "token punctuation" > (< / span > < span class = "token operator" > & < / span > options< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > client< span class = "token punctuation" > .< / span > < span class = "token function" > AppsV1beta1< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Deployments< / span > < span class = "token punctuation" > (< / span > namespace< span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Watch< / span > < span class = "token punctuation" > (< / span > options< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
< span class = "token operator" > & < / span > appsv1beta1< span class = "token punctuation" > .< / span > Deployment< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
2021-07-07 17:39:02 +08:00
resyncPeriod< span class = "token punctuation" > ,< / span > < span class = "token comment" > //创 建 factory是 指 定 的 时 间 , 如 30s< / span >
2021-07-01 10:02:38 +08:00
indexers< span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h3 id = "shareindexinformer结构" > shareIndexInformer结 构 < / h3 >
< blockquote >
< p > indexer: 底 层 缓 存 , 其 实 就 是 一 个 map记 录 对 象 , 再 通 过 一 些 其 他 map在 插 入 删 除 对 象 是 根 据 索 引 函 数 维 护 索 引 key如 ns与 对 象 pod的 关 系
controller: informer内 部 的 一 个 controller, 这 个 controller包 含 reflector: 根 据 用 户 定 义 的 ListWatch方 法 获 取 对 象 并 更 新 增 量 队 列 DeltaFIFO
processor: 知 道 如 何 处 理 DeltaFIFO队 列 中 的 对 象 , 实 现 是 sharedProcessor{}
listerWatcher: 知 道 如 何 list对 象 和 watch对 象 的 方 法
objectType: deployment{}
resyncCheckPeriod: 给 自 己 的 controller的 reflector每 隔 多 少 s< 尝 试 > 调 用 listener的 shouldResync方 法
defaultEventHandlerResyncPeriod: 通 过 AddEventHandler方 法 给 informer配 置 回 调 时 如 果 没 有 配 置 的 默 认 值 , 这 个 值 用 在 processor的 listener中 判 断 是 否 需 要 进 行 resync, 最 小 1s< / p >
< / blockquote >
< p > 两 个 字 段 的 默 认 值 都 是 来 自 创 建 factory时 指 定 的 defaultResync, 当 resyncPeriod < s.resyncCheckPeriod时 , 如 果 informer已 经 启 动 了 才 添 加 的 EventHandler, 那 么 调 整 resyncPeriod为 resyncCheckPeriod, 否 则 调 整 resyncCheckPeriod为 resyncPeriod< / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > type< / span > sharedIndexInformer < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
indexer Indexer < span class = "token comment" > //informer中 的 底 层 缓 存 cache< / span >
controller Controller < span class = "token comment" > //持 有 reflector和 deltaFIFO对 象 , reflector对 象 将 会 listWatch对 象 添 加 到 deltaFIFO, 同 时 更 新 indexer cahce, 更 新 成 功 则 通 过 sharedProcessor触 发 用 户 配 置 的 Eventhandler< / span >
processor < span class = "token operator" > *< / span > sharedProcessor < span class = "token comment" > //持 有 一 系 列 的 listener, 每 个 listener对 应 用 户 的 EventHandler< / span >
cacheMutationDetector MutationDetector < span class = "token comment" > //可 以 先 忽 略 , 这 个 对 象 可 以 用 来 监 测 local cache是 否 被 外 部 直 接 修 改 < / span >
< span class = "token comment" > // This block is tracked to handle late initialization of the controller< / span >
listerWatcher ListerWatcher < span class = "token comment" > //deployment的 listWatch方 法 < / span >
objectType runtime< span class = "token punctuation" > .< / span > Object
< span class = "token comment" > // resyncCheckPeriod is how often we want the reflector' s resync timer to fire so it can call< / span >
< span class = "token comment" > // shouldResync to check if any of our listeners need a resync.< / span >
resyncCheckPeriod time< span class = "token punctuation" > .< / span > Duration
< span class = "token comment" > // defaultEventHandlerResyncPeriod is the default resync period for any handlers added via< / span >
< span class = "token comment" > // AddEventHandler (i.e. they don' t specify one and just want to use the shared informer' s default< / span >
< span class = "token comment" > // value).< / span >
defaultEventHandlerResyncPeriod time< span class = "token punctuation" > .< / span > Duration
< span class = "token comment" > // clock allows for testability< / span >
clock clock< span class = "token punctuation" > .< / span > Clock
started< span class = "token punctuation" > ,< / span > stopped < span class = "token builtin" > bool< / span >
startedLock sync< span class = "token punctuation" > .< / span > Mutex
< span class = "token comment" > // blockDeltas gives a way to stop all event distribution so that a late event handler< / span >
< span class = "token comment" > // can safely join the shared informer.< / span >
blockDeltas sync< span class = "token punctuation" > .< / span > Mutex
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< p > sharedIndexInformer对 象 的 关 键 方 法 : < / p >
< h4 id = "sharedindexinformer的run方法" > sharedIndexInformer的 Run方 法 < / h4 >
< p > 前 面 factory的 start方 法 就 是 调 用 了 这 个 Run方 法 < / p >
< p > 该 方 法 初 始 化 了 controller对 象 并 启 动 , 同 时 调 用 processor.run启 动 所 有 的 listener, 用 于 回 调 用 户 配 置 的 EventHandler< / p >
2021-07-07 17:39:02 +08:00
< blockquote >
2021-07-01 10:02:38 +08:00
< p > 具 体 sharedIndexInformer中 的 processor中 的 listener是 怎 么 添 加 的 , 看 下 文 shareProcessor的 分 析 < / p >
2021-07-07 17:39:02 +08:00
< / blockquote >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > s < span class = "token operator" > *< / span > sharedIndexInformer< span class = "token punctuation" > )< / span > < span class = "token function" > Run< / span > < span class = "token punctuation" > (< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > defer< / span > utilruntime< span class = "token punctuation" > .< / span > < span class = "token function" > HandleCrash< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > //创 建 一 个 DeltaFIFO, 用 于 shareIndexInformer.controller.reflector< / span >
< span class = "token comment" > //可 以 看 到 这 里 把 indexer即 本 地 缓 存 传 入 , 用 来 初 始 化 deltaFIFO的 knownObject字 段 < / span >
fifo < span class = "token operator" > :=< / span > < span class = "token function" > NewDeltaFIFO< / span > < span class = "token punctuation" > (< / span > MetaNamespaceKeyFunc< span class = "token punctuation" > ,< / span > s< span class = "token punctuation" > .< / span > indexer< span class = "token punctuation" > )< / span >
< span class = "token comment" > //shareIndexInformer中 的 controller的 配 置 < / span >
cfg < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > Config< span class = "token punctuation" > {< / span >
Queue< span class = "token punctuation" > :< / span > fifo< span class = "token punctuation" > ,< / span >
ListerWatcher< span class = "token punctuation" > :< / span > s< span class = "token punctuation" > .< / span > listerWatcher< span class = "token punctuation" > ,< / span >
ObjectType< span class = "token punctuation" > :< / span > s< span class = "token punctuation" > .< / span > objectType< span class = "token punctuation" > ,< / span >
FullResyncPeriod< span class = "token punctuation" > :< / span > s< span class = "token punctuation" > .< / span > resyncCheckPeriod< span class = "token punctuation" > ,< / span >
RetryOnError< span class = "token punctuation" > :< / span > < span class = "token boolean" > false< / span > < span class = "token punctuation" > ,< / span >
ShouldResync< span class = "token punctuation" > :< / span > s< span class = "token punctuation" > .< / span > processor< span class = "token punctuation" > .< / span > shouldResync< span class = "token punctuation" > ,< / span > < span class = "token comment" > // 这 个 shouldResync方 法 将 被 用 在 reflector ListAndWatch方 法 中 判 断 定 时 时 间 resyncCheckPeriod到 了 之 后 该 不 该 进 行 resync动 作 < / span >
< span class = "token comment" > //一 个 知 道 如 何 处 理 从 informer中 的 controller中 的 deltaFIFO pop出 来 的 对 象 的 方 法 < / span >
Process< span class = "token punctuation" > :< / span > s< span class = "token punctuation" > .< / span > HandleDeltas< span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
s< span class = "token punctuation" > .< / span > startedLock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > s< span class = "token punctuation" > .< / span > startedLock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // 这 里 New一 个 具 体 的 controller< / span >
s< span class = "token punctuation" > .< / span > controller < span class = "token operator" > =< / span > < span class = "token function" > New< / span > < span class = "token punctuation" > (< / span > cfg< span class = "token punctuation" > )< / span >
s< span class = "token punctuation" > .< / span > controller< span class = "token punctuation" > .< / span > < span class = "token punctuation" > (< / span > < span class = "token operator" > *< / span > controller< span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > clock < span class = "token operator" > =< / span > s< span class = "token punctuation" > .< / span > clock
s< span class = "token punctuation" > .< / span > started < span class = "token operator" > =< / span > < span class = "token boolean" > true< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // Separate stop channel because Processor should be stopped strictly after controller< / span >
processorStopCh < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > var< / span > wg wait< span class = "token punctuation" > .< / span > Group
< span class = "token keyword" > defer< / span > wg< span class = "token punctuation" > .< / span > < span class = "token function" > Wait< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token comment" > // Wait for Processor to stop< / span >
< span class = "token keyword" > defer< / span > < span class = "token function" > close< / span > < span class = "token punctuation" > (< / span > processorStopCh< span class = "token punctuation" > )< / span > < span class = "token comment" > // Tell Processor to stop< / span >
< span class = "token comment" > // 调 用 processor.run启 动 所 有 的 listener, 回 调 用 户 配 置 的 EventHandler< / span >
wg< span class = "token punctuation" > .< / span > < span class = "token function" > StartWithChannel< / span > < span class = "token punctuation" > (< / span > processorStopCh< span class = "token punctuation" > ,< / span > s< span class = "token punctuation" > .< / span > processor< span class = "token punctuation" > .< / span > run< span class = "token punctuation" > )< / span >
< span class = "token comment" > // 启 动 controller< / span >
s< span class = "token punctuation" > .< / span > controller< span class = "token punctuation" > .< / span > < span class = "token function" > Run< / span > < span class = "token punctuation" > (< / span > stopCh< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "为shareindexinformer创建controller" > 为 shareIndexInformer创 建 controller< / h4 >
< p > 创 建 Controller的 New方 法 会 生 成 一 个 controller对 象 , 只 初 始 化 controller的 config成 员 , controller的 reflector成 员 是 在 Run的 时 候 初 始 化 : < / p >
< ul >
< li > < p > 通 过 执 行 reflector.Run方 法 启 动 reflector, 开 启 对 指 定 对 象 的 listAndWatch过 程 , 获 取 的 对 象 将 添 加 到 reflector的 deltaFIFO中 < / p >
< / li >
< li > < p > 通 过 不 断 执 行 processLoop方 法 , 从 DeltaFIFO pop出 对 象 , 再 调 用 reflector的 Process( 就 是 shareIndexInformer的 HandleDeltas方 法 ) 处 理 < / p >
< / li >
< / ul >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token function" > New< / span > < span class = "token punctuation" > (< / span > c < span class = "token operator" > *< / span > Config< span class = "token punctuation" > )< / span > Controller < span class = "token punctuation" > {< / span >
ctlr < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > controller< span class = "token punctuation" > {< / span >
config< span class = "token punctuation" > :< / span > < span class = "token operator" > *< / span > c< span class = "token punctuation" > ,< / span >
clock< span class = "token punctuation" > :< / span > < span class = "token operator" > & < / span > clock< span class = "token punctuation" > .< / span > RealClock< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > ctlr
< span class = "token punctuation" > }< / span >
< span class = "token comment" > //更 多 字 段 的 配 置 是 在 Run的 时 候 < / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > c < span class = "token operator" > *< / span > controller< span class = "token punctuation" > )< / span > < span class = "token function" > Run< / span > < span class = "token punctuation" > (< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 使 用 config创 建 一 个 Reflector< / span >
r < span class = "token operator" > :=< / span > < span class = "token function" > NewReflector< / span > < span class = "token punctuation" > (< / span >
c< span class = "token punctuation" > .< / span > config< span class = "token punctuation" > .< / span > ListerWatcher< span class = "token punctuation" > ,< / span > < span class = "token comment" > // deployment的 listWatch方 法 < / span >
c< span class = "token punctuation" > .< / span > config< span class = "token punctuation" > .< / span > ObjectType< span class = "token punctuation" > ,< / span > < span class = "token comment" > // deployment{}< / span >
c< span class = "token punctuation" > .< / span > config< span class = "token punctuation" > .< / span > Queue< span class = "token punctuation" > ,< / span > < span class = "token comment" > //DeltaFIFO< / span >
c< span class = "token punctuation" > .< / span > config< span class = "token punctuation" > .< / span > FullResyncPeriod< span class = "token punctuation" > ,< / span > < span class = "token comment" > //30s< / span >
< span class = "token punctuation" > )< / span >
r< span class = "token punctuation" > .< / span > ShouldResync < span class = "token operator" > =< / span > c< span class = "token punctuation" > .< / span > config< span class = "token punctuation" > .< / span > ShouldResync < span class = "token comment" > //来 自 sharedProcessor的 方 法 < / span >
r< span class = "token punctuation" > .< / span > clock < span class = "token operator" > =< / span > c< span class = "token punctuation" > .< / span > clock
c< span class = "token punctuation" > .< / span > reflectorMutex< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
c< span class = "token punctuation" > .< / span > reflector < span class = "token operator" > =< / span > r
c< span class = "token punctuation" > .< / span > reflectorMutex< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > var< / span > wg wait< span class = "token punctuation" > .< / span > Group
< span class = "token keyword" > defer< / span > wg< span class = "token punctuation" > .< / span > < span class = "token function" > Wait< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // 启 动 reflector, 执 行 ListWatch方 法 < / span >
wg< span class = "token punctuation" > .< / span > < span class = "token function" > StartWithChannel< / span > < span class = "token punctuation" > (< / span > stopCh< span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > Run< span class = "token punctuation" > )< / span >
< span class = "token comment" > // 不 断 执 行 processLoop, 这 个 方 法 其 实 就 是 从 DeltaFIFO pop出 对 象 , 再 调 用 reflector的 Process( 其 实 是 shareIndexInformer的 HandleDeltas方 法 ) 处 理 < / span >
wait< span class = "token punctuation" > .< / span > < span class = "token function" > Until< / span > < span class = "token punctuation" > (< / span > c< span class = "token punctuation" > .< / span > processLoop< span class = "token punctuation" > ,< / span > time< span class = "token punctuation" > .< / span > Second< span class = "token punctuation" > ,< / span > stopCh< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "controller的processloop方法" > controller的 processLoop方 法 < / h4 >
< p > 不 断 执 行 processLoop, 这 个 方 法 其 实 就 是 从 DeltaFIFO pop出 对 象 , 再 调 用 reflector的 Process( 其 实 是 shareIndexInformer的 HandleDeltas方 法 ) 处 理 < / p >
< pre class = "language-" > < code > func (c *controller) processLoop() {
for {
obj, err := c.config.Queue.Pop(PopProcessFunc(c.config.Process))
if err != nil {
if err == ErrFIFOClosed {
return
}
if c.config.RetryOnError {
// This is the safe way to re-enqueue.
c.config.Queue.AddIfNotPresent(obj)
}
}
}
}
< / code > < / pre > < h4 id = "deltafifo-pop出来的对象处理逻辑" > deltaFIFO pop出 来 的 对 象 处 理 逻 辑 < / h4 >
2021-07-01 10:09:26 +08:00
< p > 先 看 看 controller怎 么 处 理 DeltaFIFO中 的 对 象 , 需 要 注 意 DeltaFIFO中 的 Deltas的 结 构 , 是 一 个 slice, 保 存 同 一 个 对 象 的 所 有 增 量 事 件 < / p >
2021-10-20 18:07:35 +08:00
< figure id = "fig9.5.1.2" > < a href = "https://github.com/jianlongzhou/client-go-source-analysis/blob/main/deltaFIFO.png?raw=true" data-lightbox = "68c0278c-6cd0-4090-b4c8-eb6371f13ea5" data-title = "image" target = "_blank" > < img src = "https://github.com/jianlongzhou/client-go-source-analysis/blob/main/deltaFIFO.png?raw=true" alt = "image" > < / a > < figcaption > 图 9.5.1.2: image< / figcaption > < / figure >
2021-07-01 10:02:38 +08:00
< p > sharedIndexInformer的 HandleDeltas处 理 从 deltaFIFO pod出 来 的 增 量 时 , 先 尝 试 更 新 到 本 地 缓 存 cache, 更 新 成 功 的 话 会 调 用 processor.distribute方 法 向 processor中 的 listener添 加 notification, listener启 动 之 后 会 不 断 获 取 notification回 调 用 户 的 EventHandler方 法 < / p >
< ul >
< li > Sync: reflector list到 对 象 时 Replace到 deltaFIFO时 daltaType为 Sync或 者 resync把 localstrore中 的 对 象 加 回 到 deltaFIFO< / li >
< li > Added、 Updated: reflector watch到 对 象 时 根 据 watch event type是 Add还 是 Modify对 应 deltaType为 Added或 者 Updated< / li >
< li > < p > Deleted: reflector watch到 对 象 的 watch event type是 Delete或 者 re-list Replace到 deltaFIFO时 local store多 出 的 对 象 以 Delete的 方 式 加 入 deltaFIFO< / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > s < span class = "token operator" > *< / span > sharedIndexInformer< span class = "token punctuation" > )< / span > < span class = "token function" > HandleDeltas< / span > < span class = "token punctuation" > (< / span > obj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
s< span class = "token punctuation" > .< / span > blockDeltas< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > s< span class = "token punctuation" > .< / span > blockDeltas< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // from oldest to newest< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > d < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > obj< span class = "token punctuation" > .< / span > < span class = "token punctuation" > (< / span > Deltas< span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > switch< / span > d< span class = "token punctuation" > .< / span > Type < span class = "token punctuation" > {< / span >
< span class = "token keyword" > case< / span > Sync< span class = "token punctuation" > ,< / span > Added< span class = "token punctuation" > ,< / span > Updated< span class = "token punctuation" > :< / span >
isSync < span class = "token operator" > :=< / span > d< span class = "token punctuation" > .< / span > Type < span class = "token operator" > ==< / span > Sync
< span class = "token comment" > // 对 象 先 通 过 shareIndexInformer中 的 indexer更 新 到 缓 存 < / span >
< span class = "token keyword" > if< / span > old< span class = "token punctuation" > ,< / span > exists< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > s< span class = "token punctuation" > .< / span > indexer< span class = "token punctuation" > .< / span > < span class = "token function" > Get< / span > < span class = "token punctuation" > (< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token operator" > & & < / span > exists < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > s< span class = "token punctuation" > .< / span > indexer< span class = "token punctuation" > .< / span > < span class = "token function" > Update< / span > < span class = "token punctuation" > (< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 如 果 informer的 本 地 缓 存 更 新 成 功 , 那 么 就 调 用 shareProcess分 发 对 象 给 用 户 自 定 义 controller处 理 < / span >
< span class = "token comment" > // 可 以 看 到 , 对 EventHandler来 说 , 本 地 缓 存 已 经 存 在 该 对 象 就 认 为 是 update< / span >
s< span class = "token punctuation" > .< / span > processor< span class = "token punctuation" > .< / span > < span class = "token function" > distribute< / span > < span class = "token punctuation" > (< / span > updateNotification< span class = "token punctuation" > {< / span > oldObj< span class = "token punctuation" > :< / span > old< span class = "token punctuation" > ,< / span > newObj< span class = "token punctuation" > :< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > isSync< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span > < span class = "token keyword" > else< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > s< span class = "token punctuation" > .< / span > indexer< span class = "token punctuation" > .< / span > < span class = "token function" > Add< / span > < span class = "token punctuation" > (< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
s< span class = "token punctuation" > .< / span > processor< span class = "token punctuation" > .< / span > < span class = "token function" > distribute< / span > < span class = "token punctuation" > (< / span > addNotification< span class = "token punctuation" > {< / span > newObj< span class = "token punctuation" > :< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > isSync< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > case< / span > Deleted< span class = "token punctuation" > :< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > s< span class = "token punctuation" > .< / span > indexer< span class = "token punctuation" > .< / span > < span class = "token function" > Delete< / span > < span class = "token punctuation" > (< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
s< span class = "token punctuation" > .< / span > processor< span class = "token punctuation" > .< / span > < span class = "token function" > distribute< / span > < span class = "token punctuation" > (< / span > deleteNotification< span class = "token punctuation" > {< / span > oldObj< span class = "token punctuation" > :< / span > d< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token boolean" > false< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< / li >
< / ul >
< p > 前 面 描 述 了 shareIndexInformer内 部 如 何 从 deltaFIFO取 出 对 象 更 新 缓 存 并 通 过 processor回 调 用 户 的 EventHandler, 那 deltaFIFO中 的 增 量 事 件 是 怎 么 加 进 入 的 呢 ? 先 看 看 shareIndexInformer中 controller中 的 reflector实 现 < / p >
< h4 id = "reflectorrun发起listwatch" > reflector.run发 起 ListWatch< / h4 >
< p > reflector.run将 会 调 用 指 定 资 源 的 ListAndWatch方 法 , 注 意 这 里 什 么 时 候 可 能 发 生 re-list或 者 re-watch: 因 为 是 通 过 wait.Util不 断 调 用 ListAndWatch方 法 , 所 以 只 要 该 方 法 return了 , 那 么 就 会 发 生 re-list, watch过 程 则 被 嵌 套 在 for循 环 中 < / p >
< ul >
< li > 以 ResourceVersion=0开 始 首 次 的 List操 作 获 取 指 定 资 源 的 全 量 对 象 , 并 通 过 reflector的 syncWith方 法 将 所 有 对 象 批 量 插 入 deltaFIFO< / li >
< li > List完 成 之 后 将 会 更 新 ResourceVersion用 户 Watch操 作 , 通 过 reflector的 watchHandler方 法 把 watch到 的 增 量 对 象 加 入 到 deltaFIFO< / li >
< / ul >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > r < span class = "token operator" > *< / span > Reflector< span class = "token punctuation" > )< / span > < span class = "token function" > ListAndWatch< / span > < span class = "token punctuation" > (< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 以 版 本 号 ResourceVersion=0开 始 首 次 list< / span >
options < span class = "token operator" > :=< / span > metav1< span class = "token punctuation" > .< / span > ListOptions< span class = "token punctuation" > {< / span > ResourceVersion< span class = "token punctuation" > :< / span > < span class = "token string" > " 0" < / span > < span class = "token punctuation" > }< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
initTrace < span class = "token operator" > :=< / span > trace< span class = "token punctuation" > .< / span > < span class = "token function" > New< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " Reflector ListAndWatch" < / span > < span class = "token punctuation" > ,< / span > trace< span class = "token punctuation" > .< / span > Field< span class = "token punctuation" > {< / span > < span class = "token string" > " name" < / span > < span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > name< span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > var< / span > list runtime< span class = "token punctuation" > .< / span > Object
< span class = "token keyword" > go< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 获 取 list的 结 果 < / span >
list< span class = "token punctuation" > ,< / span > err < span class = "token operator" > =< / span > pager< span class = "token punctuation" > .< / span > < span class = "token function" > List< / span > < span class = "token punctuation" > (< / span > context< span class = "token punctuation" > .< / span > < span class = "token function" > Background< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ,< / span > options< span class = "token punctuation" > )< / span >
< span class = "token function" > close< / span > < span class = "token punctuation" > (< / span > listCh< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
listMetaInterface< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > meta< span class = "token punctuation" > .< / span > < span class = "token function" > ListAccessor< / span > < span class = "token punctuation" > (< / span > list< span class = "token punctuation" > )< / span >
< span class = "token comment" > // 根 据 结 果 更 新 版 本 号 , 用 于 接 下 来 的 watch< / span >
resourceVersion < span class = "token operator" > =< / span > listMetaInterface< span class = "token punctuation" > .< / span > < span class = "token function" > GetResourceVersion< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
items< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > meta< span class = "token punctuation" > .< / span > < span class = "token function" > ExtractList< / span > < span class = "token punctuation" > (< / span > list< span class = "token punctuation" > )< / span >
< span class = "token comment" > // 这 里 的 syncWith是 把 首 次 list到 的 结 果 通 过 DeltaFIFO的 Replce方 法 批 量 添 加 到 队 列 < / span >
< span class = "token comment" > // 队 列 提 供 了 Resync方 法 用 于 判 断 Replace批 量 插 入 的 对 象 是 否 都 pop出 去 了 , factory/informer的 WaitForCacheSync就 是 调 用 了 DeltaFIFO的 的 Resync方 法 < / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > < span class = "token function" > syncWith< / span > < span class = "token punctuation" > (< / span > items< span class = "token punctuation" > ,< / span > resourceVersion< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " %s: Unable to sync list result: %v" < / span > < span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > name< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
r< span class = "token punctuation" > .< / span > < span class = "token function" > setLastSyncResourceVersion< / span > < span class = "token punctuation" > (< / span > resourceVersion< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 以 list对 象 中 获 取 的 ResourceVersion不 断 watch< / span >
< span class = "token keyword" > for< / span > < span class = "token punctuation" > {< / span >
start < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > clock< span class = "token punctuation" > .< / span > < span class = "token function" > Now< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
w< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > listerWatcher< span class = "token punctuation" > .< / span > < span class = "token function" > Watch< / span > < span class = "token punctuation" > (< / span > options< span class = "token punctuation" > )< / span >
< span class = "token comment" > // watchhandler处 理 watch到 的 数 据 , 即 把 对 象 根 据 watch.type增 加 到 DeltaFIFO中 < / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > < span class = "token function" > watchHandler< / span > < span class = "token punctuation" > (< / span > start< span class = "token punctuation" > ,< / span > w< span class = "token punctuation" > ,< / span > < span class = "token operator" > & < / span > resourceVersion< span class = "token punctuation" > ,< / span > resyncerrc< span class = "token punctuation" > ,< / span > stopCh< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > errorStopRequested < span class = "token punctuation" > {< / span >
< span class = "token keyword" > switch< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > case< / span > apierrs< span class = "token punctuation" > .< / span > < span class = "token function" > IsResourceExpired< / span > < span class = "token punctuation" > (< / span > err< span class = "token punctuation" > )< / span > < span class = "token punctuation" > :< / span >
klog< span class = "token punctuation" > .< / span > < span class = "token function" > V< / span > < span class = "token punctuation" > (< / span > < span class = "token number" > 4< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Infof< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " %s: watch of %v ended with: %v" < / span > < span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > name< span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > expectedType< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token keyword" > default< / span > < span class = "token punctuation" > :< / span >
klog< span class = "token punctuation" > .< / span > < span class = "token function" > Warningf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " %s: watch of %v ended with: %v" < / span > < span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > name< span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > expectedType< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h5 id = "list出的对象批量插入deltafifo" > list出 的 对 象 批 量 插 入 deltaFIFO< / h5 >
< blockquote >
< p > 可 以 看 到 是 syncWith方 法 是 通 过 调 用 deltaFIFO的 Replace实 现 批 量 插 入 , 具 体 实 现 见 下 文 中 deltaFIFO的 实 现 描 述 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > r < span class = "token operator" > *< / span > Reflector< span class = "token punctuation" > )< / span > < span class = "token function" > syncWith< / span > < span class = "token punctuation" > (< / span > items < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > runtime< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > ,< / span > resourceVersion < span class = "token builtin" > string< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
found < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > ,< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > items< span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > item < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > items < span class = "token punctuation" > {< / span >
found < span class = "token operator" > =< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > found< span class = "token punctuation" > ,< / span > item< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > r< span class = "token punctuation" > .< / span > store< span class = "token punctuation" > .< / span > < span class = "token function" > Replace< / span > < span class = "token punctuation" > (< / span > found< span class = "token punctuation" > ,< / span > resourceVersion< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h5 id = "watch出的增量对象插入到deltafifo" > watch出 的 增 量 对 象 插 入 到 deltaFIFO< / h5 >
< blockquote >
< p > watch到 的 对 象 直 接 根 据 watch到 的 事 件 类 型 eventType更 新 store( 即 deltaFIFO) , 注 意 这 个 event是 api直 接 返 回 的 , watch event type可 能 是 Added、 Modifyed、 Deleted< / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // watchHandler watches w and keeps *resourceVersion up to date.< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > r < span class = "token operator" > *< / span > Reflector< span class = "token punctuation" > )< / span > < span class = "token function" > watchHandler< / span > < span class = "token punctuation" > (< / span > start time< span class = "token punctuation" > .< / span > Time< span class = "token punctuation" > ,< / span > w watch< span class = "token punctuation" > .< / span > Interface< span class = "token punctuation" > ,< / span > resourceVersion < span class = "token operator" > *< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ,< / span > errc < span class = "token keyword" > chan< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > ,< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > for< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > select< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > case< / span > < span class = "token operator" > < -< / span > stopCh< span class = "token punctuation" > :< / span >
< span class = "token keyword" > return< / span > errorStopRequested
< span class = "token keyword" > case< / span > err < span class = "token operator" > :=< / span > < span class = "token operator" > < -< / span > errc< span class = "token punctuation" > :< / span >
< span class = "token keyword" > return< / span > err
< span class = "token keyword" > case< / span > event< span class = "token punctuation" > ,< / span > ok < span class = "token operator" > :=< / span > < span class = "token operator" > < -< / span > w< span class = "token punctuation" > .< / span > < span class = "token function" > ResultChan< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > :< / span >
< span class = "token keyword" > switch< / span > event< span class = "token punctuation" > .< / span > Type < span class = "token punctuation" > {< / span >
< span class = "token keyword" > case< / span > watch< span class = "token punctuation" > .< / span > Added< span class = "token punctuation" > :< / span >
err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > store< span class = "token punctuation" > .< / span > < span class = "token function" > Add< / span > < span class = "token punctuation" > (< / span > event< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span >
< span class = "token keyword" > case< / span > watch< span class = "token punctuation" > .< / span > Modified< span class = "token punctuation" > :< / span >
err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > store< span class = "token punctuation" > .< / span > < span class = "token function" > Update< / span > < span class = "token punctuation" > (< / span > event< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span >
< span class = "token keyword" > case< / span > watch< span class = "token punctuation" > .< / span > Deleted< span class = "token punctuation" > :< / span >
err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > store< span class = "token punctuation" > .< / span > < span class = "token function" > Delete< / span > < span class = "token punctuation" > (< / span > event< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > )< / span >
< span class = "token keyword" > case< / span > watch< span class = "token punctuation" > .< / span > Bookmark< span class = "token punctuation" > :< / span >
< span class = "token comment" > // A `Bookmark` means watch has synced here, just update the resourceVersion< / span >
< span class = "token keyword" > default< / span > < span class = "token punctuation" > :< / span >
utilruntime< span class = "token punctuation" > .< / span > < span class = "token function" > HandleError< / span > < span class = "token punctuation" > (< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " %s: unable to understand watch event %#v" < / span > < span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > name< span class = "token punctuation" > ,< / span > event< span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token operator" > *< / span > resourceVersion < span class = "token operator" > =< / span > newResourceVersion
r< span class = "token punctuation" > .< / span > < span class = "token function" > setLastSyncResourceVersion< / span > < span class = "token punctuation" > (< / span > newResourceVersion< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h5 id = "定时触发resync" > 定 时 触 发 resync< / h5 >
< p > 在 ListAndWatch中 还 起 了 一 个 gorouting定 时 的 进 行 resync动 作 < / p >
< pre class = "language-" > < code class = "lang-go" > resyncerrc < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > chan< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > ,< / span > < span class = "token number" > 1< / span > < span class = "token punctuation" > )< / span >
cancelCh < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > < span class = "token function" > close< / span > < span class = "token punctuation" > (< / span > cancelCh< span class = "token punctuation" > )< / span >
< span class = "token keyword" > go< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > //获 取 一 个 定 时 channel, 定 时 的 时 间 是 创 建 informer factory时 传 入 的 resyncPeriod< / span >
resyncCh< span class = "token punctuation" > ,< / span > cleanup < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > < span class = "token function" > resyncChan< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token function" > cleanup< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token comment" > // Call the last one written into cleanup< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > select< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > case< / span > < span class = "token operator" > < -< / span > resyncCh< span class = "token punctuation" > :< / span >
< span class = "token keyword" > case< / span > < span class = "token operator" > < -< / span > stopCh< span class = "token punctuation" > :< / span >
< span class = "token keyword" > return< / span >
< span class = "token keyword" > case< / span > < span class = "token operator" > < -< / span > cancelCh< span class = "token punctuation" > :< / span >
< span class = "token keyword" > return< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > if< / span > r< span class = "token punctuation" > .< / span > ShouldResync < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token operator" > ||< / span > r< span class = "token punctuation" > .< / span > < span class = "token function" > ShouldResync< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
klog< span class = "token punctuation" > .< / span > < span class = "token function" > V< / span > < span class = "token punctuation" > (< / span > < span class = "token number" > 4< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > .< / span > < span class = "token function" > Infof< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " %s: forcing resync" < / span > < span class = "token punctuation" > ,< / span > r< span class = "token punctuation" > .< / span > name< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > r< span class = "token punctuation" > .< / span > store< span class = "token punctuation" > .< / span > < span class = "token function" > Resync< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
resyncerrc < span class = "token operator" > < -< / span > err
< span class = "token keyword" > return< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token function" > cleanup< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
resyncCh< span class = "token punctuation" > ,< / span > cleanup < span class = "token operator" > =< / span > r< span class = "token punctuation" > .< / span > < span class = "token function" > resyncChan< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< / code > < / pre >
< p > 调 用 deltaFIFO的 Resync方 法 , 把 底 层 缓 存 的 对 象 全 部 重 新 添 加 到 deltaFIFO中 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > Resync< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > f< span class = "token punctuation" > .< / span > knownObjects < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
keys < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > knownObjects< span class = "token punctuation" > .< / span > < span class = "token function" > ListKeys< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > k < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > keys < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > syncKeyLocked< / span > < span class = "token punctuation" > (< / span > k< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< p > 需 要 注 意 的 是 , 在 添 加 对 象 到 deltaFIFO时 会 检 查 该 队 列 中 有 没 有 增 量 没 有 处 理 完 的 , 如 果 有 则 忽 略 这 个 对 象 的 此 次 resync< / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > syncKeyLocked< / span > < span class = "token punctuation" > (< / span > key < span class = "token builtin" > string< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
obj< span class = "token punctuation" > ,< / span > exists< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > knownObjects< span class = "token punctuation" > .< / span > < span class = "token function" > GetByKey< / span > < span class = "token punctuation" > (< / span > key< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
klog< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " Unexpected error %v during lookup of key %v, unable to queue object for sync" < / span > < span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > ,< / span > key< span class = "token punctuation" > )< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span > < span class = "token keyword" > else< / span > < span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > exists < span class = "token punctuation" > {< / span >
klog< span class = "token punctuation" > .< / span > < span class = "token function" > Infof< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " Key %v does not exist in known objects store, unable to queue object for sync" < / span > < span class = "token punctuation" > ,< / span > key< span class = "token punctuation" > )< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // If we are doing Resync() and there is already an event queued for that object,< / span >
< span class = "token comment" > // we ignore the Resync for it. This is to avoid the race, in which the resync< / span >
< span class = "token comment" > // comes with the previous value of object (since queueing an event for the object< / span >
< span class = "token comment" > // doesn' t trigger changing the underlying store < knownObjects> .< / span >
id< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > KeyOf< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > KeyError< span class = "token punctuation" > {< / span > obj< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 如 果 deltaFIFO中 该 对 象 还 有 增 量 没 有 处 理 , 则 忽 略 以 避 免 冲 突 , 原 因 如 上 面 注 释 : 在 同 一 个 对 象 的 增 量 列 表 中 , 排 在 后 面 的 增 量 的 object相 比 前 面 的 增 量 应 该 更 新 才 是 合 理 的 < / span >
< span class = "token keyword" > if< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > id< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > )< / span > < span class = "token operator" > > < / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 跟 deltaFIFO的 Replace方 法 一 样 , 都 是 添 加 一 个 Sync类 型 的 增 量 < / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > queueActionLocked< / span > < span class = "token punctuation" > (< / span > Sync< span class = "token punctuation" > ,< / span > obj< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " couldn' t queue object: %v" < / span > < span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h3 id = "底层缓存的实现" > 底 层 缓 存 的 实 现 < / h3 >
2021-07-07 17:39:02 +08:00
< p > shareIndexInformer中 带 有 一 个 缓 存 indexer, 是 一 个 支 持 索 引 的 map, 优 点 是 支 持 快 速 查 询 : < / p >
2021-07-01 10:02:38 +08:00
< ul >
< li > Indexer、 Queue接 口 和 cache结 构 体 都 实 现 了 顶 层 的 Store接 口 < / li >
2021-07-07 17:39:02 +08:00
< li > cache结 构 体 持 有 threadSafeStore对 象 , threadSafeStore是 线 程 安 全 的 , 并 且 具 备 自 定 义 索 引 查 找 的 能 力 < / li >
2021-07-01 10:02:38 +08:00
< / ul >
< p > threadSafeMap的 结 构 如 下 : < / p >
< blockquote >
< p > items:存 储 具 体 的 对 象 , 比 如 key为 ns/podName, value为 pod{}
Indexers:一 个 map[string]IndexFunc结 构 , 其 中 key为 索 引 的 名 称 , 如 ’ namespace’ 字 符 串 , value则 是 一 个 具 体 的 索 引 函 数
Indices:一 个 map[string]Index结 构 , 其 中 key也 是 索 引 的 名 称 , value是 一 个 map[string]sets.String结 构 , 其 中 key是 具 体 的 namespace, 如 default这 个 ns, vlaue则 是 这 个 ns下 的 按 照 索 引 函 数 求 出 来 的 值 的 集 合 , 比 如 default这 个 ns下 的 所 有 pod对 象 名 称 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > type< / span > threadSafeMap < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
lock sync< span class = "token punctuation" > .< / span > RWMutex
items < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ]< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token comment" > // indexers maps a name to an IndexFunc< / span >
indexers Indexers
< span class = "token comment" > // indices maps a name to an Index< / span >
indices Indices
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // Indexers maps a name to a IndexFunc< / span >
< span class = "token keyword" > type< / span > Indexers < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ]< / span > IndexFunc
< span class = "token comment" > // Indices maps a name to an Index< / span >
< span class = "token keyword" > type< / span > Indices < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ]< / span > Index
< span class = "token keyword" > type< / span > Index < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ]< / span > sets< span class = "token punctuation" > .< / span > String
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "索引的维护" > 索 引 的 维 护 < / h4 >
< p > 通 过 在 向 items插 入 对 象 的 过 程 中 , 遍 历 所 有 的 Indexers中 的 索 引 函 数 , 根 据 索 引 函 数 存 储 索 引 key到 value的 集 合 关 系 , 以 下 图 式 结 构 可 以 很 好 的 说 明 : < / p >
2021-10-20 18:07:35 +08:00
< figure id = "fig9.5.1.3" > < a href = "https://user-images.githubusercontent.com/41672087/116666278-5981ca00-a9cd-11eb-9570-8ee6eb447d05.png" data-lightbox = "984968e2-fb1e-4021-83ae-a1b11578dd28" data-title = "图片来源于网络" target = "_blank" > < img src = "https://user-images.githubusercontent.com/41672087/116666278-5981ca00-a9cd-11eb-9570-8ee6eb447d05.png" alt = "图片来源于网络" > < / a > < figcaption > 图 9.5.1.3: 图 片 来 源 于 网 络 < / figcaption > < / figure >
2021-07-01 10:02:38 +08:00
< h4 id = "缓存中增加对象" > 缓 存 中 增 加 对 象 < / h4 >
2021-07-07 17:39:02 +08:00
< p > 在 向 threadSafeMap的 items map中 增 加 完 对 象 后 , 再 通 过 updateIndices更 新 索 引 结 构 < / p >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > c < span class = "token operator" > *< / span > threadSafeMap< span class = "token punctuation" > )< / span > < span class = "token function" > Add< / span > < span class = "token punctuation" > (< / span > key < span class = "token builtin" > string< / span > < span class = "token punctuation" > ,< / span > obj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
c< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > c< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
oldObject < span class = "token operator" > :=< / span > c< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > key< span class = "token punctuation" > ]< / span >
< span class = "token comment" > //存 储 对 象 < / span >
c< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > key< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > obj
< span class = "token comment" > //更 新 索 引 < / span >
c< span class = "token punctuation" > .< / span > < span class = "token function" > updateIndices< / span > < span class = "token punctuation" > (< / span > oldObject< span class = "token punctuation" > ,< / span > obj< span class = "token punctuation" > ,< / span > key< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // updateIndices modifies the objects location in the managed indexes, if this is an update, you must provide an oldObj< / span >
< span class = "token comment" > // updateIndices must be called from a function that already has a lock on the cache< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > c < span class = "token operator" > *< / span > threadSafeMap< span class = "token punctuation" > )< / span > < span class = "token function" > updateIndices< / span > < span class = "token punctuation" > (< / span > oldObj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > newObj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > key < span class = "token builtin" > string< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // if we got an old object, we need to remove it before we add it again< / span >
< span class = "token keyword" > if< / span > oldObj < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 这 是 一 个 更 新 操 作 , 先 删 除 原 对 象 的 索 引 记 录 < / span >
c< span class = "token punctuation" > .< / span > < span class = "token function" > deleteFromIndices< / span > < span class = "token punctuation" > (< / span > oldObj< span class = "token punctuation" > ,< / span > key< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 枚 举 所 有 添 加 的 索 引 函 数 < / span >
< span class = "token keyword" > for< / span > name< span class = "token punctuation" > ,< / span > indexFunc < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > c< span class = "token punctuation" > .< / span > indexers < span class = "token punctuation" > {< / span >
< span class = "token comment" > //根 据 索 引 函 数 计 算 obj对 应 的 < / span >
indexValues< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > < span class = "token function" > indexFunc< / span > < span class = "token punctuation" > (< / span > newObj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token function" > panic< / span > < span class = "token punctuation" > (< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " unable to calculate an index entry for key %q on index %q: %v" < / span > < span class = "token punctuation" > ,< / span > key< span class = "token punctuation" > ,< / span > name< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
index < span class = "token operator" > :=< / span > c< span class = "token punctuation" > .< / span > indices< span class = "token punctuation" > [< / span > name< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > index < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
index < span class = "token operator" > =< / span > Index< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
c< span class = "token punctuation" > .< / span > indices< span class = "token punctuation" > [< / span > name< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > index
< span class = "token punctuation" > }< / span >
< span class = "token comment" > //索 引 函 数 计 算 出 多 个 value, 也 可 能 是 一 个 , 比 如 pod的 ns就 只 有 一 个 值 , pod的 label可 能 就 有 多 个 值 < / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > indexValue < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > indexValues < span class = "token punctuation" > {< / span >
< span class = "token comment" > //比 如 namespace索 引 , 根 据 indexValue=default, 获 取 default对 应 的 ji he再 把 当 前 对 象 插 入 < / span >
set < span class = "token operator" > :=< / span > index< span class = "token punctuation" > [< / span > indexValue< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > set < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
set < span class = "token operator" > =< / span > sets< span class = "token punctuation" > .< / span > String< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
index< span class = "token punctuation" > [< / span > indexValue< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > set
< span class = "token punctuation" > }< / span >
set< span class = "token punctuation" > .< / span > < span class = "token function" > Insert< / span > < span class = "token punctuation" > (< / span > key< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "indexfunc索引函数" > IndexFunc索 引 函 数 < / h4 >
2021-07-01 10:02:38 +08:00
< p > 一 个 典 型 的 索 引 函 数 MetaNamespaceIndexFunc, 方 便 查 询 时 可 以 根 据 namespace获 取 该 namespace下 的 所 有 对 象 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // MetaNamespaceIndexFunc is a default index function that indexes based on an object' s namespace< / span >
< span class = "token keyword" > func< / span > < span class = "token function" > MetaNamespaceIndexFunc< / span > < span class = "token punctuation" > (< / span > obj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ,< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
meta< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > meta< span class = "token punctuation" > .< / span > < span class = "token function" > Accessor< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > {< / span > < span class = "token string" > " " < / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " object has no meta: %v" < / span > < span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > {< / span > meta< span class = "token punctuation" > .< / span > < span class = "token function" > GetNamespace< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "index方法利用索引查找对象" > Index方 法 利 用 索 引 查 找 对 象 < / h4 >
< p > 提 供 利 用 索 引 来 查 询 的 能 力 , Index方 法 可 以 根 据 索 引 名 称 和 对 象 , 查 询 所 有 的 关 联 对 象 < / p >
< blockquote >
2021-07-07 17:39:02 +08:00
< p > 例 如 通 过 < code > Index(“ namespace” , & metav1.ObjectMeta{Namespace: namespace})< / code > 获 取 指 定 ns下 的 所 有 对 象 , 具 体 可 以 参 考 tools/cache/listers.go#ListAllByNamespace< / p >
2021-07-01 10:02:38 +08:00
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > c < span class = "token operator" > *< / span > threadSafeMap< span class = "token punctuation" > )< / span > < span class = "token function" > Index< / span > < span class = "token punctuation" > (< / span > indexName < span class = "token builtin" > string< / span > < span class = "token punctuation" > ,< / span > obj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
c< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > RLock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > c< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > RUnlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
indexFunc < span class = "token operator" > :=< / span > c< span class = "token punctuation" > .< / span > indexers< span class = "token punctuation" > [< / span > indexName< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > indexFunc < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > ,< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " Index with name %s does not exist" < / span > < span class = "token punctuation" > ,< / span > indexName< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
indexKeys< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > < span class = "token function" > indexFunc< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > ,< / span > err
< span class = "token punctuation" > }< / span >
index < span class = "token operator" > :=< / span > c< span class = "token punctuation" > .< / span > indices< span class = "token punctuation" > [< / span > indexName< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > var< / span > returnKeySet sets< span class = "token punctuation" > .< / span > String
< span class = "token comment" > //例 如 namespace索 引 < / span >
< span class = "token keyword" > if< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > indexKeys< span class = "token punctuation" > )< / span > < span class = "token operator" > ==< / span > < span class = "token number" > 1< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // In majority of cases, there is exactly one value matching.< / span >
< span class = "token comment" > // Optimize the most common path - deduping is not needed here.< / span >
returnKeySet < span class = "token operator" > =< / span > index< span class = "token punctuation" > [< / span > indexKeys< span class = "token punctuation" > [< / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > ]< / span > < span class = "token punctuation" > ]< / span >
< span class = "token comment" > //例 如 label索 引 < / span >
< span class = "token punctuation" > }< / span > < span class = "token keyword" > else< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // Need to de-dupe the return list.< / span >
< span class = "token comment" > // Since multiple keys are allowed, this can happen.< / span >
returnKeySet < span class = "token operator" > =< / span > sets< span class = "token punctuation" > .< / span > String< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > indexKey < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > indexKeys < span class = "token punctuation" > {< / span >
< span class = "token keyword" > for< / span > key < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > index< span class = "token punctuation" > [< / span > indexKey< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > {< / span >
returnKeySet< span class = "token punctuation" > .< / span > < span class = "token function" > Insert< / span > < span class = "token punctuation" > (< / span > key< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
list < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > ,< / span > returnKeySet< span class = "token punctuation" > .< / span > < span class = "token function" > Len< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > absoluteKey < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > returnKeySet < span class = "token punctuation" > {< / span >
list < span class = "token operator" > =< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > list< span class = "token punctuation" > ,< / span > c< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > absoluteKey< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > list< span class = "token punctuation" > ,< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h3 id = "deltafifo实现" > deltaFIFO实 现 < / h3 >
< p > shareIndexInformer.controller.reflector中 的 deltaFIFO实 现 < / p >
< blockquote >
2021-07-07 17:39:02 +08:00
< p > items: 记 录 deltaFIFO中 的 对 象 , 注 意 map的 value是 一 个 delta slice
queue: 记 录 上 面 items中 的 key, 维 护 对 象 的 fifo顺 序
2021-07-01 10:02:38 +08:00
populated: 队 列 中 是 否 填 充 过 数 据 , LIST时 调 用 Replace或 调 用 Delete/Add/Update都 会 置 为 true
2021-07-07 17:39:02 +08:00
initialPopulationCount: 首 次 List的 时 候 获 取 到 的 数 据 就 会 调 用 Replace批 量 增 加 到 队 列 , 同 时 设 置 initialPopulationCount为 List到 的 对 象 数 量 , 每 次 Pop出 来 会 减 一 , 用 于 判 断 是 否 把 首 次 批 量 插 入 的 数 据 都 POP出 去 了
keyFunc: 知 道 怎 么 从 对 象 中 解 析 出 对 应 key的 函 数 , 如 MetaNamespaceKeyFunc可 以 解 析 出 namespace/name的 形 式
2021-07-01 10:02:38 +08:00
knownObjects: 这 个 其 实 就 是 shareIndexInformer中 的 indexer底 层 缓 存 的 引 用 , 可 以 认 为 和 etcd中 的 数 据 一 致 < / p >
< / blockquote >
2021-07-07 17:39:02 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // NewDeltaFIFO方 法 在 前 面 分 析 的 sharedIndexInformer的 Run方 法 中 调 用 < / span >
< span class = "token comment" > // fifo := NewDeltaFIFO(MetaNamespaceKeyFunc, s.indexer)< / span >
< span class = "token keyword" > func< / span > < span class = "token function" > NewDeltaFIFO< / span > < span class = "token punctuation" > (< / span > keyFunc KeyFunc< span class = "token punctuation" > ,< / span > knownObjects KeyListerGetter< span class = "token punctuation" > )< / span > < span class = "token operator" > *< / span > DeltaFIFO < span class = "token punctuation" > {< / span >
f < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > DeltaFIFO< span class = "token punctuation" > {< / span >
items< span class = "token punctuation" > :< / span > < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ]< / span > Deltas< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
queue< span class = "token punctuation" > :< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
keyFunc< span class = "token punctuation" > :< / span > keyFunc< span class = "token punctuation" > ,< / span >
knownObjects< span class = "token punctuation" > :< / span > knownObjects< span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > }< / span >
f< span class = "token punctuation" > .< / span > cond< span class = "token punctuation" > .< / span > L < span class = "token operator" > =< / span > < span class = "token operator" > & < / span > f< span class = "token punctuation" > .< / span > lock
< span class = "token keyword" > return< / span > f
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > type< / span > DeltaFIFO < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
2021-07-01 10:02:38 +08:00
< span class = "token comment" > // lock/cond protects access to ' items' and ' queue' .< / span >
lock sync< span class = "token punctuation" > .< / span > RWMutex
cond sync< span class = "token punctuation" > .< / span > Cond
< span class = "token comment" > // We depend on the property that items in the set are in< / span >
< span class = "token comment" > // the queue and vice versa, and that all Deltas in this< / span >
< span class = "token comment" > // map have at least one Delta.< / span >
< span class = "token comment" > // 这 里 的 Deltas是 []Delta类 型 < / span >
items < span class = "token keyword" > map< / span > < span class = "token punctuation" > [< / span > < span class = "token builtin" > string< / span > < span class = "token punctuation" > ]< / span > Deltas
queue < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token builtin" > string< / span >
< span class = "token comment" > // populated is true if the first batch of items inserted by Replace() has been populated< / span >
< span class = "token comment" > // or Delete/Add/Update was called first.< / span >
populated < span class = "token builtin" > bool< / span >
< span class = "token comment" > // initialPopulationCount is the number of items inserted by the first call of Replace()< / span >
initialPopulationCount < span class = "token builtin" > int< / span >
< span class = "token comment" > // keyFunc is used to make the key used for queued item< / span >
< span class = "token comment" > // insertion and retrieval, and should be deterministic.< / span >
keyFunc KeyFunc
< span class = "token comment" > // knownObjects list keys that are " known" , for the< / span >
< span class = "token comment" > // purpose of figuring out which items have been deleted< / span >
< span class = "token comment" > // when Replace() or Delete() is called.< / span >
< span class = "token comment" > // 这 个 其 实 就 是 shareIndexInformer中 的 indexer底 层 缓 存 的 引 用 < / span >
knownObjects KeyListerGetter
< span class = "token comment" > // Indication the queue is closed.< / span >
< span class = "token comment" > // Used to indicate a queue is closed so a control loop can exit when a queue is empty.< / span >
< span class = "token comment" > // Currently, not used to gate any of CRED operations.< / span >
closed < span class = "token builtin" > bool< / span >
closedLock sync< span class = "token punctuation" > .< / span > Mutex
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > type< / span > Delta < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
Type DeltaType
Object < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // Deltas is a list of one or more ' Delta' s to an individual object.< / span >
< span class = "token comment" > // The oldest delta is at index 0, the newest delta is the last one.< / span >
< span class = "token keyword" > type< / span > Deltas < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > Delta
< / code > < / pre >
< p > DeltaFIFO关 键 的 方 法 : < / p >
< h4 id = "向deltafifo批量插入对象" > 向 deltaFIFO批 量 插 入 对 象 < / h4 >
2021-07-07 17:39:02 +08:00
< p > 批 量 向 队 列 插 入 数 据 的 方 法 , 注 意 knownObjects是 informer中 本 地 缓 存 indexer的 引 用 < / p >
< p > 这 里 会 更 新 deltaFIFO的 initialPopulationCount为 Replace list的 对 象 总 数 加 上 list中 相 比 knownObjects多 出 的 对 象 数 量 。 < / p >
< blockquote >
< p > 因 为 Replace方 法 可 能 是 reflector发 生 re-list的 时 候 再 次 调 用 , 这 个 时 候 就 会 出 现 knownObjects中 存 在 的 对 象 不 在 Replace list的 情 况 ( 比 如 watch的 delete事 件 丢 失 了 ) , 这 个 时 候 是 把 这 些 对 象 筛 选 出 来 , 封 装 成 DeletedFinalStateUnknown对 象 以 Delete type类 型 再 次 加 入 到 deltaFIFO中 , 这 样 最 终 从 detaFIFO处 理 这 个 DeletedFinalStateUnknown 增 量 时 就 可 以 更 新 本 地 缓 存 并 且 触 发 reconcile。
因 为 这 个 对 象 最 终 的 结 构 确 实 找 不 到 了 , 所 以 只 能 用 knownObjects里 面 的 记 录 来 封 装 delta, 所 以 叫 做 FinalStateUnknown。 < / p >
< / blockquote >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > Replace< / span > < span class = "token punctuation" > (< / span > list < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > resourceVersion < span class = "token builtin" > string< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
keys < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > sets< span class = "token punctuation" > .< / span > String< span class = "token punctuation" > ,< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > list< span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > item < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > list < span class = "token punctuation" > {< / span >
key< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > KeyOf< / span > < span class = "token punctuation" > (< / span > item< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > KeyError< span class = "token punctuation" > {< / span > item< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
keys< span class = "token punctuation" > .< / span > < span class = "token function" > Insert< / span > < span class = "token punctuation" > (< / span > key< span class = "token punctuation" > )< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 调 用 deltaFIFO的 queueActionLocked向 deltaFIFO增 加 一 个 增 量 < / span >
< span class = "token comment" > // 可 以 看 到 Replace添 加 的 Delta type都 是 Sync< / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > queueActionLocked< / span > < span class = "token punctuation" > (< / span > Sync< span class = "token punctuation" > ,< / span > item< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " couldn' t enqueue object: %v" < / span > < span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 底 层 的 缓 存 不 应 该 会 是 nil, 可 以 忽 略 这 种 情 况 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > if< / span > f< span class = "token punctuation" > .< / span > knownObjects < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // Do deletion detection against our own list.< / span >
queuedDeletions < span class = "token operator" > :=< / span > < span class = "token number" > 0< / span >
< span class = "token keyword" > for< / span > k< span class = "token punctuation" > ,< / span > oldItem < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > f< span class = "token punctuation" > .< / span > items < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > keys< span class = "token punctuation" > .< / span > < span class = "token function" > Has< / span > < span class = "token punctuation" > (< / span > k< span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > continue< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 当 knownObjects为 空 时 , 如 果 item中 存 在 对 象 不 在 新 来 的 list中 , 那 么 该 对 象 被 认 为 要 被 删 除 < / span >
< span class = "token keyword" > var< / span > deletedObj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token keyword" > if< / span > n < span class = "token operator" > :=< / span > oldItem< span class = "token punctuation" > .< / span > < span class = "token function" > Newest< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > n < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
deletedObj < span class = "token operator" > =< / span > n< span class = "token punctuation" > .< / span > Object
< span class = "token punctuation" > }< / span >
queuedDeletions< span class = "token operator" > ++< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > queueActionLocked< / span > < span class = "token punctuation" > (< / span > Deleted< span class = "token punctuation" > ,< / span > DeletedFinalStateUnknown< span class = "token punctuation" > {< / span > k< span class = "token punctuation" > ,< / span > deletedObj< span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > f< span class = "token punctuation" > .< / span > populated < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > populated < span class = "token operator" > =< / span > < span class = "token boolean" > true< / span >
< span class = "token comment" > // While there shouldn' t be any queued deletions in the initial< / span >
< span class = "token comment" > // population of the queue, it' s better to be on the safe side.< / span >
f< span class = "token punctuation" > .< / span > initialPopulationCount < span class = "token operator" > =< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > list< span class = "token punctuation" > )< / span > < span class = "token operator" > +< / span > queuedDeletions
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // Detect deletions not already in the queue.< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 当 reflector发 生 re-list时 , 可 能 会 出 现 knownObjects中 存 在 的 对 象 不 在 Replace list的 情 况 < / span >
2021-07-01 10:02:38 +08:00
knownKeys < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > knownObjects< span class = "token punctuation" > .< / span > < span class = "token function" > ListKeys< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // 记 录 这 次 替 换 相 当 于 在 缓 存 中 删 除 多 少 对 象 < / span >
queuedDeletions < span class = "token operator" > :=< / span > < span class = "token number" > 0< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 枚 举 local store中 的 所 有 对 象 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > k < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > knownKeys < span class = "token punctuation" > {< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 对 象 也 在 Replace list中 , 所 以 跳 过 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > if< / span > keys< span class = "token punctuation" > .< / span > < span class = "token function" > Has< / span > < span class = "token punctuation" > (< / span > k< span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > continue< / span >
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 对 象 在 缓 存 , 但 不 在 list中 , 说 明 替 换 操 作 完 成 后 , 这 个 对 象 相 当 于 被 删 除 了 < / span >
< span class = "token comment" > // 注 意 这 里 的 所 谓 替 换 , 对 deltaFIFO来 说 , 是 给 队 列 中 的 对 应 对 象 增 加 一 个 < / span >
< span class = "token comment" > // delete增 量 queueActionLocked(Deleted, DeletedFinalStateUnknown{k, deletedObj})< / span >
< span class = "token comment" > // 真 正 删 除 缓 存 需 要 等 到 DeletedFinalStateUnknown增 量 被 POP出 来 操 作 local store时 < / span >
2021-07-01 10:02:38 +08:00
deletedObj< span class = "token punctuation" > ,< / span > exists< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > knownObjects< span class = "token punctuation" > .< / span > < span class = "token function" > GetByKey< / span > < span class = "token punctuation" > (< / span > k< span class = "token punctuation" > )< / span >
queuedDeletions< span class = "token operator" > ++< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > queueActionLocked< / span > < span class = "token punctuation" > (< / span > Deleted< span class = "token punctuation" > ,< / span > DeletedFinalStateUnknown< span class = "token punctuation" > {< / span > k< span class = "token punctuation" > ,< / span > deletedObj< span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 设 置 f.initialPopulationCount, 该 值 大 于 0表 示 首 次 插 入 的 对 象 还 没 有 全 部 pop出 去 < / span >
< span class = "token comment" > // informer WaitForCacheSync就 是 在 等 待 该 值 为 0< / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > f< span class = "token punctuation" > .< / span > populated < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > populated < span class = "token operator" > =< / span > < span class = "token boolean" > true< / span >
f< span class = "token punctuation" > .< / span > initialPopulationCount < span class = "token operator" > =< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > list< span class = "token punctuation" > )< / span > < span class = "token operator" > +< / span > queuedDeletions
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "从deltafifo-pop出对象" > 从 deltaFIFO pop出 对 象 < / h4 >
2021-07-07 17:39:02 +08:00
< p > 从 队 列 中 Pop出 一 个 方 法 , 并 由 函 数 process来 处 理 , 其 实 就 是 shareIndexInformer的 HandleDeltas< / p >
2021-07-01 10:02:38 +08:00
< blockquote >
< p > 每 次 从 DeltaFIFO Pop出 一 个 对 象 , f.initialPopulationCount会 减 一 , 初 始 值 为 List时 的 对 象 数 量
前 面 的 Informer的 WaitForCacheSync最 终 就 是 调 用 了 这 个 HasSynced方 法 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > Pop< / span > < span class = "token punctuation" > (< / span > process PopProcessFunc< span class = "token punctuation" > )< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > for< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > queue< span class = "token punctuation" > )< / span > < span class = "token operator" > ==< / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // When the queue is empty, invocation of Pop() is blocked until new item is enqueued.< / span >
< span class = "token comment" > // When Close() is called, the f.closed is set and the condition is broadcasted.< / span >
< span class = "token comment" > // Which causes this loop to continue and return from the Pop().< / span >
< span class = "token keyword" > if< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > IsClosed< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > ,< / span > ErrFIFOClosed
< span class = "token punctuation" > }< / span >
f< span class = "token punctuation" > .< / span > cond< span class = "token punctuation" > .< / span > < span class = "token function" > Wait< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > //取 出 队 首 元 素 < / span >
id < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > queue< span class = "token punctuation" > [< / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > ]< / span >
< span class = "token comment" > //去 掉 队 首 元 素 < / span >
f< span class = "token punctuation" > .< / span > queue < span class = "token operator" > =< / span > f< span class = "token punctuation" > .< / span > queue< span class = "token punctuation" > [< / span > < span class = "token number" > 1< / span > < span class = "token punctuation" > :< / span > < span class = "token punctuation" > ]< / span >
< span class = "token comment" > //首 次 填 充 的 对 象 数 减 一 < / span >
< span class = "token keyword" > if< / span > f< span class = "token punctuation" > .< / span > initialPopulationCount < span class = "token operator" > > < / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > initialPopulationCount< span class = "token operator" > --< / span >
< span class = "token punctuation" > }< / span >
item< span class = "token punctuation" > ,< / span > ok < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > id< span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > ok < span class = "token punctuation" > {< / span >
< span class = "token comment" > // Item may have been deleted subsequently.< / span >
< span class = "token keyword" > continue< / span >
< span class = "token punctuation" > }< / span >
< span class = "token function" > delete< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > ,< / span > id< span class = "token punctuation" > )< / span >
< span class = "token comment" > //处 理 增 量 对 象 < / span >
err < span class = "token operator" > :=< / span > < span class = "token function" > process< / span > < span class = "token punctuation" > (< / span > item< span class = "token punctuation" > )< / span >
< span class = "token comment" > // 如 果 没 有 处 理 成 功 , 那 么 就 会 重 新 加 到 deltaFIFO队 列 中 < / span >
< span class = "token keyword" > if< / span > e< span class = "token punctuation" > ,< / span > ok < span class = "token operator" > :=< / span > err< span class = "token punctuation" > .< / span > < span class = "token punctuation" > (< / span > ErrRequeue< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > ok < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > < span class = "token function" > addIfNotPresent< / span > < span class = "token punctuation" > (< / span > id< span class = "token punctuation" > ,< / span > item< span class = "token punctuation" > )< / span >
err < span class = "token operator" > =< / span > e< span class = "token punctuation" > .< / span > Err
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // Don' t need to copyDeltas here, because we' re transferring< / span >
< span class = "token comment" > // ownership to the caller.< / span >
< span class = "token keyword" > return< / span > item< span class = "token punctuation" > ,< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "deltafifo是否同步完成" > deltaFIFO是 否 同 步 完 成 < / h4 >
2021-07-07 17:39:02 +08:00
< p > 串 连 前 面 的 问 题 : factory的 WaitForCacheSync是 如 何 等 待 缓 存 同 步 完 成 < / p >
2021-07-01 10:02:38 +08:00
< blockquote >
< p > factory的 WaitForCacheSync方 法 调 用 informer的 HasSync方 法 , 继 而 调 用 deltaFIFO的 HasSync方 法 , 也 就 是 判 断 从 reflector list到 的 数 据 是 否 pop完 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > HasSynced< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > bool< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > return< / span > f< span class = "token punctuation" > .< / span > populated < span class = "token operator" > & & < / span > f< span class = "token punctuation" > .< / span > initialPopulationCount < span class = "token operator" > ==< / span > < span class = "token number" > 0< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "同步local-store到deltafifo" > 同 步 local store到 deltaFIFO< / h4 >
2021-07-01 10:02:38 +08:00
< blockquote >
< p > 所 谓 的 resync, 其 实 就 是 把 knownObjects即 缓 存 中 的 对 象 全 部 再 通 过 queueActionLocked(Sync, obj)加 到 队 列 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > Resync< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > f< span class = "token punctuation" > .< / span > lock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > f< span class = "token punctuation" > .< / span > knownObjects < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
keys < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > knownObjects< span class = "token punctuation" > .< / span > < span class = "token function" > ListKeys< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 把 local store中 的 对 象 都 以 Sync类 型 增 量 的 形 式 重 新 放 回 到 deltaFIFO< / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > k < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > keys < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > syncKeyLocked< / span > < span class = "token punctuation" > (< / span > k< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > err
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > syncKeyLocked< / span > < span class = "token punctuation" > (< / span > key < span class = "token builtin" > string< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
obj< span class = "token punctuation" > ,< / span > exists< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > knownObjects< span class = "token punctuation" > .< / span > < span class = "token function" > GetByKey< / span > < span class = "token punctuation" > (< / span > key< span class = "token punctuation" > )< / span >
< span class = "token comment" > // If we are doing Resync() and there is already an event queued for that object,< / span >
< span class = "token comment" > // we ignore the Resync for it. This is to avoid the race, in which the resync< / span >
< span class = "token comment" > // comes with the previous value of object (since queueing an event for the object< / span >
< span class = "token comment" > // doesn' t trigger changing the underlying store < knownObjects> .< / span >
id< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > KeyOf< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > KeyError< span class = "token punctuation" > {< / span > obj< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 如 上 述 注 释 , 在 resync时 , 如 果 deltaFIFO中 该 对 象 还 存 在 其 他 delta没 处 理 , 那 么 忽 略 这 次 的 resync< / span >
< span class = "token comment" > // 因 为 调 用 queueActionLocked是 增 加 delta是 通 过 append的 , 且 处 理 对 象 的 增 量 delta时 , 是 从 oldest到 newdest的 < / span >
< span class = "token comment" > // 所 以 如 果 某 个 对 象 还 存 在 增 量 没 处 理 , 再 append就 可 能 导 致 后 处 理 的 delta是 旧 的 对 象 < / span >
< span class = "token keyword" > if< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > id< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > )< / span > < span class = "token operator" > > < / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 可 以 看 到 这 里 跟 list一 样 , 增 加 到 deltaFIFO的 是 一 个 Sync类 型 的 增 量 < / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > if< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > queueActionLocked< / span > < span class = "token punctuation" > (< / span > Sync< span class = "token punctuation" > ,< / span > obj< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " couldn' t queue object: %v" < / span > < span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "在deltafifo增加一个对象" > 在 deltaFIFO增 加 一 个 对 象 < / h4 >
< p > 注 意 这 里 在 append增 量 时 的 去 重 逻 辑 : 如 果 连 续 的 两 个 增 量 类 型 都 是 Deleted, 那 么 就 去 掉 一 个 ( 正 常 情 况 确 实 不 会 出 现 这 样 , 且 没 必 要 ) , 优 先 去 掉 前 面 所 说 的 因 为 re-list可 能 导 致 的 api与 local store不 一 致 而 增 加 的 DeletedFinalStateUnknown类 型 的 增 量 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > //在 队 列 中 给 指 定 的 对 象 append一 个 Delta< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > f < span class = "token operator" > *< / span > DeltaFIFO< span class = "token punctuation" > )< / span > < span class = "token function" > queueActionLocked< / span > < span class = "token punctuation" > (< / span > actionType DeltaType< span class = "token punctuation" > ,< / span > obj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > {< / span >
id< span class = "token punctuation" > ,< / span > err < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > < span class = "token function" > KeyOf< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > KeyError< span class = "token punctuation" > {< / span > obj< span class = "token punctuation" > ,< / span > err< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 把 增 量 append到 slice的 后 面 < / span >
newDeltas < span class = "token operator" > :=< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > id< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > ,< / span > Delta< span class = "token punctuation" > {< / span > actionType< span class = "token punctuation" > ,< / span > obj< span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // 连 续 的 两 个 Deleted delta将 会 去 掉 一 个 < / span >
newDeltas < span class = "token operator" > =< / span > < span class = "token function" > dedupDeltas< / span > < span class = "token punctuation" > (< / span > newDeltas< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > newDeltas< span class = "token punctuation" > )< / span > < span class = "token operator" > > < / span > < span class = "token number" > 0< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 维 护 queue队 列 < / span >
< span class = "token keyword" > if< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > exists < span class = "token operator" > :=< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > id< span class = "token punctuation" > ]< / span > < span class = "token punctuation" > ;< / span > < span class = "token operator" > !< / span > exists < span class = "token punctuation" > {< / span >
f< span class = "token punctuation" > .< / span > queue < span class = "token operator" > =< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > queue< span class = "token punctuation" > ,< / span > id< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > [< / span > id< span class = "token punctuation" > ]< / span > < span class = "token operator" > =< / span > newDeltas
f< span class = "token punctuation" > .< / span > cond< span class = "token punctuation" > .< / span > < span class = "token function" > Broadcast< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span > < span class = "token keyword" > else< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // We need to remove this from our map (extra items in the queue are< / span >
< span class = "token comment" > // ignored if they are not in the map).< / span >
< span class = "token function" > delete< / span > < span class = "token punctuation" > (< / span > f< span class = "token punctuation" > .< / span > items< span class = "token punctuation" > ,< / span > id< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< p > 当 前 认 为 只 有 连 续 的 两 个 Delete delta才 有 必 要 去 重 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token function" > dedupDeltas< / span > < span class = "token punctuation" > (< / span > deltas Deltas< span class = "token punctuation" > )< / span > Deltas < span class = "token punctuation" > {< / span >
n < span class = "token operator" > :=< / span > < span class = "token function" > len< / span > < span class = "token punctuation" > (< / span > deltas< span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > n < span class = "token operator" > < < / span > < span class = "token number" > 2< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > deltas
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // 每 次 取 最 后 两 个 delta来 判 断 < / span >
a < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > deltas< span class = "token punctuation" > [< / span > n< span class = "token operator" > -< / span > < span class = "token number" > 1< / span > < span class = "token punctuation" > ]< / span >
b < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > deltas< span class = "token punctuation" > [< / span > n< span class = "token operator" > -< / span > < span class = "token number" > 2< / span > < span class = "token punctuation" > ]< / span >
< span class = "token keyword" > if< / span > out < span class = "token operator" > :=< / span > < span class = "token function" > isDup< / span > < span class = "token punctuation" > (< / span > a< span class = "token punctuation" > ,< / span > b< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > out < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
d < span class = "token operator" > :=< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > Deltas< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > deltas< span class = "token punctuation" > [< / span > < span class = "token punctuation" > :< / span > n< span class = "token operator" > -< / span > < span class = "token number" > 2< / span > < span class = "token punctuation" > ]< / span > < span class = "token operator" > ...< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > return< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > d< span class = "token punctuation" > ,< / span > < span class = "token operator" > *< / span > out< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > deltas
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > func< / span > < span class = "token function" > isDup< / span > < span class = "token punctuation" > (< / span > a< span class = "token punctuation" > ,< / span > b < span class = "token operator" > *< / span > Delta< span class = "token punctuation" > )< / span > < span class = "token operator" > *< / span > Delta < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 当 前 认 为 只 有 连 续 的 两 个 Delete delta才 有 必 要 去 重 < / span >
< span class = "token keyword" > if< / span > out < span class = "token operator" > :=< / span > < span class = "token function" > isDeletionDup< / span > < span class = "token punctuation" > (< / span > a< span class = "token punctuation" > ,< / span > b< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > out < span class = "token operator" > !=< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > out
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // TODO: Detect other duplicate situations? Are there any?< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // keep the one with the most information if both are deletions.< / span >
< span class = "token keyword" > func< / span > < span class = "token function" > isDeletionDup< / span > < span class = "token punctuation" > (< / span > a< span class = "token punctuation" > ,< / span > b < span class = "token operator" > *< / span > Delta< span class = "token punctuation" > )< / span > < span class = "token operator" > *< / span > Delta < span class = "token punctuation" > {< / span >
< span class = "token keyword" > if< / span > b< span class = "token punctuation" > .< / span > Type < span class = "token operator" > !=< / span > Deleted < span class = "token operator" > ||< / span > a< span class = "token punctuation" > .< / span > Type < span class = "token operator" > !=< / span > Deleted < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // Do more sophisticated checks, or is this sufficient?< / span >
< span class = "token comment" > // 优 先 去 重 DeletedFinalStateUnknown类 型 的 Deleted delta< / span >
< span class = "token keyword" > if< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > ok < span class = "token operator" > :=< / span > b< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > .< / span > < span class = "token punctuation" > (< / span > DeletedFinalStateUnknown< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ;< / span > ok < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span > a
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > b
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h3 id = "sharedprocessor的实现" > sharedProcessor的 实 现 < / h3 >
< p > shareIndexInformer中 的 sharedProcess结 构 , 用 于 分 发 deltaFIFO的 对 象 , 回 调 用 户 配 置 的 EventHandler方 法 < / p >
< p > 可 以 看 到 shareIndexInformer中 的 process直 接 通 过 & sharedProcessor{clock: realClock}初 始 化 < / p >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // NewSharedIndexInformer creates a new instance for the listwatcher.< / span >
< span class = "token keyword" > func< / span > < span class = "token function" > NewSharedIndexInformer< / span > < span class = "token punctuation" > (< / span > lw ListerWatcher< span class = "token punctuation" > ,< / span > objType runtime< span class = "token punctuation" > .< / span > Object< span class = "token punctuation" > ,< / span > defaultEventHandlerResyncPeriod time< span class = "token punctuation" > .< / span > Duration< span class = "token punctuation" > ,< / span > indexers Indexers< span class = "token punctuation" > )< / span > SharedIndexInformer < span class = "token punctuation" > {< / span >
realClock < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > clock< span class = "token punctuation" > .< / span > RealClock< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
sharedIndexInformer < span class = "token operator" > :=< / span > < span class = "token operator" > & < / span > sharedIndexInformer< span class = "token punctuation" > {< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 初 始 化 一 个 默 认 的 processor< / span >
2021-07-01 10:02:38 +08:00
processor< span class = "token punctuation" > :< / span > < span class = "token operator" > & < / span > sharedProcessor< span class = "token punctuation" > {< / span > clock< span class = "token punctuation" > :< / span > realClock< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span >
indexer< span class = "token punctuation" > :< / span > < span class = "token function" > NewIndexer< / span > < span class = "token punctuation" > (< / span > DeletionHandlingMetaNamespaceKeyFunc< span class = "token punctuation" > ,< / span > indexers< span class = "token punctuation" > )< / span > < span class = "token punctuation" > ,< / span >
listerWatcher< span class = "token punctuation" > :< / span > lw< span class = "token punctuation" > ,< / span >
objectType< span class = "token punctuation" > :< / span > objType< span class = "token punctuation" > ,< / span >
resyncCheckPeriod< span class = "token punctuation" > :< / span > defaultEventHandlerResyncPeriod< span class = "token punctuation" > ,< / span >
defaultEventHandlerResyncPeriod< span class = "token punctuation" > :< / span > defaultEventHandlerResyncPeriod< span class = "token punctuation" > ,< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // cacheMutationDetector: 可 以 记 录 local store是 否 被 外 部 修 改 < / span >
2021-07-01 10:02:38 +08:00
cacheMutationDetector< span class = "token punctuation" > :< / span > < span class = "token function" > NewCacheMutationDetector< / span > < span class = "token punctuation" > (< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Sprintf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " %T" < / span > < span class = "token punctuation" > ,< / span > objType< span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > ,< / span >
clock< span class = "token punctuation" > :< / span > realClock< span class = "token punctuation" > ,< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > sharedIndexInformer
< span class = "token punctuation" > }< / span >
2021-07-07 17:39:02 +08:00
< / code > < / pre >
< p > 如 下 为 sharedProcessor结 构 : < / p >
< blockquote >
< p > listenersStarted: listeners中 包 含 的 listener是 否 都 已 经 启 动 了
listeners: 已 添 加 的 listener列 表 , 用 来 处 理 watch到 的 数 据
syncingListeners: 已 添 加 的 listener列 表 , 用 来 处 理 list或 者 resync的 数 据 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > type< / span > sharedProcessor < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
2021-07-01 10:02:38 +08:00
listenersStarted < span class = "token builtin" > bool< / span >
listenersLock sync< span class = "token punctuation" > .< / span > RWMutex
listeners < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token operator" > *< / span > processorListener
syncingListeners < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token operator" > *< / span > processorListener
clock clock< span class = "token punctuation" > .< / span > Clock
wg wait< span class = "token punctuation" > .< / span > Group
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "理解listeners和syncinglisteners的区别" > 理 解 listeners和 syncingListeners的 区 别 < / h4 >
< p > processor可 以 支 持 listener的 维 度 配 置 是 否 需 要 resync: 一 个 informer可 以 配 置 多 个 EventHandler, 而 一 个 EventHandler对 应 processor中 的 一 个 listener, 每 个 listener可 以 配 置 需 不 需 要 resync, 如 果 某 个 listener需 要 resync, 那 么 添 加 到 deltaFIFO的 Sync增 量 最 终 也 只 会 回 到 对 应 的 listener< / p >
< p > reflector中 会 定 时 判 断 每 一 个 listener是 否 需 要 进 行 resync, 判 断 的 依 据 是 看 配 置 EventHandler的 时 候 指 定 的 resyncPeriod, 0代 表 该 listener不 需 要 resync, 否 则 就 每 隔 resyncPeriod看 看 是 否 到 时 间 了 < / p >
< ul >
< li > < p > listeners: 记 录 了 informer添 加 的 所 有 listener< / p >
< / li >
< li > < p > syncingListeners: 记 录 了 informer中 哪 些 listener处 于 sync状 态 < / p >
< / li >
< / ul >
< p > syncingListeners是 listeners的 子 集 , syncingListeners记 录 那 些 开 启 了 resync且 时 间 已 经 到 达 了 的 listener, 把 它 们 放 在 一 个 独 立 的 slice是 避 免 下 面 分 析 的 distribute方 法 中 把 obj增 加 到 了 还 不 需 要 resync的 listener中 < / p >
2021-07-01 10:02:38 +08:00
< h4 id = "为sharedprocessor添加listener" > 为 sharedProcessor添 加 listener< / h4 >
< p > 在 sharedProcessor中 添 加 一 个 listener< / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > sharedProcessor< span class = "token punctuation" > )< / span > < span class = "token function" > addListenerLocked< / span > < span class = "token punctuation" > (< / span > listener < span class = "token operator" > *< / span > processorListener< span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 同 时 添 加 到 listeners和 syncingListeners列 表 , 但 其 实 添 加 的 是 同 一 个 对 象 的 引 用 < / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 所 以 下 面 run启 动 的 时 候 只 需 要 启 动 listeners中 listener就 可 以 了 < / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > listeners < span class = "token operator" > =< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > p< span class = "token punctuation" > .< / span > listeners< span class = "token punctuation" > ,< / span > listener< span class = "token punctuation" > )< / span >
p< span class = "token punctuation" > .< / span > syncingListeners < span class = "token operator" > =< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > p< span class = "token punctuation" > .< / span > syncingListeners< span class = "token punctuation" > ,< / span > listener< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
2021-07-07 17:39:02 +08:00
< h4 id = "启动sharedprocessor中的listener" > 启 动 sharedProcessor中 的 listener< / h4 >
2021-07-01 10:02:38 +08:00
< p > sharedProcessor启 动 所 有 的 listener
是 通 过 调 用 listener.run和 listener.pop来 启 动 一 个 listener, 两 个 方 法 具 体 作 用 看 下 文 processorListener说 明 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > sharedProcessor< span class = "token punctuation" > )< / span > < span class = "token function" > run< / span > < span class = "token punctuation" > (< / span > stopCh < span class = "token operator" > < -< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > RLock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > RUnlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > listener < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > p< span class = "token punctuation" > .< / span > listeners < span class = "token punctuation" > {< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // listener的 run方 法 不 断 的 从 listener自 身 的 缓 冲 区 取 出 对 象 回 调 handler< / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > wg< span class = "token punctuation" > .< / span > < span class = "token function" > Start< / span > < span class = "token punctuation" > (< / span > listener< span class = "token punctuation" > .< / span > run< span class = "token punctuation" > )< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // listener的 pod方 法 不 断 的 接 收 对 象 并 暂 存 在 自 身 的 缓 冲 区 中 < / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > wg< span class = "token punctuation" > .< / span > < span class = "token function" > Start< / span > < span class = "token punctuation" > (< / span > listener< span class = "token punctuation" > .< / span > pop< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
p< span class = "token punctuation" > .< / span > listenersStarted < span class = "token operator" > =< / span > < span class = "token boolean" > true< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token operator" > < -< / span > stopCh
p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > RLock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > RUnlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > listener < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > p< span class = "token punctuation" > .< / span > listeners < span class = "token punctuation" > {< / span >
< span class = "token function" > close< / span > < span class = "token punctuation" > (< / span > listener< span class = "token punctuation" > .< / span > addCh< span class = "token punctuation" > )< / span > < span class = "token comment" > // Tell .pop() to stop. .pop() will tell .run() to stop< / span >
< span class = "token punctuation" > }< / span >
p< span class = "token punctuation" > .< / span > wg< span class = "token punctuation" > .< / span > < span class = "token function" > Wait< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token comment" > // Wait for all .pop() and .run() to stop< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "sharedprocessor分发对象" > sharedProcessor分 发 对 象 < / h4 >
2021-07-07 17:39:02 +08:00
< p > distribute方 法 是 在 前 面 介 绍 < code > [deltaFIFO pop出 来 的 对 象 处 理 逻 辑 ]< / code > 时 提 到 的 , 把 notification事 件 添 加 到 listener中 , listener如 何 pop出 notification回 调 EventHandler见 下 文 listener部 分 分 析 < / p >
< p > 当 通 过 distribute分 发 从 deltaFIFO获 取 的 对 象 时 , 如 果 delta type是 Sync, 那 么 就 会 把 对 象 交 给 sync listener来 处 理 , 而 Sync类 型 的 delta只 能 来 源 于 下 面 两 种 情 况 : < / p >
< ul >
< li > reflector list Replace到 deltaFIFO的 对 象 : 因 为 首 次 在 sharedProcessor增 加 一 个 listener的 时 候 是 同 时 加 在 listeners和 syncingListeners中 的 < / li >
< li > reflector定 时 触 发 resync local store到 deltaFIFO的 对 象 : 因 为 每 次 reflector调 用 processor的 shouldResync时 , 都 会 把 达 到 resync条 件 的 listener筛 选 出 来 重 新 放 到 p.syncingListeners< / li >
< / ul >
< p > 上 面 两 种 情 况 都 可 以 在 p.syncingListeners中 准 备 好 listener< / p >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > sharedProcessor< span class = "token punctuation" > )< / span > < span class = "token function" > distribute< / span > < span class = "token punctuation" > (< / span > obj < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > sync < span class = "token builtin" > bool< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > RLock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > RUnlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 如 果 是 通 过 reflector list Replace到 deltaFIFO的 对 象 或 者 reflector定 时 触 发 resync到 deltaFIFO的 对 象 , 那 么 distribute到 syncingListeners< / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > if< / span > sync < span class = "token punctuation" > {< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 保 证 deltaFIFO Resync方 法 过 来 的 delta obj只 给 开 启 了 resync能 力 的 listener< / span >
2021-07-01 10:02:38 +08:00
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > listener < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > p< span class = "token punctuation" > .< / span > syncingListeners < span class = "token punctuation" > {< / span >
listener< span class = "token punctuation" > .< / span > < span class = "token function" > add< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span > < span class = "token keyword" > else< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > listener < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > p< span class = "token punctuation" > .< / span > listeners < span class = "token punctuation" > {< / span >
listener< span class = "token punctuation" > .< / span > < span class = "token function" > add< / span > < span class = "token punctuation" > (< / span > obj< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h3 id = "processorlistener结构" > processorListener结 构 < / h3 >
< p > sharedProcessor中 的 listener具 体 的 类 型 : 运 转 逻 辑 就 是 把 用 户 通 过 addCh增 加 的 事 件 发 送 到 nextCh供 run方 法 取 出 回 调 Eventhandler, 因 为 addCh和 nectCh都 是 无 缓 冲 channel, 所 以 中 间 引 入 ringBuffer做 缓 存 < / p >
2021-07-07 17:39:02 +08:00
< p > processorListener是 sharedIndexInformer调 用 AddEventHandler时 创 建 并 添 加 到 sharedProcessor, 对 于 一 个 Informer, 可 以 多 次 调 用 AddEventHandler来 添 加 多 个 listener< / p >
2021-07-01 10:02:38 +08:00
< blockquote >
2021-07-07 17:39:02 +08:00
< p > addCh: 无 缓 冲 的 chan, listener的 pod方 法 不 断 从 addCh取 出 对 象 丢 给 nextCh。 addCh中 的 对 象 来 源 于 listener的 add方 法 , 如 果 nextCh不 能 及 时 消 费 , 则 放 入 缓 冲 区 pendingNotifications
nextCh: 无 缓 冲 的 chan, listener的 run方 法 不 断 从 nextCh取 出 对 象 回 调 用 户 handler。 nextCh的 对 象 来 源 于 addCh或 者 缓 冲 区
pendingNotifications: 一 个 无 容 量 限 制 的 环 形 缓 冲 区 , 可 以 理 解 为 可 以 无 限 存 储 的 队 列 , 用 来 存 储 deltaFIFO分 发 过 来 的 消 息
nextResync: 由 resyncPeriod和 requestedResyncPeriod计 算 得 出 , 与 当 前 时 间 now比 较 判 断 listener是 否 该 进 行 resync了
resyncPeriod: listener自 身 期 待 多 长 时 间 进 行 resync
requestedResyncPeriod: informer希 望 listener多 长 时 间 进 行 resync< / p >
2021-07-01 10:02:38 +08:00
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > type< / span > processorListener < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span >
nextCh < span class = "token keyword" > chan< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
addCh < span class = "token keyword" > chan< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
handler ResourceEventHandler
< span class = "token comment" > // pendingNotifications is an unbounded ring buffer that holds all notifications not yet distributed.< / span >
< span class = "token comment" > // There is one per listener, but a failing/stalled listener will have infinite pendingNotifications< / span >
< span class = "token comment" > // added until we OOM.< / span >
< span class = "token comment" > // TODO: This is no worse than before, since reflectors were backed by unbounded DeltaFIFOs, but< / span >
< span class = "token comment" > // we should try to do something better.< / span >
pendingNotifications buffer< span class = "token punctuation" > .< / span > RingGrowing
< span class = "token comment" > // requestedResyncPeriod is how frequently the listener wants a full resync from the shared informer< / span >
requestedResyncPeriod time< span class = "token punctuation" > .< / span > Duration
< span class = "token comment" > // resyncPeriod is how frequently the listener wants a full resync from the shared informer. This< / span >
< span class = "token comment" > // value may differ from requestedResyncPeriod if the shared informer adjusts it to align with the< / span >
< span class = "token comment" > // informer' s overall resync check period.< / span >
resyncPeriod time< span class = "token punctuation" > .< / span > Duration
< span class = "token comment" > // nextResync is the earliest time the listener should get a full resync< / span >
nextResync time< span class = "token punctuation" > .< / span > Time
< span class = "token comment" > // resyncLock guards access to resyncPeriod and nextResync< / span >
resyncLock sync< span class = "token punctuation" > .< / span > Mutex
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "在listener中添加事件" > 在 listener中 添 加 事 件 < / h4 >
< p > shareProcessor中 的 distribute方 法 调 用 的 是 listener的 add来 向 addCh增 加 消 息 , 注 意 addCh是 无 缓 冲 的 chan, 依 赖 pop不 断 从 addCh取 出 数 据 < / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > processorListener< span class = "token punctuation" > )< / span > < span class = "token function" > add< / span > < span class = "token punctuation" > (< / span > notification < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 虽 然 p.addCh是 一 个 无 缓 冲 的 channel, 但 是 因 为 listener中 存 在 ring buffer, 所 以 这 里 并 不 会 一 直 阻 塞 < / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > addCh < span class = "token operator" > < -< / span > notification
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "判断是否需要resync" > 判 断 是 否 需 要 resync< / h4 >
2021-07-07 17:39:02 +08:00
< p > 如 果 resyncPeriod为 0表 示 不 需 要 resync, 否 则 判 断 当 前 时 间 now是 否 已 经 超 过 了 nextResync, 是 的 话 则 返 回 true表 示 需 要 resync。 其 中 nextResync在 每 次 调 用 listener的 shouldResync方 法 成 功 时 更 新 < / p >
2021-07-01 10:02:38 +08:00
< pre class = "language-" > < code class = "lang-go" > < span class = "token comment" > // shouldResync queries every listener to determine if any of them need a resync, based on each< / span >
< span class = "token comment" > // listener' s resyncPeriod.< / span >
< span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > sharedProcessor< span class = "token punctuation" > )< / span > < span class = "token function" > shouldResync< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token builtin" > bool< / span > < span class = "token punctuation" > {< / span >
p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > Lock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > p< span class = "token punctuation" > .< / span > listenersLock< span class = "token punctuation" > .< / span > < span class = "token function" > Unlock< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 这 里 每 次 都 会 先 置 空 列 表 , 保 证 里 面 记 录 了 当 前 需 要 resync的 listener< / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > syncingListeners < span class = "token operator" > =< / span > < span class = "token punctuation" > [< / span > < span class = "token punctuation" > ]< / span > < span class = "token operator" > *< / span > processorListener< span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
resyncNeeded < span class = "token operator" > :=< / span > < span class = "token boolean" > false< / span >
now < span class = "token operator" > :=< / span > p< span class = "token punctuation" > .< / span > clock< span class = "token punctuation" > .< / span > < span class = "token function" > Now< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > for< / span > < span class = "token boolean" > _< / span > < span class = "token punctuation" > ,< / span > listener < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > p< span class = "token punctuation" > .< / span > listeners < span class = "token punctuation" > {< / span >
< span class = "token comment" > // need to loop through all the listeners to see if they need to resync so we can prepare any< / span >
< span class = "token comment" > // listeners that are going to be resyncing.< / span >
< span class = "token keyword" > if< / span > listener< span class = "token punctuation" > .< / span > < span class = "token function" > shouldResync< / span > < span class = "token punctuation" > (< / span > now< span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
resyncNeeded < span class = "token operator" > =< / span > < span class = "token boolean" > true< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 达 到 resync条 件 的 listener被 加 入 syncingListeners< / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > syncingListeners < span class = "token operator" > =< / span > < span class = "token function" > append< / span > < span class = "token punctuation" > (< / span > p< span class = "token punctuation" > .< / span > syncingListeners< span class = "token punctuation" > ,< / span > listener< span class = "token punctuation" > )< / span >
listener< span class = "token punctuation" > .< / span > < span class = "token function" > determineNextResync< / span > < span class = "token punctuation" > (< / span > now< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > return< / span > resyncNeeded
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "listener的run方法回调eventhandler" > listener的 run方 法 回 调 EventHandler< / h4 >
< p > listener的 run方 法 不 断 的 从 nextCh中 获 取 notification, 并 根 据 notification的 类 型 来 调 用 用 户 自 定 的 EventHandler< / p >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > processorListener< span class = "token punctuation" > )< / span > < span class = "token function" > run< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // this call blocks until the channel is closed. When a panic happens during the notification< / span >
< span class = "token comment" > // we will catch it, **the offending item will be skipped!**, and after a short delay (one second)< / span >
< span class = "token comment" > // the next notification will be attempted. This is usually better than the alternative of never< / span >
< span class = "token comment" > // delivering again.< / span >
stopCh < span class = "token operator" > :=< / span > < span class = "token function" > make< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > chan< / span > < span class = "token keyword" > struct< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span >
wait< span class = "token punctuation" > .< / span > < span class = "token function" > Until< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // this gives us a few quick retries before a long pause and then a few more quick retries< / span >
err < span class = "token operator" > :=< / span > wait< span class = "token punctuation" > .< / span > < span class = "token function" > ExponentialBackoff< / span > < span class = "token punctuation" > (< / span > retry< span class = "token punctuation" > .< / span > DefaultRetry< span class = "token punctuation" > ,< / span > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > (< / span > < span class = "token builtin" > bool< / span > < span class = "token punctuation" > ,< / span > < span class = "token builtin" > error< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > for< / span > next < span class = "token operator" > :=< / span > < span class = "token keyword" > range< / span > p< span class = "token punctuation" > .< / span > nextCh < span class = "token punctuation" > {< / span >
< span class = "token keyword" > switch< / span > notification < span class = "token operator" > :=< / span > next< span class = "token punctuation" > .< / span > < span class = "token punctuation" > (< / span > < span class = "token keyword" > type< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > case< / span > updateNotification< span class = "token punctuation" > :< / span >
2021-07-07 17:39:02 +08:00
< span class = "token comment" > // 回 调 用 户 配 置 的 handler< / span >
2021-07-01 10:02:38 +08:00
p< span class = "token punctuation" > .< / span > handler< span class = "token punctuation" > .< / span > < span class = "token function" > OnUpdate< / span > < span class = "token punctuation" > (< / span > notification< span class = "token punctuation" > .< / span > oldObj< span class = "token punctuation" > ,< / span > notification< span class = "token punctuation" > .< / span > newObj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > case< / span > addNotification< span class = "token punctuation" > :< / span >
p< span class = "token punctuation" > .< / span > handler< span class = "token punctuation" > .< / span > < span class = "token function" > OnAdd< / span > < span class = "token punctuation" > (< / span > notification< span class = "token punctuation" > .< / span > newObj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > case< / span > deleteNotification< span class = "token punctuation" > :< / span >
p< span class = "token punctuation" > .< / span > handler< span class = "token punctuation" > .< / span > < span class = "token function" > OnDelete< / span > < span class = "token punctuation" > (< / span > notification< span class = "token punctuation" > .< / span > oldObj< span class = "token punctuation" > )< / span >
< span class = "token keyword" > default< / span > < span class = "token punctuation" > :< / span >
utilruntime< span class = "token punctuation" > .< / span > < span class = "token function" > HandleError< / span > < span class = "token punctuation" > (< / span > fmt< span class = "token punctuation" > .< / span > < span class = "token function" > Errorf< / span > < span class = "token punctuation" > (< / span > < span class = "token string" > " unrecognized notification: %T" < / span > < span class = "token punctuation" > ,< / span > next< span class = "token punctuation" > )< / span > < span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > // the only way to get here is if the p.nextCh is empty and closed< / span >
< span class = "token keyword" > return< / span > < span class = "token boolean" > true< / span > < span class = "token punctuation" > ,< / span > < span class = "token boolean" > nil< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > )< / span >
< span class = "token comment" > // the only way to get here is if the p.nextCh is empty and closed< / span >
< span class = "token keyword" > if< / span > err < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span >
< span class = "token function" > close< / span > < span class = "token punctuation" > (< / span > stopCh< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span > < span class = "token punctuation" > ,< / span > < span class = "token number" > 1< / span > < span class = "token operator" > *< / span > time< span class = "token punctuation" > .< / span > Minute< span class = "token punctuation" > ,< / span > stopCh< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< h4 id = "addch到nextch的对象传递" > addCh到 nextCh的 对 象 传 递 < / h4 >
< p > listener中 pop方 法 的 逻 辑 相 对 比 较 绕 , 最 终 目 的 就 是 把 分 发 到 addCh的 数 据 从 nextCh或 者 pendingNotifications取 出 来 < / p >
< blockquote >
< p > notification变 量 记 录 下 一 次 要 被 放 到 p.nextCh供 pop方 法 取 出 的 对 象
开 始 seletct时 必 然 只 有 case2可 能 ready
Case2做 的 事 可 以 描 述 为 : 从 p.addCh获 取 对 象 , 如 果 临 时 变 量 notification还 是 nil, 说 明 需 要 往 notification赋 值 , 供 case1推 送 到 p.nextCh
如 果 notification已 经 有 值 了 , 那 个 当 前 从 p.addCh取 出 的 值 要 先 放 到 环 形 缓 冲 区 中 < / p >
< p > Case1做 的 事 可 以 描 述 为 : 看 看 能 不 能 把 临 时 变 量 notification推 送 到 nextCh( nil chan会 阻 塞 在 读 写 操 作 上 ) , 可 以 写 的 话 , 说 明 这 个 nextCh是 p.nextCh, 写 成 功 之 后 , 需 要 从 缓 存 中 取 出 一 个 对 象 放 到 notification为 下 次 执 行 这 个 case做 准 备 , 如 果 缓 存 是 空 的 , 通 过 把 nextCh chan设 置 为 nil来 禁 用 case1, 以 便 case2位 notification赋 值 < / p >
< / blockquote >
< pre class = "language-" > < code class = "lang-go" > < span class = "token keyword" > func< / span > < span class = "token punctuation" > (< / span > p < span class = "token operator" > *< / span > processorListener< span class = "token punctuation" > )< / span > < span class = "token function" > pop< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > defer< / span > utilruntime< span class = "token punctuation" > .< / span > < span class = "token function" > HandleCrash< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > defer< / span > < span class = "token function" > close< / span > < span class = "token punctuation" > (< / span > p< span class = "token punctuation" > .< / span > nextCh< span class = "token punctuation" > )< / span > < span class = "token comment" > // Tell .run() to stop< / span >
< span class = "token comment" > //nextCh没 有 利 用 make初 始 化 , 将 阻 塞 在 读 和 写 上 < / span >
< span class = "token keyword" > var< / span > nextCh < span class = "token keyword" > chan< / span > < span class = "token operator" > < -< / span > < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token comment" > //notification初 始 值 为 nil< / span >
< span class = "token keyword" > var< / span > notification < span class = "token keyword" > interface< / span > < span class = "token punctuation" > {< / span > < span class = "token punctuation" > }< / span >
< span class = "token keyword" > for< / span > < span class = "token punctuation" > {< / span >
< span class = "token keyword" > select< / span > < span class = "token punctuation" > {< / span >
< span class = "token comment" > // 执 行 这 个 case, 相 当 于 给 p.nextCh添 加 来 自 p.addCh的 内 容 < / span >
< span class = "token keyword" > case< / span > nextCh < span class = "token operator" > < -< / span > notification< span class = "token punctuation" > :< / span >
< span class = "token comment" > // Notification dispatched< / span >
< span class = "token keyword" > var< / span > ok < span class = "token builtin" > bool< / span >
< span class = "token comment" > //前 面 的 notification已 经 加 到 p.nextCh了 , 为 下 一 次 这 个 case再 次 ready做 准 备 < / span >
notification< span class = "token punctuation" > ,< / span > ok < span class = "token operator" > =< / span > p< span class = "token punctuation" > .< / span > pendingNotifications< span class = "token punctuation" > .< / span > < span class = "token function" > ReadOne< / span > < span class = "token punctuation" > (< / span > < span class = "token punctuation" > )< / span >
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > ok < span class = "token punctuation" > {< / span > < span class = "token comment" > // Nothing to pop< / span >
nextCh < span class = "token operator" > =< / span > < span class = "token boolean" > nil< / span > < span class = "token comment" > // Disable this select case< / span >
< span class = "token punctuation" > }< / span >
< span class = "token comment" > //第 一 次 select只 有 这 个 case ready< / span >
< span class = "token keyword" > case< / span > notificationToAdd< span class = "token punctuation" > ,< / span > ok < span class = "token operator" > :=< / span > < span class = "token operator" > < -< / span > p< span class = "token punctuation" > .< / span > addCh< span class = "token punctuation" > :< / span >
< span class = "token keyword" > if< / span > < span class = "token operator" > !< / span > ok < span class = "token punctuation" > {< / span >
< span class = "token keyword" > return< / span >
< span class = "token punctuation" > }< / span >
< span class = "token keyword" > if< / span > notification < span class = "token operator" > ==< / span > < span class = "token boolean" > nil< / span > < span class = "token punctuation" > {< / span > < span class = "token comment" > // No notification to pop (and pendingNotifications is empty)< / span >
< span class = "token comment" > // Optimize the case - skip adding to pendingNotifications< / span >
< span class = "token comment" > //为 notification赋 值 < / span >
notification < span class = "token operator" > =< / span > notificationToAdd
< span class = "token comment" > //唤 醒 第 一 个 case< / span >
nextCh < span class = "token operator" > =< / span > p< span class = "token punctuation" > .< / span > nextCh
< span class = "token punctuation" > }< / span > < span class = "token keyword" > else< / span > < span class = "token punctuation" > {< / span > < span class = "token comment" > // There is already a notification waiting to be dispatched< / span >
< span class = "token comment" > //select没 有 命 中 第 一 个 case, 那 么 notification就 没 有 被 消 耗 , 那 么 把 从 p.addCh获 取 的 对 象 加 到 缓 存 中 < / span >
p< span class = "token punctuation" > .< / span > pendingNotifications< span class = "token punctuation" > .< / span > < span class = "token function" > WriteOne< / span > < span class = "token punctuation" > (< / span > notificationToAdd< span class = "token punctuation" > )< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< span class = "token punctuation" > }< / span >
< / code > < / pre >
< footer class = "page-footer" > < span class = "copyright" > < a href = "https://mp.weixin.qq.com/s/vWlSdzz2MNdXRr0sd2-LFg" target = "_blank" > 加 入 云 原 生 社 区 < / a > < p > < / p > Copyright © 2017-2021 | Distributed under < a href = "https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target = "_blank" > CC BY 4.0< / a > | < a href = "https://jimmysong.io" target = "_blank" > jimmysong.io< / a > all right reserved, powered by Gitbook< / span > < span class = "footer-modification" > Updated at
2021-10-20 18:07:35 +08:00
2021-10-20 10:02:33
2021-07-01 10:02:38 +08:00
< / span > < / footer >
< / section >
< / div >
< div class = "search-results" >
< div class = "has-results" >
< h1 class = "search-results-title" > < span class = 'search-results-count' > < / span > results matching "< span class = 'search-query' > < / span > "< / h1 >
< ul class = "search-results-list" > < / ul >
< / div >
< div class = "no-results" >
< h1 class = "search-results-title" > No results matching "< span class = 'search-query' > < / span > "< / h1 >
< / div >
< / div >
< / div >
< / div >
< / div >
< / div >
< a href = "client-go-sample.html" class = "navigation navigation-prev " aria-label = "Previous page: client-go 示例" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "operator.html" class = "navigation navigation-next " aria-label = "Next page: Operator" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
< script >
var gitbook = gitbook || [];
gitbook.push(function() {
2021-10-20 18:07:35 +08:00
gitbook.page.hasChanged({"page":{"title":"client-go 中的 informer 源码分析","level":"9.5.1","depth":2,"next":{"title":"Operator","level":"9.6","depth":1,"path":"develop/operator.md","ref":"develop/operator.md","articles":[{"title":"operator-sdk","level":"9.6.1","depth":2,"path":"develop/operator-sdk.md","ref":"develop/operator-sdk.md","articles":[]}]},"previous":{"title":"client-go 示例","level":"9.5","depth":1,"path":"develop/client-go-sample.md","ref":"develop/client-go-sample.md","articles":[{"title":"client-go 中的 informer 源码分析","level":"9.5.1","depth":2,"path":"develop/client-go-informer-sourcecode-analyse.md","ref":"develop/client-go-informer-sourcecode-analyse.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["github","codesnippet","splitter","page-toc-button","image-captions","editlink","back-to-top-button","-lunr","-search","search-plus","github-buttons@2.1.0","favicon@^0.0.2","tbfed-pagefooter@^0.0.1","3-ba","theme-default","-highlight","prism","prism-themes","lightbox","ga","sitemap-general"],"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"< a href = https://mp.weixin.qq.com/s/vWlSdzz2MNdXRr0sd2-LFg > 加入云原生社区< / a > < / p > Copyright © 2017-2021 | Distributed under < a href = https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh > CC BY 4.0< / a > | < a href = https://jimmysong.io > jimmysong.io< / a > ","modify_label":" Updated at ","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prism-themes/themes/prism-ghcolors.css"]},"github":{"url":"https://github.com/rootsongjc/kubernetes-handbook"},"editlink":{"label":"编辑本页","multilingual":false,"base":"https://github.com/rootsongjc/kubernetes-handbook/blob/master/"},"splitter":{},"codesnippet":{},"sitemap-general":{"prefix":"https://jimmysong.io/kubernetes-handbook/"},"fontsettings":{"theme":"white","family":"sans","size":2},"favicon":{"shortcut":"favicon.ico","bookmark":"favicon.ico"},"lightbox":{"jquery":true,"sameUuid":false},"page-toc-button":{},"back-to-top-button":{},"prism-themes":{},"github-buttons":{"repo":"rootsongjc/kubernetes-handbook","types":["star"],"size":"small"},"3-ba":{"configuration":"auto","token":"11f7d254cfa4e0ca44b175c66d379ecc"},"ga":{"configuration":"auto","token":"UA-93485976-1"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"showLevel":true,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"search-plus":{},"image-captions":{"caption":"图 _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_: _CAPTION_","variable_name":"_pictures"}},"theme":"default","author":"Jimmy Song( 宋净超) ","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"_pictures":[{"backlink":"cloud-native/kubernetes-and-cloud-native-app-overview.html#fig2.4.1","level":"2.4","list_caption":"Figure: 云计算演进历程","alt":"云计算演进历程","nro":1,"url":"../images/cloud-computing-evolution-road.jpg","index":1,"caption_template":"图 _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_: _CAPTION_","label":"云计算演进历程","attributes":{},"skip":false,"key":"2.4.1"},{"backlink":"cloud-native/kubernetes-and-cloud-native-app-overview.html#fig2.4.2","level":"2.4","list_caption":"Figure: 来自Twitter @MarcWilczek","alt":"来自Twitter @MarcWilczek","nro":2,"url":"../images/cloud-native-comes-of-age.jpg","index":2,"caption_template":"图 _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_: _CAPTION_","label":"来自Twitter @MarcWilczek","attributes":{},"skip":false,"key":"2.4.2"},{"backlink":"cloud-native/kubernetes-and
2021-07-01 10:02:38 +08:00
});
< / script >
< / div >
< script src = "../gitbook/gitbook.js" > < / script >
< script src = "../gitbook/theme.js" > < / script >
< script src = "../gitbook/gitbook-plugin-github/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-splitter/splitter.js" > < / script >
< script src = "../gitbook/gitbook-plugin-page-toc-button/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-editlink/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-back-to-top-button/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js" > < / script >
< script src = "../gitbook/gitbook-plugin-search-plus/search.js" > < / script >
< script src = "../gitbook/gitbook-plugin-github-buttons/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-3-ba/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js" > < / script >
< script src = "../gitbook/gitbook-plugin-ga/plugin.js" > < / script >
< script src = "../gitbook/gitbook-plugin-sharing/buttons.js" > < / script >
< script src = "../gitbook/gitbook-plugin-fontsettings/fontsettings.js" > < / script >
< / body >
< / html >