Skip to content

Commit 28d05fc

Browse files
vgramermjuraga
authored andcommitted
MINOR: support keyword cpu-set in global section
Signed-off-by: Vincent Gramer <vgramer@haproxy.com>
1 parent d5e0564 commit 28d05fc

18 files changed

Lines changed: 1213 additions & 10 deletions

config-parser/parsers/cpu-set.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package parsers
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
7+
"github.com/haproxytech/client-native/v6/config-parser/common"
8+
"github.com/haproxytech/client-native/v6/config-parser/errors"
9+
"github.com/haproxytech/client-native/v6/config-parser/types"
10+
)
11+
12+
// Supported directives for cpu-set keyword
13+
var cpuSetSupportedDirectives = []string{ //nolint:gochecknoglobals
14+
CPUSetResetDirective,
15+
"drop-cpu",
16+
"only-cpu",
17+
"drop-node",
18+
"only-node",
19+
"drop-cluster",
20+
"only-cluster",
21+
"drop-core",
22+
"only-core",
23+
"drop-thread",
24+
"only-thread",
25+
}
26+
27+
const CPUSetResetDirective = "reset"
28+
29+
type CPUSet struct {
30+
data []types.CPUSet
31+
preComments []string // comments that appear before the actual line
32+
}
33+
34+
func (c *CPUSet) parse(line string, parts []string, comment string) (*types.CPUSet, error) {
35+
// Check if we have at least the command (cpu-set) and directive
36+
if len(parts) < 2 {
37+
return nil, &errors.ParseError{Parser: "CPUSet", Line: line, Message: "Parse error"}
38+
}
39+
40+
// Validate the directive
41+
directive := parts[1]
42+
if !slices.Contains(cpuSetSupportedDirectives, directive) {
43+
return nil, &errors.ParseError{Parser: "CPUSet", Line: line, Message: fmt.Sprintf("Unknown directive %q, supported directives %v", directive, cpuSetSupportedDirectives)}
44+
}
45+
46+
// Handle reset directive (no 'set' parameters)
47+
if directive == CPUSetResetDirective {
48+
if len(parts) != 2 {
49+
return nil, &errors.ParseError{Parser: "CPUSet", Line: line, Message: "cpu-set reset directive does not accept 'set' parameter"}
50+
}
51+
return &types.CPUSet{Directive: directive, Comment: comment}, nil
52+
}
53+
54+
// Handle other directives (require one parameter)
55+
if len(parts) != 3 {
56+
return nil, &errors.ParseError{Parser: "CPUSet", Line: line, Message: fmt.Sprintf("cpu-set %s missing 'set' parameter", directive)}
57+
}
58+
59+
return &types.CPUSet{Directive: directive, Set: parts[2], Comment: comment}, nil
60+
}
61+
62+
func (c *CPUSet) Result() ([]common.ReturnResultLine, error) {
63+
if len(c.data) == 0 {
64+
return nil, errors.ErrFetch
65+
}
66+
result := make([]common.ReturnResultLine, len(c.data))
67+
for index, cpuSet := range c.data {
68+
data := "cpu-set " + cpuSet.Directive
69+
if cpuSet.Directive != CPUSetResetDirective { // reset is the only directive that has no 'set' parameter
70+
data += " " + cpuSet.Set
71+
}
72+
result[index] = common.ReturnResultLine{
73+
Data: data,
74+
Comment: cpuSet.Comment,
75+
}
76+
}
77+
return result, nil
78+
}

config-parser/parsers/cpu-set_generated.go

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

config-parser/section-parsers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ func (p *configParser) getGlobalParser() *Parsers { //nolint: maintidx
251251
addParser(parser, &sequence, &parsers.NbThread{})
252252
addParser(parser, &sequence, &parsers.CPUMap{})
253253
addParser(parser, &sequence, &parsers.CPUPolicy{})
254+
addParser(parser, &sequence, &parsers.CPUSet{})
254255
addParser(parser, &sequence, &parsers.Mode{})
255256
addParser(parser, &sequence, &parsers.MaxConn{})
256257
addParser(parser, &sequence, &simple.Number{Name: "maxconnrate"})

config-parser/tests/configs/haproxy.cfg.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ global
2929
cpu-map 2 1
3030
cpu-map 3 2
3131
cpu-policy none
32+
cpu-set reset
33+
cpu-set only-node 0
34+
cpu-set drop-core 8-9
3235
maxconn 5000
3336
pidfile /var/run/haproxy.pid
3437
stats socket /var/run/haproxy-runtime-api.1.sock level admin mode 777 expose-fd listeners process 1

config-parser/tests/configs/haproxy_generated.cfg.go

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

0 commit comments

Comments
 (0)