Skip to content

Commit 0957492

Browse files
authored
Merge pull request #1081 from chrono2002/lua2
LUA filter inline code support
2 parents 82dc7a9 + 7cb0e1d commit 0957492

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

apis/fluentbit/v1alpha2/plugins/filter/lua_types.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package filter
33
import (
44
"strconv"
55
"strings"
6+
"regexp"
67

78
"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins"
89
"github.com/fluent/fluent-operator/v2/apis/fluentbit/v1alpha2/plugins/params"
@@ -20,6 +21,8 @@ type Lua struct {
2021
// Lua function name that will be triggered to do filtering.
2122
// It's assumed that the function is declared inside the Script defined above.
2223
Call string `json:"call"`
24+
// Inline LUA code instead of loading from a path via script.
25+
Code string `json:"code"`
2326
// If these keys are matched, the fields are converted to integer.
2427
// If more than one key, delimit by space.
2528
// 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) {
4548
if err != nil {
4649
return kvs, err
4750
}
48-
kvs.Insert("script", "/fluent-bit/config/"+l.Script.Key)
51+
52+
if l.Code != "" {
53+
var singleLineLua string = ""
54+
var lineTrim = ""
55+
for _, line := range strings.Split(strings.TrimSuffix(l.Code, "\n"), "\n") {
56+
lineTrim = strings.TrimSpace(line)
57+
if lineTrim != "" {
58+
operator, _ := regexp.MatchString("^function |^if |^for |^else|^elseif |^end|--[[]+", lineTrim)
59+
if operator {
60+
singleLineLua = singleLineLua + lineTrim + " "
61+
} else {
62+
singleLineLua = singleLineLua + lineTrim + "; "
63+
}
64+
}
65+
}
66+
kvs.Insert("code", singleLineLua)
67+
}
68+
69+
if l.Script.Key != "" {
70+
kvs.Insert("script", "/fluent-bit/config/"+l.Script.Key)
71+
}
72+
4973
kvs.Insert("call", l.Call)
5074

5175
if l.TypeIntKey != nil && len(l.TypeIntKey) > 0 {

charts/fluent-operator/charts/fluent-bit-crds/crds/fluentbit.fluent.io_filters.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ spec:
289289
do filtering. It's assumed that the function is declared
290290
inside the Script defined above.
291291
type: string
292+
code:
293+
description: Inline LUA code instead of loading from a path via script.
294+
type: string
292295
protectedMode:
293296
description: If enabled, Lua script will be executed in
294297
protected mode. It prevents to crash when invalid Lua
@@ -341,7 +344,6 @@ spec:
341344
type: array
342345
required:
343346
- call
344-
- script
345347
type: object
346348
modify:
347349
description: Modify defines Modify Filter configuration.

0 commit comments

Comments
 (0)