Skip to content

Commit d8a94a5

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 7653954 commit d8a94a5

11 files changed

Lines changed: 172 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ func ParseFilter(f types.Filter) *models.Filter {
323323
CacheName: v.Name,
324324
Metadata: misc.ParseMetadata(v.Comment),
325325
}
326+
case *filters.Lua:
327+
return &models.Filter{
328+
Type: models.FilterTypeLua,
329+
LuaName: v.Name,
330+
LuaArgs: append([]string{}, v.Args...),
331+
Metadata: misc.ParseMetadata(v.Comment),
332+
}
326333
}
327334
return nil
328335
}
@@ -389,6 +396,12 @@ func SerializeFilter(f models.Filter, opt *options.ConfigurationOptions) types.F
389396
Table: &f.Table,
390397
Comment: comment,
391398
}
399+
case models.FilterTypeLua:
400+
return &filters.Lua{
401+
Name: f.LuaName,
402+
Args: append([]string{}, f.LuaArgs...),
403+
Comment: comment,
404+
}
392405
}
393406
return nil
394407
}

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
@@ -8078,6 +8078,23 @@ definitions:
80788078
- bwlim-in
80798079
- bwlim-out
80808080
x-size: true
8081+
lua_args:
8082+
description: Optional arguments passed to the Lua filter constructor.
8083+
items:
8084+
type: string
8085+
type: array
8086+
x-dependency:
8087+
type:
8088+
value: lua
8089+
x-omitempty: true
8090+
lua_name:
8091+
description: Name of the Lua filter as registered by core.register_filter(), used in the 'filter lua.<name>' directive.
8092+
pattern: ^[^\s]+$
8093+
type: string
8094+
x-dependency:
8095+
type:
8096+
required: true
8097+
value: lua
80818098
metadata:
80828099
additionalProperties:
80838100
type: object
@@ -8152,6 +8169,7 @@ definitions:
81528169
- fcgi-app
81538170
- spoe
81548171
- trace
8172+
- lua
81558173
type: string
81568174
x-nullable: false
81578175
required:

0 commit comments

Comments
 (0)