diff --git a/pkg/gather/collect/collect.go b/pkg/gather/collect/collect.go index 4b8c8c8aca1..381764b6c72 100644 --- a/pkg/gather/collect/collect.go +++ b/pkg/gather/collect/collect.go @@ -237,14 +237,14 @@ func retrieveContainerLogs(ctx context.Context, podClient corev1client.PodInterf return nil } -func (c *Collector) collectContainerLogs(ctx context.Context, logsDir string, pod *corev1.Pod, container *corev1.Container) error { +func (c *Collector) collectContainerLogs(ctx context.Context, logsDir string, podMeta *metav1.ObjectMeta, podCSs []corev1.ContainerStatus, containerName string) error { var err error - cs, _, found := slices.Find(pod.Status.ContainerStatuses, func(s corev1.ContainerStatus) bool { - return s.Name == container.Name + cs, _, found := slices.Find(podCSs, func(s corev1.ContainerStatus) bool { + return s.Name == containerName }) if !found { - klog.InfoS("Container doesn't yet have a status", "Pod", naming.ObjRef(pod), "Container", container.Name) + klog.InfoS("Container doesn't yet have a status", "Pod", naming.ObjRef(podMeta), "Container", containerName) return nil } @@ -254,7 +254,7 @@ func (c *Collector) collectContainerLogs(ctx context.Context, logsDir string, po } logOptions := &corev1.PodLogOptions{ - Container: container.Name, + Container: containerName, Timestamps: true, Follow: false, LimitBytes: limitBytes, @@ -267,17 +267,17 @@ func (c *Collector) collectContainerLogs(ctx context.Context, logsDir string, po if cs.State.Running != nil { // Retrieve current logs. logOptions.Previous = false - err = retrieveContainerLogs(ctx, c.corev1Client.Pods(pod.Namespace), filepath.Join(logsDir, container.Name+".current"), pod.Name, logOptions) + err = retrieveContainerLogs(ctx, c.corev1Client.Pods(podMeta.Namespace), filepath.Join(logsDir, containerName+".current"), podMeta.Name, logOptions) if err != nil { - return fmt.Errorf("can't retrieve pod logs for container %q in pod %q: %w", container.Name, naming.ObjRef(pod), err) + return fmt.Errorf("can't retrieve pod logs for container %q in pod %q: %w", containerName, naming.ObjRef(podMeta), err) } } if cs.LastTerminationState.Terminated != nil { logOptions.Previous = true - err = retrieveContainerLogs(ctx, c.corev1Client.Pods(pod.Namespace), filepath.Join(logsDir, container.Name+".previous"), pod.Name, logOptions) + err = retrieveContainerLogs(ctx, c.corev1Client.Pods(podMeta.Namespace), filepath.Join(logsDir, containerName+".previous"), podMeta.Name, logOptions) if err != nil { - return fmt.Errorf("can't retrieve previous pod logs for container %q in pod %q: %w", container.Name, naming.ObjRef(pod), err) + return fmt.Errorf("can't retrieve previous pod logs for container %q in pod %q: %w", containerName, naming.ObjRef(podMeta), err) } } @@ -308,19 +308,26 @@ func (c *Collector) collectPod(ctx context.Context, u *unstructured.Unstructured } for _, container := range pod.Spec.InitContainers { - err = c.collectContainerLogs(ctx, logsDir, pod, &container) + err = c.collectContainerLogs(ctx, logsDir, &pod.ObjectMeta, pod.Status.InitContainerStatuses, container.Name) if err != nil { return fmt.Errorf("can't collect logs for init container %q in pod %q: %w", container.Name, naming.ObjRef(pod), err) } } for _, container := range pod.Spec.Containers { - err = c.collectContainerLogs(ctx, logsDir, pod, &container) + err = c.collectContainerLogs(ctx, logsDir, &pod.ObjectMeta, pod.Status.ContainerStatuses, container.Name) if err != nil { return fmt.Errorf("can't collect logs for container %q in pod %q: %w", container.Name, naming.ObjRef(pod), err) } } + for _, container := range pod.Spec.EphemeralContainers { + err = c.collectContainerLogs(ctx, logsDir, &pod.ObjectMeta, pod.Status.EphemeralContainerStatuses, container.Name) + if err != nil { + return fmt.Errorf("can't collect logs for ephemeral container %q in pod %q: %w", container.Name, naming.ObjRef(pod), err) + } + } + return nil } diff --git a/pkg/gather/collect/collect_test.go b/pkg/gather/collect/collect_test.go index f3d7af2573a..181cc12f3c7 100644 --- a/pkg/gather/collect/collect_test.go +++ b/pkg/gather/collect/collect_test.go @@ -160,13 +160,34 @@ status: Name: "my-pod", }, Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "my-init-container", + }, + }, Containers: []corev1.Container{ { Name: "my-container", }, }, + EphemeralContainers: []corev1.EphemeralContainer{ + { + EphemeralContainerCommon: corev1.EphemeralContainerCommon{ + Name: "my-ephemeral-container", + }, + }, + }, }, Status: corev1.PodStatus{ + InitContainerStatuses: []corev1.ContainerStatus{ + { + Name: "my-init-container", + State: corev1.ContainerState{ + Terminated: nil, + Running: &corev1.ContainerStateRunning{}, + }, + }, + }, ContainerStatuses: []corev1.ContainerStatus{ { Name: "my-container", @@ -176,6 +197,15 @@ status: }, }, }, + EphemeralContainerStatuses: []corev1.ContainerStatus{ + { + Name: "my-ephemeral-container", + State: corev1.ContainerState{ + Terminated: nil, + Running: &corev1.ContainerStateRunning{}, + }, + }, + }, }, }, existingObjects: nil, @@ -198,6 +228,12 @@ spec: containers: - name: my-container resources: {} + ephemeralContainers: + - name: my-ephemeral-container + resources: {} + initContainers: + - name: my-init-container + resources: {} status: containerStatuses: - image: "" @@ -209,12 +245,40 @@ status: state: running: startedAt: null + ephemeralContainerStatuses: + - image: "" + imageID: "" + lastState: {} + name: my-ephemeral-container + ready: false + restartCount: 0 + state: + running: + startedAt: null + initContainerStatuses: + - image: "" + imageID: "" + lastState: {} + name: my-init-container + ready: false + restartCount: 0 + state: + running: + startedAt: null `, "\n"), }, { Name: "namespaces/test/pods/my-pod/my-container.current", Content: "fake logs", }, + { + Name: "namespaces/test/pods/my-pod/my-ephemeral-container.current", + Content: "fake logs", + }, + { + Name: "namespaces/test/pods/my-pod/my-init-container.current", + Content: "fake logs", + }, }, }, }, @@ -226,11 +290,23 @@ status: Name: "my-pod", }, Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "my-init-container", + }, + }, Containers: []corev1.Container{ { Name: "my-container", }, }, + EphemeralContainers: []corev1.EphemeralContainer{ + { + EphemeralContainerCommon: corev1.EphemeralContainerCommon{ + Name: "my-ephemeral-container", + }, + }, + }, }, Status: corev1.PodStatus{ ContainerStatuses: []corev1.ContainerStatus{ @@ -244,6 +320,28 @@ status: }, }, }, + InitContainerStatuses: []corev1.ContainerStatus{ + { + Name: "my-init-container", + State: corev1.ContainerState{ + Running: &corev1.ContainerStateRunning{}, + }, + LastTerminationState: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{}, + }, + }, + }, + EphemeralContainerStatuses: []corev1.ContainerStatus{ + { + Name: "my-ephemeral-container", + State: corev1.ContainerState{ + Running: &corev1.ContainerStateRunning{}, + }, + LastTerminationState: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{}, + }, + }, + }, }, }, existingObjects: nil, @@ -266,6 +364,12 @@ spec: containers: - name: my-container resources: {} + ephemeralContainers: + - name: my-ephemeral-container + resources: {} + initContainers: + - name: my-init-container + resources: {} status: containerStatuses: - image: "" @@ -281,6 +385,34 @@ status: state: running: startedAt: null + ephemeralContainerStatuses: + - image: "" + imageID: "" + lastState: + terminated: + exitCode: 0 + finishedAt: null + startedAt: null + name: my-ephemeral-container + ready: false + restartCount: 0 + state: + running: + startedAt: null + initContainerStatuses: + - image: "" + imageID: "" + lastState: + terminated: + exitCode: 0 + finishedAt: null + startedAt: null + name: my-init-container + ready: false + restartCount: 0 + state: + running: + startedAt: null `, "\n"), }, { @@ -291,6 +423,22 @@ status: Name: "namespaces/test/pods/my-pod/my-container.previous", Content: "fake logs", }, + { + Name: "namespaces/test/pods/my-pod/my-ephemeral-container.current", + Content: "fake logs", + }, + { + Name: "namespaces/test/pods/my-pod/my-ephemeral-container.previous", + Content: "fake logs", + }, + { + Name: "namespaces/test/pods/my-pod/my-init-container.current", + Content: "fake logs", + }, + { + Name: "namespaces/test/pods/my-pod/my-init-container.previous", + Content: "fake logs", + }, }, }, },