@@ -3,106 +3,44 @@ package k8seventwatcher
33import (
44 "errors"
55 "fmt"
6+ "github.com/cmaster11/k8s-event-watcher/lookup"
67 "gopkg.in/yaml.v2"
7- "k8s.io/api/core/v1"
88 "strings"
99)
1010
1111type EventFilter struct {
12- ObjectNamespace * Regexp `yaml:"objectNamespace,omitempty"`
13- ObjectKind * Regexp `yaml:"objectKind,omitempty"`
14- ObjectName * Regexp `yaml:"objectName,omitempty"`
15- EventType * Regexp `yaml:"eventType,omitempty"`
16- EventReason * Regexp `yaml:"eventReason,omitempty"`
12+ Rules map [string ]* Regexp `yaml:"rules"`
1713}
1814
1915func (f * EventFilter ) Validate () error {
2016 // At least one filter must exist
21- if f . ObjectNamespace != nil {
17+ if len ( f . Rules ) > 0 {
2218 return nil
2319 }
24- if f .ObjectKind != nil {
25- return nil
26- }
27- if f .ObjectName != nil {
28- return nil
29- }
30- if f .EventType != nil {
31- return nil
32- }
33- if f .EventReason != nil {
34- return nil
35- }
36- return errors .New ("no filter attributes provided" )
20+ return errors .New ("no rules provided" )
3721}
3822
39- func (f * EventFilter ) Matches (event * v1.Event ) bool {
40- if f .ObjectNamespace != nil {
41- if ! f .ObjectNamespace .MatchString (event .InvolvedObject .Namespace ) {
42- return false
23+ func (f * EventFilter ) Matches (event map [string ]interface {}) (bool , error ) {
24+ for path , regex := range f .Rules {
25+ value , err := lookup .LookupString (event , path )
26+ if err != nil {
27+ return false , errorf ("lookup error: %s" , err )
4328 }
44- }
45- if f .ObjectKind != nil {
46- if ! f .ObjectKind .MatchString (event .InvolvedObject .Kind ) {
47- return false
48- }
49- }
50- if f .ObjectName != nil {
51- if ! f .ObjectName .MatchString (event .InvolvedObject .Name ) {
52- return false
53- }
54- }
55- if f .EventType != nil {
56- if ! f .EventType .MatchString (event .Type ) {
57- return false
58- }
59- }
60- if f .EventReason != nil {
61- if ! f .EventReason .MatchString (event .Reason ) {
62- return false
29+
30+ valueStr := fmt .Sprintf ("%v" , value .Interface ())
31+ if ! regex .MatchString (valueStr ) {
32+ return false , nil
6333 }
6434 }
6535
66- return true
36+ return true , nil
6737}
6838
6939func (f * EventFilter ) String () string {
7040 var elements []string
71- if f .EventReason != nil {
72- elements = append (elements , fmt .Sprintf ("eventReason=%s" , f .EventReason .String ()))
73- }
74- if f .EventType != nil {
75- elements = append (elements , fmt .Sprintf ("eventType=%s" , f .EventType .String ()))
76- }
77- if f .ObjectNamespace != nil {
78- elements = append (elements , fmt .Sprintf ("objectNamespace=%s" , f .ObjectNamespace .String ()))
79- }
80- if f .ObjectKind != nil {
81- elements = append (elements , fmt .Sprintf ("objectKind=%s" , f .ObjectKind .String ()))
82- }
83- if f .ObjectName != nil {
84- elements = append (elements , fmt .Sprintf ("objectName=%s" , f .ObjectName .String ()))
85- }
86-
87- return strings .Join (elements , "," )
88- }
8941
90- func (f * EventFilter ) StringShort () string {
91- var elements []string
92- if f .EventReason != nil {
93- elements = append (elements , fmt .Sprintf ("reason=%s" , f .EventReason .String ()))
94- }
95- if f .EventType != nil {
96- elements = append (elements , fmt .Sprintf ("evtType=%s" , f .EventType .String ()))
97- }
98- if f .ObjectNamespace != nil {
99- elements = append (elements , fmt .Sprintf ("objNS=%s" , f .ObjectNamespace .String ()))
100- }
101- if f .ObjectKind != nil {
102- elements = append (elements , fmt .Sprintf ("objKind=%s" , f .ObjectKind .String ()))
103- }
104- if f .ObjectName != nil {
105- elements = append (elements , fmt .Sprintf ("objName=%s" , f .ObjectName .String ()))
42+ for path , regex := range f .Rules {
43+ elements = append (elements , fmt .Sprintf ("%s=%s" , path , regex .String ()))
10644 }
10745
10846 return strings .Join (elements , "," )
0 commit comments