Skip to content

Commit 1d239a2

Browse files
authored
fix: Only check pod phase in isPodReady (#1127)
The implementation of `isPodReady`doesn't work, when there are init containers that are sidecars. They stay in `state=running` and currently this will always make `isPodReady` return false. This fixes determining readiness of pod containers. If a sidecar container is added to the `initContainers` of a pod, the status check must check a `Running` state and not a `Terminated` state. The `podIsReady` function is rewritten to make use of the container status conditions. If a condition exists with `Type=Ready` and `Status=True`, it is considered ready. This removes the investigation of container status for that purpose.
1 parent b9823e1 commit 1d239a2

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

internal/controller/status.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@ func containerCounts(pod *v1.Pod) (amaltheadevv1alpha1.ContainerCounts, amalthea
2121
if pod == nil {
2222
return initCounts, counts
2323
}
24+
2425
for _, container := range pod.Status.InitContainerStatuses {
25-
containerCompleted := container.State.Terminated != nil && container.State.Terminated.ExitCode == 0 && container.State.Terminated.Reason == "Completed"
26+
var containerOk bool
27+
if isContainerSidecar(container, *pod) {
28+
containerOk = container.Ready && container.State.Running != nil
29+
} else {
30+
containerOk = container.State.Terminated != nil && container.State.Terminated.ExitCode == 0 && container.State.Terminated.Reason == "Completed"
31+
}
32+
2633
initCounts.Total += 1
27-
if containerCompleted {
34+
if containerOk {
2835
initCounts.Ready += 1
2936
}
3037
}
@@ -39,14 +46,38 @@ func containerCounts(pod *v1.Pod) (amaltheadevv1alpha1.ContainerCounts, amalthea
3946
return initCounts, counts
4047
}
4148

49+
func containerRestartPolicy(status v1.ContainerStatus, pod v1.Pod) *v1.ContainerRestartPolicy {
50+
for _, c := range pod.Spec.Containers {
51+
if c.Name == status.Name {
52+
return c.RestartPolicy
53+
}
54+
}
55+
return nil
56+
}
57+
58+
func isContainerSidecar(status v1.ContainerStatus, pod v1.Pod) bool {
59+
restartPolicy := containerRestartPolicy(status, pod)
60+
if restartPolicy != nil {
61+
switch *restartPolicy {
62+
case v1.ContainerRestartPolicyAlways:
63+
return true
64+
}
65+
}
66+
return false
67+
}
68+
4269
func podIsReady(pod *v1.Pod) bool {
4370
if pod == nil || pod.GetDeletionTimestamp() != nil {
44-
// A missing pod or a pod being deleted is not considered ready
4571
return false
4672
}
4773
phaseOk := pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodRunning
48-
initCounts, counts := containerCounts(pod)
49-
return initCounts.Ok() && counts.Ok() && phaseOk
74+
condOk := false
75+
for _, c := range pod.Status.Conditions {
76+
if c.Type == v1.PodReady && c.Status == v1.ConditionTrue {
77+
condOk = true
78+
}
79+
}
80+
return phaseOk && condOk
5081
}
5182

5283
func podIsCompleted(pod *v1.Pod) bool {

0 commit comments

Comments
 (0)