Skip to content

Commit

Permalink
k8s: update pod status logic to support native sidecars (#3169)
Browse files Browse the repository at this point in the history
<!--- TITLE FORMAT: "component: short description", e.g. "k8s: add pod
log reader" -->

### Description
A k8s sidecar is an init container that starts before the main app
container and **continues to run**. This is unlike a regular init
container which always runs to completion before the app container
starts.

**Reading**
*
https://github.com/kubernetes/enhancements/blob/master/keps/sig-node/753-sidecar-containers/README.md
* https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
*
https://github.com/kubernetes/kubernetes/blob/66e34012255abf1bbd0956a712817dad77c69c41/pkg/printers/internalversion/printers.go#L905-L908

**PR changes**
we need to account for the difference between an init sidecar container
and a regular init container.
* to identify which is an init sidecar container, we check for where
restart policy is not nil and set to restartalways
* to identify if the init sidecar is ready, we check that it has a
running state and the container started is set to true. This is the same
pattern as upstream [kubectl get
pods](https://github.com/kubernetes/kubernetes/blob/66e34012255abf1bbd0956a712817dad77c69c41/pkg/printers/internalversion/printers.go#L905-L908)
command.


### Testing Performed
Existing unit tests
Manual
  • Loading branch information
scarlettperry authored Nov 5, 2024
1 parent 5b7841c commit 8471558
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions backend/service/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,22 @@ func getPodStatus(pod *corev1.Pod) string {
reason = pod.Status.Reason
}

nativeSidecarContainer := make(map[string]bool)
for _, container := range pod.Spec.InitContainers {
// https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/
if container.RestartPolicy != nil && *container.RestartPolicy == corev1.ContainerRestartPolicyAlways {
nativeSidecarContainer[container.Name] = true
}
}

initializing := false
for i := range pod.Status.InitContainerStatuses {
container := pod.Status.InitContainerStatuses[i]
restarts += int(container.RestartCount)
switch {
case nativeSidecarContainer[container.Name] && container.Started != nil && *container.Started && container.Ready:
// https://github.com/kubernetes/kubernetes/blob/66e34012255abf1bbd0956a712817dad77c69c41/pkg/printers/internalversion/printers.go#L908
continue
case container.State.Terminated != nil && container.State.Terminated.ExitCode == 0:
continue
case container.State.Terminated != nil:
Expand Down

0 comments on commit 8471558

Please sign in to comment.