Skip to content

Commit 07557c7

Browse files
committed
feat(watcher): Include failed containers in message
1 parent 45b0f20 commit 07557c7

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

watcher/main.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,27 @@ func handleUpdate(obj interface{}, newObj interface{}) {
142142
buf := new(strings.Builder)
143143
buf.WriteString("\n")
144144

145-
buf.WriteString("*Failed Pods*\n\n")
146-
147145
for _, pod := range pods {
146+
// Ignore non-failed pods
148147
if pod.Status.Phase != corev1.PodFailed {
149148
continue
150149
}
150+
151151
logs, _ := getPodLogs(pod)
152152
fmt.Fprintf(buf, "Pod `%s`\n```\n%s\n```\n\n", pod.Name, logs)
153153

154+
// Log all terminated containers
155+
for _, cstate := range pod.Status.ContainerStatuses {
156+
containerId := cstate.Name
157+
exitCode := cstate.State.Terminated.ExitCode
158+
reason := cstate.State.Terminated.Reason
159+
time := cstate.State.Terminated.FinishedAt
160+
fmt.Fprintf(buf, "Container `%s` exited at %s with code `%d` (reason `%s`)\n", containerId, time, exitCode, reason)
161+
}
154162
}
155163
body = buf.String()
156164
} else {
157-
subject = "Job completed"
165+
subject = "Job failed"
158166
body = formatJob(job, false)
159167
}
160168

@@ -202,7 +210,7 @@ func getPodLogs(pod corev1.Pod) (string, error) {
202210
if err != nil {
203211
return "", nil
204212
}
205-
return buf.String(), nil
213+
return strings.TrimSpace(buf.String()), nil
206214
}
207215

208216
func collectJobOutputs(job *batchv1.Job) (map[string]string, error) {

0 commit comments

Comments
 (0)