/
container-message.go
48 lines (44 loc) · 1.19 KB
/
container-message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package operator
import (
corev1 "k8s.io/api/core/v1"
)
type ContainerMessage struct {
State string `json:"state,omitempty"`
Pod string `json:"pod,omitempty"`
Container string `json:"container,omitempty"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
ExitCode int32 `json:"exitCode,omitempty"`
}
func GetMessagesFromPods(pods ...corev1.Pod) []ContainerMessage {
cMsgs := make([]ContainerMessage, 0, len(pods))
for i := range pods {
for j := range pods[i].Status.ContainerStatuses {
st := pods[i].Status.ContainerStatuses[j]
if st.State.Terminated != nil {
cMsgs = append(
cMsgs, ContainerMessage{
Pod: pods[i].Name,
Container: st.Name,
State: "terminated",
Reason: st.State.Terminated.Reason,
Message: st.State.Terminated.Message,
ExitCode: st.State.Terminated.ExitCode,
},
)
}
if st.State.Waiting != nil {
cMsgs = append(
cMsgs, ContainerMessage{
Pod: pods[i].Name,
Container: st.Name,
State: "waiting",
Reason: st.State.Waiting.Reason,
Message: st.State.Waiting.Message,
},
)
}
}
}
return cMsgs
}