-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathreplicaset_controller.html
1655 lines (987 loc) · 80.4 KB
/
replicaset_controller.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>replicaset controller 源码分析 · 田飞雨</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="gosoon">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-base16-ateliersulphurpool.light.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-ace/ace.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-local-video/video-js.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-terminal/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-versions-select/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
<link rel="stylesheet" href="../styles/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="kube_scheduler_process.html" />
<link rel="prev" href="deployment_controller.html" />
<link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
<script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
<script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
<script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
<script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>
<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":"gosoon/source-code-reading-notes","types":["star"],"size":"large"};
</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://blog.tianfeiyu.com" target="_blank" class="custom-link">Home</a>
</li>
<li class="divider"></li>
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
<b>1.1.</b>
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="./">
<a href="./">
<b>1.2.</b>
kubernetes
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="kube_apiserver.html">
<a href="kube_apiserver.html">
<b>1.2.1.</b>
kube-apiserver 的设计与实现
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="apiserver_bootstrap_controller.html">
<a href="apiserver_bootstrap_controller.html">
<b>1.2.2.</b>
kube-apiserver 中 apiserver service 的实现
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="nodelifecycle_controller.html">
<a href="nodelifecycle_controller.html">
<b>1.2.3.</b>
node controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="job_controller.html">
<a href="job_controller.html">
<b>1.2.4.</b>
job controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.5" data-path="garbagecollector_controller.html">
<a href="garbagecollector_controller.html">
<b>1.2.5.</b>
garbage collector controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.6" data-path="daemonset_controller.html">
<a href="daemonset_controller.html">
<b>1.2.6.</b>
daemonset controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.7" data-path="statefulset_controller.html">
<a href="statefulset_controller.html">
<b>1.2.7.</b>
statefulset controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.8" data-path="deployment_controller.html">
<a href="deployment_controller.html">
<b>1.2.8.</b>
deployment controller 源码分析
</a>
</li>
<li class="chapter active" data-level="1.2.9" data-path="replicaset_controller.html">
<a href="replicaset_controller.html">
<b>1.2.9.</b>
replicaset controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.10" data-path="kube_scheduler_process.html">
<a href="kube_scheduler_process.html">
<b>1.2.10.</b>
kube-scheduler 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.11" data-path="kube_scheduler_algorithm.html">
<a href="kube_scheduler_algorithm.html">
<b>1.2.11.</b>
kube-scheduler predicates 与 priorities 调度算法源码分析
</a>
</li>
<li class="chapter " data-level="1.2.12" data-path="kube_scheduler_preempt.html">
<a href="kube_scheduler_preempt.html">
<b>1.2.12.</b>
kube-scheduler 优先级与抢占机制源码分析
</a>
</li>
<li class="chapter " data-level="1.2.13" data-path="k8s_service_theory.html">
<a href="k8s_service_theory.html">
<b>1.2.13.</b>
kubernetes service 原理解析
</a>
</li>
<li class="chapter " data-level="1.2.14" data-path="kube_proxy_process.html">
<a href="kube_proxy_process.html">
<b>1.2.14.</b>
kube-proxy 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.15" data-path="kube_proxy_iptables.html">
<a href="kube_proxy_iptables.html">
<b>1.2.15.</b>
kube-proxy iptables 模式源码分析
</a>
</li>
<li class="chapter " data-level="1.2.16" data-path="kube_proxy_ipvs.html">
<a href="kube_proxy_ipvs.html">
<b>1.2.16.</b>
kube-proxy ipvs 模式源码分析
</a>
</li>
<li class="chapter " data-level="1.2.17" data-path="kubelet-modules.html">
<a href="kubelet-modules.html">
<b>1.2.17.</b>
kubelet 架构浅析
</a>
</li>
<li class="chapter " data-level="1.2.18" data-path="kubelet_init.html">
<a href="kubelet_init.html">
<b>1.2.18.</b>
kubelet 启动流程分析
</a>
</li>
<li class="chapter " data-level="1.2.19" data-path="kubelet_create_pod.html">
<a href="kubelet_create_pod.html">
<b>1.2.19.</b>
kubelet 创建 pod 的流程
</a>
</li>
<li class="chapter " data-level="1.2.20" data-path="node_status.html">
<a href="node_status.html">
<b>1.2.20.</b>
kubelet 状态上报的方式
</a>
</li>
<li class="chapter " data-level="1.2.21" data-path="k8s_events.html">
<a href="k8s_events.html">
<b>1.2.21.</b>
kubelet 中事件处理机制
</a>
</li>
<li class="chapter " data-level="1.2.22" data-path="kubelet_status_manager.html">
<a href="kubelet_status_manager.html">
<b>1.2.22.</b>
kubelet statusManager 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.23" data-path="kubelet_qos.html">
<a href="kubelet_qos.html">
<b>1.2.23.</b>
kubernetes 中 Qos 的设计与实现
</a>
</li>
<li class="chapter " data-level="1.2.24" data-path="kubelet_garbage_collect.html">
<a href="kubelet_garbage_collect.html">
<b>1.2.24.</b>
kubelet 中垃圾回收机制的设计与实现
</a>
</li>
<li class="chapter " data-level="1.2.25" data-path="k8s_evicted.html">
<a href="k8s_evicted.html">
<b>1.2.25.</b>
kubernetes 中 Evicted pod 是如何产生的
</a>
</li>
<li class="chapter " data-level="1.2.26" data-path="kubernetes_descheduler.html">
<a href="kubernetes_descheduler.html">
<b>1.2.26.</b>
Kubernetes 中 Descheduler 组件的使用与扩展
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../knative/">
<a href="../knative/">
<b>1.3.</b>
knative
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../knative/deploy_with_minkube.html">
<a href="../knative/deploy_with_minkube.html">
<b>1.3.1.</b>
在 minkube 上部署 knative
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../knative/knative_serving.html">
<a href="../knative/knative_serving.html">
<b>1.3.2.</b>
knative serving 组件分析
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../cgroup/">
<a href="../cgroup/">
<b>1.4.</b>
cgroup
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../cgroup/blkio_cgroup.html">
<a href="../cgroup/blkio_cgroup.html">
<b>1.4.1.</b>
blkio cgroup
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../cgroup/pid_cgroup.html">
<a href="../cgroup/pid_cgroup.html">
<b>1.4.2.</b>
pid cgroup
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../go/">
<a href="../go/">
<b>1.5.</b>
go
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../go/func_return_value_or_pointer.html">
<a href="../go/func_return_value_or_pointer.html">
<b>1.5.1.</b>
golang 中函数使用值返回与指针返回的区别,底层原理分析
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../go/golang_bootstrap.html">
<a href="../go/golang_bootstrap.html">
<b>1.5.2.</b>
Golang 程序启动流程分析
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../go/golang_gpm.html">
<a href="../go/golang_gpm.html">
<b>1.5.3.</b>
Golang GPM 模型剖析
</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=".." >replicaset controller 源码分析</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<ul>
<li><a href="#replicasetcontroller-源码分析">ReplicaSetController 源码分析</a><ul>
<li><a href="#启动流程">启动流程</a></li>
<li><a href="#eventhandler">EventHandler</a><ul>
<li><a href="#addpod">addPod</a></li>
<li><a href="#updatepod">updatePod</a></li>
<li><a href="#deletepod">deletePod</a></li>
<li><a href="#addrs-和-deleters">AddRS 和 DeleteRS</a></li>
</ul>
</li>
<li><a href="#syncreplicaset">syncReplicaSet</a><ul>
<li><a href="#satisfiedexpectations">SatisfiedExpectations</a></li>
<li><a href="#managereplicas">manageReplicas</a></li>
<li><a href="#calculatestatus">calculateStatus</a></li>
</ul>
</li>
<li><a href="#expectations-机制">expectations 机制</a></li>
</ul>
</li>
<li><a href="#总结">总结</a></li>
</ul>
<p>在前面的文章中已经介绍了 deployment controller 的设计与实现,deployment 控制的是 replicaset,而 replicaset 控制 pod 的创建与删除,deployment 通过控制 replicaset 实现了滚动更新、回滚等操作。而 replicaset 会直接控制 pod 的创建与删除,本文会继续从源码层面分析 replicaset 的设计与实现。</p>
<p>在分析源码前先考虑一下 replicaset 的使用场景,在平时的操作中其实我们并不会直接操作 replicaset,replicaset 也仅有几个简单的操作,创建、删除、更新等,但其地位是非常重要的,replicaset 的主要功能就是通过 add/del pod 来达到期望的状态。</p>
<h3 id="replicasetcontroller-源码分析"><a name="replicasetcontroller-源码分析" class="anchor-navigation-ex-anchor" href="#replicasetcontroller-源码分析"><i class="fa fa-link" aria-hidden="true"></i></a>ReplicaSetController 源码分析</h3>
<blockquote>
<p>kubernetes 版本: v1.16</p>
</blockquote>
<h4 id="启动流程"><a name="启动流程" class="anchor-navigation-ex-anchor" href="#启动流程"><i class="fa fa-link" aria-hidden="true"></i></a>启动流程</h4>
<p>首先来看 replicaSetController 对象初始化以及启动的代码,在 startReplicaSetController 中有两个比较重要的变量:</p>
<ul>
<li>BurstReplicas:用来控制在一个 syncLoop 过程中 rs 最多能创建的 pod 数量,设置上限值是为了避免单个 rs 影响整个系统,默认值为 500;</li>
<li>ConcurrentRSSyncs:指的是需要启动多少个 goroutine 处理 informer 队列中的对象,默认值为 5;</li>
</ul>
<p><code>k8s.io/kubernetes/cmd/kube-controller-manager/app/apps.go:69</code></p>
<pre class="language-"><code>func startReplicaSetController(ctx ControllerContext) (http.Handler, bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "replicasets"}] {
return nil, false, nil
}
go replicaset.NewReplicaSetController(
ctx.InformerFactory.Apps().V1().ReplicaSets(),
ctx.InformerFactory.Core().V1().Pods(),
ctx.ClientBuilder.ClientOrDie("replicaset-controller"),
replicaset.BurstReplicas,
).Run(int(ctx.ComponentConfig.ReplicaSetController.ConcurrentRSSyncs), ctx.Stop)
return nil, true, nil
}
</code></pre><p>下面是 replicaSetController 初始化的具体步骤,可以看到其会监听 pod 以及 rs 两个对象的事件。</p>
<p><code>k8s.io/kubernetes/pkg/controller/replicaset/replica_set.go:109</code></p>
<pre class="language-"><code>func NewReplicaSetController(......) *ReplicaSetController {
......
// 1、此处调用 NewBaseController
return NewBaseController(rsInformer, podInformer, kubeClient, burstReplicas,
apps.SchemeGroupVersion.WithKind("ReplicaSet"),
"replicaset_controller",
"replicaset",
controller.RealPodControl{
KubeClient: kubeClient,
Recorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "replicaset-controller"}),
},
)
}
func NewBaseController(......) *ReplicaSetController {
......
// 2、ReplicaSetController 初始化
rsc := &ReplicaSetController{
GroupVersionKind: gvk,
kubeClient: kubeClient,
podControl: podControl,
burstReplicas: burstReplicas,
// 3、expectations 的初始化
expectations: controller.NewUIDTrackingControllerExpectations(controller.NewControllerExpectations()),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), queueName),
}
// 4、rsInformer 中注册的 EventHandler
rsInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: rsc.enqueueReplicaSet,
UpdateFunc: rsc.updateRS,
DeleteFunc: rsc.enqueueReplicaSet,
})
......
// 5、podInformer 中注册的 EventHandler
podInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: rsc.addPod,
UpdateFunc: rsc.updatePod,
DeleteFunc: rsc.deletePod,
})
......
return rsc
}
</code></pre><p>replicaSetController 初始化完成后会调用 <code>Run</code> 方法启动 5 个 goroutine 处理 informer 队列中的事件并进行 sync 操作,kube-controller-manager 中每个 controller 的启动操作都是如下所示流程。</p>
<p><code>k8s.io/kubernetes/pkg/controller/replicaset/replica_set.go:177</code></p>
<pre class="language-"><code>func (rsc *ReplicaSetController) Run(workers int, stopCh <-chan struct{}) {
......
// 1、等待 informer 同步缓存
if !cache.WaitForNamedCacheSync(rsc.Kind, stopCh, rsc.podListerSynced, rsc.rsListerSynced) {
return
}
// 2、启动 5 个 goroutine 执行 worker 方法
for i := 0; i < workers; i++ {
go wait.Until(rsc.worker, time.Second, stopCh)
}
<-stopCh
}
// 3、worker 方法中调用 rocessNextWorkItem
func (rsc *ReplicaSetController) worker() {
for rsc.processNextWorkItem() {
}
}
func (rsc *ReplicaSetController) processNextWorkItem() bool {
// 4、从队列中取出对象
key, quit := rsc.queue.Get()
if quit {
return false
}
defer rsc.queue.Done(key)
// 5、执行 sync 操作
err := rsc.syncHandler(key.(string))
......
return true
}
</code></pre><h4 id="eventhandler"><a name="eventhandler" class="anchor-navigation-ex-anchor" href="#eventhandler"><i class="fa fa-link" aria-hidden="true"></i></a>EventHandler</h4>
<p>初始化 replicaSetController 时,其中有一个 <code>expectations</code> 字段,这是 rs 中一个比较特殊的机制,为了说清楚 expectations,先来看一下 controller 中所注册的 eventHandler,replicaSetController 会 watch pod 和 replicaSet 两个对象,eventHandler 中注册了对这两种对象的 add、update、delete 三个操作。</p>
<h5 id="addpod"><a name="addpod" class="anchor-navigation-ex-anchor" href="#addpod"><i class="fa fa-link" aria-hidden="true"></i></a>addPod</h5>
<ul>
<li>1、判断 pod 是否处于删除状态;</li>
<li>2、获取该 pod 关联的 rs 以及 rsKey,入队 rs 并更新 rsKey 的 expectations;</li>
<li>3、若 pod 对象没体现出关联的 rs 则为孤儿 pod,遍历 rsList 查找匹配的 rs,若该 rs.Namespace == pod.Namespace 并且 rs.Spec.Selector 匹配 pod.Labels,则说明该 pod 应该与此 rs 关联,将匹配的 rs 入队;</li>
</ul>
<p><code>k8s.io/kubernetes/pkg/controller/replicaset/replica_set.go:255</code></p>
<pre class="language-"><code>func (rsc *ReplicaSetController) addPod(obj interface{}) {
pod := obj.(*v1.Pod)
if pod.DeletionTimestamp != nil {
rsc.deletePod(pod)
return
}
// 1、获取 pod 所关联的 rs
if controllerRef := metav1.GetControllerOf(pod); controllerRef != nil {
rs := rsc.resolveControllerRef(pod.Namespace, controllerRef)
if rs == nil {
return
}