-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
ev.go
94 lines (78 loc) 路 1.79 KB
/
ev.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Popeye
package dao
import (
"context"
"fmt"
"strings"
"github.com/derailed/popeye/internal"
"github.com/derailed/popeye/internal/client"
"github.com/derailed/popeye/types"
"github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
WarnEvt = "Warning"
)
type EventInfo struct {
Kind string
Reason string
Message string
Count int64
}
func (e EventInfo) IsIssue() bool {
return e.Kind == WarnEvt ||
(e.Reason != "Success" && e.Reason != "SawCompletedJob")
}
type EventInfos []EventInfo
func (ee EventInfos) Issues() []string {
if len(ee) == 0 {
return nil
}
ss := make([]string, 0, len(ee))
for _, e := range ee {
if e.IsIssue() {
ss = append(ss, e.Message)
}
}
return ss
}
type Event struct {
Table
}
func EventsFor(ctx context.Context, gvr types.GVR, level, kind, fqn string) (EventInfos, error) {
ns, n := client.Namespaced(fqn)
f, ok := ctx.Value(internal.KeyFactory).(types.Factory)
if !ok {
return nil, nil
}
ss := make([]string, 0, 2)
if level != "" {
ss = append(ss, fmt.Sprintf("type=%s", level))
}
if kind != "" {
ss = append(ss, fmt.Sprintf("involvedObject.name=%s,involvedObject.kind=%s", n, kind))
}
ctx = context.WithValue(ctx, internal.KeyFields, strings.Join(ss, ","))
var t Table
t.Init(f, types.NewGVR("v1/events"))
oo, err := t.List(ctx, ns)
if err != nil {
return nil, err
}
if len(oo) == 0 {
log.Debug().Msgf("No events found %s: %s", gvr, fqn)
return nil, nil
}
tt := oo[0].(*metav1.Table)
ee := make(EventInfos, 0, len(tt.Rows))
for _, r := range tt.Rows {
ee = append(ee, EventInfo{
Kind: r.Cells[1].(string),
Reason: r.Cells[2].(string),
Message: r.Cells[6].(string),
Count: r.Cells[8].(int64),
})
}
return ee, nil
}