-
Notifications
You must be signed in to change notification settings - Fork 38
/
intents.go
179 lines (153 loc) · 4.96 KB
/
intents.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package intents
import (
"github.com/bottlerocket-os/bottlerocket-update-operator/pkg/intent"
"github.com/bottlerocket-os/bottlerocket-update-operator/pkg/marker"
)
const (
// NodeName is a suitable reusable NodeName that may be provided by callers.
NodeName = "intents-node"
)
// ret is a wrapper that provides the extendable interface to canned and
// distinguished Intents for use in tests.
func ret(name string, i intent.Intent, initOpts ...func(i *intent.Intent)) func(opts ...func(i *intent.Intent)) *intent.Intent {
i.NodeName = name
for _, opt := range initOpts {
opt(&i)
}
return func(opts ...func(i *intent.Intent)) *intent.Intent {
c := i.Clone()
for _, opt := range opts {
opt(c)
}
return c
}
}
// WithReset resets the provided intent to the initial state.
func WithReset() func(i *intent.Intent) {
return func(i *intent.Intent) {
*i = *i.Reset() // replace the pointer's value
}
}
// WithNodeName sets the intent's NodeName.
func WithNodeName(name string) func(i *intent.Intent) {
return func(i *intent.Intent) {
i.NodeName = name
}
}
// WithBusy marks the intent as busy with the Active intent.
func WithBusy() func(i *intent.Intent) {
return func(i *intent.Intent) {
if i.Wanted != i.Active {
panic("provided Wanted and Active do not match - incoherent")
}
i.State = marker.NodeStateBusy
}
}
// WithUpdateAvailable marks the intent with the provided NodeUpdate marker.
func WithUpdateAvailable(up ...marker.NodeUpdate) func(i *intent.Intent) {
return func(i *intent.Intent) {
if len(up) == 1 {
i.UpdateAvailable = up[0]
return
}
i.UpdateAvailable = marker.NodeUpdateAvailable
}
}
// NormalizeNodeName makes all provided intents' NodeName uniform for comparison
// or otherwise normalized expectations.
func NormalizeNodeName(name string, is ...*intent.Intent) {
namer := WithNodeName(name)
for _, in := range is {
if in != nil {
namer(in)
}
}
}
// Set the intent to be pending the provided NodeAction.
func Pending(wanted marker.NodeAction) func(*intent.Intent) {
return func(i *intent.Intent) {
i.Wanted = wanted
}
}
// Use the provided intent as the targeted next NodeAction.
func NextAs(next *intent.Intent) func(*intent.Intent) {
return func(in *intent.Intent) {
if next == nil {
return
}
in.Wanted = next.Wanted
}
}
var (
Stabilized = ret("Stabilized", intent.Intent{
Wanted: marker.NodeActionStabilize,
Active: marker.NodeActionStabilize,
State: marker.NodeStateReady,
})
Stabilizing = ret("Stabilizing", intent.Intent{
Wanted: marker.NodeActionStabilize,
Active: marker.NodeActionStabilize,
State: marker.NodeStateBusy,
})
PendingStabilizing = ret("PendingStabilizing", intent.Intent{
Wanted: marker.NodeActionStabilize,
Active: marker.NodeActionUnknown,
State: marker.NodeStateUnknown,
})
BusyRebootUpdate = ret("PendingRebootUpdate", intent.Intent{
Wanted: marker.NodeActionRebootUpdate,
Active: marker.NodeActionRebootUpdate,
State: marker.NodeStateBusy,
}, WithUpdateAvailable())
PendingRebootUpdate = ret("PendingRebootUpdate", intent.Intent{
Wanted: marker.NodeActionRebootUpdate,
Active: marker.NodeActionPrepareUpdate,
State: marker.NodeStateReady,
})
UpdateError = ret("UpdateError", intent.Intent{
Wanted: marker.NodeActionRebootUpdate,
Active: marker.NodeActionRebootUpdate,
State: marker.NodeStateError,
}, WithUpdateAvailable())
UpdateSuccess = ret("UpdateSuccess", intent.Intent{
Wanted: marker.NodeActionRebootUpdate,
Active: marker.NodeActionRebootUpdate,
State: marker.NodeStateReady,
}, WithUpdateAvailable(marker.NodeUpdateUnknown))
UpdatePrepared = ret("UpdatePrepared", intent.Intent{
Wanted: marker.NodeActionPrepareUpdate,
Active: marker.NodeActionPrepareUpdate,
State: marker.NodeStateReady,
}, WithUpdateAvailable())
PendingPrepareUpdate = ret("PendingPrepareUpdate", intent.Intent{
Wanted: marker.NodeActionPrepareUpdate,
Active: marker.NodeActionStabilize,
State: marker.NodeStateReady,
}, WithUpdateAvailable())
PreparingUpdate = ret("PendingPrepareUpdate", intent.Intent{
Wanted: marker.NodeActionPrepareUpdate,
Active: marker.NodeActionPrepareUpdate,
State: marker.NodeStateBusy,
}, WithUpdateAvailable())
UpdatePerformed = ret("UpdatePerformed", intent.Intent{
Wanted: marker.NodeActionPerformUpdate,
Active: marker.NodeActionPerformUpdate,
State: marker.NodeStateReady,
}, WithUpdateAvailable())
PerformingUpdate = ret("PerformingUpdate", intent.Intent{
Wanted: marker.NodeActionPerformUpdate,
Active: marker.NodeActionPerformUpdate,
State: marker.NodeStateBusy,
}, WithUpdateAvailable())
PendingUpdate = ret("PendingUpdate", intent.Intent{
Wanted: marker.NodeActionPerformUpdate,
Active: marker.NodeActionPrepareUpdate,
State: marker.NodeStateReady,
}, WithUpdateAvailable())
Unknown = ret("Unknown", intent.Intent{
Wanted: marker.NodeActionUnknown,
Active: marker.NodeActionUnknown,
State: marker.NodeStateUnknown,
}, WithUpdateAvailable())
Reset = ret("Reset", intent.Intent{}, WithReset())
)