Skip to content

Commit d5e0564

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

15 files changed

Lines changed: 401 additions & 3 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
type CPUPolicy struct {
13+
data *types.StringC
14+
preComments []string // comments that appear before the actual line
15+
}
16+
17+
func (p *CPUPolicy) Parse(line string, parts []string, comment string) (string, error) {
18+
if parts[0] == "cpu-policy" {
19+
if len(parts) < 2 {
20+
return "", &errors.ParseError{Parser: "CPUPolicy", Line: line, Message: "Parse error"}
21+
}
22+
23+
policyList := []string{
24+
"none",
25+
"efficiency",
26+
"first-usable-node",
27+
"group-by-2-ccx",
28+
"group-by-2-clusters",
29+
"group-by-3-ccx",
30+
"group-by-3-clusters",
31+
"group-by-4-ccx",
32+
"group-by-4-cluster",
33+
"group-by-ccx",
34+
"group-by-cluster",
35+
"performance",
36+
"resource",
37+
}
38+
if slices.Contains(policyList, parts[1]) {
39+
p.data = &types.StringC{
40+
Value: parts[1],
41+
Comment: comment,
42+
}
43+
return "", nil
44+
}
45+
46+
return "", &errors.ParseError{Parser: "CPUPolicy", Line: line, Message: fmt.Sprintf("Unknown policy %q. Must be one of %v", parts[1], policyList)}
47+
}
48+
return "", &errors.ParseError{Parser: "CPUPolicy", Line: line}
49+
}
50+
51+
func (p *CPUPolicy) Result() ([]common.ReturnResultLine, error) {
52+
if p.data == nil {
53+
return nil, errors.ErrFetch
54+
}
55+
return []common.ReturnResultLine{
56+
{
57+
Data: "cpu-policy " + p.data.Value,
58+
Comment: p.data.Comment,
59+
},
60+
}, nil
61+
}

config-parser/parsers/cpu-policy_generated.go

Lines changed: 99 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
@@ -250,6 +250,7 @@ func (p *configParser) getGlobalParser() *Parsers { //nolint: maintidx
250250
addParser(parser, &sequence, &parsers.NbProc{})
251251
addParser(parser, &sequence, &parsers.NbThread{})
252252
addParser(parser, &sequence, &parsers.CPUMap{})
253+
addParser(parser, &sequence, &parsers.CPUPolicy{})
253254
addParser(parser, &sequence, &parsers.Mode{})
254255
addParser(parser, &sequence, &parsers.MaxConn{})
255256
addParser(parser, &sequence, &simple.Number{Name: "maxconnrate"})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ global
2828
cpu-map 1 3
2929
cpu-map 2 1
3030
cpu-map 3 2
31+
cpu-policy none
3132
maxconn 5000
3233
pidfile /var/run/haproxy.pid
3334
stats socket /var/run/haproxy-runtime-api.1.sock level admin mode 777 expose-fd listeners process 1

config-parser/tests/cpu-policy_generated_test.go

Lines changed: 93 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-generic.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type Int64C struct {
7676
Comment string
7777
}
7878

79-
// String is used by parsers Mode, DefaultBackend, SimpleTimeTwoWords, StatsTimeout, CompressionDirection, CompressionAlgoReq
79+
// String is used by parsers Mode, DefaultBackend, SimpleTimeTwoWords, StatsTimeout, CompressionDirection, CompressionAlgoReq, CPUPolicy
8080
//
8181
//generate:type:Mode
8282
//name:mode
@@ -107,6 +107,23 @@ type Int64C struct {
107107
//name:compression algo-req
108108
//test:ok:compression algo-req gzip
109109
//test:fail:compression algo-req
110+
//generate:type:CPUPolicy
111+
//name:cpu-policy
112+
//test:ok:cpu-policy none
113+
//test:ok:cpu-policy efficiency
114+
//test:ok:cpu-policy first-usable-node
115+
//test:ok:cpu-policy group-by-2-ccx
116+
//test:ok:cpu-policy group-by-2-clusters
117+
//test:ok:cpu-policy group-by-3-ccx
118+
//test:ok:cpu-policy group-by-3-clusters
119+
//test:ok:cpu-policy group-by-4-ccx
120+
//test:ok:cpu-policy group-by-4-cluster
121+
//test:ok:cpu-policy group-by-ccx
122+
//test:ok:cpu-policy group-by-cluster
123+
//test:ok:cpu-policy performance
124+
//test:ok:cpu-policy resource
125+
//test:ko:cpu-policy
126+
//test:ko:cpu-policy notvalid
110127
type StringC struct {
111128
Value string
112129
Comment string

configuration/global.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,12 @@ func ParseGlobalSection(p parser.Parser) (*models.Global, error) { //nolint:goco
21782178
}
21792179
global.ClusterSecret = clusterSecret
21802180

2181+
cpuPolicy, err := parseStringOption(p, "cpu-policy")
2182+
if err != nil {
2183+
return nil, err
2184+
}
2185+
global.CPUPolicy = cpuPolicy
2186+
21812187
daemon, err := parseBoolOption(p, "daemon")
21822188
if err != nil {
21832189
return nil, err
@@ -3092,6 +3098,10 @@ func SerializeGlobalSection(p parser.Parser, data *models.Global, opt *options.C
30923098
return err
30933099
}
30943100

3101+
if err := serializeStringOption(p, "cpu-policy", data.CPUPolicy); err != nil {
3102+
return err
3103+
}
3104+
30953105
if err := serializeBoolOption(p, "daemon", data.Daemon); err != nil {
30963106
return err
30973107
}

0 commit comments

Comments
 (0)