Skip to content

Commit

Permalink
[sidecars] Missing non-sidecar status must not allow progress
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EricMountain authored and amardomingo committed Jun 1, 2022
1 parent 26c6943 commit d538c67
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/kubelet/kubelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,7 @@ func (kl *Kubelet) HandlePodReconcile(pods []*v1.Pod) {
kl.podManager.UpdatePod(pod)

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

// Reconcile Pod "Ready" condition if necessary. Trigger sync pod for reconciliation.
if status.NeedToReconcilePodReadiness(pod) {
Expand Down
44 changes: 36 additions & 8 deletions pkg/kubelet/status/status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,21 +897,39 @@ type SidecarsStatus struct {
}

// GetSidecarsStatus returns the SidecarsStatus for the given pod
// We assume the worst: if we are unable to determine the status of all containers we make defensive assumptions that
// there are sidecars, they are not ready, and that there are non-sidecars waiting. This is to prevent starting non-
// -sidecars accidentally.
func GetSidecarsStatus(pod *v1.Pod) SidecarsStatus {
var containerStatusesCopy []v1.ContainerStatus
if pod == nil {
klog.Infof("Pod was nil, returning empty sidecar status")
return SidecarsStatus{}
klog.Infof("Pod was nil, returning sidecar status that prevents progress")
return SidecarsStatus{SidecarsPresent: true, SidecarsReady: false, ContainersWaiting: true}
}
if pod.Spec.Containers == nil || pod.Status.ContainerStatuses == nil {
klog.Infof("Pod Containers or Container status was nil, returning empty sidecar status")
return SidecarsStatus{}
if pod.Spec.Containers == nil {
klog.Infof("Pod %s: Containers was nil, returning sidecar status that prevents progress", format.Pod(pod))
return SidecarsStatus{SidecarsPresent: true, SidecarsReady: false, ContainersWaiting: true}
}
if pod.Status.ContainerStatuses == nil {
klog.Infof("Pod %s: ContainerStatuses was nil, doing best effort using spec", format.Pod(pod))
} else {
// Make a copy of ContainerStatuses to avoid having the carpet pulled from under our feet
containerStatusesCopy = make([]v1.ContainerStatus, len(pod.Status.ContainerStatuses))
copy(containerStatusesCopy, pod.Status.ContainerStatuses)
}

sidecarsStatus := SidecarsStatus{SidecarsPresent: false, SidecarsReady: true, ContainersWaiting: false}
for _, container := range pod.Spec.Containers {
for _, status := range pod.Status.ContainerStatuses {
foundStatus := false
isSidecar := false
if pod.Annotations[fmt.Sprintf("sidecars.lyft.net/container-lifecycle-%s", container.Name)] == "Sidecar" {
isSidecar = true
sidecarsStatus.SidecarsPresent = true
}
for _, status := range containerStatusesCopy {
if status.Name == container.Name {
if pod.Annotations[fmt.Sprintf("sidecars.lyft.net/container-lifecycle-%s", container.Name)] == "Sidecar" {
sidecarsStatus.SidecarsPresent = true
foundStatus = true
if isSidecar {
if !status.Ready {
klog.Infof("Pod %s: %s: sidecar not ready", format.Pod(pod), container.Name)
sidecarsStatus.SidecarsReady = false
Expand All @@ -923,6 +941,16 @@ func GetSidecarsStatus(pod *v1.Pod) SidecarsStatus {
klog.Infof("Pod: %s: %s: non-sidecar waiting", format.Pod(pod), container.Name)
sidecarsStatus.ContainersWaiting = true
}
break
}
}
if !foundStatus {
if isSidecar {
klog.Infof("Pod %s: %s (sidecar): status not found, assuming not ready", format.Pod(pod), container.Name)
sidecarsStatus.SidecarsReady = false
} else {
klog.Infof("Pod: %s: %s (non-sidecar): status not found, assuming waiting", format.Pod(pod), container.Name)
sidecarsStatus.ContainersWaiting = true
}
}
}
Expand Down

0 comments on commit d538c67

Please sign in to comment.