-
Notifications
You must be signed in to change notification settings - Fork 13
/
reporter.go
488 lines (418 loc) · 14.7 KB
/
reporter.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package reporter
import (
"context"
"os"
"slices"
"sort"
"strings"
"k8s.io/client-go/util/retry"
"github.com/hashicorp/go-multierror"
"github.com/solo-io/go-utils/contextutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/resources"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"github.com/solo-io/solo-kit/pkg/errors"
)
const (
// 1024 chars = 1kb
MaxStatusBytes = 1024
MaxStatusKeys = 100
)
var (
// only public for unit tests!
DisableTruncateStatus = false
)
func init() {
DisableTruncateStatus = os.Getenv("DISABLE_TRUNCATE_STATUS") == "true"
}
type Report struct {
Warnings []string
Errors error
// Additional information about the current state of the resource.
Messages []string
}
type ResourceReports map[resources.InputResource]Report
func (e ResourceReports) Accept(res ...resources.InputResource) ResourceReports {
for _, r := range res {
e[r] = Report{}
}
return e
}
// Merge merges the given resourceReports into this resourceReports.
// Any resources which appear in both resourceReports will
// have their warnings and errors merged.
// Errors appearing in both reports, as determined by the error strings,
// will not be duplicated in the resulting merged report.
func (e ResourceReports) Merge(resErrs ResourceReports) {
for k, v := range resErrs {
if firstReport, exists := e[k]; exists {
// report already exists for this resource,
// merge new report into existing report:
secondReport := v
// Merge warnings lists
allWarnings := make(map[string]bool)
for _, warning := range firstReport.Warnings {
allWarnings[warning] = true
}
for _, warning := range secondReport.Warnings {
if _, found := allWarnings[warning]; !found {
firstReport.Warnings = append(firstReport.Warnings, warning)
}
}
// Merge messages lists
allMessages := make(map[string]struct{})
for _, message := range firstReport.Messages {
allMessages[message] = struct{}{}
}
for _, message := range secondReport.Messages {
if _, found := allMessages[message]; !found {
firstReport.Messages = append(firstReport.Messages, message)
}
}
if firstReport.Errors == nil {
// Only 2nd has errs
firstReport.Errors = secondReport.Errors
e[k] = firstReport
continue
} else if secondReport.Errors == nil {
// Only 1st has errs
e[k] = firstReport
continue
}
// Both first and second have errors for the same resource:
// Any errors which are identical won't be duplicated,
// Any errors which are unique will be added to the final list
errs1, isFirstMulti := firstReport.Errors.(*multierror.Error)
errs2, isSecondMulti := secondReport.Errors.(*multierror.Error)
// If the errors are not mutliErrs, wrap them in multiErrs:
if !isFirstMulti {
errs1 = &multierror.Error{Errors: []error{firstReport.Errors}}
}
if !isSecondMulti {
errs2 = &multierror.Error{Errors: []error{secondReport.Errors}}
}
allErrsMap := make(map[string]error)
for _, err := range errs1.Errors {
allErrsMap[err.Error()] = err
}
for _, err := range errs2.Errors {
if _, found := allErrsMap[err.Error()]; !found {
allErrsMap[err.Error()] = err
errs1.Errors = append(errs1.Errors, err)
}
}
firstReport.Errors = errs1
e[k] = firstReport
} else {
// Resource in 2nd report is not yet in 1st report
e[k] = v
}
}
}
func (e ResourceReports) AddErrors(res resources.InputResource, errs ...error) {
for _, err := range errs {
e.AddError(res, err)
}
}
func (e ResourceReports) AddError(res resources.InputResource, err error) {
if err == nil {
return
}
rpt := e[res]
rpt.Errors = multierror.Append(rpt.Errors, err)
e[res] = rpt
}
func (e ResourceReports) AddWarnings(res resources.InputResource, warning ...string) {
for _, warn := range warning {
e.AddWarning(res, warn)
}
}
func (e ResourceReports) AddWarning(res resources.InputResource, warning string) {
if warning == "" {
return
}
rpt := e[res]
rpt.Warnings = append(rpt.Warnings, warning)
e[res] = rpt
}
func (e ResourceReports) AddMessages(res resources.InputResource, messages ...string) {
for _, message := range messages {
e.AddMessage(res, message)
}
}
func (e ResourceReports) AddMessage(res resources.InputResource, message string) {
if message == "" {
return
}
rpt := e[res]
rpt.Messages = append(rpt.Messages, message)
e[res] = rpt
}
func (e ResourceReports) Find(kind string, ref *core.ResourceRef) (resources.InputResource, Report) {
for res, rpt := range e {
if resources.Kind(res) == kind && res.GetMetadata().Ref().Equal(ref) {
return res, rpt
}
}
return nil, Report{}
}
func (e ResourceReports) FilterByKind(kind string) ResourceReports {
reports := make(map[resources.InputResource]Report)
for res, rpt := range e {
if resources.Kind(res) == kind {
reports[res] = rpt
}
}
return reports
}
// refMapAndSortedKeys returns a map of resource refs to resources and a sorted list of resource refs
// This is used to iterate over the resources in a consistent order.
// The reports are keyed by references to the resources, so are not sortable.
// There is no unique key for a resource, so we use the string representation of the name/namespace as the key,
// and collect all the resources with the same key together.
// The previous implementation did not guarantee a consistent order when iterating over the resources,
// so any order used here will be acceptable for backwards compatibility.
func (e ResourceReports) refMapAndSortedKeys() (map[string][]resources.InputResource, []string) {
// refKeys contains all the unique keys for the resources
var refKeys []string
// refMaps is a map of resource keys to a slice of resources with that key
var refMap = make(map[string][]resources.InputResource)
// Loop over the resources
for res := range e {
// Get a string representation of the resource ref. This is not guaranteed to be unique.
resKey := res.GetMetadata().Ref().String()
// Add the resource to the map of resources with the same key
refMap[resKey] = append(refMap[resKey], res)
}
// Get the list of name-namespace keys
refKeys = make([]string, len(refMap))
i := 0
for k := range refMap {
refKeys[i] = k
i++
}
// Sort the name-namespace keys. This will allow the reports to be accssed in a consistent order,
// except in cases where the name/namespace is not unique. In those cases, the individual validaiton
// functions will have to handle consistent ordering.
slices.Sort(refKeys)
return refMap, refKeys
}
// sortErrors sorts errors based on string representation
// Note: because we are using multierror the string representation starts with "x errors occurred".
// This will be consistent, but possibly counter-intuitive for tests.
func sortErrors(errs []error) {
sort.Slice(errs, func(i, j int) bool {
return errs[i].Error() < errs[j].Error()
})
}
// Validate ignores warnings
func (e ResourceReports) Validate() error {
var errs error
refMap, refKeys := e.refMapAndSortedKeys()
// the refKeys are sorted/sortable and point to the unsortable resources refs that are the keys to the report map
for _, refKey := range refKeys {
// name/namespace is not unique, so we collect those references together
reses := refMap[refKey]
var errsForKey []error
for _, res := range reses {
rpt := e[res]
if rpt.Errors != nil {
errsForKey = append(errsForKey, rpt.Errors)
}
}
if len(errsForKey) > 0 {
if errs == nil {
// All of the resources in the group have the same metadata, so use the first one
errs = errors.Errorf("invalid resource %v.%v", reses[0].GetMetadata().Namespace, reses[0].GetMetadata().Name)
}
sortErrors(errsForKey)
for _, err := range errsForKey {
errs = multierror.Append(errs, err)
}
}
}
return errs
}
// ValidateStrict does not ignore warnings. If warnings are present, they will be included in the error.
// If an error is not present but warnings are, an "invalid resource" error will be returned along with each warning.
func (e ResourceReports) ValidateStrict() error {
errs := e.Validate()
refMap, refKeys := e.refMapAndSortedKeys()
for _, refKey := range refKeys {
var errsForKey []error
reses := refMap[refKey]
// name/namespace is not unique, so we collect those references together
for _, res := range reses {
rpt := e[res]
if len(rpt.Warnings) > 0 {
errsForKey = append(errsForKey, errors.Errorf("WARN: \n %v", rpt.Warnings))
}
}
if len(errsForKey) > 0 {
if errs == nil {
// All of the resources in the group have the same metadata, so use the first one
errs = errors.Errorf(
"invalid resource %v.%v",
reses[0].GetMetadata().GetNamespace(),
reses[0].GetMetadata().GetName(),
)
}
sortErrors(errsForKey)
for _, err := range errsForKey {
errs = multierror.Append(errs, err)
}
}
}
return errs
}
// Minimal set of client operations required for reporters.
type ReporterResourceClient interface {
Kind() string
ApplyStatus(statusClient resources.StatusClient, inputResource resources.InputResource, opts clients.ApplyStatusOpts) (resources.Resource, error)
}
type Reporter interface {
WriteReports(ctx context.Context, errs ResourceReports, subresourceStatuses map[string]*core.Status) error
}
type StatusReporter interface {
Reporter
StatusFromReport(report Report, subresourceStatuses map[string]*core.Status) *core.Status
}
type reporter struct {
reporterRef string
statusClient resources.StatusClient
clients map[string]ReporterResourceClient
}
func NewReporter(reporterRef string, statusClient resources.StatusClient, reporterClients ...ReporterResourceClient) StatusReporter {
clientsByKind := make(map[string]ReporterResourceClient)
for _, client := range reporterClients {
clientsByKind[client.Kind()] = client
}
return &reporter{
reporterRef: reporterRef,
statusClient: statusClient,
clients: clientsByKind,
}
}
// ResourceReports may be modified, and end up with fewer resources than originally requested.
// If resources referenced in the resourceErrs don't exist, they will be removed.
func (r *reporter) WriteReports(ctx context.Context, resourceErrs ResourceReports, subresourceStatuses map[string]*core.Status) error {
ctx = contextutils.WithLogger(ctx, "reporter")
logger := contextutils.LoggerFrom(ctx)
var merr *multierror.Error
// copy the map so we can iterate over the copy, deleting resources from
// the original map if they are not found/no longer exist.
resourceErrsCopy := make(ResourceReports, len(resourceErrs))
for resource, report := range resourceErrs {
resourceErrsCopy[resource] = report
}
for resource, report := range resourceErrsCopy {
kind := resources.Kind(resource)
client, ok := r.clients[kind]
if !ok {
return errors.Errorf("reporter: was passed resource of kind %v but no client to support it", kind)
}
status := r.StatusFromReport(report, subresourceStatuses)
if !DisableTruncateStatus {
status = trimStatus(status)
}
resourceStatus := r.statusClient.GetStatus(resource)
if status.Equal(resourceStatus) {
logger.Debugf("skipping report for %v as it has not changed", resource.GetMetadata().Ref())
continue
}
resourceToWrite := resources.Clone(resource).(resources.InputResource)
r.statusClient.SetStatus(resourceToWrite, status)
writeErr := errors.RetryOnConflict(retry.DefaultBackoff, func() error {
return r.attemptUpdateStatus(ctx, client, resourceToWrite, status)
})
if errors.IsNotExist(writeErr) {
logger.Debugf("did not write report for %v : %v because resource was not found", resourceToWrite.GetMetadata().Ref(), status)
delete(resourceErrs, resource)
continue
}
if writeErr != nil {
err := errors.Wrapf(writeErr, "failed to write status %v for resource %v", status, resource.GetMetadata().GetName())
logger.Warn(err)
merr = multierror.Append(merr, err)
continue
}
logger.Debugf("wrote report for %v : %v", resource.GetMetadata().Ref(), status)
}
return merr.ErrorOrNil()
}
func (r *reporter) attemptUpdateStatus(ctx context.Context, client ReporterResourceClient, resourceToWrite resources.InputResource, statusToWrite *core.Status) error {
_, patchErr := client.ApplyStatus(r.statusClient, resourceToWrite, clients.ApplyStatusOpts{Ctx: ctx})
return patchErr
}
func (r *reporter) StatusFromReport(report Report, subresourceStatuses map[string]*core.Status) *core.Status {
var messages []string
if len(report.Messages) != 0 {
messages = report.Messages
}
var warningReason string
if len(report.Warnings) > 0 {
warningReason = "warning: \n " + strings.Join(report.Warnings, "\n")
}
if report.Errors != nil {
errorReason := report.Errors.Error()
if warningReason != "" {
errorReason += "\n" + warningReason
}
return &core.Status{
State: core.Status_Rejected,
Reason: errorReason,
ReportedBy: r.reporterRef,
SubresourceStatuses: subresourceStatuses,
Messages: messages,
}
}
if warningReason != "" {
return &core.Status{
State: core.Status_Warning,
Reason: warningReason,
ReportedBy: r.reporterRef,
SubresourceStatuses: subresourceStatuses,
Messages: messages,
}
}
return &core.Status{
State: core.Status_Accepted,
ReportedBy: r.reporterRef,
SubresourceStatuses: subresourceStatuses,
Messages: messages,
}
}
func trimStatus(status *core.Status) *core.Status {
// truncate status reason to a kilobyte, with max 100 keys in subresource statuses
return trimStatusForMaxSize(status, MaxStatusBytes, MaxStatusKeys)
}
func trimStatusForMaxSize(status *core.Status, bytesPerKey, maxKeys int) *core.Status {
if status == nil {
return nil
}
if len(status.Reason) > bytesPerKey {
status.Reason = status.Reason[:bytesPerKey]
}
if len(status.SubresourceStatuses) > maxKeys {
// sort for idempotency
keys := make([]string, 0, len(status.SubresourceStatuses))
for key := range status.SubresourceStatuses {
keys = append(keys, key)
}
sort.Strings(keys)
trimmedSubresourceStatuses := make(map[string]*core.Status, maxKeys)
for _, key := range keys[:maxKeys] {
trimmedSubresourceStatuses[key] = status.SubresourceStatuses[key]
}
status.SubresourceStatuses = trimmedSubresourceStatuses
}
for key, childStatus := range status.SubresourceStatuses {
// divide by two so total memory usage is bounded at: (num_keys * bytes_per_key) + (num_keys / 2 * bytes_per_key / 2) + ...
// 100 * 1024b + 50 * 512b + 25 * 256b + 12 * 128b + 6 * 64b + 3 * 32b + 1 * 16b ~= 136 kilobytes
//
// 2147483647 bytes is k8s -> etcd limit in grpc connection. 2147483647 / 136 ~= 15788 resources at limit before we see an issue
// https://github.com/solo-io/solo-projects/issues/4120
status.SubresourceStatuses[key] = trimStatusForMaxSize(childStatus, bytesPerKey/2, maxKeys/2)
}
return status
}