Skip to content

Commit

Permalink
compile regex once
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Dec 2, 2019
1 parent f0a6e56 commit 5475907
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 28 deletions.
91 changes: 91 additions & 0 deletions deploy/crds/eventlogger.bakito.ch_eventloggers_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,97 @@ spec:
scope: Namespaced
subresources:
status: {}
validation:
openAPIV3Schema:
description: EventLogger is the Schema for the eventloggers API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: EventLoggerSpec defines the desired state of EventLogger
properties:
eventTypes:
description: EventTypes the event types to log. If empty all events
are logged.
items:
type: string
minItems: 0
type: array
uniqueItems: true
kinds:
description: Kinds the kinds to logg the events for
items:
description: Kind defines a kind to loge events for
properties:
eventTypes:
description: EventTypes the event types to log. If empty events
are logged as defined in spec.
items:
type: string
minItems: 0
type: array
uniqueItems: true
matchingPatterns:
description: MatchingPatterns optional regex pattern that must
be contained in the message to be logged
items:
description: MatchingPattern defines a message matching pattern
properties:
pattern:
description: Pattern the match regex pattern
minLength: 3
type: string
skip:
description: Skip skip the entry if matched
type: boolean
required:
- pattern
type: object
minItems: 0
type: array
uniqueItems: true
name:
minLength: 3
type: string
required:
- name
type: object
minItems: 1
type: array
uniqueItems: true
required:
- kinds
type: object
status:
description: EventLoggerStatus defines the observed state of EventLogger
properties:
error:
description: Error
type: string
lastProcessed:
description: LastProcessed the timestamp the cr was last processed
type: string
operatorVersion:
description: OperatorVersion the version of the operator that processed
the cr
type: string
required:
- lastProcessed
- operatorVersion
type: object
required:
- spec
type: object
version: v1
versions:
- name: v1
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/eventlogger/v1/eventlogger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type Kind struct {
// +kubebuilder:validation:UniqueItems=true
// +listType=set
MatchingPatterns []string `json:"matchingPatterns,omitempty"`

// SkipOnMatch skip the entry if matched
SkipOnMatch *bool `json:"skip,omitempty"`
}

// EventLoggerStatus defines the observed state of EventLogger
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/eventlogger/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 41 additions & 11 deletions pkg/apis/eventlogger/v1/zz_generated.openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

func GetOpenAPIDefinitions(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
return map[string]common.OpenAPIDefinition{
"./pkg/apis/eventlogger/v1.EventLogger": schema_pkg_apis_eventlogger_v1_EventLogger(ref),
"./pkg/apis/eventlogger/v1.EventLoggerSpec": schema_pkg_apis_eventlogger_v1_EventLoggerSpec(ref),
"./pkg/apis/eventlogger/v1.EventLoggerStatus": schema_pkg_apis_eventlogger_v1_EventLoggerStatus(ref),
"./pkg/apis/eventlogger/v1.Kind": schema_pkg_apis_eventlogger_v1_Kind(ref),
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLogger": schema_pkg_apis_eventlogger_v1_EventLogger(ref),
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLoggerSpec": schema_pkg_apis_eventlogger_v1_EventLoggerSpec(ref),
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLoggerStatus": schema_pkg_apis_eventlogger_v1_EventLoggerStatus(ref),
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.Kind": schema_pkg_apis_eventlogger_v1_Kind(ref),
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.MatchingPattern": schema_pkg_apis_eventlogger_v1_MatchingPattern(ref),
}
}

Expand Down Expand Up @@ -46,20 +47,20 @@ func schema_pkg_apis_eventlogger_v1_EventLogger(ref common.ReferenceCallback) co
},
"spec": {
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/eventlogger/v1.EventLoggerSpec"),
Ref: ref("github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLoggerSpec"),
},
},
"status": {
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/eventlogger/v1.EventLoggerStatus"),
Ref: ref("github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLoggerStatus"),
},
},
},
Required: []string{"spec"},
},
},
Dependencies: []string{
"./pkg/apis/eventlogger/v1.EventLoggerSpec", "./pkg/apis/eventlogger/v1.EventLoggerStatus", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"},
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLoggerSpec", "github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.EventLoggerStatus", "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta"},
}
}

Expand All @@ -82,7 +83,7 @@ func schema_pkg_apis_eventlogger_v1_EventLoggerSpec(ref common.ReferenceCallback
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: ref("./pkg/apis/eventlogger/v1.Kind"),
Ref: ref("github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.Kind"),
},
},
},
Expand Down Expand Up @@ -112,7 +113,7 @@ func schema_pkg_apis_eventlogger_v1_EventLoggerSpec(ref common.ReferenceCallback
},
},
Dependencies: []string{
"./pkg/apis/eventlogger/v1.Kind"},
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.Kind"},
}
}

