-
Notifications
You must be signed in to change notification settings - Fork 76
/
event.go
325 lines (275 loc) · 8.7 KB
/
event.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package event
import (
"fmt"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
"sigs.k8s.io/cli-utils/pkg/object"
)
// Type determines the type of events that are available.
//
//go:generate stringer -type=Type
type Type int
const (
InitType Type = iota
ErrorType
ActionGroupType
ApplyType
StatusType
PruneType
DeleteType
WaitType
ValidationType
)
// Event is the type of the objects that will be returned through
// the channel that is returned from a call to Run. It contains
// information about progress and errors encountered during
// the process of doing apply, waiting for status and doing a prune.
type Event struct {
// Type is the type of event.
Type Type
// InitEvent contains information about which resources will
// be applied/pruned.
InitEvent InitEvent
// ErrorEvent contains information about any errors encountered.
ErrorEvent ErrorEvent
// ActionGroupEvent contains information about the progression of tasks
// to apply, prune, and destroy resources, and tasks that involves waiting
// for a set of resources to reach a specific state.
ActionGroupEvent ActionGroupEvent
// ApplyEvent contains information about progress pertaining to
// applying a resource to the cluster.
ApplyEvent ApplyEvent
// StatusEvents contains information about the status of one of
// the applied resources.
StatusEvent StatusEvent
// PruneEvent contains information about objects that have been
// pruned.
PruneEvent PruneEvent
// DeleteEvent contains information about object that have been
// deleted.
DeleteEvent DeleteEvent
// WaitEvent contains information about any errors encountered in a WaitTask.
WaitEvent WaitEvent
// ValidationEvent contains information about validation errors.
ValidationEvent ValidationEvent
}
// String returns a string suitable for logging
func (e Event) String() string {
var sb strings.Builder
switch e.Type {
case InitType:
sb.WriteString(e.InitEvent.String())
case ErrorType:
sb.WriteString(e.ErrorEvent.String())
case ActionGroupType:
sb.WriteString(e.ActionGroupEvent.String())
case ApplyType:
sb.WriteString(e.ApplyEvent.String())
case StatusType:
sb.WriteString(e.StatusEvent.String())
case PruneType:
sb.WriteString(e.PruneEvent.String())
case DeleteType:
sb.WriteString(e.DeleteEvent.String())
case WaitType:
sb.WriteString(e.WaitEvent.String())
case ValidationType:
sb.WriteString(e.ValidationEvent.String())
}
return sb.String()
}
type InitEvent struct {
ActionGroups ActionGroupList
}
// String returns a string suitable for logging
func (ie InitEvent) String() string {
return fmt.Sprintf("InitEvent{ ActionGroups: %s }", ie.ActionGroups)
}
//go:generate stringer -type=ResourceAction -linecomment
type ResourceAction int
const (
ApplyAction ResourceAction = iota // Apply
PruneAction // Prune
DeleteAction // Delete
WaitAction // Wait
InventoryAction // Inventory
)
type ActionGroupList []ActionGroup
// String returns a string suitable for logging
func (agl ActionGroupList) String() string {
var sb strings.Builder
sb.WriteString("[ ")
for i, ag := range agl {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString("[ ")
sb.WriteString(ag.String())
sb.WriteString(" ]")
}
sb.WriteString(" ]")
return sb.String()
}
type ActionGroup struct {
Name string
Action ResourceAction
Identifiers object.ObjMetadataSet
}
// String returns a string suitable for logging
func (ag ActionGroup) String() string {
return fmt.Sprintf("ActionGroup{ Name: %q, Action: %q, Identifiers: %s }",
ag.Name, ag.Action, ag.Identifiers)
}
type ErrorEvent struct {
Err error
}
// String returns a string suitable for logging
func (ee ErrorEvent) String() string {
return fmt.Sprintf("ErrorEvent{ Err: %q }", ee.Err.Error())
}
//go:generate stringer -type=WaitEventStatus -linecomment
type WaitEventStatus int
const (
ReconcilePending WaitEventStatus = iota // Pending
ReconcileSuccessful // Successful
ReconcileSkipped // Skipped
ReconcileTimeout // Timeout
ReconcileFailed // Failed
)
type WaitEvent struct {
GroupName string
Identifier object.ObjMetadata
Status WaitEventStatus
}
// String returns a string suitable for logging
func (we WaitEvent) String() string {
return fmt.Sprintf("WaitEvent{ GroupName: %q, Status: %q, Identifier: %q }",
we.GroupName, we.Status, we.Identifier)
}
//go:generate stringer -type=ActionGroupEventStatus
type ActionGroupEventStatus int
const (
Started ActionGroupEventStatus = iota
Finished
)
type ActionGroupEvent struct {
GroupName string
Action ResourceAction
Status ActionGroupEventStatus
}
// String returns a string suitable for logging
func (age ActionGroupEvent) String() string {
return fmt.Sprintf("ActionGroupEvent{ GroupName: %q, Action: %q, Type: %q }",
age.GroupName, age.Action, age.Status)
}
//go:generate stringer -type=ApplyEventStatus -linecomment
type ApplyEventStatus int
const (
ApplyPending ApplyEventStatus = iota // Pending
ApplySuccessful // Successful
ApplySkipped // Skipped
ApplyFailed // Failed
)
type ApplyEvent struct {
GroupName string
Identifier object.ObjMetadata
Status ApplyEventStatus
Resource *unstructured.Unstructured
Error error
}
// String returns a string suitable for logging
func (ae ApplyEvent) String() string {
if ae.Error != nil {
return fmt.Sprintf("ApplyEvent{ GroupName: %q, Status: %q, Identifier: %q, Error: %q }",
ae.GroupName, ae.Status, ae.Identifier, ae.Error)
}
return fmt.Sprintf("ApplyEvent{ GroupName: %q, Status: %q, Identifier: %q }",
ae.GroupName, ae.Status, ae.Identifier)
}
type StatusEvent struct {
Identifier object.ObjMetadata
PollResourceInfo *pollevent.ResourceStatus
Resource *unstructured.Unstructured
Error error
}
// String returns a string suitable for logging
func (se StatusEvent) String() string {
status := "nil"
gen := int64(0)
if se.PollResourceInfo != nil {
status = se.PollResourceInfo.Status.String()
if se.PollResourceInfo.Resource != nil {
gen = se.PollResourceInfo.Resource.GetGeneration()
}
}
if se.Error != nil {
return fmt.Sprintf("StatusEvent{ Status: %q, Generation: %d, Identifier: %q, Error: %q }",
status, gen, se.Identifier, se.Error)
}
return fmt.Sprintf("StatusEvent{ Status: %q, Generation: %d, Identifier: %q }",
status, gen, se.Identifier)
}
//go:generate stringer -type=PruneEventStatus -linecomment
type PruneEventStatus int
const (
PrunePending PruneEventStatus = iota // Pending
PruneSuccessful // Successful
PruneSkipped // Skipped
PruneFailed // Failed
)
type PruneEvent struct {
GroupName string
Identifier object.ObjMetadata
Status PruneEventStatus
Object *unstructured.Unstructured
Error error
}
// String returns a string suitable for logging
func (pe PruneEvent) String() string {
if pe.Error != nil {
return fmt.Sprintf("PruneEvent{ GroupName: %q, Status: %q, Identifier: %q, Error: %q }",
pe.GroupName, pe.Status, pe.Identifier, pe.Error)
}
return fmt.Sprintf("PruneEvent{ GroupName: %q, Status: %q, Identifier: %q }",
pe.GroupName, pe.Status, pe.Identifier)
}
//go:generate stringer -type=DeleteEventStatus -linecomment
type DeleteEventStatus int
const (
DeletePending DeleteEventStatus = iota // Pending
DeleteSuccessful // Successful
DeleteSkipped // Skipped
DeleteFailed // Failed
)
type DeleteEvent struct {
GroupName string
Identifier object.ObjMetadata
Status DeleteEventStatus
Object *unstructured.Unstructured
Error error
}
// String returns a string suitable for logging
func (de DeleteEvent) String() string {
if de.Error != nil {
return fmt.Sprintf("DeleteEvent{ GroupName: %q, Status: %q, Identifier: %q, Error: %q }",
de.GroupName, de.Status, de.Identifier, de.Error)
}
return fmt.Sprintf("DeleteEvent{ GroupName: %q, Status: %q, Identifier: %q }",
de.GroupName, de.Status, de.Identifier)
}
type ValidationEvent struct {
Identifiers object.ObjMetadataSet
Error error
}
// String returns a string suitable for logging
func (ve ValidationEvent) String() string {
if ve.Error != nil {
return fmt.Sprintf("ValidationEvent{ Identifiers: %+v, Error: %q }",
ve.Identifiers, ve.Error)
}
return fmt.Sprintf("ValidationEvent{ Identifiers: %+v }",
ve.Identifiers)
}