From 382ada76b298a125eb7b1ac7feb88d2dce86ff8d Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Wed, 6 Mar 2024 14:28:53 +0200 Subject: [PATCH 1/2] lua inline code support added Signed-off-by: Paul Smith --- .../v1alpha2/plugins/filter/lua_types.go | 26 ++++++++++++++++++- .../crds/fluentbit.fluent.io_filters.yaml | 4 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go index 0719f8ef5..09cce2afc 100644 --- a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go +++ b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go @@ -3,6 +3,7 @@ package filter import ( "strconv" "strings" + "regexp" "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins" "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins/params" @@ -20,6 +21,8 @@ type Lua struct { // Lua function name that will be triggered to do filtering. // It's assumed that the function is declared inside the Script defined above. Call string `json:"call"` + // Inline LUA code instead of loading from a path via script. + Code string `json:"code"` // If these keys are matched, the fields are converted to integer. // If more than one key, delimit by space. // Note that starting from Fluent Bit v1.6 integer data types are preserved @@ -45,7 +48,28 @@ func (l *Lua) Params(_ plugins.SecretLoader) (*params.KVs, error) { if err != nil { return kvs, err } - kvs.Insert("script", "/fluent-bit/config/"+l.Script.Key) + + if l.Code != "" { + var singleLineLua string = "" + var lineTrim = "" + for _, line := range strings.Split(strings.TrimSuffix(l.Code, "\n"), "\n") { + lineTrim = strings.TrimSpace(line) + if lineTrim != "" { + operator, _ := regexp.MatchString("^function |^if |^for |^else|^elseif |^end|--[[]+", lineTrim) + if operator { + singleLineLua = singleLineLua + lineTrim + " " + } else { + singleLineLua = singleLineLua + lineTrim + "; " + } + } + } + kvs.Insert("code", singleLineLua) + } + + if l.Script.Key != "" { + kvs.Insert("script", "/fluent-bit/config/"+l.Script.Key) + } + kvs.Insert("call", l.Call) if l.TypeIntKey != nil && len(l.TypeIntKey) > 0 { diff --git a/charts/fluent-operator/charts/fluent-bit-crds/crds/fluentbit.fluent.io_filters.yaml b/charts/fluent-operator/charts/fluent-bit-crds/crds/fluentbit.fluent.io_filters.yaml index f84c8b4d2..814ae6bbb 100644 --- a/charts/fluent-operator/charts/fluent-bit-crds/crds/fluentbit.fluent.io_filters.yaml +++ b/charts/fluent-operator/charts/fluent-bit-crds/crds/fluentbit.fluent.io_filters.yaml @@ -289,6 +289,9 @@ spec: do filtering. It's assumed that the function is declared inside the Script defined above. type: string + code: + description: Inline LUA code instead of loading from a path via script. + type: string protectedMode: description: If enabled, Lua script will be executed in protected mode. It prevents to crash when invalid Lua @@ -341,7 +344,6 @@ spec: type: array required: - call - - script type: object modify: description: Modify defines Modify Filter configuration. From 7cb0e1d7fa2059554eb1e5524299e83c901745f6 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Thu, 7 Mar 2024 21:25:08 +0200 Subject: [PATCH 2/2] indent fix Signed-off-by: Paul Smith --- .../v1alpha2/plugins/filter/lua_types.go | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go index 09cce2afc..b64e839a5 100644 --- a/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go +++ b/apis/fluentbit/v1alpha2/plugins/filter/lua_types.go @@ -3,7 +3,7 @@ package filter import ( "strconv" "strings" - "regexp" + "regexp" "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins" "github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins/params" @@ -21,7 +21,7 @@ type Lua struct { // Lua function name that will be triggered to do filtering. // It's assumed that the function is declared inside the Script defined above. Call string `json:"call"` - // Inline LUA code instead of loading from a path via script. + // Inline LUA code instead of loading from a path via script. Code string `json:"code"` // If these keys are matched, the fields are converted to integer. // If more than one key, delimit by space. @@ -49,26 +49,26 @@ func (l *Lua) Params(_ plugins.SecretLoader) (*params.KVs, error) { return kvs, err } - if l.Code != "" { - var singleLineLua string = "" - var lineTrim = "" - for _, line := range strings.Split(strings.TrimSuffix(l.Code, "\n"), "\n") { - lineTrim = strings.TrimSpace(line) - if lineTrim != "" { - operator, _ := regexp.MatchString("^function |^if |^for |^else|^elseif |^end|--[[]+", lineTrim) - if operator { - singleLineLua = singleLineLua + lineTrim + " " - } else { - singleLineLua = singleLineLua + lineTrim + "; " - } - } - } - kvs.Insert("code", singleLineLua) - } + if l.Code != "" { + var singleLineLua string = "" + var lineTrim = "" + for _, line := range strings.Split(strings.TrimSuffix(l.Code, "\n"), "\n") { + lineTrim = strings.TrimSpace(line) + if lineTrim != "" { + operator, _ := regexp.MatchString("^function |^if |^for |^else|^elseif |^end|--[[]+", lineTrim) + if operator { + singleLineLua = singleLineLua + lineTrim + " " + } else { + singleLineLua = singleLineLua + lineTrim + "; " + } + } + } + kvs.Insert("code", singleLineLua) + } - if l.Script.Key != "" { - kvs.Insert("script", "/fluent-bit/config/"+l.Script.Key) - } + if l.Script.Key != "" { + kvs.Insert("script", "/fluent-bit/config/"+l.Script.Key) + } kvs.Insert("call", l.Call)