-
Notifications
You must be signed in to change notification settings - Fork 301
/
summary.go
95 lines (77 loc) · 1.96 KB
/
summary.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 store
import (
"time"
"github.com/google/go-cmp/cmp"
"k8s.io/apimachinery/pkg/types"
)
// Represents all the IDs of a particular type of resource
// that have changed.
type ChangeSet struct {
Changes map[types.NamespacedName]bool
}
func NewChangeSet(names ...types.NamespacedName) ChangeSet {
cs := ChangeSet{}
for _, name := range names {
cs.Add(name)
}
return cs
}
func (s *ChangeSet) Empty() bool {
return len(s.Changes) == 0
}
// Add a changed resource name.
func (s *ChangeSet) Add(nn types.NamespacedName) {
if s.Changes == nil {
s.Changes = make(map[types.NamespacedName]bool)
}
s.Changes[nn] = true
}
// Merge another change set into this one.
func (s *ChangeSet) AddAll(other ChangeSet) {
if len(other.Changes) > 0 {
if s.Changes == nil {
s.Changes = make(map[types.NamespacedName]bool)
}
for k, v := range other.Changes {
s.Changes[k] = v
}
}
}
// Summarize the changes to the EngineState since the last change.
type ChangeSummary struct {
// True if we saw one or more legacy actions that don't know how
// to summarize their changes.
Legacy bool
// True if this change added logs.
Log bool
// Cmds with their specs changed.
CmdSpecs ChangeSet
UISessions ChangeSet
UIResources ChangeSet
UIButtons ChangeSet
Clusters ChangeSet
// If non-zero, that means we tried to apply this change and got
// an error.
LastBackoff time.Duration
}
func (s ChangeSummary) IsLogOnly() bool {
return cmp.Equal(s, ChangeSummary{Log: true})
}
func (s *ChangeSummary) Add(other ChangeSummary) {
s.Legacy = s.Legacy || other.Legacy
s.Log = s.Log || other.Log
s.CmdSpecs.AddAll(other.CmdSpecs)
s.UISessions.AddAll(other.UISessions)
s.UIResources.AddAll(other.UIResources)
s.Clusters.AddAll(other.Clusters)
if other.LastBackoff > s.LastBackoff {
s.LastBackoff = other.LastBackoff
}
}
func LegacyChangeSummary() ChangeSummary {
return ChangeSummary{Legacy: true}
}
type Summarizer interface {
Action
Summarize(summary *ChangeSummary)
}