Skip to content

Commit 10ad30e

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 65ca616 commit 10ad30e

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
@@ -2070,6 +2070,7 @@ func (kl *Kubelet) HandlePodReconcile(pods []*v1.Pod) {
20702070
kl.podManager.UpdatePod(pod)
20712071

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

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

pkg/kubelet/status/status_manager.go

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

754754
// GetSidecarsStatus returns the SidecarsStatus for the given pod
755+
// We assume the worst: if we are unable to determine the status of all containers we make defensive assumptions that
756+
// there are sidecars, they are not ready, and that there are non-sidecars waiting. This is to prevent starting non-
757+
// -sidecars accidentally.
755758
func GetSidecarsStatus(pod *v1.Pod) SidecarsStatus {
759+
var containerStatusesCopy []v1.ContainerStatus
756760
if pod == nil {
757-
klog.Infof("Pod was nil, returning empty sidecar status")
758-
return SidecarsStatus{}
761+
klog.Infof("Pod was nil, returning sidecar status that prevents progress")
762+
return SidecarsStatus{SidecarsPresent: true, SidecarsReady: false, ContainersWaiting: true}
759763
}
760-
if pod.Spec.Containers == nil || pod.Status.ContainerStatuses == nil {
761-
klog.Infof("Pod Containers or Container status was nil, returning empty sidecar status")
762-
return SidecarsStatus{}
764+
if pod.Spec.Containers == nil {
765+
klog.Infof("Pod %s: Containers was nil, returning sidecar status that prevents progress", format.Pod(pod))
766+
return SidecarsStatus{SidecarsPresent: true, SidecarsReady: false, ContainersWaiting: true}
763767
}
768+
if pod.Status.ContainerStatuses == nil {
769+
klog.Infof("Pod %s: ContainerStatuses was nil, doing best effort using spec", format.Pod(pod))
770+
} else {
771+
// Make a copy of ContainerStatuses to avoid having the carpet pulled from under our feet
772+
containerStatusesCopy = make([]v1.ContainerStatus, len(pod.Status.ContainerStatuses))
773+
copy(containerStatusesCopy, pod.Status.ContainerStatuses)
774+
}
775+
764776
sidecarsStatus := SidecarsStatus{SidecarsPresent: false, SidecarsReady: true, ContainersWaiting: false}
765777
for _, container := range pod.Spec.Containers {
766-
for _, status := range pod.Status.ContainerStatuses {
778+
foundStatus := false
779+
isSidecar := false
780+
if pod.Annotations[fmt.Sprintf("sidecars.lyft.net/container-lifecycle-%s", container.Name)] == "Sidecar" {
781+
isSidecar = true
782+
sidecarsStatus.SidecarsPresent = true
783+
}
784+
for _, status := range containerStatusesCopy {
767785
if status.Name == container.Name {
768-
if pod.Annotations[fmt.Sprintf("sidecars.lyft.net/container-lifecycle-%s", container.Name)] == "Sidecar" {
769-
sidecarsStatus.SidecarsPresent = true
786+
foundStatus = true
787+
if isSidecar {
770788
if !status.Ready {
771789
klog.Infof("Pod %s: %s: sidecar not ready", format.Pod(pod), container.Name)
772790
sidecarsStatus.SidecarsReady = false
@@ -778,6 +796,16 @@ func GetSidecarsStatus(pod *v1.Pod) SidecarsStatus {
778796
klog.Infof("Pod: %s: %s: non-sidecar waiting", format.Pod(pod), container.Name)
779797
sidecarsStatus.ContainersWaiting = true
780798
}
799+
break
800+
}
801+
}
802+
if !foundStatus {
803+
if isSidecar {
804+
klog.Infof("Pod %s: %s (sidecar): status not found, assuming not ready", format.Pod(pod), container.Name)
805+
sidecarsStatus.SidecarsReady = false
806+
} else {
807+
klog.Infof("Pod: %s: %s (non-sidecar): status not found, assuming waiting", format.Pod(pod), container.Name)
808+
sidecarsStatus.ContainersWaiting = true
781809
}
782810
}
783811
}

0 commit comments

Comments
 (0)