/
pod.go
90 lines (73 loc) · 1.87 KB
/
pod.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
package proc
import (
"github.com/MagalixCorp/magalix-agent/watcher"
karma "github.com/reconquest/karma-go"
)
// GetPodStatus a helper function to get the status of a pod
func GetPodStatus(pod Pod) watcher.Status {
context := karma.
Describe("application_id", pod.ApplicationID).
Describe("service_id", pod.ServiceID).
Describe("kubernetes/status", pod.Status.String())
if pod.Status == watcher.StatusTerminated {
debugf(
context,
"pod: %s (%s) status: %s",
pod.ID,
pod.Name,
pod.Status.String(),
)
return pod.Status
}
var running int
var pending int
var completed int
var errors int
for container, state := range pod.Containers {
// handle case when all container terminated
status, _ := GetContainerStateStatus(state)
switch {
case status == watcher.StatusRunning:
running++
case status == watcher.StatusPending:
pending++
case status == watcher.StatusCompleted:
completed++
case status == watcher.StatusUnknown:
warningf(
nil,
"container: %s unknown status, proceeding as error anyway",
container,
)
fallthrough
default:
errors++
}
}
total := len(pod.Containers)
context = context.
Describe("containers/running", running).
Describe("containers/pending", pending).
Describe("containers/completed", completed).
Describe("containers/errors", errors).
Describe("containers/total", total)
newStatus := pod.Status
switch {
case errors > 0:
newStatus = watcher.StatusError
case pending > 0:
newStatus = watcher.StatusPending
case completed == total && total > 0:
newStatus = watcher.StatusCompleted
case running == total && total > 0:
newStatus = watcher.StatusRunning
case running > 0 && completed > 0 && running+completed == total:
newStatus = watcher.StatusRunning
}
debugf(
context,
"pod: %s (%s) status: %s",
pod.ID, pod.Name, newStatus.String(),
)
return newStatus
}