@@ -872,22 +872,88 @@ Here is the proposed approach:
872
872
1 . Sidecar containers that have a ` PreStop ` hook will be notified when the Pod has begun terminating
873
873
by executing the ` PreStop ` hook. This happens at the same time as regular containers, and begins
874
874
the Pod's termination grace period countdown.
875
- 2 . Sidecar containers enter the ` Terminated ` state and are no longer restarted if they fail.
876
- 3 . Once the last primary container terminates, the last started sidecar container is notified by
875
+ 2 . Once the last primary container terminates, the last started sidecar container is notified by
877
876
sending a ` SIGTERM ` signal.
878
- 4 . The next sidecar (in reverse order) is notified by sending a ` SIGTERM ` signal after the previous
877
+ 3 . The next sidecar (in reverse order) is notified by sending a ` SIGTERM ` signal after the previous
879
878
sidecar container terminates.
880
- 5 . This continues until all sidecar containers have terminated, or the Pod's termination grace period
881
- expires. In the latter case, all containers are terminated with minimum grace period and the Pod
882
- will be terminated after that.
879
+ 4 . This continues until all sidecar containers have terminated, or the Pod's termination grace period
880
+ expires.
881
+ 5 . In the latter case, all remaining containers are notified by a ` SIGTERM ` , followed by a fixed
882
+ grace period of 2s and finally terminated.
883
+ 6 . The Pod will be terminated after that.
884
+
885
+ Pseudocode for the above:
886
+
887
+ ```
888
+ func terminatePod() {
889
+ // notify all sidecar containers with preStop hook, asynchronously
890
+ for sidecar in sidecarContainers {
891
+ if sidecar has preStop hook {
892
+ go execute preStop hook // async
893
+ }
894
+ }
895
+ // notify all containers with preStop hook and then SIGTERM, asynchronously
896
+ for container in containers {
897
+ if container has preStop hook {
898
+ go func(container) { // async
899
+ execute preStop hook
900
+ send SIGTERM
901
+ }
902
+ }
903
+ }
904
+ for {
905
+ switch {
906
+ case grace period expired:
907
+ for anyContainer in sidecarContainers + containers {
908
+ if anyContainer is running {
909
+ send SIGTERM
910
+ }
911
+ }
912
+ sleep 2s
913
+ for anyContainer in sidecarContainer + containers {
914
+ if anyContainer is running {
915
+ send SIGKILL
916
+ }
917
+ }
918
+ return
919
+ case all containers are terminated:
920
+ // sidecars are terminated in reverse order
921
+ for sidecar in reverse(sidecarContainers) {
922
+ // sidecar is already terminating, let it finish
923
+ if sidecar is terminating {
924
+ break
925
+ }
926
+ // next sidecar to terminate
927
+ else if sidecar is running {
928
+ send SIGTERM
929
+ break
930
+ }
931
+ }
932
+ sleep 1s
933
+ case all sidecarContainers are terminated:
934
+ return
935
+ default:
936
+ sleep 1s
937
+ }
938
+ }
939
+ }
940
+ ```
883
941
884
942
It is worth noting that, like with regular containers, ` PreStop ` hook must complete before the ` SIGTERM `
885
943
signal to stop the sidecar container can be sent. Therefore, ordering and graceful termination of sidecars
886
944
can only be guaranteed if the ` PreStop ` hook completes within the Pod's termination grace period.
887
945
946
+ Sidecars continue to be restarted until they enter the ` Terminated ` state which they are notified
947
+ by a ` SIGTERM ` signal. This is to ensure that sidecars that fail are restarted until the TGPS expires.
948
+
949
+ We might postpone running the ` livenessProbe ` for restarted sidecar containers during termination
950
+ until GA, depending on the implementation complexity.
951
+
888
952
If we compare this to the initial proposal, the following behaviors are preserved:
889
953
- Sidecars should not begin termination until all primary containers have
890
954
terminated.
955
+ - Implicit in this is that sidecars should continue to be restarted until all
956
+ primary containers have terminated.
891
957
- Sidecars should terminate serially and in reverse order. I.e. the first
892
958
sidecar to initialize should be the last sidecar to terminate.
893
959
@@ -898,12 +964,6 @@ The additional benefits of this approach comparing to initial proposal:
898
964
same time as before and can utilize as much of the graceful termination period as they need. The Pod graceful
899
965
termination period semantic also stay unchanged.
900
966
901
- However, the following behaviors is not preserved:
902
- - Sidecars should continue to be restarted until all primary containers have terminated.
903
-
904
- We think this is a reasonable tradeoff and consistent with the best effort nature of keeping
905
- sidecar containers running during the Pod's lifetime.
906
-
907
967
### Other
908
968
909
969
This behavior needs to be adjusted:
0 commit comments