8000 fix(keeper): do not treat a pod without container statuses as started by levin-kitty · Pull Request #2162 · Altinity/clickhouse-operator · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/model/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func PodHasNotReadyContainers(pod *core.Pod) bool {
}

func PodHasAllContainersStarted(pod *core.Pod) bool {
// No container statuses yet means the pod is not scheduled or kubelet has not reported.
// Without this check an empty list is vacuously "all started".
if len(pod.Status.ContainerStatuses) < 1 {
return false
}
allContainersStarted := true
for _, containerStatus := range pod.Status.ContainerStatuses {
// Started indicates whether the container has finished its postStart lifecycle hook
Expand Down
41 changes: 41 additions & 0 deletions pkg/model/k8s/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,44 @@ func TestPodContainersRestartCountsGet(t *testing.T) {
require.Nil(t, PodContainersRestartCountsGet(nil, "clickhouse"))
require.Nil(t, PodContainersRestartCountsGet(&core.Pod{}))
}

// A pod that is not scheduled yet reports no container statuses at all - it must not count as started
func TestPodHasAllContainersStarted(t *testing.T) {
started := true
notStarted := false

// Pending pod, not scheduled to a node - kubelet has reported nothing
pending := &core.Pod{
Spec: core.PodSpec{Containers: []core.Container{{Name: "keeper"}}},
Status: core.PodStatus{Phase: core.PodPending},
}
require.False(t, PodHasAllContainersStarted(pending), "no container statuses means nothing has started")
require.True(t, PodHasNotStartedContainers(pending))

// Scheduled, container is waiting
waiting := &core.Pod{Status: core.PodStatus{ContainerStatuses: []core.ContainerStatus{
{Name: "keeper", Started: &notStarted},
}}}
require.False(t, PodHasAllContainersStarted(waiting))

// Started is unknown - the null value must be treated the same as false
unknown := &core.Pod{Status: core.PodStatus{ContainerStatuses: []core.ContainerStatus{
{Name: "keeper"},
}}}
require.False(t, PodHasAllContainersStarted(unknown))

// One of the containers is not started
partial := &core.Pod{Status: core.PodStatus{ContainerStatuses: []core.ContainerStatus{
{Name: "keeper", Started: &started},
{Name: "sidecar", Started: &notStarted},
}}}
require.False(t, PodHasAllContainersStarted(partial))

// All containers are started
all := &core.Pod{Status: core.PodStatus{ContainerStatuses: []core.ContainerStatus{
{Name: "keeper", Started: &started},
{Name: "sidecar", Started: &started},
}}}
require.True(t, PodHasAllContainersStarted(all))
require.False(t, PodHasNotStartedContainers(all))
}
0