Skip to content

Commit 926cae9

Browse files
committed
fix(catalog): be defensive about directly indexing catalog pods
Prevent index out of bound panic(s) when directly indexing catalog pod status.
1 parent 3cb2cfd commit 926cae9

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

pkg/controller/registry/reconciler/grpc.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import (
66
"hash/fnv"
77
"time"
88

9-
controllerclient "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/controller-runtime/client"
10-
11-
"github.com/operator-framework/api/pkg/operators/v1alpha1"
12-
hashutil "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubernetes/pkg/util/hash"
13-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
14-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
15-
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
169
"github.com/pkg/errors"
1710
"github.com/sirupsen/logrus"
1811
corev1 "k8s.io/api/core/v1"
1912
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2013
"k8s.io/apimachinery/pkg/labels"
2114
"k8s.io/apimachinery/pkg/util/intstr"
2215
"k8s.io/apimachinery/pkg/util/rand"
16+
17+
"github.com/operator-framework/api/pkg/operators/v1alpha1"
18+
controllerclient "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/controller-runtime/client"
19+
hashutil "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubernetes/pkg/util/hash"
20+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
21+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorlister"
22+
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/ownerutil"
2323
)
2424

2525
const (
@@ -349,9 +349,14 @@ func imageChanged(updatePod *corev1.Pod, servingPods []*corev1.Pod) bool {
349349
return false
350350
}
351351

352-
// imageID returns the ImageID of the primary catalog source container.
352+
// imageID returns the ImageID of the primary catalog source container or an empty string if the image ID isn't available yet.
353353
// Note: the pod must be running and the container in a ready status to return a valid ImageID.
354354
func imageID(pod *corev1.Pod) string {
355+
if len(pod.Status.ContainerStatuses) < 1 {
356+
logrus.WithField("CatalogSource", pod.GetName()).Warn("pod status unknown")
357+
return ""
358+
}
359+
355360
return pod.Status.ContainerStatuses[0].ImageID
356361
}
357362

pkg/controller/registry/reconciler/grpc_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ func TestGetPodImageID(t *testing.T) {
397397
pod: &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{{ImageID: "xyz123"}}}},
398398
result: "xyz123",
399399
},
400+
{
401+
description: "pod has two containers: return first",
402+
pod: &corev1.Pod{Status: corev1.PodStatus{ContainerStatuses: []corev1.ContainerStatus{
403+
{ImageID: "xyz123"},
404+
{ImageID: "abc456"},
405+
}}},
406+
result: "xyz123",
407+
},
408+
{
409+
description: "pod has no status",
410+
pod: &corev1.Pod{Status: corev1.PodStatus{}},
411+
result: "",
412+
},
400413
}
401414

402415
for i, tt := range table {

0 commit comments

Comments
 (0)