Skip to content

Commit a1ea13b

Browse files
authored
Merge pull request #57 from dgrisonnet/filter-event-reason
Add Event reason filtering
2 parents f741257 + 4549841 commit a1ea13b

File tree

6 files changed

+123
-9
lines changed

6 files changed

+123
-9
lines changed

internal/collector/event.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func NewEventCollector(kubeClient kubernetes.Interface, opts *options.Options) *
6161
filter: eventFilter{
6262
creationTimestamp: time.Now(),
6363
apiGroups: opts.InvolvedObjectAPIGroups,
64+
controllers: opts.ReportingControllers,
6465
},
6566
}
6667

@@ -154,7 +155,7 @@ func filterInvolvedObjectNs(list *metav1.ListOptions, ns string) {
154155
}
155156

156157
func filterEventType(list *metav1.ListOptions, eventType string) {
157-
if eventType != options.EventTypesAll {
158+
if eventType != options.EventTypeAll {
158159
list.FieldSelector += ",type=" + eventType
159160
}
160161
}

internal/collector/filter.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
type eventFilter struct {
2727
creationTimestamp time.Time
2828
apiGroups []string
29+
controllers []string
2930
}
3031

3132
func (f *eventFilter) filter(obj interface{}) bool {
@@ -37,7 +38,11 @@ func (f *eventFilter) filter(obj interface{}) bool {
3738
return false
3839
}
3940

40-
return includedObjectAPIGroup(ev, f.apiGroups)
41+
if !includedObjectAPIGroup(ev, f.apiGroups) {
42+
return false
43+
}
44+
45+
return includedController(ev, f.controllers)
4146
}
4247

4348
func reconciledEvent(ev *v1.Event, t time.Time) bool {
@@ -69,7 +74,7 @@ func getEventLatestTimestamp(ev *v1.Event) time.Time {
6974
}
7075

7176
func includedObjectAPIGroup(ev *v1.Event, groups []string) bool {
72-
if groups[0] == options.APIGroupsAll {
77+
if groups[0] == options.APIGroupAll {
7378
return true
7479
}
7580

@@ -81,3 +86,19 @@ func includedObjectAPIGroup(ev *v1.Event, groups []string) bool {
8186

8287
return false
8388
}
89+
90+
func includedController(ev *v1.Event, controllers []string) bool {
91+
if controllers[0] == options.ReportingControllerAll {
92+
return true
93+
}
94+
95+
for _, c := range controllers {
96+
if c == ev.Source.Component {
97+
return true
98+
} else if c == ev.ReportingController {
99+
return true
100+
}
101+
}
102+
103+
return false
104+
}

internal/collector/filter_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,54 @@ func TestIncludedObjectAPIGroup(t *testing.T) {
179179
})
180180
}
181181
}
182+
183+
func TestIncludedController(t *testing.T) {
184+
testCases := []struct {
185+
desc string
186+
controllers []string
187+
event *v1.Event
188+
expect bool
189+
}{
190+
{
191+
desc: "IncludedSource",
192+
controllers: []string{"default-scheduler", "kube-proxy", "kubelet"},
193+
event: &v1.Event{Source: v1.EventSource{Component: "kubelet"}},
194+
expect: true,
195+
},
196+
{
197+
desc: "ExcludedSource",
198+
controllers: []string{"kube-proxy", "kubelet"},
199+
event: &v1.Event{Source: v1.EventSource{Component: "default-scheduler"}},
200+
expect: false,
201+
},
202+
{
203+
desc: "IncludedController",
204+
controllers: []string{"default-scheduler", "kube-proxy", "kubelet"},
205+
event: &v1.Event{ReportingController: "kubelet"},
206+
expect: true,
207+
},
208+
{
209+
desc: "ExcludedController",
210+
controllers: []string{"kube-proxy", "kubelet"},
211+
event: &v1.Event{ReportingController: "default-scheduler"},
212+
expect: false,
213+
},
214+
{
215+
desc: "IncludeAll",
216+
controllers: []string{""},
217+
event: &v1.Event{Source: v1.EventSource{Component: "kubelet"}},
218+
expect: true,
219+
},
220+
}
221+
222+
for _, tc := range testCases {
223+
tc := tc
224+
t.Run(tc.desc, func(t *testing.T) {
225+
t.Parallel()
226+
got := includedController(tc.event, tc.controllers)
227+
if got != tc.expect {
228+
t.Fatalf("expected %t, got %t", tc.expect, got)
229+
}
230+
})
231+
}
232+
}

internal/options/options.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ import (
2727
)
2828

2929
const (
30-
// EventTypesAll is the argument to specify to allow all Event types.
31-
EventTypesAll = ""
30+
// EventTypeAll is the argument to specify to allow all Event types.
31+
EventTypeAll = ""
3232

33-
// APIGroupsAll is the argument to specify to allow objects from all API
33+
// APIGroupAll is the argument to specify to allow objects from all API
3434
// groups.
35-
APIGroupsAll = ""
35+
APIGroupAll = ""
36+
37+
// ReportingControllerAll is the argument to specify to allow Event
38+
// reported by all controllers.
39+
ReportingControllerAll = ""
3640
)
3741

3842
// Options are the configurable parameters for kube-events-exporter.
@@ -48,6 +52,7 @@ type Options struct {
4852
EventTypes []string
4953
InvolvedObjectAPIGroups []string
5054
InvolvedObjectNamespaces []string
55+
ReportingControllers []string
5156

5257
flags *pflag.FlagSet
5358
}
@@ -79,9 +84,10 @@ func (o *Options) AddFlags() {
7984
o.flags.IntVar(&o.ExporterPort, "exporter-port", 8081, "Port to expose kube-events-exporter own metrics on.")
8085
o.flags.BoolVar(&o.Version, "version", false, "kube-events-exporter version information")
8186

82-
o.flags.StringArrayVar(&o.EventTypes, "event-types", []string{EventTypesAll}, "List of allowed Event types. Defaults to all types.")
83-
o.flags.StringArrayVar(&o.InvolvedObjectAPIGroups, "involved-object-api-groups", []string{APIGroupsAll}, "List of allowed Event involved object API groups. Defaults to all API groups.")
87+
o.flags.StringArrayVar(&o.EventTypes, "event-types", []string{EventTypeAll}, "List of allowed Event types. Defaults to all types.")
88+
o.flags.StringArrayVar(&o.InvolvedObjectAPIGroups, "involved-object-api-groups", []string{APIGroupAll}, "List of allowed Event involved object API groups. Defaults to all API groups.")
8489
o.flags.StringArrayVar(&o.InvolvedObjectNamespaces, "involved-object-namespaces", []string{metav1.NamespaceAll}, "List of allowed Event involved object namespaces. Defaults to all namespaces.")
90+
o.flags.StringArrayVar(&o.ReportingControllers, "reporting-controllers", []string{ReportingControllerAll}, "List of controllers allowed to report Event. Defaults to all controllers.")
8591
}
8692

8793
// Parse parses the flag definitions from the argument list.

test/e2e/filter_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,35 @@ func TestEventTypeFilter(t *testing.T) {
8686
})
8787
}
8888
}
89+
90+
func TestControllerFilter(t *testing.T) {
91+
controller := framework.NewBasicEvent().Source.Component
92+
93+
testCases := []struct {
94+
name string
95+
controller string
96+
assert framework.AssertEventsTotalFunc
97+
}{
98+
{
99+
name: "Included",
100+
controller: controller,
101+
assert: f.AssertEventsTotalPresent,
102+
},
103+
{
104+
name: "Excluded",
105+
controller: fmt.Sprintf("not-%s", controller),
106+
assert: f.AssertEventsTotalAbsent,
107+
},
108+
}
109+
110+
for _, tc := range testCases {
111+
tc := tc
112+
t.Run(tc.name, func(t *testing.T) {
113+
f := *f
114+
f.ExporterArgs = []string{fmt.Sprintf("--reporting-controllers=%s", tc.controller)}
115+
exporter := f.CreateKubeEventsExporter(t)
116+
event := f.CreateBasicEvent(t)
117+
tc.assert(t, exporter, event, 1)
118+
})
119+
}
120+
}

test/framework/event.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func NewBasicEvent() *v1.Event {
8787
Kind: "Pod",
8888
Namespace: "default",
8989
},
90+
Source: v1.EventSource{
91+
Component: "kubelet",
92+
},
9093
Count: 1,
9194
Reason: "test",
9295
Type: v1.EventTypeNormal,

0 commit comments

Comments
 (0)