From af1873be04e8acd8a5e824d1acc5f3c5f2e1c949 Mon Sep 17 00:00:00 2001 From: EsmeAR Date: Mon, 20 Jun 2022 13:39:23 +0100 Subject: [PATCH] Emit exit codes of console containers when terminating As part of the ConsoleTerminatedEvent messages to Pub/Sub the workloads controller will also emit the exit codes of console containers when terminating. --- apis/workloads/v1alpha1/lifecycle_recorder.go | 15 +++++++++------ pkg/workloads/console/events/events.go | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/apis/workloads/v1alpha1/lifecycle_recorder.go b/apis/workloads/v1alpha1/lifecycle_recorder.go index 1d79125e..8926b36b 100644 --- a/apis/workloads/v1alpha1/lifecycle_recorder.go +++ b/apis/workloads/v1alpha1/lifecycle_recorder.go @@ -197,10 +197,11 @@ func (l *lifecycleEventRecorderImpl) ConsoleAttach(ctx context.Context, csl *Con func (l *lifecycleEventRecorderImpl) ConsoleTerminate(ctx context.Context, csl *Console, timedOut bool, pod *corev1.Pod) error { containerStatuses := make(map[string]string) + exitCodes := make(map[string]int32) if pod != nil { - appendStatusMessages(containerStatuses, pod.Status.InitContainerStatuses) - appendStatusMessages(containerStatuses, pod.Status.ContainerStatuses) - appendStatusMessages(containerStatuses, pod.Status.EphemeralContainerStatuses) + appendStatusMessages(containerStatuses, exitCodes, pod.Status.InitContainerStatuses) + appendStatusMessages(containerStatuses, exitCodes, pod.Status.ContainerStatuses) + appendStatusMessages(containerStatuses, exitCodes, pod.Status.EphemeralContainerStatuses) } event := &events.ConsoleTerminatedEvent{ @@ -208,6 +209,7 @@ func (l *lifecycleEventRecorderImpl) ConsoleTerminate(ctx context.Context, csl * Spec: events.ConsoleTerminatedSpec{ TimedOut: timedOut, ContainerStatuses: containerStatuses, + ExitCodes: exitCodes, }, } @@ -222,7 +224,7 @@ func (l *lifecycleEventRecorderImpl) ConsoleTerminate(ctx context.Context, csl * return nil } -func appendStatusMessages(result map[string]string, containerStatuses []corev1.ContainerStatus) { +func appendStatusMessages(containerStatusResult map[string]string, exitCodeResult map[string]int32, containerStatuses []corev1.ContainerStatus) { if containerStatuses == nil { return } @@ -241,7 +243,8 @@ func appendStatusMessages(result map[string]string, containerStatuses []corev1.C if s.Message != "" { message.WriteString(fmt.Sprintf(". Message: %s", s.Message)) } - result[containerStatus.Name] = message.String() + containerStatusResult[containerStatus.Name] = message.String() + exitCodeResult[containerStatus.Name] = s.ExitCode } else if containerStatus.State.Waiting != nil { s := containerStatus.State.Waiting var message strings.Builder @@ -252,7 +255,7 @@ func appendStatusMessages(result map[string]string, containerStatuses []corev1.C if s.Message != "" { message.WriteString(fmt.Sprintf(" Message: %s", s.Message)) } - result[containerStatus.Name] = message.String() + containerStatusResult[containerStatus.Name] = message.String() } } } diff --git a/pkg/workloads/console/events/events.go b/pkg/workloads/console/events/events.go index 8ddef2e9..7ddc037e 100644 --- a/pkg/workloads/console/events/events.go +++ b/pkg/workloads/console/events/events.go @@ -85,6 +85,7 @@ type ConsoleAttachEvent struct { type ConsoleTerminatedSpec struct { TimedOut bool `json:"timed_out"` ContainerStatuses map[string]string `json:"container_statuses"` + ExitCodes map[string]int32 `json:"exit_codes"` } type ConsoleTerminatedEvent struct {