diff --git a/Dockerfile b/Dockerfile index 5d2d1001..825bcb06 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM golang:1.20 AS builder ARG VERSION -ENV PKG github.com/resmoio/kubernetes-event-exporter/pkg +ENV PKG=github.com/resmoio/kubernetes-event-exporter/pkg ADD . /app WORKDIR /app diff --git a/config.yaml b/config.yaml index f5f68636..b5b7fb1f 100644 --- a/config.yaml +++ b/config.yaml @@ -4,7 +4,26 @@ clusterName: my-super-local-cluster route: routes: - match: - - receiver: "dump" + - receiver: "loki" + receivers: - - name: "dump" - stdout: {} \ No newline at end of file + - name: "loki" + loki: + headers: # optional + Authorization: Basic $LOKI_BASIC_AUTH_BASE64 + streamLabels: + stream: event + cluster: local-test + url: $LOKI_URL + layout: +# namespace: "{{ .Namespace }}" + message: "{{ .Message }}" + type: "{{ .Type }}" + reason: "{{ .Reason }}" + kind: "{{ .InvolvedObject.Kind }}" + name: "{{.InvolvedObject.Name }}" + source: "{{ .Source.Component }}" +# ignore_namespaces: +# - namespace_one +# - namespace_two +# - namespace_three diff --git a/pkg/sinks/loki.go b/pkg/sinks/loki.go index 4d0a0d93..aae0cdb7 100644 --- a/pkg/sinks/loki.go +++ b/pkg/sinks/loki.go @@ -7,11 +7,11 @@ import ( "errors" "fmt" "github.com/resmoio/kubernetes-event-exporter/pkg/kube" + "github.com/rs/zerolog/log" "io/ioutil" "net/http" "strconv" "time" - "github.com/rs/zerolog/log" ) type promtailStream struct { @@ -24,11 +24,12 @@ type LokiMsg struct { } type LokiConfig struct { - Layout map[string]interface{} `yaml:"layout"` - StreamLabels map[string]string `yaml:"streamLabels"` - TLS TLS `yaml:"tls"` - URL string `yaml:"url"` - Headers map[string]string `yaml:"headers"` + Layout map[string]interface{} `yaml:"layout"` + StreamLabels map[string]string `yaml:"streamLabels"` + TLS TLS `yaml:"tls"` + URL string `yaml:"url"` + Headers map[string]string `yaml:"headers"` + IgnoreNamespaces []string `yaml:"ignore_namespaces"` } type Loki struct { @@ -52,10 +53,30 @@ func generateTimestamp() string { } func (l *Loki) Send(ctx context.Context, ev *kube.EnhancedEvent) error { + if ev.InvolvedObject.Kind == "Node" { + l.cfg.StreamLabels["host"] = ev.InvolvedObject.Name + delete(l.cfg.Layout, "name") + } else { + l.cfg.Layout["name"] = "{{ .InvolvedObject.Name }}" + } + eventBody, err := serializeEventWithLayout(l.cfg.Layout, ev) if err != nil { return err } + + for _, namespace := range l.cfg.IgnoreNamespaces { + if namespace == ev.InvolvedObject.Namespace { + log.Debug().Msgf("Skipping %s namespace, because it is in ignore list", ev.InvolvedObject.Namespace) + return nil + } + } + + if ev.InvolvedObject.Namespace != "" { + l.cfg.StreamLabels["namespace"] = ev.InvolvedObject.Namespace + l.cfg.StreamLabels["index"] = l.cfg.StreamLabels["cluster"] + "-" + ev.InvolvedObject.Namespace + } + timestamp := generateTimestamp() a := LokiMsg{ Streams: []promtailStream{{ @@ -91,6 +112,10 @@ func (l *Loki) Send(ctx context.Context, ev *kube.EnhancedEvent) error { return err } + delete(l.cfg.StreamLabels, "namespace") + delete(l.cfg.StreamLabels, "index") + delete(l.cfg.StreamLabels, "host") + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body)