Skip to content

Commit d970516

Browse files
EricMountainamardomingo
authored andcommitted
[sidecars] Missing non-sidecar status must not allow progress
When non-sidecar container statuses are missing from the pod struct, we must assume they are waiting in order to prevent computePodActions() from starting non-sidecars before sidecars are ready.
1 parent 6cb2956 commit d970516

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

pkg/kubelet/kubelet.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,7 @@ func (kl *Kubelet) HandlePodReconcile(pods []*v1.Pod) {
21542154
kl.podManager.UpdatePod(pod)
21552155

21562156
sidecarsStatus := status.GetSidecarsStatus(pod)
2157+
klog.Infof("Pod: %s, status: Present=%v,Ready=%v,ContainersWaiting=%v", format.Pod(pod), sidecarsStatus.SidecarsPresent, sidecarsStatus.SidecarsReady, sidecarsStatus.ContainersWaiting)
21572158

21582159
// Reconcile Pod "Ready" condition if necessary. Trigger sync pod for reconciliation.
21592160
if status.NeedToReconcilePodReadiness(pod) {

pkg/kubelet/status/status_manager.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,21 +777,39 @@ type SidecarsStatus struct {
777777
}
778778

779779
// GetSidecarsStatus returns the SidecarsStatus for the given pod
780+
// We assume the worst: if we are unable to determine the status of all containers we make defensive assumptions that
781+
// there are sidecars, they are not ready, and that there are non-sidecars waiting. This is to prevent starting non-
782+
// -sidecars accidentally.
780783
func GetSidecarsStatus(pod *v1.Pod) SidecarsStatus {
784+
var containerStatusesCopy []v1.ContainerStatus
781785
if pod == nil {
782-
klog.Infof("Pod was nil, returning empty sidecar status")
783-
return SidecarsStatus{}
786+
klog.Infof("Pod was nil, returning sidecar status that prevents progress")
787+
return SidecarsStatus{SidecarsPresent: true, SidecarsReady: false, ContainersWaiting: true}
784788
}
785-
if pod.Spec.Containers == nil || pod.Status.ContainerStatuses == nil {
786-
klog.Infof("Pod Containers or Container status was nil, returning empty sidecar status")
787-
return SidecarsStatus{}
789+
if pod.Spec.Containers == nil {
790+
klog.Infof("Pod %s: Containers was nil, returning sidecar status that prevents progress", format.Pod(pod))
791+
return SidecarsStatus{SidecarsPresent: true, SidecarsReady: false, ContainersWaiting: true}
788792
}
793+
if pod.Status.ContainerStatuses == nil {
794+
klog.Infof("Pod %s: ContainerStatuses was nil, doing best effort using spec", format.Pod(pod))
795+
} else {
796+
// Make a copy of ContainerStatuses to avoid having the carpet pulled from under our feet
797+
containerStatusesCopy = make([]v1.ContainerStatus, len(pod.Status.ContainerStatuses))
798+
copy(containerStatusesCopy, pod.Status.ContainerStatuses)
799+
}
800+
789801
sidecarsStatus := SidecarsStatus{SidecarsPresent: false, SidecarsReady: true, ContainersWaiting: false}
790802
for _, container := range pod.Spec.Containers {
791-
for _, status := range pod.Status.ContainerStatuses {
803+
foundStatus := false
804+
isSidecar := false
805+
if pod.Annotations[fmt.Sprintf("sidecars.lyft.net/container-lifecycle-%s", container.Name)] == "Sidecar" {
806+
isSidecar = true
807+
sidecarsStatus.SidecarsPresent = true
808+
}
809+
for _, status := range containerStatusesCopy {
792810
if status.Name == container.Name {
793-
if pod.Annotations[fmt.Sprintf("sidecars.lyft.net/container-lifecycle-%s", container.Name)] == "Sidecar" {
794-
sidecarsStatus.SidecarsPresent = true
811+
foundStatus = true
812+
if isSidecar {
795813
if !status.Ready {
796814
klog.Infof("Pod %s: %s: sidecar not ready", format.Pod(pod), container.Name)
797815
sidecarsStatus.SidecarsReady = false
@@ -803,6 +821,16 @@ func GetSidecarsStatus(pod *v1.Pod) SidecarsStatus {
803821
klog.Infof("Pod: %s: %s: non-sidecar waiting", format.Pod(pod), container.Name)
804822
sidecarsStatus.ContainersWaiting = true
805823
}
824+
break
825+
}
826+
}
827+
if !foundStatus {
828+
if isSidecar {
829+
klog.Infof("Pod %s: %s (sidecar): status not found, assuming not ready", format.Pod(pod), container.Name)
830+
sidecarsStatus.SidecarsReady = false
831+
} else {
832+
klog.Infof("Pod: %s: %s (non-sidecar): status not found, assuming waiting", format.Pod(pod), container.Name)
833+
sidecarsStatus.ContainersWaiting = true
806834
}
807835
}
808836
}

0 commit comments

Comments
 (0)