Skip to content

Commit 0e51a35

Browse files
Merge pull request #2999 from rzetelskik/collect-terminated-containers
Collect logs of terminated containers
2 parents 567177a + 8bf255e commit 0e51a35

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

pkg/gather/collect/collect_test.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,141 @@ status:
441441
},
442442
},
443443
},
444+
{
445+
name: "fetches pod logs from terminated containers",
446+
targetedObject: &corev1.Pod{
447+
ObjectMeta: metav1.ObjectMeta{
448+
Namespace: "test",
449+
Name: "my-pod",
450+
},
451+
Spec: corev1.PodSpec{
452+
InitContainers: []corev1.Container{
453+
{
454+
Name: "my-init-container",
455+
},
456+
},
457+
Containers: []corev1.Container{
458+
{
459+
Name: "my-container",
460+
},
461+
},
462+
EphemeralContainers: []corev1.EphemeralContainer{
463+
{
464+
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
465+
Name: "my-ephemeral-container",
466+
},
467+
},
468+
},
469+
},
470+
Status: corev1.PodStatus{
471+
InitContainerStatuses: []corev1.ContainerStatus{
472+
{
473+
Name: "my-init-container",
474+
State: corev1.ContainerState{
475+
Terminated: &corev1.ContainerStateTerminated{},
476+
Running: nil,
477+
},
478+
},
479+
},
480+
ContainerStatuses: []corev1.ContainerStatus{
481+
{
482+
Name: "my-container",
483+
State: corev1.ContainerState{
484+
Terminated: &corev1.ContainerStateTerminated{},
485+
Running: nil,
486+
},
487+
},
488+
},
489+
EphemeralContainerStatuses: []corev1.ContainerStatus{
490+
{
491+
Name: "my-ephemeral-container",
492+
State: corev1.ContainerState{
493+
Terminated: &corev1.ContainerStateTerminated{},
494+
Running: nil,
495+
},
496+
},
497+
},
498+
},
499+
},
500+
existingObjects: nil,
501+
relatedResources: false,
502+
keepGoing: false,
503+
expectedError: nil,
504+
expectedDump: &testhelpers.GatherDump{
505+
EmptyDirs: nil,
506+
Files: []testhelpers.File{
507+
{
508+
Name: "namespaces/test/pods/my-pod.yaml",
509+
Content: strings.TrimPrefix(`
510+
apiVersion: v1
511+
kind: Pod
512+
metadata:
513+
name: my-pod
514+
namespace: test
515+
spec:
516+
containers:
517+
- name: my-container
518+
resources: {}
519+
ephemeralContainers:
520+
- name: my-ephemeral-container
521+
resources: {}
522+
initContainers:
523+
- name: my-init-container
524+
resources: {}
525+
status:
526+
containerStatuses:
527+
- image: ""
528+
imageID: ""
529+
lastState: {}
530+
name: my-container
531+
ready: false
532+
restartCount: 0
533+
state:
534+
terminated:
535+
exitCode: 0
536+
finishedAt: null
537+
startedAt: null
538+
ephemeralContainerStatuses:
539+
- image: ""
540+
imageID: ""
541+
lastState: {}
542+
name: my-ephemeral-container
543+
ready: false
544+
restartCount: 0
545+
state:
546+
terminated:
547+
exitCode: 0
548+
finishedAt: null
549+
startedAt: null
550+
initContainerStatuses:
551+
- image: ""
552+
imageID: ""
553+
lastState: {}
554+
name: my-init-container
555+
ready: false
556+
restartCount: 0
557+
state:
558+
terminated:
559+
exitCode: 0
560+
finishedAt: null
561+
startedAt: null
562+
`, "\n"),
563+
},
564+
{
565+
Name: "namespaces/test/pods/my-pod/my-container.terminated",
566+
Content: "fake logs",
567+
},
568+
{
569+
Name: "namespaces/test/pods/my-pod/my-ephemeral-container.terminated",
570+
Content: "fake logs",
571+
},
572+
{
573+
Name: "namespaces/test/pods/my-pod/my-init-container.terminated",
574+
Content: "fake logs",
575+
},
576+
},
577+
},
578+
},
444579
{
445580
name: "namespace doesn't collect any extra resources if related resources are disabled",
446581
targetedObject: &corev1.Namespace{

pkg/gather/collect/podcollector.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ func (c *PodCollector) collectContainerLogs(ctx context.Context, logsDir string,
182182
}
183183
}
184184

185+
if cs.State.Terminated != nil {
186+
logOptions.Previous = false
187+
err = writeContainerLogsToFile(ctx, c.corev1Client.Pods(podMeta.Namespace), filepath.Join(logsDir, containerName+".terminated"), podMeta.Name, logOptions)
188+
if err != nil {
189+
return fmt.Errorf("can't retrieve pod logs for terminated container %q in pod %q: %w", containerName, naming.ObjRef(podMeta), err)
190+
}
191+
}
192+
185193
return nil
186194
}
187195

0 commit comments

Comments
 (0)