forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_workers.go
1723 lines (1565 loc) · 74.8 KB
/
pod_workers.go
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
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kubelet
import (
"context"
"fmt"
"strings"
"sync"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
"k8s.io/klog/v2"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/events"
"k8s.io/kubernetes/pkg/kubelet/eviction"
"k8s.io/kubernetes/pkg/kubelet/metrics"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/kubelet/util/queue"
"k8s.io/utils/clock"
)
// OnCompleteFunc is a function that is invoked when an operation completes.
// If err is non-nil, the operation did not complete successfully.
type OnCompleteFunc func(err error)
// PodStatusFunc is a function that is invoked to override the pod status when a pod is killed.
type PodStatusFunc func(podStatus *v1.PodStatus)
// KillPodOptions are options when performing a pod update whose update type is kill.
type KillPodOptions struct {
// CompletedCh is closed when the kill request completes (syncTerminatingPod has completed
// without error) or if the pod does not exist, or if the pod has already terminated. This
// could take an arbitrary amount of time to be closed, but is never left open once
// CouldHaveRunningContainers() returns false.
CompletedCh chan<- struct{}
// Evict is true if this is a pod triggered eviction - once a pod is evicted some resources are
// more aggressively reaped than during normal pod operation (stopped containers).
Evict bool
// PodStatusFunc is invoked (if set) and overrides the status of the pod at the time the pod is killed.
// The provided status is populated from the latest state.
PodStatusFunc PodStatusFunc
// PodTerminationGracePeriodSecondsOverride is optional override to use if a pod is being killed as part of kill operation.
PodTerminationGracePeriodSecondsOverride *int64
}
// UpdatePodOptions is an options struct to pass to a UpdatePod operation.
type UpdatePodOptions struct {
// The type of update (create, update, sync, kill).
UpdateType kubetypes.SyncPodType
// StartTime is an optional timestamp for when this update was created. If set,
// when this update is fully realized by the pod worker it will be recorded in
// the PodWorkerDuration metric.
StartTime time.Time
// Pod to update. Required.
Pod *v1.Pod
// MirrorPod is the mirror pod if Pod is a static pod. Optional when UpdateType
// is kill or terminated.
MirrorPod *v1.Pod
// RunningPod is a runtime pod that is no longer present in config. Required
// if Pod is nil, ignored if Pod is set.
RunningPod *kubecontainer.Pod
// KillPodOptions is used to override the default termination behavior of the
// pod or to update the pod status after an operation is completed. Since a
// pod can be killed for multiple reasons, PodStatusFunc is invoked in order
// and later kills have an opportunity to override the status (i.e. a preemption
// may be later turned into an eviction).
KillPodOptions *KillPodOptions
}
// PodWorkType classifies the status of pod as seen by the pod worker - setup (sync),
// teardown of containers (terminating), or cleanup (terminated).
type PodWorkerState int
const (
// SyncPod is when the pod is expected to be started and running.
SyncPod PodWorkerState = iota
// TerminatingPod is when the pod is no longer being set up, but some
// containers may be running and are being torn down.
TerminatingPod
// TerminatedPod indicates the pod is stopped, can have no more running
// containers, and any foreground cleanup can be executed.
TerminatedPod
)
func (state PodWorkerState) String() string {
switch state {
case SyncPod:
return "sync"
case TerminatingPod:
return "terminating"
case TerminatedPod:
return "terminated"
default:
panic(fmt.Sprintf("the state %d is not defined", state))
}
}
// PodWorkerSync is the summarization of a single pod worker for sync. Values
// besides state are used to provide metric counts for operators.
type PodWorkerSync struct {
// State of the pod.
State PodWorkerState
// Orphan is true if the pod is no longer in the desired set passed to SyncKnownPods.
Orphan bool
// HasConfig is true if we have a historical pod spec for this pod.
HasConfig bool
// Static is true if we have config and the pod came from a static source.
Static bool
}
// podWork is the internal changes
type podWork struct {
// WorkType is the type of sync to perform - sync (create), terminating (stop
// containers), terminated (clean up and write status).
WorkType PodWorkerState
// Options contains the data to sync.
Options UpdatePodOptions
}
// PodWorkers is an abstract interface for testability.
type PodWorkers interface {
// UpdatePod notifies the pod worker of a change to a pod, which will then
// be processed in FIFO order by a goroutine per pod UID. The state of the
// pod will be passed to the syncPod method until either the pod is marked
// as deleted, it reaches a terminal phase (Succeeded/Failed), or the pod
// is evicted by the kubelet. Once that occurs the syncTerminatingPod method
// will be called until it exits successfully, and after that all further
// UpdatePod() calls will be ignored for that pod until it has been forgotten
// due to significant time passing. A pod that is terminated will never be
// restarted.
UpdatePod(options UpdatePodOptions)
// SyncKnownPods removes workers for pods that are not in the desiredPods set
// and have been terminated for a significant period of time. Once this method
// has been called once, the workers are assumed to be fully initialized and
// subsequent calls to ShouldPodContentBeRemoved on unknown pods will return
// true. It returns a map describing the state of each known pod worker. It
// is the responsibility of the caller to re-add any desired pods that are not
// returned as knownPods.
SyncKnownPods(desiredPods []*v1.Pod) (knownPods map[types.UID]PodWorkerSync)
// IsPodKnownTerminated returns true once SyncTerminatingPod completes
// successfully - the provided pod UID it is known by the pod
// worker to be terminated. If the pod has been force deleted and the pod worker
// has completed termination this method will return false, so this method should
// only be used to filter out pods from the desired set such as in admission.
//
// Intended for use by the kubelet config loops, but not subsystems, which should
// use ShouldPod*().
IsPodKnownTerminated(uid types.UID) bool
// CouldHaveRunningContainers returns true before the pod workers have synced,
// once the pod workers see the pod (syncPod could be called), and returns false
// after the pod has been terminated (running containers guaranteed stopped).
//
// Intended for use by the kubelet config loops, but not subsystems, which should
// use ShouldPod*().
CouldHaveRunningContainers(uid types.UID) bool
// ShouldPodBeFinished returns true once SyncTerminatedPod completes
// successfully - the provided pod UID it is known to the pod worker to
// be terminated and have resources reclaimed. It returns false before the
// pod workers have synced (syncPod could be called). Once the pod workers
// have synced it returns false if the pod has a sync status until
// SyncTerminatedPod completes successfully. If the pod workers have synced,
// but the pod does not have a status it returns true.
//
// Intended for use by subsystem sync loops to avoid performing background setup
// after termination has been requested for a pod. Callers must ensure that the
// syncPod method is non-blocking when their data is absent.
ShouldPodBeFinished(uid types.UID) bool
// IsPodTerminationRequested returns true when pod termination has been requested
// until the termination completes and the pod is removed from config. This should
// not be used in cleanup loops because it will return false if the pod has already
// been cleaned up - use ShouldPodContainersBeTerminating instead. Also, this method
// may return true while containers are still being initialized by the pod worker.
//
// Intended for use by the kubelet sync* methods, but not subsystems, which should
// use ShouldPod*().
IsPodTerminationRequested(uid types.UID) bool
// ShouldPodContainersBeTerminating returns false before pod workers have synced,
// or once a pod has started terminating. This check is similar to
// ShouldPodRuntimeBeRemoved but is also true after pod termination is requested.
//
// Intended for use by subsystem sync loops to avoid performing background setup
// after termination has been requested for a pod. Callers must ensure that the
// syncPod method is non-blocking when their data is absent.
ShouldPodContainersBeTerminating(uid types.UID) bool
// ShouldPodRuntimeBeRemoved returns true if runtime managers within the Kubelet
// should aggressively cleanup pod resources that are not containers or on disk
// content, like attached volumes. This is true when a pod is not yet observed
// by a worker after the first sync (meaning it can't be running yet) or after
// all running containers are stopped.
// TODO: Once pod logs are separated from running containers, this method should
// be used to gate whether containers are kept.
//
// Intended for use by subsystem sync loops to know when to start tearing down
// resources that are used by running containers. Callers should ensure that
// runtime content they own is not required for post-termination - for instance
// containers are required in docker to preserve pod logs until after the pod
// is deleted.
ShouldPodRuntimeBeRemoved(uid types.UID) bool
// ShouldPodContentBeRemoved returns true if resource managers within the Kubelet
// should aggressively cleanup all content related to the pod. This is true
// during pod eviction (when we wish to remove that content to free resources)
// as well as after the request to delete a pod has resulted in containers being
// stopped (which is a more graceful action). Note that a deleting pod can still
// be evicted.
//
// Intended for use by subsystem sync loops to know when to start tearing down
// resources that are used by non-deleted pods. Content is generally preserved
// until deletion+removal_from_etcd or eviction, although garbage collection
// can free content when this method returns false.
ShouldPodContentBeRemoved(uid types.UID) bool
// IsPodForMirrorPodTerminatingByFullName returns true if a static pod with the
// provided pod name is currently terminating and has yet to complete. It is
// intended to be used only during orphan mirror pod cleanup to prevent us from
// deleting a terminating static pod from the apiserver before the pod is shut
// down.
IsPodForMirrorPodTerminatingByFullName(podFullname string) bool
}
// podSyncer describes the core lifecyle operations of the pod state machine. A pod is first
// synced until it naturally reaches termination (true is returned) or an external agent decides
// the pod should be terminated. Once a pod should be terminating, SyncTerminatingPod is invoked
// until it returns no error. Then the SyncTerminatedPod method is invoked until it exits without
// error, and the pod is considered terminal. Implementations of this interface must be threadsafe
// for simultaneous invocation of these methods for multiple pods.
type podSyncer interface {
// SyncPod configures the pod and starts and restarts all containers. If it returns true, the
// pod has reached a terminal state and the presence of the error indicates succeeded or failed.
// If an error is returned, the sync was not successful and should be rerun in the future. This
// is a long running method and should exit early with context.Canceled if the context is canceled.
SyncPod(ctx context.Context, updateType kubetypes.SyncPodType, pod *v1.Pod, mirrorPod *v1.Pod, podStatus *kubecontainer.PodStatus) (bool, error)
// SyncTerminatingPod attempts to ensure the pod's containers are no longer running and to collect
// any final status. This method is repeatedly invoked with diminishing grace periods until it exits
// without error. Once this method exits with no error other components are allowed to tear down
// supporting resources like volumes and devices. If the context is canceled, the method should
// return context.Canceled unless it has successfully finished, which may occur when a shorter
// grace period is detected.
SyncTerminatingPod(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus, gracePeriod *int64, podStatusFn func(*v1.PodStatus)) error
// SyncTerminatingRuntimePod is invoked when running containers are found that correspond to
// a pod that is no longer known to the kubelet to terminate those containers. It should not
// exit without error unless all containers are known to be stopped.
SyncTerminatingRuntimePod(ctx context.Context, runningPod *kubecontainer.Pod) error
// SyncTerminatedPod is invoked after all running containers are stopped and is responsible
// for releasing resources that should be executed right away rather than in the background.
// Once it exits without error the pod is considered finished on the node.
SyncTerminatedPod(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus) error
}
type syncPodFnType func(ctx context.Context, updateType kubetypes.SyncPodType, pod *v1.Pod, mirrorPod *v1.Pod, podStatus *kubecontainer.PodStatus) (bool, error)
type syncTerminatingPodFnType func(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus, gracePeriod *int64, podStatusFn func(*v1.PodStatus)) error
type syncTerminatingRuntimePodFnType func(ctx context.Context, runningPod *kubecontainer.Pod) error
type syncTerminatedPodFnType func(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus) error
// podSyncerFuncs implements podSyncer and accepts functions for each method.
type podSyncerFuncs struct {
syncPod syncPodFnType
syncTerminatingPod syncTerminatingPodFnType
syncTerminatingRuntimePod syncTerminatingRuntimePodFnType
syncTerminatedPod syncTerminatedPodFnType
}
func newPodSyncerFuncs(s podSyncer) podSyncerFuncs {
return podSyncerFuncs{
syncPod: s.SyncPod,
syncTerminatingPod: s.SyncTerminatingPod,
syncTerminatingRuntimePod: s.SyncTerminatingRuntimePod,
syncTerminatedPod: s.SyncTerminatedPod,
}
}
var _ podSyncer = podSyncerFuncs{}
func (f podSyncerFuncs) SyncPod(ctx context.Context, updateType kubetypes.SyncPodType, pod *v1.Pod, mirrorPod *v1.Pod, podStatus *kubecontainer.PodStatus) (bool, error) {
return f.syncPod(ctx, updateType, pod, mirrorPod, podStatus)
}
func (f podSyncerFuncs) SyncTerminatingPod(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus, gracePeriod *int64, podStatusFn func(*v1.PodStatus)) error {
return f.syncTerminatingPod(ctx, pod, podStatus, gracePeriod, podStatusFn)
}
func (f podSyncerFuncs) SyncTerminatingRuntimePod(ctx context.Context, runningPod *kubecontainer.Pod) error {
return f.syncTerminatingRuntimePod(ctx, runningPod)
}
func (f podSyncerFuncs) SyncTerminatedPod(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus) error {
return f.syncTerminatedPod(ctx, pod, podStatus)
}
const (
// jitter factor for resyncInterval
workerResyncIntervalJitterFactor = 0.5
// jitter factor for backOffPeriod and backOffOnTransientErrorPeriod
workerBackOffPeriodJitterFactor = 0.5
// backoff period when transient error occurred.
backOffOnTransientErrorPeriod = time.Second
)
// podSyncStatus tracks per-pod transitions through the three phases of pod
// worker sync (setup, terminating, terminated).
type podSyncStatus struct {
// ctx is the context that is associated with the current pod sync.
// TODO: remove this from the struct by having the context initialized
// in startPodSync, the cancelFn used by UpdatePod, and cancellation of
// a parent context for tearing down workers (if needed) on shutdown
ctx context.Context
// cancelFn if set is expected to cancel the current podSyncer operation.
cancelFn context.CancelFunc
// fullname of the pod
fullname string
// working is true if an update is pending or being worked by a pod worker
// goroutine.
working bool
// pendingUpdate is the updated state the pod worker should observe. It is
// cleared and moved to activeUpdate when a pod worker reads it. A new update
// may always replace a pending update as the pod worker does not guarantee
// that all intermediate states are synced to a worker, only the most recent.
// This state will not be visible to downstream components until a pod worker
// has begun processing it.
pendingUpdate *UpdatePodOptions
// activeUpdate is the most recent version of the pod's state that will be
// passed to a sync*Pod function. A pod becomes visible to downstream components
// once a worker decides to start a pod (startedAt is set). The pod and mirror
// pod fields are accumulated if they are missing on a particular call (the last
// known version), and the value of KillPodOptions is accumulated as pods cannot
// have their grace period shortened. This is the source of truth for the pod spec
// the kubelet is reconciling towards for all components that act on running pods.
activeUpdate *UpdatePodOptions
// syncedAt is the time at which the pod worker first observed this pod.
syncedAt time.Time
// startedAt is the time at which the pod worker allowed the pod to start.
startedAt time.Time
// terminatingAt is set once the pod is requested to be killed - note that
// this can be set before the pod worker starts terminating the pod, see
// terminating.
terminatingAt time.Time
// terminatedAt is set once the pod worker has completed a successful
// syncTerminatingPod call and means all running containers are stopped.
terminatedAt time.Time
// gracePeriod is the requested gracePeriod once terminatingAt is nonzero.
gracePeriod int64
// notifyPostTerminating will be closed once the pod transitions to
// terminated. After the pod is in terminated state, nothing should be
// added to this list.
notifyPostTerminating []chan<- struct{}
// statusPostTerminating is a list of the status changes associated
// with kill pod requests. After the pod is in terminated state, nothing
// should be added to this list. The worker will execute the last function
// in this list on each termination attempt.
statusPostTerminating []PodStatusFunc
// startedTerminating is true once the pod worker has observed the request to
// stop a pod (exited syncPod and observed a podWork with WorkType
// TerminatingPod). Once this is set, it is safe for other components
// of the kubelet to assume that no other containers may be started.
startedTerminating bool
// deleted is true if the pod has been marked for deletion on the apiserver
// or has no configuration represented (was deleted before).
deleted bool
// evicted is true if the kill indicated this was an eviction (an evicted
// pod can be more aggressively cleaned up).
evicted bool
// finished is true once the pod worker completes for a pod
// (syncTerminatedPod exited with no errors) until SyncKnownPods is invoked
// to remove the pod. A terminal pod (Succeeded/Failed) will have
// termination status until the pod is deleted.
finished bool
// restartRequested is true if the pod worker was informed the pod is
// expected to exist (update type of create, update, or sync) after
// it has been killed. When known pods are synced, any pod that is
// terminated and has restartRequested will have its history cleared.
restartRequested bool
// observedRuntime is true if the pod has been observed to be present in the
// runtime. A pod that has been observed at runtime must go through either
// SyncTerminatingRuntimePod or SyncTerminatingPod. Otherwise, we can avoid
// invoking the terminating methods if the pod is deleted or orphaned before
// it has been started.
observedRuntime bool
}
func (s *podSyncStatus) IsWorking() bool { return s.working }
func (s *podSyncStatus) IsTerminationRequested() bool { return !s.terminatingAt.IsZero() }
func (s *podSyncStatus) IsTerminationStarted() bool { return s.startedTerminating }
func (s *podSyncStatus) IsTerminated() bool { return !s.terminatedAt.IsZero() }
func (s *podSyncStatus) IsFinished() bool { return s.finished }
func (s *podSyncStatus) IsEvicted() bool { return s.evicted }
func (s *podSyncStatus) IsDeleted() bool { return s.deleted }
func (s *podSyncStatus) IsStarted() bool { return !s.startedAt.IsZero() }
// WorkType returns this pods' current state of the pod in pod lifecycle state machine.
func (s *podSyncStatus) WorkType() PodWorkerState {
if s.IsTerminated() {
return TerminatedPod
}
if s.IsTerminationRequested() {
return TerminatingPod
}
return SyncPod
}
// mergeLastUpdate records the most recent state from a new update. Pod and MirrorPod are
// incremented. KillPodOptions is accumulated. If RunningPod is set, Pod is synthetic and
// will *not* be used as the last pod state unless no previous pod state exists (because
// the pod worker may be responsible for terminating a pod from a previous run of the
// kubelet where no config state is visible). The contents of activeUpdate are used as the
// source of truth for components downstream of the pod workers.
func (s *podSyncStatus) mergeLastUpdate(other UpdatePodOptions) {
opts := s.activeUpdate
if opts == nil {
opts = &UpdatePodOptions{}
s.activeUpdate = opts
}
// UpdatePodOptions states (and UpdatePod enforces) that either Pod or RunningPod
// is set, and we wish to preserve the most recent Pod we have observed, so only
// overwrite our Pod when we have no Pod or when RunningPod is nil.
if opts.Pod == nil || other.RunningPod == nil {
opts.Pod = other.Pod
}
// running pods will not persist but will be remembered for replay
opts.RunningPod = other.RunningPod
// if mirrorPod was not provided, remember the last one for replay
if other.MirrorPod != nil {
opts.MirrorPod = other.MirrorPod
}
// accumulate kill pod options
if other.KillPodOptions != nil {
opts.KillPodOptions = &KillPodOptions{}
if other.KillPodOptions.Evict {
opts.KillPodOptions.Evict = true
}
if override := other.KillPodOptions.PodTerminationGracePeriodSecondsOverride; override != nil {
value := *override
opts.KillPodOptions.PodTerminationGracePeriodSecondsOverride = &value
}
}
// StartTime is not copied - that is purely for tracking latency of config propagation
// from kubelet to pod worker.
}
// podWorkers keeps track of operations on pods and ensures each pod is
// reconciled with the container runtime and other subsystems. The worker
// also tracks which pods are in flight for starting, which pods are
// shutting down but still have running containers, and which pods have
// terminated recently and are guaranteed to have no running containers.
//
// podWorkers is the source of truth for what pods should be active on a
// node at any time, and is kept up to date with the desired state of the
// node (tracked by the kubelet pod config loops and the state in the
// kubelet's podManager) via the UpdatePod method. Components that act
// upon running pods should look to the pod worker for state instead of the
// kubelet podManager. The pod worker is periodically reconciled with the
// state of the podManager via SyncKnownPods() and is responsible for
// ensuring the completion of all observed pods no longer present in
// the podManager (no longer part of the node's desired config).
//
// A pod passed to a pod worker is either being synced (expected to be
// running), terminating (has running containers but no new containers are
// expected to start), terminated (has no running containers but may still
// have resources being consumed), or cleaned up (no resources remaining).
// Once a pod is set to be "torn down" it cannot be started again for that
// UID (corresponding to a delete or eviction) until:
//
// 1. The pod worker is finalized (syncTerminatingPod and
// syncTerminatedPod exit without error sequentially)
// 2. The SyncKnownPods method is invoked by kubelet housekeeping and the pod
// is not part of the known config.
//
// Pod workers provide a consistent source of information to other kubelet
// loops about the status of the pod and whether containers can be
// running. The ShouldPodContentBeRemoved() method tracks whether a pod's
// contents should still exist, which includes non-existent pods after
// SyncKnownPods() has been called once (as per the contract, all existing
// pods should be provided via UpdatePod before SyncKnownPods is invoked).
// Generally other sync loops are expected to separate "setup" and
// "teardown" responsibilities and the information methods here assist in
// each by centralizing that state. A simple visualization of the time
// intervals involved might look like:
//
// ---| = kubelet config has synced at least once
// -------| |- = pod exists in apiserver config
// --------| |---------------- = CouldHaveRunningContainers() is true
//
// ^- pod is observed by pod worker .
// . .
//
// ----------| |------------------------- = syncPod is running
//
// . ^- pod worker loop sees change and invokes syncPod
// . . .
//
// --------------| |------- = ShouldPodContainersBeTerminating() returns true
// --------------| |------- = IsPodTerminationRequested() returns true (pod is known)
//
// . . ^- Kubelet evicts pod .
// . . .
//
// -------------------| |---------------- = syncTerminatingPod runs then exits without error
//
// . . ^ pod worker loop exits syncPod, sees pod is terminating,
// . . invokes syncTerminatingPod
// . . .
//
// ---| |------------------| . = ShouldPodRuntimeBeRemoved() returns true (post-sync)
//
// . ^ syncTerminatingPod has exited successfully
// . .
//
// ----------------------------| |------- = syncTerminatedPod runs then exits without error
//
// . ^ other loops can tear down
// . .
//
// ------------------------------------| |---- = status manager is waiting for SyncTerminatedPod() finished
//
// . ^ .
//
// ----------| |- = status manager can be writing pod status
//
// ^ status manager deletes pod because no longer exists in config
//
// Other components in the Kubelet can request a termination of the pod
// via the UpdatePod method or the killPodNow wrapper - this will ensure
// the components of the pod are stopped until the kubelet is restarted
// or permanently (if the phase of the pod is set to a terminal phase
// in the pod status change).
type podWorkers struct {
// Protects all per worker fields.
podLock sync.Mutex
// podsSynced is true once the pod worker has been synced at least once,
// which means that all working pods have been started via UpdatePod().
podsSynced bool
// Tracks all running per-pod goroutines - per-pod goroutine will be
// processing updates received through its corresponding channel. Sending
// a message on this channel will signal the corresponding goroutine to
// consume podSyncStatuses[uid].pendingUpdate if set.
podUpdates map[types.UID]chan struct{}
// Tracks by UID the termination status of a pod - syncing, terminating,
// terminated, and evicted.
podSyncStatuses map[types.UID]*podSyncStatus
// Tracks all uids for started static pods by full name
startedStaticPodsByFullname map[string]types.UID
// Tracks all uids for static pods that are waiting to start by full name
waitingToStartStaticPodsByFullname map[string][]types.UID
workQueue queue.WorkQueue
// This function is run to sync the desired state of pod.
// NOTE: This function has to be thread-safe - it can be called for
// different pods at the same time.
podSyncer podSyncer
// workerChannelFn is exposed for testing to allow unit tests to impose delays
// in channel communication. The function is invoked once each time a new worker
// goroutine starts.
workerChannelFn func(uid types.UID, in chan struct{}) (out <-chan struct{})
// The EventRecorder to use
recorder record.EventRecorder
// backOffPeriod is the duration to back off when there is a sync error.
backOffPeriod time.Duration
// resyncInterval is the duration to wait until the next sync.
resyncInterval time.Duration
// podCache stores kubecontainer.PodStatus for all pods.
podCache kubecontainer.Cache
// clock is used for testing timing
clock clock.PassiveClock
}
func newPodWorkers(
podSyncer podSyncer,
recorder record.EventRecorder,
workQueue queue.WorkQueue,
resyncInterval, backOffPeriod time.Duration,
podCache kubecontainer.Cache,
) PodWorkers {
return &podWorkers{
podSyncStatuses: map[types.UID]*podSyncStatus{},
podUpdates: map[types.UID]chan struct{}{},
startedStaticPodsByFullname: map[string]types.UID{},
waitingToStartStaticPodsByFullname: map[string][]types.UID{},
podSyncer: podSyncer,
recorder: recorder,
workQueue: workQueue,
resyncInterval: resyncInterval,
backOffPeriod: backOffPeriod,
podCache: podCache,
clock: clock.RealClock{},
}
}
func (p *podWorkers) IsPodKnownTerminated(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
return status.IsTerminated()
}
// if the pod is not known, we return false (pod worker is not aware of it)
return false
}
func (p *podWorkers) CouldHaveRunningContainers(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
return !status.IsTerminated()
}
// once all pods are synced, any pod without sync status is known to not be running.
return !p.podsSynced
}
func (p *podWorkers) ShouldPodBeFinished(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
return status.IsFinished()
}
// once all pods are synced, any pod without sync status is assumed to
// have SyncTerminatedPod finished.
return p.podsSynced
}
func (p *podWorkers) IsPodTerminationRequested(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
// the pod may still be setting up at this point.
return status.IsTerminationRequested()
}
// an unknown pod is considered not to be terminating (use ShouldPodContainersBeTerminating in
// cleanup loops to avoid failing to cleanup pods that have already been removed from config)
return false
}
func (p *podWorkers) ShouldPodContainersBeTerminating(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
// we wait until the pod worker goroutine observes the termination, which means syncPod will not
// be executed again, which means no new containers can be started
return status.IsTerminationStarted()
}
// once we've synced, if the pod isn't known to the workers we should be tearing them
// down
return p.podsSynced
}
func (p *podWorkers) ShouldPodRuntimeBeRemoved(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
return status.IsTerminated()
}
// a pod that hasn't been sent to the pod worker yet should have no runtime components once we have
// synced all content.
return p.podsSynced
}
func (p *podWorkers) ShouldPodContentBeRemoved(uid types.UID) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
if status, ok := p.podSyncStatuses[uid]; ok {
return status.IsEvicted() || (status.IsDeleted() && status.IsTerminated())
}
// a pod that hasn't been sent to the pod worker yet should have no content on disk once we have
// synced all content.
return p.podsSynced
}
func (p *podWorkers) IsPodForMirrorPodTerminatingByFullName(podFullName string) bool {
p.podLock.Lock()
defer p.podLock.Unlock()
uid, started := p.startedStaticPodsByFullname[podFullName]
if !started {
return false
}
status, exists := p.podSyncStatuses[uid]
if !exists {
return false
}
if !status.IsTerminationRequested() || status.IsTerminated() {
return false
}
return true
}
func isPodStatusCacheTerminal(status *kubecontainer.PodStatus) bool {
for _, container := range status.ContainerStatuses {
if container.State == kubecontainer.ContainerStateRunning {
return false
}
}
for _, sb := range status.SandboxStatuses {
if sb.State == runtimeapi.PodSandboxState_SANDBOX_READY {
return false
}
}
return true
}
// UpdatePod carries a configuration change or termination state to a pod. A pod is either runnable,
// terminating, or terminated, and will transition to terminating if: deleted on the apiserver,
// discovered to have a terminal phase (Succeeded or Failed), or evicted by the kubelet.
func (p *podWorkers) UpdatePod(options UpdatePodOptions) {
// Handle when the pod is an orphan (no config) and we only have runtime status by running only
// the terminating part of the lifecycle. A running pod contains only a minimal set of information
// about the pod
var isRuntimePod bool
var uid types.UID
var name, ns string
if runningPod := options.RunningPod; runningPod != nil {
if options.Pod == nil {
// the sythetic pod created here is used only as a placeholder and not tracked
if options.UpdateType != kubetypes.SyncPodKill {
klog.InfoS("Pod update is ignored, runtime pods can only be killed", "pod", klog.KRef(runningPod.Namespace, runningPod.Name), "podUID", runningPod.ID, "updateType", options.UpdateType)
return
}
uid, ns, name = runningPod.ID, runningPod.Namespace, runningPod.Name
isRuntimePod = true
} else {
options.RunningPod = nil
uid, ns, name = options.Pod.UID, options.Pod.Namespace, options.Pod.Name
klog.InfoS("Pod update included RunningPod which is only valid when Pod is not specified", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
}
} else {
uid, ns, name = options.Pod.UID, options.Pod.Namespace, options.Pod.Name
}
p.podLock.Lock()
defer p.podLock.Unlock()
// decide what to do with this pod - we are either setting it up, tearing it down, or ignoring it
var firstTime bool
now := p.clock.Now()
status, ok := p.podSyncStatuses[uid]
if !ok {
klog.V(4).InfoS("Pod is being synced for the first time", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
firstTime = true
status = &podSyncStatus{
syncedAt: now,
fullname: kubecontainer.BuildPodFullName(name, ns),
}
// if this pod is being synced for the first time, we need to make sure it is an active pod
if options.Pod != nil && (options.Pod.Status.Phase == v1.PodFailed || options.Pod.Status.Phase == v1.PodSucceeded) {
// Check to see if the pod is not running and the pod is terminal; if this succeeds then record in the podWorker that it is terminated.
// This is needed because after a kubelet restart, we need to ensure terminal pods will NOT be considered active in Pod Admission. See http://issues.k8s.io/105523
// However, `filterOutInactivePods`, considers pods that are actively terminating as active. As a result, `IsPodKnownTerminated()` needs to return true and thus `terminatedAt` needs to be set.
if statusCache, err := p.podCache.Get(uid); err == nil {
if isPodStatusCacheTerminal(statusCache) {
// At this point we know:
// (1) The pod is terminal based on the config source.
// (2) The pod is terminal based on the runtime cache.
// This implies that this pod had already completed `SyncTerminatingPod` sometime in the past. The pod is likely being synced for the first time due to a kubelet restart.
// These pods need to complete SyncTerminatedPod to ensure that all resources are cleaned and that the status manager makes the final status updates for the pod.
// As a result, set finished: false, to ensure a Terminated event will be sent and `SyncTerminatedPod` will run.
status = &podSyncStatus{
terminatedAt: now,
terminatingAt: now,
syncedAt: now,
startedTerminating: true,
finished: false,
fullname: kubecontainer.BuildPodFullName(name, ns),
}
}
}
}
p.podSyncStatuses[uid] = status
}
// RunningPods represent an unknown pod execution and don't contain pod spec information
// sufficient to perform any action other than termination. If we received a RunningPod
// after a real pod has already been provided, use the most recent spec instead. Also,
// once we observe a runtime pod we must drive it to completion, even if we weren't the
// ones who started it.
pod := options.Pod
if isRuntimePod {
status.observedRuntime = true
switch {
case status.pendingUpdate != nil && status.pendingUpdate.Pod != nil:
pod = status.pendingUpdate.Pod
options.Pod = pod
options.RunningPod = nil
case status.activeUpdate != nil && status.activeUpdate.Pod != nil:
pod = status.activeUpdate.Pod
options.Pod = pod
options.RunningPod = nil
default:
// we will continue to use RunningPod.ToAPIPod() as pod here, but
// options.Pod will be nil and other methods must handle that appropriately.
pod = options.RunningPod.ToAPIPod()
}
}
// When we see a create update on an already terminating pod, that implies two pods with the same UID were created in
// close temporal proximity (usually static pod but it's possible for an apiserver to extremely rarely do something
// similar) - flag the sync status to indicate that after the pod terminates it should be reset to "not running" to
// allow a subsequent add/update to start the pod worker again. This does not apply to the first time we see a pod,
// such as when the kubelet restarts and we see already terminated pods for the first time.
if !firstTime && status.IsTerminationRequested() {
if options.UpdateType == kubetypes.SyncPodCreate {
status.restartRequested = true
klog.V(4).InfoS("Pod is terminating but has been requested to restart with same UID, will be reconciled later", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
return
}
}
// once a pod is terminated by UID, it cannot reenter the pod worker (until the UID is purged by housekeeping)
if status.IsFinished() {
klog.V(4).InfoS("Pod is finished processing, no further updates", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
return
}
// check for a transition to terminating
var becameTerminating bool
if !status.IsTerminationRequested() {
switch {
case isRuntimePod:
klog.V(4).InfoS("Pod is orphaned and must be torn down", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
status.deleted = true
status.terminatingAt = now
becameTerminating = true
case pod.DeletionTimestamp != nil:
klog.V(4).InfoS("Pod is marked for graceful deletion, begin teardown", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
status.deleted = true
status.terminatingAt = now
becameTerminating = true
case pod.Status.Phase == v1.PodFailed, pod.Status.Phase == v1.PodSucceeded:
klog.V(4).InfoS("Pod is in a terminal phase (success/failed), begin teardown", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
status.terminatingAt = now
becameTerminating = true
case options.UpdateType == kubetypes.SyncPodKill:
if options.KillPodOptions != nil && options.KillPodOptions.Evict {
klog.V(4).InfoS("Pod is being evicted by the kubelet, begin teardown", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
status.evicted = true
} else {
klog.V(4).InfoS("Pod is being removed by the kubelet, begin teardown", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
}
status.terminatingAt = now
becameTerminating = true
}
}
// once a pod is terminating, all updates are kills and the grace period can only decrease
var wasGracePeriodShortened bool
switch {
case status.IsTerminated():
// A terminated pod may still be waiting for cleanup - if we receive a runtime pod kill request
// due to housekeeping seeing an older cached version of the runtime pod simply ignore it until
// after the pod worker completes.
if isRuntimePod {
klog.V(3).InfoS("Pod is waiting for termination, ignoring runtime-only kill until after pod worker is fully terminated", "pod", klog.KRef(ns, name), "podUID", uid, "updateType", options.UpdateType)
return
}
if options.KillPodOptions != nil {
if ch := options.KillPodOptions.CompletedCh; ch != nil {
close(ch)
}
}
options.KillPodOptions = nil
case status.IsTerminationRequested():
if options.KillPodOptions == nil {
options.KillPodOptions = &KillPodOptions{}
}
if ch := options.KillPodOptions.CompletedCh; ch != nil {
status.notifyPostTerminating = append(status.notifyPostTerminating, ch)
}
if fn := options.KillPodOptions.PodStatusFunc; fn != nil {
status.statusPostTerminating = append(status.statusPostTerminating, fn)
}
gracePeriod, gracePeriodShortened := calculateEffectiveGracePeriod(status, pod, options.KillPodOptions)
wasGracePeriodShortened = gracePeriodShortened
status.gracePeriod = gracePeriod
// always set the grace period for syncTerminatingPod so we don't have to recalculate,
// will never be zero.
options.KillPodOptions.PodTerminationGracePeriodSecondsOverride = &gracePeriod
default:
// KillPodOptions is not valid for sync actions outside of the terminating phase
if options.KillPodOptions != nil {
if ch := options.KillPodOptions.CompletedCh; ch != nil {
close(ch)
}
options.KillPodOptions = nil
}
}
// start the pod worker goroutine if it doesn't exist
podUpdates, exists := p.podUpdates[uid]
if !exists {
// buffer the channel to avoid blocking this method
podUpdates = make(chan struct{}, 1)
p.podUpdates[uid] = podUpdates
// ensure that static pods start in the order they are received by UpdatePod
if kubetypes.IsStaticPod(pod) {
p.waitingToStartStaticPodsByFullname[status.fullname] =
append(p.waitingToStartStaticPodsByFullname[status.fullname], uid)
}
// allow testing of delays in the pod update channel
var outCh <-chan struct{}
if p.workerChannelFn != nil {
outCh = p.workerChannelFn(uid, podUpdates)
} else {
outCh = podUpdates
}
// spawn a pod worker
go func() {
// TODO: this should be a wait.Until with backoff to handle panics, and
// accept a context for shutdown
defer runtime.HandleCrash()
defer klog.V(3).InfoS("Pod worker has stopped", "podUID", uid)
p.podWorkerLoop(uid, outCh)
}()
}
// measure the maximum latency between a call to UpdatePod and when the pod worker reacts to it
// by preserving the oldest StartTime
if status.pendingUpdate != nil && !status.pendingUpdate.StartTime.IsZero() && status.pendingUpdate.StartTime.Before(options.StartTime) {
options.StartTime = status.pendingUpdate.StartTime
}
// notify the pod worker there is a pending update
status.pendingUpdate = &options
status.working = true
klog.V(4).InfoS("Notifying pod of pending update", "pod", klog.KRef(ns, name), "podUID", uid, "workType", status.WorkType())
select {
case podUpdates <- struct{}{}:
default:
}
if (becameTerminating || wasGracePeriodShortened) && status.cancelFn != nil {
klog.V(3).InfoS("Cancelling current pod sync", "pod", klog.KRef(ns, name), "podUID", uid, "workType", status.WorkType())
status.cancelFn()
return
}
}
// calculateEffectiveGracePeriod sets the initial grace period for a newly terminating pod or allows a
// shorter grace period to be provided, returning the desired value.
func calculateEffectiveGracePeriod(status *podSyncStatus, pod *v1.Pod, options *KillPodOptions) (int64, bool) {
// enforce the restriction that a grace period can only decrease and track whatever our value is,
// then ensure a calculated value is passed down to lower levels
gracePeriod := status.gracePeriod
overridden := false
// this value is bedrock truth - the apiserver owns telling us this value calculated by apiserver
if override := pod.DeletionGracePeriodSeconds; override != nil {
if gracePeriod == 0 || *override < gracePeriod {
gracePeriod = *override
overridden = true
}
}
// we allow other parts of the kubelet (namely eviction) to request this pod be terminated faster
if options != nil {
if override := options.PodTerminationGracePeriodSecondsOverride; override != nil {
if gracePeriod == 0 || *override < gracePeriod {
gracePeriod = *override
overridden = true
}
}
}
// make a best effort to default this value to the pod's desired intent, in the event
// the kubelet provided no requested value (graceful termination?)