|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + v1 "k8s.io/api/core/v1" |
| 6 | +) |
| 7 | + |
| 8 | +type EventFilter struct { |
| 9 | + ObjectNamespace *Regexp `yaml:"objectNamespace,omitempty"` |
| 10 | + ObjectKind *Regexp `yaml:"objectKind,omitempty"` |
| 11 | + ObjectName *Regexp `yaml:"objectName,omitempty"` |
| 12 | + EventType *Regexp `yaml:"eventType,omitempty"` |
| 13 | + EventReason *Regexp `yaml:"eventReason,omitempty"` |
| 14 | +} |
| 15 | + |
| 16 | +func (f *EventFilter) Validate() error { |
| 17 | + // At least one filter must exist |
| 18 | + if f.ObjectNamespace != nil { |
| 19 | + return nil |
| 20 | + } |
| 21 | + if f.ObjectKind != nil { |
| 22 | + return nil |
| 23 | + } |
| 24 | + if f.ObjectName != nil { |
| 25 | + return nil |
| 26 | + } |
| 27 | + if f.EventType != nil { |
| 28 | + return nil |
| 29 | + } |
| 30 | + if f.EventReason != nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + return errors.New("no filter attributes provided") |
| 34 | +} |
| 35 | + |
| 36 | +func (f *EventFilter) Matches(event *v1.Event) bool { |
| 37 | + if f.ObjectNamespace != nil { |
| 38 | + if !f.ObjectNamespace.MatchString(event.InvolvedObject.Namespace) { |
| 39 | + return false |
| 40 | + } |
| 41 | + } |
| 42 | + if f.ObjectKind != nil { |
| 43 | + if !f.ObjectKind.MatchString(event.InvolvedObject.Kind) { |
| 44 | + return false |
| 45 | + } |
| 46 | + } |
| 47 | + if f.ObjectName != nil { |
| 48 | + if !f.ObjectName.MatchString(event.InvolvedObject.Name) { |
| 49 | + return false |
| 50 | + } |
| 51 | + } |
| 52 | + if f.EventType != nil { |
| 53 | + if !f.EventType.MatchString(event.Type) { |
| 54 | + return false |
| 55 | + } |
| 56 | + } |
| 57 | + if f.EventReason != nil { |
| 58 | + if !f.EventReason.MatchString(event.Reason) { |
| 59 | + return false |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return true |
| 64 | +} |
0 commit comments