Skip to content

Commit

Permalink
introduce interface for filters that are namespaceable
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Freitag <[email protected]>
  • Loading branch information
MarkusFreitag committed Apr 22, 2024
1 parent dc1eedd commit ae9a676
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
28 changes: 3 additions & 25 deletions apis/fluentbit/v1alpha2/filter_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ package v1alpha2

import (
"bytes"
"crypto/md5"
"fmt"
"reflect"
"sort"
"strings"

"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins"
"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins/custom"
"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins/filter"
"github.com/fluent/fluent-operator/v2/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -83,27 +79,9 @@ func (list FilterList) Load(sl plugins.SecretLoader) (string, error) {
buf.WriteString(fmt.Sprintf(" Match_Regex %s\n", utils.GenerateNamespacedMatchRegExpr(item.Namespace, item.Spec.MatchRegex)))
}

switch f := p.(type) {
case *filter.Kubernetes:
kubeTagPrefix := f.KubeTagPrefix
if kubeTagPrefix == "" {
kubeTagPrefix = "kube.var.log.containers."
}
f.KubeTagPrefix = fmt.Sprintf("%x.%s", md5.Sum([]byte(item.Namespace)), kubeTagPrefix)
if f.RegexParser != "" {
f.RegexParser = fmt.Sprintf("%s-%x", f.RegexParser, md5.Sum([]byte(item.Namespace)))
}
case *filter.Parser:
parsers := strings.Split(f.Parser, ",")
for i := range parsers {
parsers[i] = strings.Trim(parsers[i], " ")
parsers[i] = fmt.Sprintf("%s-%x", parsers[i], md5.Sum([]byte(item.Namespace)))
}
f.Parser = strings.Join(parsers, ",")
case *custom.CustomPlugin:
if f.Config != "" {
f.Config = custom.MakeCustomConfigNamespaced(f.Config, item.Namespace)
}
var iface interface{} = p
if f, ok := iface.(plugins.Namespaceable); ok {
f.MakeNamespaced(item.Namespace)
}

kvs, err := p.Params(sl)
Expand Down
9 changes: 8 additions & 1 deletion apis/fluentbit/v1alpha2/plugins/custom/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package custom
import (
"bytes"
"fmt"
"github.com/fluent/fluent-operator/v2/pkg/utils"
"strings"

"github.com/fluent/fluent-operator/v2/pkg/utils"

"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins"
"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins/params"
)
Expand All @@ -28,6 +29,12 @@ func (a *CustomPlugin) Params(_ plugins.SecretLoader) (*params.KVs, error) {
return kvs, nil
}

func (c *CustomPlugin) MakeNamespaced(ns string) {
if c.Config != "" {
c.Config = MakeCustomConfigNamespaced(c.Config, ns)
}
}

func indentation(str string) string {
splits := strings.Split(str, "\n")
var buf bytes.Buffer
Expand Down
11 changes: 11 additions & 0 deletions apis/fluentbit/v1alpha2/plugins/filter/kubernetes_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"crypto/md5"
"fmt"

"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins"
Expand Down Expand Up @@ -189,3 +190,13 @@ func (k *Kubernetes) Params(_ plugins.SecretLoader) (*params.KVs, error) {
}
return kvs, nil
}

func (k *Kubernetes) MakeNamespaced(ns string) {
if k.KubeTagPrefix == "" {
k.KubeTagPrefix = "kube.var.log.containers."
}
k.KubeTagPrefix = fmt.Sprintf("%x.%s", md5.Sum([]byte(ns)), k.KubeTagPrefix)
if k.RegexParser != "" {
k.RegexParser = fmt.Sprintf("%s-%x", k.RegexParser, md5.Sum([]byte(ns)))
}
}
10 changes: 10 additions & 0 deletions apis/fluentbit/v1alpha2/plugins/filter/parser_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"crypto/md5"
"fmt"
"strings"

Expand Down Expand Up @@ -59,3 +60,12 @@ func (p *Parser) Params(_ plugins.SecretLoader) (*params.KVs, error) {
}
return kvs, nil
}

func (p *Parser) MakeNamespaced(ns string) {
parsers := strings.Split(p.Parser, ",")
for i := range parsers {
parsers[i] = strings.Trim(parsers[i], " ")
parsers[i] = fmt.Sprintf("%s-%x", parsers[i], md5.Sum([]byte(ns)))
}
p.Parser = strings.Join(parsers, ",")
}
6 changes: 6 additions & 0 deletions apis/fluentbit/v1alpha2/plugins/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ type Plugin interface {
Name() string
Params(SecretLoader) (*params.KVs, error)
}

// The Namespaceable interface defines a method for adding a namespace
// to a plugins identifier.
type Namespaceable interface {
MakeNamespaced(string)
}

0 comments on commit ae9a676

Please sign in to comment.