Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUA filter inline code support #1081

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apis/fluentbit/v1alpha2/plugins/filter/lua_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @chrono2002 , looks like there're indent issues here.
You may need to run :

make generate
make manifests
make fmt
make vet
make docs-update

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -341,7 +344,6 @@ spec:
type: array
required:
- call
- script
type: object
modify:
description: Modify defines Modify Filter configuration.
Expand Down
Loading