Modify InitContainer checking function that potentially incurs error (#3198)

Signed-off-by: Taeuk Kim <taeuk_kim@tmax.co.kr>
This commit is contained in:
Taeuk Kim
2021-02-09 03:26:56 +09:00
committed by GitHub
parent 38c08e087b
commit 529e05d6b2
3 changed files with 24 additions and 2 deletions

View File

@@ -220,10 +220,11 @@ func isPodOnNode(pod *corev1api.Pod, node string) bool {
}
func isResticInitContainerRunning(pod *corev1api.Pod) bool {
// Restic wait container can be anywhere in the list of init containers, but must be running.
i := getResticInitContainerIndex(pod)
return i >= 0 && pod.Status.InitContainerStatuses[i].State.Running != nil
return i >= 0 &&
len(pod.Status.InitContainerStatuses)-1 >= i &&
pod.Status.InitContainerStatuses[i].State.Running != nil
}
func getResticInitContainerIndex(pod *corev1api.Pod) int {

View File

@@ -590,6 +590,26 @@ func TestIsResticContainerRunning(t *testing.T) {
},
expected: true,
},
{
name: "pod with restic init container with empty InitContainerStatuses should return 0",
pod: &corev1api.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns-1",
Name: "pod-1",
},
Spec: corev1api.PodSpec{
InitContainers: []corev1api.Container{
{
Name: restic.InitContainer,
},
},
},
Status: corev1api.PodStatus{
InitContainerStatuses: []corev1api.ContainerStatus{},
},
},
expected: false,
},
}
for _, test := range tests {