From 6c926d23adab4db06f2cb4c8f6d3c7d8cb16b578 Mon Sep 17 00:00:00 2001 From: juicer Date: Fri, 13 Sep 2024 19:43:49 -0700 Subject: [PATCH 1/2] support namespaced lua filter Signed-off-by: juicer --- .../v1alpha2/clusterfluentbitconfig_types.go | 22 +++++++++++++++++++ .../v1alpha2/plugins/filter/lua_types.go | 6 +++++ controllers/fluentbitconfig_controller.go | 20 ++++++++++++----- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go index 736e159a7..4edeb1678 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go @@ -18,6 +18,7 @@ package v1alpha2 import ( "bytes" + "crypto/md5" "fmt" "sort" @@ -537,3 +538,24 @@ func (cfg ClusterFluentBitConfig) RenderLuaScript( return scripts, nil } + +func (cfg ClusterFluentBitConfig) RenderNamespacedLuaScript( + cl plugins.ConfigMapLoader, nsfiltersLists []FilterList) ([]Script, error) { + scripts := make([]Script, 0) + for _, nsfilters := range nsfiltersLists { + for _, f := range nsfilters.Items { + for _, p := range f.Spec.FilterItems { + if p.Lua != nil && p.Lua.Script.Key != "" { + script, err := cl.LoadConfigMap(p.Lua.Script, f.ObjectMeta.Namespace) + if err != nil { + return nil, err + } + namespacedScriptName := fmt.Sprintf("%s-%x", p.Lua.Script.Key, md5.Sum([]byte(f.ObjectMeta.Namespace))) + scripts = append(scripts, Script{Name: namespacedScriptName, Content: script}) + } + } + } + } + sort.Sort(ByName(scripts)) + return scripts, nil +} diff --git a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go index 5aee10c60..762d82ad2 100644 --- a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go +++ b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go @@ -1,6 +1,8 @@ package filter import ( + "crypto/md5" + "fmt" "regexp" "strconv" "strings" @@ -93,3 +95,7 @@ func (l *Lua) Params(_ plugins.SecretLoader) (*params.KVs, error) { return kvs, nil } + +func (l *Lua) MakeNamespaced(ns string) { + l.Script.Key = fmt.Sprintf("%s-%x", l.Script.Key, md5.Sum([]byte(ns))) +} diff --git a/controllers/fluentbitconfig_controller.go b/controllers/fluentbitconfig_controller.go index 2f4fff39b..1fa149e56 100644 --- a/controllers/fluentbitconfig_controller.go +++ b/controllers/fluentbitconfig_controller.go @@ -154,12 +154,26 @@ func (r *FluentBitConfigReconciler) Reconcile(ctx context.Context, req ctrl.Requ if err != nil { return ctrl.Result{}, err } + var ns string if cfg.Spec.Namespace != nil { ns = fmt.Sprintf(*cfg.Spec.Namespace) } else { ns = os.Getenv("NAMESPACE") } + cl := plugins.NewConfigMapLoader(r.Client, ns) + // load scripts for namespaced filters + nsScripts, err := cfg.RenderNamespacedLuaScript(cl, nsFilterLists) + if err != nil { + return ctrl.Result{}, err + } + // load scripts for cluster filters + scripts, err := cfg.RenderLuaScript(cl, filters, ns) + if err != nil { + return ctrl.Result{}, err + } + scripts = append(scripts, nsScripts...) + // Inject config data into Secret sl := plugins.NewSecretLoader(r.Client, ns) mainAppCfg, err := cfg.RenderMainConfigWithTargetFormat( @@ -179,12 +193,6 @@ func (r *FluentBitConfigReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, err } - cl := plugins.NewConfigMapLoader(r.Client, ns) - scripts, err := cfg.RenderLuaScript(cl, filters, ns) - if err != nil { - return ctrl.Result{}, err - } - // Create or update the corresponding Secret sec := &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ From 47ac1d3d7065fcc003a15b154c64d1deec47a175 Mon Sep 17 00:00:00 2001 From: juicer Date: Fri, 13 Sep 2024 20:31:39 -0700 Subject: [PATCH 2/2] adjust namespaced lua script name Signed-off-by: juicer --- apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go | 2 +- apis/fluentbit/v1alpha2/plugins/filter/lua_types.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go index 4edeb1678..54c1d3f9f 100644 --- a/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go +++ b/apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go @@ -550,7 +550,7 @@ func (cfg ClusterFluentBitConfig) RenderNamespacedLuaScript( if err != nil { return nil, err } - namespacedScriptName := fmt.Sprintf("%s-%x", p.Lua.Script.Key, md5.Sum([]byte(f.ObjectMeta.Namespace))) + namespacedScriptName := fmt.Sprintf("%x-%s", md5.Sum([]byte(f.ObjectMeta.Namespace)), p.Lua.Script.Key) scripts = append(scripts, Script{Name: namespacedScriptName, Content: script}) } } diff --git a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go index 762d82ad2..7025b1a15 100644 --- a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go +++ b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go @@ -97,5 +97,5 @@ func (l *Lua) Params(_ plugins.SecretLoader) (*params.KVs, error) { } func (l *Lua) MakeNamespaced(ns string) { - l.Script.Key = fmt.Sprintf("%s-%x", l.Script.Key, md5.Sum([]byte(ns))) + l.Script.Key = fmt.Sprintf("%x-%s", md5.Sum([]byte(ns)), l.Script.Key) }