This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
replicas.go
95 lines (80 loc) · 2.89 KB
/
replicas.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package appstatus
import (
"sort"
"strconv"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/labels"
corev1 "k8s.io/api/core/v1"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (a *appStatusRenderer) getReplicasSummary(nameLabel string) (map[string]v1.ReplicasSummary, error) {
var (
pods = &corev1.PodList{}
result = map[string]v1.ReplicasSummary{}
)
sel := klabels.SelectorFromSet(map[string]string{
labels.AcornManaged: "true",
labels.AcornAppName: a.app.Name,
})
hasNameLabel, err := klabels.NewRequirement(nameLabel, selection.Exists, nil)
if err != nil {
return nil, err
}
err = a.c.List(a.ctx, pods, &kclient.ListOptions{
Namespace: a.app.Status.Namespace,
LabelSelector: sel.Add(*hasNameLabel),
})
if err != nil {
return nil, err
}
sort.Slice(pods.Items, func(i, j int) bool {
return pods.Items[i].CreationTimestamp.Before(&pods.Items[j].CreationTimestamp)
})
for _, pod := range pods.Items {
var summary v1.ReplicasSummary
for _, cond := range pod.Status.Conditions {
if cond.Type == corev1.PodScheduled {
if cond.Status != corev1.ConditionTrue {
summary.TransitioningMessages = append(summary.TransitioningMessages, podName(&pod)+" is not scheduled to a node")
}
}
}
transition, errored := containerMessages(&pod, append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...))
summary.TransitioningMessages = append(summary.TransitioningMessages, transition...)
summary.ErrorMessages = append(summary.ErrorMessages, errored...)
for _, status := range append(pod.Status.InitContainerStatuses, pod.Status.ContainerStatuses...) {
if status.RestartCount > summary.MaxReplicaRestartCount {
summary.MaxReplicaRestartCount = status.RestartCount
}
}
result[pod.Labels[nameLabel]] = summary
}
return result, nil
}
func containerMessages(pod *corev1.Pod, status []corev1.ContainerStatus) (transitionMessages, errorMessages []string) {
for _, container := range status {
if container.State.Waiting != nil && container.State.Waiting.Reason != "" {
if container.State.Waiting.Message == "" {
transitionMessages = append(transitionMessages, podName(pod)+" "+
container.State.Waiting.Reason)
} else {
transitionMessages = append(transitionMessages, podName(pod)+" "+
container.State.Waiting.Reason+": "+container.State.Waiting.Message)
}
}
if container.State.Terminated != nil && container.State.Terminated.ExitCode > 0 {
errorMessages = append(errorMessages, podName(pod)+" "+container.State.Terminated.Reason+": Exit Code "+
strconv.Itoa(int(container.State.Terminated.ExitCode)))
}
}
return
}
func podName(pod *corev1.Pod) string {
jobName := pod.Labels[labels.AcornJobName]
if jobName != "" {
return jobName
}
return pod.Labels[labels.AcornContainerName]
}