Skip to content

Commit 406e1e6

Browse files
authored
Merge pull request #168 from replicatedhq/laverya/collect-logs-from-all-containers-in-pod
collect logs from all containers in a pod if no container is specified
2 parents 4dfb02e + cbaf966 commit 406e1e6

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

pkg/collect/logs.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,34 @@ func Logs(ctx *Context, logsCollector *troubleshootv1beta1.Logs) ([]byte, error)
3838
if len(pods) > 0 {
3939
for _, pod := range pods {
4040
if len(logsCollector.ContainerNames) == 0 {
41-
podLogs, err := getPodLogs(client, pod, logsCollector.Name, "", logsCollector.Limits, false)
42-
if err != nil {
43-
key := fmt.Sprintf("%s/%s-errors.json", logsCollector.Name, pod.Name)
44-
logsOutput[key], err = marshalNonNil([]string{err.Error()})
41+
// make a list of all the containers in the pod, so that we can get logs from all of them
42+
containerNames := []string{}
43+
for _, container := range pod.Spec.Containers {
44+
containerNames = append(containerNames, container.Name)
45+
}
46+
for _, container := range pod.Spec.InitContainers {
47+
containerNames = append(containerNames, container.Name)
48+
}
49+
50+
for _, containerName := range containerNames {
51+
if len(containerNames) == 1 {
52+
containerName = "" // if there was only one container, use the old behavior of not including the container name in the path
53+
}
54+
podLogs, err := getPodLogs(client, pod, logsCollector.Name, containerName, logsCollector.Limits, false)
4555
if err != nil {
46-
return nil, err
56+
key := fmt.Sprintf("%s/%s-errors.json", logsCollector.Name, pod.Name)
57+
if containerName != "" {
58+
key = fmt.Sprintf("%s/%s/%s-errors.json", logsCollector.Name, pod.Name, containerName)
59+
}
60+
logsOutput[key], err = marshalNonNil([]string{err.Error()})
61+
if err != nil {
62+
return nil, err
63+
}
64+
continue
65+
}
66+
for k, v := range podLogs {
67+
logsOutput[k] = v
4768
}
48-
continue
49-
}
50-
for k, v := range podLogs {
51-
logsOutput[k] = v
5269
}
5370
} else {
5471
for _, container := range logsCollector.ContainerNames {

0 commit comments

Comments
 (0)