Skip to content

Commit cb966b4

Browse files
authored
Merge pull request #1723 from mfojtik/fix-panic
internal/store: fix potential panic in pod store
2 parents 1f9457b + 43d374b commit cb966b4

File tree

3 files changed

+72
-31
lines changed

3 files changed

+72
-31
lines changed

internal/store/pod.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,18 @@ func createPodContainerInfoFamilyGenerator() generator.FamilyGenerator {
130130
ms := make([]*metric.Metric, len(p.Status.ContainerStatuses))
131131
labelKeys := []string{"container", "image_spec", "image", "image_id", "container_id"}
132132

133-
for i, cs := range p.Status.ContainerStatuses {
134-
specImage := p.Spec.Containers[i].Image
135-
ms[i] = &metric.Metric{
136-
LabelKeys: labelKeys,
137-
LabelValues: []string{cs.Name, specImage, cs.Image, cs.ImageID, cs.ContainerID},
138-
Value: 1,
133+
for i, c := range p.Spec.Containers {
134+
for _, cs := range p.Status.ContainerStatuses {
135+
if cs.Name != c.Name {
136+
continue
137+
}
138+
ms[i] = &metric.Metric{
139+
LabelKeys: labelKeys,
140+
LabelValues: []string{cs.Name, c.Image, cs.Image, cs.ImageID, cs.ContainerID},
141+
Value: 1,
142+
}
139143
}
140144
}
141-
142145
return &metric.Family{
143146
Metrics: ms,
144147
}
@@ -587,12 +590,16 @@ func createPodInitContainerInfoFamilyGenerator() generator.FamilyGenerator {
587590
ms := make([]*metric.Metric, len(p.Status.InitContainerStatuses))
588591
labelKeys := []string{"container", "image_spec", "image", "image_id", "container_id"}
589592

590-
for i, cs := range p.Status.InitContainerStatuses {
591-
specImage := p.Spec.InitContainers[i].Image
592-
ms[i] = &metric.Metric{
593-
LabelKeys: labelKeys,
594-
LabelValues: []string{cs.Name, specImage, cs.Image, cs.ImageID, cs.ContainerID},
595-
Value: 1,
593+
for i, c := range p.Spec.InitContainers {
594+
for _, cs := range p.Status.InitContainerStatuses {
595+
if cs.Name != c.Name {
596+
continue
597+
}
598+
ms[i] = &metric.Metric{
599+
LabelKeys: labelKeys,
600+
LabelValues: []string{cs.Name, c.Image, cs.Image, cs.ImageID, cs.ContainerID},
601+
Value: 1,
602+
}
596603
}
597604
}
598605

internal/store/pod_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestPodStore(t *testing.T) {
4545
Spec: v1.PodSpec{
4646
Containers: []v1.Container{
4747
{
48+
Name: "container1",
4849
Image: "k8s.gcr.io/hyperkube1_spec",
4950
},
5051
},
@@ -76,14 +77,17 @@ func TestPodStore(t *testing.T) {
7677
Spec: v1.PodSpec{
7778
Containers: []v1.Container{
7879
{
80+
Name: "container2",
7981
Image: "k8s.gcr.io/hyperkube2_spec",
8082
},
8183
{
84+
Name: "container3",
8285
Image: "k8s.gcr.io/hyperkube3_spec",
8386
},
8487
},
8588
InitContainers: []v1.Container{
8689
{
90+
Name: "initContainer",
8791
Image: "k8s.gcr.io/initfoo_spec",
8892
},
8993
},
@@ -133,6 +137,7 @@ func TestPodStore(t *testing.T) {
133137
Spec: v1.PodSpec{
134138
Containers: []v1.Container{
135139
{
140+
Name: "container1",
136141
Image: "k8s.gcr.io/hyperkube1_spec",
137142
},
138143
},
@@ -162,9 +167,11 @@ func TestPodStore(t *testing.T) {
162167
Spec: v1.PodSpec{
163168
Containers: []v1.Container{
164169
{
170+
Name: "container2",
165171
Image: "k8s.gcr.io/hyperkube2_spec",
166172
},
167173
{
174+
Name: "container3",
168175
Image: "k8s.gcr.io/hyperkube3_spec",
169176
},
170177
},
@@ -200,17 +207,21 @@ func TestPodStore(t *testing.T) {
200207
Spec: v1.PodSpec{
201208
Containers: []v1.Container{
202209
{
210+
Name: "container2",
203211
Image: "k8s.gcr.io/hyperkube2_spec",
204212
},
205213
{
214+
Name: "container3",
206215
Image: "k8s.gcr.io/hyperkube3_spec",
207216
},
208217
},
209218
InitContainers: []v1.Container{
210219
{
220+
Name: "initcontainer1",
211221
Image: "k8s.gcr.io/initfoo_spec",
212222
},
213223
{
224+
Name: "initcontainer2",
214225
Image: "k8s.gcr.io/initfoo_spec",
215226
},
216227
},
@@ -256,6 +267,7 @@ func TestPodStore(t *testing.T) {
256267
Spec: v1.PodSpec{
257268
Containers: []v1.Container{
258269
{
270+
Name: "container1",
259271
Image: "k8s.gcr.io/hyperkube1_spec",
260272
},
261273
},
@@ -286,6 +298,7 @@ func TestPodStore(t *testing.T) {
286298
Spec: v1.PodSpec{
287299
InitContainers: []v1.Container{
288300
{
301+
Name: "initcontainer1",
289302
Image: "k8s.gcr.io/initfoo_spec",
290303
},
291304
},
@@ -316,9 +329,11 @@ func TestPodStore(t *testing.T) {
316329
Spec: v1.PodSpec{
317330
Containers: []v1.Container{
318331
{
332+
Name: "container2",
319333
Image: "k8s.gcr.io/hyperkube2_spec",
320334
},
321335
{
336+
Name: "container3",
322337
Image: "k8s.gcr.io/hyperkube3_spec",
323338
},
324339
},
@@ -354,9 +369,11 @@ func TestPodStore(t *testing.T) {
354369
Spec: v1.PodSpec{
355370
InitContainers: []v1.Container{
356371
{
372+
Name: "initcontainer2",
357373
Image: "k8s.gcr.io/initfoo_spec",
358374
},
359375
{
376+
Name: "initcontainer3",
360377
Image: "k8s.gcr.io/initfoo_spec",
361378
},
362379
},
@@ -392,11 +409,13 @@ func TestPodStore(t *testing.T) {
392409
Spec: v1.PodSpec{
393410
Containers: []v1.Container{
394411
{
412+
Name: "container1",
395413
Image: "k8s.gcr.io/hyperkube2_spec",
396414
},
397415
},
398416
InitContainers: []v1.Container{
399417
{
418+
Name: "initcontainer1",
400419
Image: "k8s.gcr.io/initfoo_spec",
401420
},
402421
},
@@ -478,6 +497,7 @@ func TestPodStore(t *testing.T) {
478497
Spec: v1.PodSpec{
479498
Containers: []v1.Container{
480499
{
500+
Name: "container1",
481501
Image: "k8s.gcr.io/hyperkube1_spec",
482502
},
483503
},
@@ -535,9 +555,11 @@ func TestPodStore(t *testing.T) {
535555
Spec: v1.PodSpec{
536556
Containers: []v1.Container{
537557
{
558+
Name: "container2",
538559
Image: "k8s.gcr.io/hyperkube2_spec",
539560
},
540561
{
562+
Name: "container3",
541563
Image: "k8s.gcr.io/hyperkube3_spec",
542564
},
543565
},
@@ -607,6 +629,7 @@ func TestPodStore(t *testing.T) {
607629
Spec: v1.PodSpec{
608630
Containers: []v1.Container{
609631
{
632+
Name: "container4",
610633
Image: "k8s.gcr.io/hyperkube4_spec",
611634
},
612635
},
@@ -668,6 +691,7 @@ func TestPodStore(t *testing.T) {
668691
Spec: v1.PodSpec{
669692
Containers: []v1.Container{
670693
{
694+
Name: "container7",
671695
Image: "k8s.gcr.io/hyperkube7_spec",
672696
},
673697
},
@@ -732,6 +756,7 @@ func TestPodStore(t *testing.T) {
732756
Spec: v1.PodSpec{
733757
Containers: []v1.Container{
734758
{
759+
Name: "container7",
735760
Image: "k8s.gcr.io/hyperkube7_spec",
736761
},
737762
},
@@ -796,6 +821,7 @@ func TestPodStore(t *testing.T) {
796821
Spec: v1.PodSpec{
797822
Containers: []v1.Container{
798823
{
824+
Name: "container5",
799825
Image: "k8s.gcr.io/hyperkube5_spec",
800826
},
801827
},
@@ -847,6 +873,7 @@ func TestPodStore(t *testing.T) {
847873
Spec: v1.PodSpec{
848874
Containers: []v1.Container{
849875
{
876+
Name: "container6",
850877
Image: "k8s.gcr.io/hyperkube1_spec",
851878
},
852879
},
@@ -898,6 +925,7 @@ func TestPodStore(t *testing.T) {
898925
Spec: v1.PodSpec{
899926
Containers: []v1.Container{
900927
{
928+
Name: "container8",
901929
Image: "k8s.gcr.io/hyperkube1_spec",
902930
},
903931
},
@@ -1063,12 +1091,15 @@ func TestPodStore(t *testing.T) {
10631091
NodeName: "node2",
10641092
Containers: []v1.Container{
10651093
{
1094+
Name: "container2_1",
10661095
Image: "k8s.gcr.io/hyperkube2_spec",
10671096
},
10681097
{
1098+
Name: "container2_2",
10691099
Image: "k8s.gcr.io/hyperkube2_spec",
10701100
},
10711101
{
1102+
Name: "container2_3",
10721103
Image: "k8s.gcr.io/hyperkube2_spec",
10731104
},
10741105
},
@@ -1908,12 +1939,15 @@ func BenchmarkPodStore(b *testing.B) {
19081939
Spec: v1.PodSpec{
19091940
Containers: []v1.Container{
19101941
{
1942+
Name: "container1",
19111943
Image: "k8s.gcr.io/hyperkube1_spec",
19121944
},
19131945
{
1946+
Name: "container2",
19141947
Image: "k8s.gcr.io/hyperkube1_spec",
19151948
},
19161949
{
1950+
Name: "container3",
19171951
Image: "k8s.gcr.io/hyperkube1_spec",
19181952
},
19191953
},
@@ -1937,7 +1971,7 @@ func BenchmarkPodStore(b *testing.B) {
19371971
},
19381972
},
19391973
{
1940-
Name: "container1",
1974+
Name: "container2",
19411975
Image: "k8s.gcr.io/hyperkube1",
19421976
ImageID: "docker://sha256:aaa",
19431977
ContainerID: "docker://ab123",
@@ -1953,7 +1987,7 @@ func BenchmarkPodStore(b *testing.B) {
19531987
},
19541988
},
19551989
{
1956-
Name: "container1",
1990+
Name: "container3",
19571991
Image: "k8s.gcr.io/hyperkube1",
19581992
ImageID: "docker://sha256:aaa",
19591993
ContainerID: "docker://ab123",

pkg/app/server_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ func TestFullScrapeCycle(t *testing.T) {
277277
# TYPE kube_pod_status_scheduled_time gauge
278278
# TYPE kube_pod_status_unschedulable gauge
279279
kube_pod_annotations{namespace="default",pod="pod0",uid="abc-0"} 1
280-
kube_pod_container_info{namespace="default",pod="pod0",uid="abc-0",container="container2",image_spec="k8s.gcr.io/hyperkube2_spec",image="k8s.gcr.io/hyperkube2",image_id="docker://sha256:bbb",container_id="docker://cd456"} 1
281-
kube_pod_container_info{namespace="default",pod="pod0",uid="abc-0",container="container3",image_spec="k8s.gcr.io/hyperkube3_spec",image="k8s.gcr.io/hyperkube3",image_id="docker://sha256:ccc",container_id="docker://ef789"} 1
280+
kube_pod_container_info{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",image_spec="k8s.gcr.io/hyperkube2_spec",image="k8s.gcr.io/hyperkube2",image_id="docker://sha256:bbb",container_id="docker://cd456"} 1
281+
kube_pod_container_info{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2",image_spec="k8s.gcr.io/hyperkube3_spec",image="k8s.gcr.io/hyperkube3",image_id="docker://sha256:ccc",container_id="docker://ef789"} 1
282282
kube_pod_container_resource_limits{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",node="node1",resource="cpu",unit="core"} 0.2
283283
kube_pod_container_resource_limits{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",node="node1",resource="ephemeral_storage",unit="byte"} 3e+08
284284
kube_pod_container_resource_limits{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",node="node1",resource="memory",unit="byte"} 1e+08
@@ -293,18 +293,18 @@ kube_pod_container_resource_requests{namespace="default",pod="pod0",uid="abc-0",
293293
kube_pod_container_resource_requests{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",node="node1",resource="storage",unit="byte"} 4e+08
294294
kube_pod_container_resource_requests{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2",node="node1",resource="cpu",unit="core"} 0.3
295295
kube_pod_container_resource_requests{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2",node="node1",resource="memory",unit="byte"} 2e+08
296-
kube_pod_container_status_last_terminated_reason{namespace="default",pod="pod0",uid="abc-0",container="container2",reason="OOMKilled"} 1
297-
kube_pod_container_status_ready{namespace="default",pod="pod0",uid="abc-0",container="container2"} 0
298-
kube_pod_container_status_ready{namespace="default",pod="pod0",uid="abc-0",container="container3"} 0
299-
kube_pod_container_status_restarts_total{namespace="default",pod="pod0",uid="abc-0",container="container2"} 0
300-
kube_pod_container_status_restarts_total{namespace="default",pod="pod0",uid="abc-0",container="container3"} 0
301-
kube_pod_container_status_running{namespace="default",pod="pod0",uid="abc-0",container="container2"} 0
302-
kube_pod_container_status_running{namespace="default",pod="pod0",uid="abc-0",container="container3"} 0
303-
kube_pod_container_status_terminated{namespace="default",pod="pod0",uid="abc-0",container="container2"} 0
304-
kube_pod_container_status_terminated{namespace="default",pod="pod0",uid="abc-0",container="container3"} 0
305-
kube_pod_container_status_waiting_reason{namespace="default",pod="pod0",uid="abc-0",container="container2",reason="CrashLoopBackOff"} 1
306-
kube_pod_container_status_waiting{namespace="default",pod="pod0",uid="abc-0",container="container2"} 1
307-
kube_pod_container_status_waiting{namespace="default",pod="pod0",uid="abc-0",container="container3"} 0
296+
kube_pod_container_status_last_terminated_reason{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",reason="OOMKilled"} 1
297+
kube_pod_container_status_ready{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1"} 0
298+
kube_pod_container_status_ready{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2"} 0
299+
kube_pod_container_status_restarts_total{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1"} 0
300+
kube_pod_container_status_restarts_total{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2"} 0
301+
kube_pod_container_status_running{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1"} 0
302+
kube_pod_container_status_running{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2"} 0
303+
kube_pod_container_status_terminated{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1"} 0
304+
kube_pod_container_status_terminated{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2"} 0
305+
kube_pod_container_status_waiting_reason{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1",reason="CrashLoopBackOff"} 1
306+
kube_pod_container_status_waiting{namespace="default",pod="pod0",uid="abc-0",container="pod1_con1"} 1
307+
kube_pod_container_status_waiting{namespace="default",pod="pod0",uid="abc-0",container="pod1_con2"} 0
308308
kube_pod_created{namespace="default",pod="pod0",uid="abc-0"} 1.5e+09
309309
kube_pod_info{namespace="default",pod="pod0",uid="abc-0",host_ip="1.1.1.1",pod_ip="1.2.3.4",node="node1",created_by_kind="<none>",created_by_name="<none>",priority_class="",host_network="false"} 1
310310
kube_pod_labels{namespace="default",pod="pod0",uid="abc-0"} 1
@@ -779,7 +779,7 @@ func pod(client *fake.Clientset, index int) error {
779779
Phase: v1.PodRunning,
780780
ContainerStatuses: []v1.ContainerStatus{
781781
{
782-
Name: "container2",
782+
Name: "pod1_con1",
783783
Image: "k8s.gcr.io/hyperkube2",
784784
ImageID: "docker://sha256:bbb",
785785
ContainerID: "docker://cd456",
@@ -795,7 +795,7 @@ func pod(client *fake.Clientset, index int) error {
795795
},
796796
},
797797
{
798-
Name: "container3",
798+
Name: "pod1_con2",
799799
Image: "k8s.gcr.io/hyperkube3",
800800
ImageID: "docker://sha256:ccc",
801801
ContainerID: "docker://ef789",

0 commit comments

Comments
 (0)