Skip to content

Commit

Permalink
Merge pull request #1342 from cw-Guo/feat/script
Browse files Browse the repository at this point in the history
feat: support lua filter in namespaced CRD
  • Loading branch information
benjaminhuo authored Sep 14, 2024
2 parents 6298d40 + 47ac1d3 commit 88a1614
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
22 changes: 22 additions & 0 deletions apis/fluentbit/v1alpha2/clusterfluentbitconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha2

import (
"bytes"
"crypto/md5"
"fmt"

"sort"
Expand Down Expand Up @@ -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("%x-%s", md5.Sum([]byte(f.ObjectMeta.Namespace)), p.Lua.Script.Key)
scripts = append(scripts, Script{Name: namespacedScriptName, Content: script})
}
}
}
}
sort.Sort(ByName(scripts))
return scripts, nil
}
6 changes: 6 additions & 0 deletions apis/fluentbit/v1alpha2/plugins/filter/lua_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package filter

import (
"crypto/md5"
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -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("%x-%s", md5.Sum([]byte(ns)), l.Script.Key)
}
20 changes: 14 additions & 6 deletions controllers/fluentbitconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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{
Expand Down

0 comments on commit 88a1614

Please sign in to comment.