Skip to content

Commit 0203471

Browse files
Jakub Suchymjuraga
authored andcommitted
MINOR: filter: add support for lua filters
Add a new "lua" filter type accepting the "filter lua.<name> [args...]" directive. This includes the config-parser Lua parser and dispatch, the specification enum and lua_name/lua_args fields, the regenerated models, and the configuration CRUD wiring.
1 parent df1681f commit 0203471

11 files changed

Lines changed: 170 additions & 6 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Copyright 2019 HAProxy Technologies
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package filters
18+
19+
import (
20+
"strings"
21+
22+
"github.com/haproxytech/client-native/v6/config-parser/common"
23+
"github.com/haproxytech/client-native/v6/config-parser/errors"
24+
)
25+
26+
// Lua represents a Lua filter declared with "filter lua.<name> [args...]".
27+
// The filter keyword is the name registered from Lua via core.register_filter().
28+
type Lua struct {
29+
Name string
30+
Args []string
31+
Comment string
32+
}
33+
34+
func (f *Lua) Parse(parts []string, comment string) error {
35+
if comment != "" {
36+
f.Comment = comment
37+
}
38+
if len(parts) < 2 {
39+
return errors.ErrInvalidData
40+
}
41+
name := strings.TrimPrefix(parts[1], "lua.")
42+
if name == "" || name == parts[1] {
43+
return errors.ErrInvalidData
44+
}
45+
f.Name = name
46+
if len(parts) > 2 {
47+
f.Args = append([]string{}, parts[2:]...)
48+
}
49+
return nil
50+
}
51+
52+
func (f *Lua) Result() common.ReturnResultLine {
53+
var result strings.Builder
54+
result.WriteString("filter lua.")
55+
result.WriteString(f.Name)
56+
for _, arg := range f.Args {
57+
result.WriteString(" ")
58+
result.WriteString(arg)
59+
}
60+
return common.ReturnResultLine{
61+
Data: result.String(),
62+
Comment: f.Comment,
63+
}
64+
}

config-parser/parsers/filters/filter.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package filters
1818

1919
import (
20+
"strings"
21+
2022
"github.com/haproxytech/client-native/v6/config-parser/common"
2123
"github.com/haproxytech/client-native/v6/config-parser/errors"
2224
"github.com/haproxytech/client-native/v6/config-parser/types"
@@ -60,7 +62,12 @@ func (h *Filters) Parse(line string, parts []string, comment string) (string, er
6062
case "bwlim-in", "bwlim-out":
6163
err = h.ParseFilter(&BandwidthLimit{}, parts, comment)
6264
default:
63-
return "", &errors.ParseError{Parser: "FilterLines", Line: line}
65+
// Lua filters use a dynamic keyword "lua.<name>", so they can't be matched as a fixed case above.
66+
if strings.HasPrefix(parts[1], "lua.") {
67+
err = h.ParseFilter(&Lua{}, parts, comment)
68+
} else {
69+
return "", &errors.ParseError{Parser: "FilterLines", Line: line}
70+
}
6471
}
6572
if err != nil {
6673
return "", err

config-parser/tests/filter_generated_test.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config-parser/types/types-other.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ type Filter interface {
209209
//test:ok:filter trace random-forwarding hexdump
210210
//test:ok:filter trace hexdump
211211
//test:ok:filter trace
212+
//test:ok:filter lua.my-filter
213+
//test:ok:filter lua.my-filter arg1 arg2
214+
//test:fail:filter lua
215+
//test:fail:filter lua.
212216
//test:fail:filter bwlim-in
213217
//test:fail:filter bwlim-in name
214218
//test:fail:filter bwlim-in name default-limit

configuration/filter.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ func ParseFilter(f types.Filter) *models.Filter {
317317
Type: "cache",
318318
CacheName: v.Name,
319319
}
320+
case *filters.Lua:
321+
return &models.Filter{
322+
Type: models.FilterTypeLua,
323+
LuaName: v.Name,
324+
LuaArgs: append([]string{}, v.Args...),
325+
}
320326
}
321327
return nil
322328
}
@@ -367,6 +373,11 @@ func SerializeFilter(f models.Filter, opt *options.ConfigurationOptions) types.F
367373
Key: f.Key,
368374
Table: &f.Table,
369375
}
376+
case models.FilterTypeLua:
377+
return &filters.Lua{
378+
Name: f.LuaName,
379+
Args: append([]string{}, f.LuaArgs...),
380+
}
370381
}
371382
return nil
372383
}

models/filter.go

Lines changed: 30 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/filter_compare_test.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/filter_diff_generated.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/filter_equal_generated.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

specification/build/haproxy_spec.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7419,6 +7419,23 @@ definitions:
74197419
- bwlim-in
74207420
- bwlim-out
74217421
x-size: true
7422+
lua_args:
7423+
description: Optional arguments passed to the Lua filter constructor.
7424+
items:
7425+
type: string
7426+
type: array
7427+
x-dependency:
7428+
type:
7429+
value: lua
7430+
x-omitempty: true
7431+
lua_name:
7432+
description: Name of the Lua filter as registered by core.register_filter(), used in the 'filter lua.<name>' directive.
7433+
pattern: ^[^\s]+$
7434+
type: string
7435+
x-dependency:
7436+
type:
7437+
required: true
7438+
value: lua
74227439
min_size:
74237440
description: |-
74247441
The optional minimum number of bytes forwarded at a time by a stream excluding the last packet that may be smaller.
@@ -7490,6 +7507,7 @@ definitions:
74907507
- fcgi-app
74917508
- spoe
74927509
- trace
7510+
- lua
74937511
type: string
74947512
x-nullable: false
74957513
required:

0 commit comments

Comments
 (0)