-
Notifications
You must be signed in to change notification settings - Fork 88
/
types.go
108 lines (93 loc) · 2.31 KB
/
types.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
96
97
98
99
100
101
102
103
104
105
106
107
108
package types
import (
"errors"
"regexp"
"time"
)
var (
StateReady State = "ready"
StateUpdating State = "updating"
StateDegraded State = "degraded"
StateUnavailable State = "unavailable"
StateMissing State = "missing"
StatusInformerRegexp = regexp.MustCompile(`^(?:([^\/]+)\/)?([^\/]+)\/([^\/]+)$`)
)
type StatusInformerString string
type StatusInformer struct {
Kind string
Name string
Namespace string
}
func (s StatusInformerString) Parse() (i StatusInformer, err error) {
matches := StatusInformerRegexp.FindStringSubmatch(string(s))
if len(matches) != 4 {
err = errors.New("status informer format string incorrect")
return
}
i.Namespace = matches[1]
i.Kind = matches[2]
i.Name = matches[3]
return
}
type AppStatus struct {
AppID string `json:"appId"`
ResourceStates ResourceStates `json:"resourceStates" hash:"set"`
UpdatedAt time.Time `json:"updatedAt" hash:"ignore"`
State State `json:"state"`
Sequence int64 `json:"sequence"`
}
type ResourceStates []ResourceState
type ResourceState struct {
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
State State `json:"state"`
}
type State string
func GetState(resourceStates []ResourceState) State {
if len(resourceStates) == 0 {
return StateMissing
}
max := StateReady
for _, resourceState := range resourceStates {
max = MinState(max, resourceState.State)
}
return max
}
func MinState(ss ...State) (min State) {
if len(ss) == 0 {
return StateMissing
}
for _, s := range ss {
if s == StateMissing || min == StateMissing {
return StateMissing
} else if s == StateUnavailable || min == StateUnavailable {
min = StateUnavailable
} else if s == StateDegraded || min == StateDegraded {
min = StateDegraded
} else if s == StateUpdating || min == StateUpdating {
min = StateUpdating
} else if s == StateReady || min == StateReady {
min = StateReady
}
}
return
}
func (a ResourceStates) Len() int {
return len(a)
}
func (a ResourceStates) Less(i, j int) bool {
if a[i].Kind < a[j].Kind {
return true
}
if a[i].Name < a[j].Name {
return true
}
if a[i].Namespace < a[j].Namespace {
return true
}
return false
}
func (a ResourceStates) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}