Expand Down Expand Up @@ -195,8 +196,7 @@ func schema_pkg_apis_eventlogger_v1_Kind(ref common.ReferenceCallback) common.Op
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "",
Ref: ref("github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.MatchingPattern"),
},
},
},
Expand All @@ -206,5 +206,35 @@ func schema_pkg_apis_eventlogger_v1_Kind(ref common.ReferenceCallback) common.Op
Required: []string{"name"},
},
},
Dependencies: []string{
"github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1.MatchingPattern"},
}
}

func schema_pkg_apis_eventlogger_v1_MatchingPattern(ref common.ReferenceCallback) common.OpenAPIDefinition {
return common.OpenAPIDefinition{
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Description: "MatchingPattern defines a message matching pattern",
Type: []string{"object"},
Properties: map[string]spec.Schema{
"pattern": {
SchemaProps: spec.SchemaProps{
Description: "Pattern the match regex pattern",
Type: []string{"string"},
Format: "",
},
},
"skip": {
SchemaProps: spec.SchemaProps{
Description: "Skip skip the entry if matched",
Type: []string{"boolean"},
Format: "",
},
},
},
Required: []string{"pattern"},
},
},
}
}
46 changes: 32 additions & 14 deletions pkg/controller/event/event_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,30 @@ type loggingPredicate struct {
predicate.Funcs
lastVersion string

kinds map[string]*eventloggerv1.Kind
kinds map[string]*filter
}

func (p *loggingPredicate) init(config *eventloggerv1.EventLoggerSpec) {
// TODO pre init regex pattern
p.kinds = make(map[string]*eventloggerv1.Kind)
p.kinds = make(map[string]*filter)
for _, k := range config.Kinds {
kp := &k
p.kinds[k.Name] = kp
p.kinds[k.Name] = &filter{
matchingPatterns: []*regexp.Regexp{},
}
if kp.EventTypes == nil {
kp.EventTypes = config.EventTypes
p.kinds[k.Name].eventTypes = config.EventTypes
} else {
p.kinds[k.Name].eventTypes = kp.EventTypes
}

if k.MatchingPatterns != nil {
if k.SkipOnMatch != nil && *k.SkipOnMatch {
p.kinds[k.Name].skipOnMatch = true
}
for _, mp := range k.MatchingPatterns {

p.kinds[k.Name].matchingPatterns = append(p.kinds[k.Name].matchingPatterns, regexp.MustCompile(mp))
}
}
}
}
Expand Down Expand Up @@ -95,29 +108,28 @@ func (p loggingPredicate) logEvent(mo metav1.Object, e runtime.Object) bool {
}

func (p *loggingPredicate) shouldLog(e *corev1.Event) bool {
k, ok := p.kinds[e.InvolvedObject.Kind]
f, ok := p.kinds[e.InvolvedObject.Kind]
if !ok {
return false
}

if !p.contains(k.EventTypes, e.Type) {
if !p.contains(f.eventTypes, e.Type) {
return false
}

return p.matches(k.MatchingPatterns, e.Message)
return p.matches(f.matchingPatterns, f.skipOnMatch, e.Message)
}

func (p *loggingPredicate) matches(list []string, val string) bool {
if len(list) == 0 {
func (p *loggingPredicate) matches(patterns []*regexp.Regexp, skipOnMatch bool, val string) bool {
if len(patterns) == 0 {
return true
}
for _, v := range list {
var p = regexp.MustCompile(v)
for _, p := range patterns {
if p.MatchString(val) {
return true
return !skipOnMatch
}
}
return false
return skipOnMatch
}

func (p *loggingPredicate) contains(list []string, val string) bool {
Expand All @@ -131,3 +143,9 @@ func (p *loggingPredicate) contains(list []string, val string) bool {
}
return false
}

type filter struct {
eventTypes []string
matchingPatterns []*regexp.Regexp
skipOnMatch bool
}
7 changes: 4 additions & 3 deletions pkg/controller/event/event_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package event

import (
"regexp"
"testing"

v1 "github.com/bakito/k8s-event-logger-operator/pkg/apis/eventlogger/v1"
Expand All @@ -11,9 +12,9 @@ import (
func Test_matches(t *testing.T) {
lp := &loggingPredicate{}

Assert(t, lp.matches([]string{"abc", "xyz"}, "abc"))
Assert(t, lp.matches([]string{"abc", "xyz"}, "^abc$"))
Assert(t, !lp.matches([]string{"abc", "xyz"}, "^ab$"))
Assert(t, lp.matches([]*regexp.Regexp{regexp.MustCompile("abc")}, false, "abc"))
Assert(t, lp.matches([]*regexp.Regexp{regexp.MustCompile("^abc$")}, false, "abc"))
Assert(t, !lp.matches([]*regexp.Regexp{regexp.MustCompile("^ab$")}, false, "abc"))
}

func Test_contains(t *testing.T) {
Expand Down

0 comments on commit 5475907

Please sign in to comment.