From ae9a6767b056814aa505b0e8fa6f579b5d38f418 Mon Sep 17 00:00:00 2001 From: Markus Freitag Date: Mon, 22 Apr 2024 11:57:20 +0200 Subject: [PATCH] introduce interface for filters that are namespaceable Signed-off-by: Markus Freitag --- apis/fluentbit/v1alpha2/filter_types.go | 28 ++----------------- .../v1alpha2/plugins/custom/custom_types.go | 9 +++++- .../plugins/filter/kubernetes_types.go | 11 ++++++++ .../v1alpha2/plugins/filter/parser_types.go | 10 +++++++ apis/fluentbit/v1alpha2/plugins/interface.go | 6 ++++ 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/apis/fluentbit/v1alpha2/filter_types.go b/apis/fluentbit/v1alpha2/filter_types.go index 3e5c87cd9..e3c9b671d 100644 --- a/apis/fluentbit/v1alpha2/filter_types.go +++ b/apis/fluentbit/v1alpha2/filter_types.go @@ -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" ) @@ -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) diff --git a/apis/fluentbit/v1alpha2/plugins/custom/custom_types.go b/apis/fluentbit/v1alpha2/plugins/custom/custom_types.go index ab3dbb8b0..1c9659822 100644 --- a/apis/fluentbit/v1alpha2/plugins/custom/custom_types.go +++ b/apis/fluentbit/v1alpha2/plugins/custom/custom_types.go @@ -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" ) @@ -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 diff --git a/apis/fluentbit/v1alpha2/plugins/filter/kubernetes_types.go b/apis/fluentbit/v1alpha2/plugins/filter/kubernetes_types.go index bd5d89a4f..99493a34f 100644 --- a/apis/fluentbit/v1alpha2/plugins/filter/kubernetes_types.go +++ b/apis/fluentbit/v1alpha2/plugins/filter/kubernetes_types.go @@ -1,6 +1,7 @@ package filter import ( + "crypto/md5" "fmt" "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins" @@ -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))) + } +} diff --git a/apis/fluentbit/v1alpha2/plugins/filter/parser_types.go b/apis/fluentbit/v1alpha2/plugins/filter/parser_types.go index 9aa4e0a3a..dfdfbcfb3 100644 --- a/apis/fluentbit/v1alpha2/plugins/filter/parser_types.go +++ b/apis/fluentbit/v1alpha2/plugins/filter/parser_types.go @@ -1,6 +1,7 @@ package filter import ( + "crypto/md5" "fmt" "strings" @@ -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, ",") +} diff --git a/apis/fluentbit/v1alpha2/plugins/interface.go b/apis/fluentbit/v1alpha2/plugins/interface.go index 6ac42caa2..f21ebad28 100644 --- a/apis/fluentbit/v1alpha2/plugins/interface.go +++ b/apis/fluentbit/v1alpha2/plugins/interface.go @@ -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) +}