-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainers.go
90 lines (71 loc) · 1.79 KB
/
containers.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 main
import (
"fmt"
"strings"
"time"
"github.com/docker/docker/api/types"
)
type Container struct {
Changed time.Time
Status string
Name string
}
type Containers map[string]Container
// Add a container to the list of monitored containers.
func (c *Containers) Add(id string, new Container) {
// Is container already in containers?
existing, ok := (*c)[id]
if !ok {
(*c)[id] = new
return
}
// This is an old state (we know a newer state). Ignore and
// return.
if existing.Changed.After(new.Changed) {
return
}
// Remember the new state.
(*c)[id] = new
}
// Healthy if all containers are healthy.
func (c Containers) Healthy() bool {
for _, container := range c {
if container.Status != types.Healthy && container.Status != types.NoHealthcheck {
return false
}
}
return true
}
// Unhealthy if one of the containers get unhealthy.
func (c Containers) Unhealthy() error {
for _, container := range c {
if container.Status == types.Unhealthy {
return fmt.Errorf(
"%w: %s",
unhealthyError,
strings.Join(c.UnhealtyContainers(), ", "),
)
}
}
return nil
}
// NonHealtyContainers returns a list of container names of containers that are not healthy (yet).
func (c Containers) NonHealtyContainers() []string {
var nonHealthy []string
for _, container := range c {
if container.Status != types.Healthy && container.Status != types.NoHealthcheck {
nonHealthy = append(nonHealthy, container.Name)
}
}
return nonHealthy
}
// UnhealtyContainers returns a list of container names of containers that are not healthy (yet).
func (c Containers) UnhealtyContainers() []string {
var unhealthy []string
for _, container := range c {
if container.Status == types.Unhealthy {
unhealthy = append(unhealthy, container.Name)
}
}
return unhealthy
}