Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
return rsp, nil
}

func match(in *v1alpha1.Input, e corev1.Event) bool {
func match(in *v1alpha1.Input, e corev1.Event) bool { //nolint:gocyclo // This method is above our cyclomatic complexity level. Be wary of adding additional complexity.
ib, err := json.Marshal(in.InvolvedObject)
if err != nil {
return false
Expand Down Expand Up @@ -131,5 +131,9 @@ func match(in *v1alpha1.Input, e corev1.Event) bool {
if in.Type != "" && in.Type != e.Type {
return false
}

if in.Count != 0 && in.Count >= e.Count {
return false
}
return true
}
52 changes: 52 additions & 0 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,58 @@ func TestMatch(t *testing.T) {
match: true,
},
},
"MatchCount": {
args: args{
in: &v1alpha1.Input{
Type: "Normal",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Name: "oom-killed",
APIVersion: "v1",
},
Count: 1,
},
e: corev1.Event{
Type: "Normal",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Name: "oom-killed",
Namespace: "default",
APIVersion: "v1",
},
Count: 2,
},
},
want: want{
match: true,
},
},
"MatchNotEnoughEvents": {
args: args{
in: &v1alpha1.Input{
Type: "Normal",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Name: "oom-killed",
APIVersion: "v1",
},
Count: 3,
},
e: corev1.Event{
Type: "Normal",
InvolvedObject: corev1.ObjectReference{
Kind: "Pod",
Name: "oom-killed",
Namespace: "default",
APIVersion: "v1",
},
Count: 1,
},
},
want: want{
match: false,
},
},
}

for name, tc := range cases {
Expand Down
7 changes: 6 additions & 1 deletion input/v1alpha1/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ type Input struct {
// InvolvedObject specifies properties of the Object that can be used to
// filter incoming events.
// +optional
InvolvedObject corev1.ObjectReference `json:"involvedObject" protobuf:"bytes,2,opt,name=involvedObject"`
InvolvedObject corev1.ObjectReference `json:"involvedObject"`
// Count the minimal number of times the event has occurred.
// e.g. if Count: 1, then the event needs to have occurred more than 1
// time.
// +optional
Count int32 `json:"count"`
}
7 changes: 7 additions & 0 deletions package/input/filter.event.fn.upbound.io_inputs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ spec:
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
count:
description: |-
Count the minimal number of times the event has occurred.
e.g. if Count: 1, then the event needs to have occurred more than 1
time.
format: int32
type: integer
involvedObject:
description: |-
InvolvedObject specifies properties of the Object that can be used to
Expand Down