From 847155811b76cd556bdc53560666ecb41d067d86 Mon Sep 17 00:00:00 2001 From: Scarlett Perry <39421794+scarlettperry@users.noreply.github.com> Date: Tue, 5 Nov 2024 15:34:07 -0500 Subject: [PATCH] k8s: update pod status logic to support native sidecars (#3169) ### 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 --- backend/service/k8s/pods.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/backend/service/k8s/pods.go b/backend/service/k8s/pods.go index e9e40a21d1..1060e0be03 100644 --- a/backend/service/k8s/pods.go +++ b/backend/service/k8s/pods.go @@ -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: