Skip to content
This repository was archived by the owner on Aug 31, 2022. It is now read-only.

feat: support match event with namespace-labels #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
39 changes: 27 additions & 12 deletions pkg/exporter/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ func matchString(pattern, s string) bool {

// Rule is for matching an event
type Rule struct {
Labels map[string]string
Annotations map[string]string
Message string
APIVersion string
Kind string
Namespace string
Reason string
Type string
MinCount int32
Component string
Host string
Receiver string
Labels map[string]string
Annotations map[string]string
NamespaceLabels map[string]string
Message string
APIVersion string
Kind string
Namespace string
Reason string
Type string
MinCount int32
Component string
Host string
Receiver string
}

// MatchesEvent compares the rule to an event and returns a boolean value to indicate
Expand Down Expand Up @@ -84,6 +85,20 @@ func (r *Rule) MatchesEvent(ev *kube.EnhancedEvent) bool {
}
}

// NamespaceLabels are also mutually exclusive, they all need to be present
if r.NamespaceLabels != nil && len(r.NamespaceLabels) > 0 {
for k, v := range r.NamespaceLabels {
if val, ok := ev.InvolvedObject.NamespaceLabels[k]; !ok {
return false
} else {
matches := matchString(v, val)
if !matches {
return false
}
}
}
}

// If minCount is not given via a config, it's already 0 and the count is already 1 and this passes.
if ev.Count >= r.MinCount {
return true
Expand Down
2 changes: 2 additions & 0 deletions pkg/kube/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func (e EnhancedEvent) DeDot() EnhancedEvent {
c.Annotations = dedotMap(e.Annotations)
c.InvolvedObject.Labels = dedotMap(e.InvolvedObject.Labels)
c.InvolvedObject.Annotations = dedotMap(e.InvolvedObject.Annotations)
c.InvolvedObject.NamespaceLabels = dedotMap(e.InvolvedObject.NamespaceLabels)
return c
}

Expand All @@ -40,6 +41,7 @@ type EnhancedObjectReference struct {
corev1.ObjectReference `json:",inline"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
NamespaceLabels map[string]string `json:"namespaceLabels,omitempty"`
}

// ToJSON does not return an error because we are %99 confident it is JSON serializable.
Expand Down
52 changes: 52 additions & 0 deletions pkg/kube/namespace_labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package kube

import (
"context"

lru "github.com/hashicorp/golang-lru"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

type NamespaceLabelCache struct {
dynClient dynamic.Interface
clientset *kubernetes.Clientset

cache *lru.ARCCache
}

func NewNamespaceLabelCache(kubeconfig *rest.Config) *NamespaceLabelCache {
cache, err := lru.NewARC(1024)
if err != nil {
panic("cannot init cache: " + err.Error())
}
return &NamespaceLabelCache{
dynClient: dynamic.NewForConfigOrDie(kubeconfig),
clientset: kubernetes.NewForConfigOrDie(kubeconfig),
cache: cache,
}
}

func (n *NamespaceLabelCache) GetNamespaceLabelsWithCache(nsName string) (map[string]string, error) {
if val, ok := n.cache.Get(nsName); ok {
return val.(map[string]string), nil
}

ns, err := n.clientset.CoreV1().Namespaces().Get(context.Background(), nsName, metav1.GetOptions{})
if err == nil {
nsLabels := ns.GetLabels()
n.cache.Add(nsName, nsLabels)
return nsLabels, nil
}

if errors.IsNotFound(err) {
var empty map[string]string
n.cache.Add(nsName, empty)
return nil, nil
}

return nil, err
}
33 changes: 21 additions & 12 deletions pkg/kube/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
type EventHandler func(event *EnhancedEvent)

type EventWatcher struct {
informer cache.SharedInformer
stopper chan struct{}
labelCache *LabelCache
annotationCache *AnnotationCache
fn EventHandler
throttlePeriod time.Duration
informer cache.SharedInformer
stopper chan struct{}
labelCache *LabelCache
annotationCache *AnnotationCache
namespaceLabelCache *NamespaceLabelCache
fn EventHandler
throttlePeriod time.Duration
}

func NewEventWatcher(config *rest.Config, namespace string, throttlePeriod int64, fn EventHandler) *EventWatcher {
Expand All @@ -28,12 +29,13 @@ func NewEventWatcher(config *rest.Config, namespace string, throttlePeriod int64
informer := factory.Core().V1().Events().Informer()

watcher := &EventWatcher{
informer: informer,
stopper: make(chan struct{}),
labelCache: NewLabelCache(config),
annotationCache: NewAnnotationCache(config),
fn: fn,
throttlePeriod: time.Second*time.Duration(throttlePeriod),
informer: informer,
stopper: make(chan struct{}),
labelCache: NewLabelCache(config),
annotationCache: NewAnnotationCache(config),
namespaceLabelCache: NewNamespaceLabelCache(config),
fn: fn,
throttlePeriod: time.Second * time.Duration(throttlePeriod),
}

informer.AddEventHandler(watcher)
Expand Down Expand Up @@ -87,6 +89,13 @@ func (e *EventWatcher) onEvent(event *corev1.Event) {
ev.InvolvedObject.ObjectReference = *event.InvolvedObject.DeepCopy()
}

namespaceLabels, err := e.namespaceLabelCache.GetNamespaceLabelsWithCache(event.Namespace)
if err != nil {
log.Error().Err(err).Msg("Cannot list namespace labels of the object")
} else {
ev.InvolvedObject.NamespaceLabels = namespaceLabels
}

e.fn(ev)
return
}
Expand Down