/
status_extensions.go
64 lines (59 loc) · 1.33 KB
/
status_extensions.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
package core
import (
"fmt"
"sort"
"github.com/golang/protobuf/proto"
)
// collapse a status into a status with no children
func (s Status) Flatten() Status {
if len(s.SubresourceStatuses) == 0 {
return s
}
out := Status{
State: s.State,
Reason: s.Reason,
}
orderedMapIterator(s.SubresourceStatuses, func(key string, stat *Status) {
status := stat.Flatten()
switch status.State {
case Status_Rejected:
out.State = Status_Rejected
out.Reason += key + fmt.Sprintf("child %v rejected with reason: %v.\n", key, status.Reason)
case Status_Pending:
if out.State == Status_Accepted {
out.State = Status_Pending
}
out.Reason += key + " is still pending.\n"
}
})
return out
}
func (s Status) DeepCopyInto(out *Status) {
clone := proto.Clone(&s).(*Status)
*out = *clone
}
func (s NamespacedStatuses) DeepCopyInto(out *NamespacedStatuses) {
clone := proto.Clone(&s).(*NamespacedStatuses)
*out = *clone
}
func orderedMapIterator(m map[string]*Status, onKey func(key string, value *Status)) {
var list []struct {
key string
value *Status
}
for k, v := range m {
list = append(list, struct {
key string
value *Status
}{
key: k,
value: v,
})
}
sort.SliceStable(list, func(i, j int) bool {
return list[i].key < list[j].key
})
for _, el := range list {
onKey(el.key, el.value)
}
}