Skip to content

Commit 024a7b7

Browse files
authored
[ Bugfix ] [TG-204] Bugfix panic when pod statuses empty (#897)
1 parent afcb5cc commit 024a7b7

File tree

4 files changed

+113
-19
lines changed

4 files changed

+113
-19
lines changed

cmd/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ func getMyPodInfo(kubecli kubernetes.Interface, namespace, name string) (string,
442442
return maskAny(err)
443443
}
444444
sa = pod.Spec.ServiceAccountName
445-
image = k8sutil.GetArangoDBImageIDFromPod(pod)
445+
if image, err = k8sutil.GetArangoDBImageIDFromPod(pod); err != nil {
446+
return errors.Wrap(err, "failed to get image ID from pod")
447+
}
446448
if image == "" {
447449
// Fallback in case we don't know the id.
448450
image = pod.Spec.Containers[0].Image

pkg/deployment/images.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ func (ib *imagesBuilder) fetchArangoDBImageIDAndVersion(ctx context.Context, cac
160160
return true, nil
161161
}
162162

163-
if len(pod.Status.ContainerStatuses) == 0 {
164-
log.Warn().Msg("Empty list of ContainerStatuses")
163+
imageID, err := k8sutil.GetArangoDBImageIDFromPod(pod)
164+
if err != nil {
165+
log.Warn().Err(err).Msg("failed to get image ID from pod")
165166
return true, nil
166167
}
167-
imageID := k8sutil.GetArangoDBImageIDFromPod(pod)
168168
if imageID == "" {
169169
// Fall back to specified image
170170
imageID = image

pkg/util/k8sutil/images.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strings"
2525

2626
corev1 "k8s.io/api/core/v1"
27+
28+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
2729
)
2830

2931
const (
@@ -40,7 +42,15 @@ func ConvertImageID2Image(imageID string) string {
4042
}
4143

4244
// GetArangoDBImageIDFromPod returns the ArangoDB specific image from a pod
43-
func GetArangoDBImageIDFromPod(pod *corev1.Pod) string {
45+
func GetArangoDBImageIDFromPod(pod *corev1.Pod) (string, error) {
46+
if pod == nil {
47+
return "", errors.New("failed to get container statuses from nil pod")
48+
}
49+
50+
if len(pod.Status.ContainerStatuses) == 0 {
51+
return "", errors.New("empty list of ContainerStatuses")
52+
}
53+
4454
rawImageID := pod.Status.ContainerStatuses[0].ImageID
4555
if len(pod.Status.ContainerStatuses) > 1 {
4656
for _, containerStatus := range pod.Status.ContainerStatuses {
@@ -49,19 +59,6 @@ func GetArangoDBImageIDFromPod(pod *corev1.Pod) string {
4959
}
5060
}
5161
}
52-
return ConvertImageID2Image(rawImageID)
53-
}
54-
55-
// GetArangoDBContainerFromPod returns the ArangoDB container from a pod
56-
func GetArangoDBContainerFromPod(pod *corev1.Pod) corev1.Container {
57-
arangoc := pod.Spec.Containers[0]
58-
if len(pod.Status.ContainerStatuses) > 1 {
59-
for _, container := range pod.Spec.Containers {
60-
if strings.Contains(container.Name, "server") {
61-
arangoc = container
62-
}
63-
}
64-
}
6562

66-
return arangoc
63+
return ConvertImageID2Image(rawImageID), nil
6764
}

pkg/util/k8sutil/images_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package k8sutil
22+
23+
import (
24+
"testing"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
corev1 "k8s.io/api/core/v1"
30+
)
31+
32+
func TestGetArangoDBImageIDFromPod(t *testing.T) {
33+
type args struct {
34+
pod *corev1.Pod
35+
}
36+
tests := map[string]struct {
37+
args args
38+
want string
39+
wantErr error
40+
}{
41+
"pid is nil": {
42+
wantErr: errors.New("failed to get container statuses from nil pod"),
43+
},
44+
"container statuses list is empty": {
45+
args: args{
46+
pod: &corev1.Pod{},
47+
},
48+
wantErr: errors.New("empty list of ContainerStatuses"),
49+
},
50+
"image ID from the only container": {
51+
args: args{
52+
pod: &corev1.Pod{
53+
Status: corev1.PodStatus{
54+
ContainerStatuses: []corev1.ContainerStatus{
55+
{
56+
ImageID: dockerPullableImageIDPrefix + "test",
57+
},
58+
},
59+
},
60+
},
61+
},
62+
want: "test",
63+
},
64+
"image ID from two containers": {
65+
args: args{
66+
pod: &corev1.Pod{
67+
Status: corev1.PodStatus{
68+
ContainerStatuses: []corev1.ContainerStatus{
69+
{
70+
ImageID: dockerPullableImageIDPrefix + "test_arango",
71+
},
72+
{
73+
ImageID: dockerPullableImageIDPrefix + "test1_arango",
74+
},
75+
},
76+
},
77+
},
78+
},
79+
want: "test1_arango",
80+
},
81+
}
82+
83+
for testName, testCase := range tests {
84+
t.Run(testName, func(t *testing.T) {
85+
got, err := GetArangoDBImageIDFromPod(testCase.args.pod)
86+
if testCase.wantErr != nil {
87+
require.EqualError(t, err, testCase.wantErr.Error())
88+
return
89+
}
90+
91+
require.NoError(t, err)
92+
assert.Equalf(t, testCase.want, got, "image ID is not as expected")
93+
})
94+
}
95+
}

0 commit comments

Comments
 (0)