Skip to content

Commit 450ba6f

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

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

watcher/main.go

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

145-
buf.WriteString("*Failed Pods*\n\n")
146-
147-
for _, pod := range pods {
145+
for idx, pod := range pods {
146+
// Ignore non-failed pods
148147
if pod.Status.Phase != corev1.PodFailed {
149148
continue
150149
}
150+
if idx > 0 {
151+
fmt.Fprintln(buf, strings.Repeat("-", 80))
152+
}
153+
151154
logs, _ := getPodLogs(pod)
152-
fmt.Fprintf(buf, "Pod `%s`\n```\n%s\n```\n\n", pod.Name, logs)
155+
fmt.Fprintf(buf, "*Pod `%s`*\n\nLogs:\n\n```\n%s\n```\n\n", pod.Name, logs)
156+
157+
// Log all terminated containers
158+
fmt.Fprint(buf, "Failed containers:\n\n")
159+
for _, cstate := range pod.Status.ContainerStatuses {
160+
reason := cstate.State.Terminated.Reason
161+
if reason != "Error" {
162+
continue
163+
}
153164

165+
containerId := cstate.Name
166+
exitCode := cstate.State.Terminated.ExitCode
167+
time := cstate.State.Terminated.FinishedAt
168+
169+
fmt.Fprintf(buf, "· `%s`, exited <!date^%d^{date_short_pretty} {time_secs}|at %s>, exit code %d\n\n", containerId, time.Unix(), time, exitCode)
170+
}
154171
}
155172
body = buf.String()
156173
} else {
157-
subject = "Job completed"
174+
subject = "Job failed"
158175
body = formatJob(job, false)
159176
}
160177

@@ -202,7 +219,7 @@ func getPodLogs(pod corev1.Pod) (string, error) {
202219
if err != nil {
203220
return "", nil
204221
}
205-
return buf.String(), nil
222+
return strings.TrimSpace(buf.String()), nil
206223
}
207224

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

0 commit comments

Comments
 (0)