Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the rule syntax mechanism for TCP #10680

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 37 additions & 34 deletions pkg/server/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ func mergeConfiguration(configurations dynamic.Configurations, defaultEntryPoint
for serviceName, service := range configuration.TCP.Services {
conf.TCP.Services[provider.MakeQualifiedName(pvd, serviceName)] = service
}
for modelName, model := range configuration.TCP.Models {
conf.TCP.Models[provider.MakeQualifiedName(pvd, modelName)] = model
}
for serversTransportName, serversTransport := range configuration.TCP.ServersTransports {
conf.TCP.ServersTransports[provider.MakeQualifiedName(pvd, serversTransportName)] = serversTransport
}
Expand Down Expand Up @@ -146,60 +149,58 @@ func mergeConfiguration(configurations dynamic.Configurations, defaultEntryPoint
}

func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
if cfg.HTTP == nil || len(cfg.HTTP.Models) == 0 {
return cfg
}

rts := make(map[string]*dynamic.Router)
if cfg.HTTP != nil && len(cfg.HTTP.Models) > 0 {
rts := make(map[string]*dynamic.Router)

for name, rt := range cfg.HTTP.Routers {
router := rt.DeepCopy()
for name, rt := range cfg.HTTP.Routers {
router := rt.DeepCopy()

if !router.DefaultRule && router.RuleSyntax == "" {
for _, model := range cfg.HTTP.Models {
router.RuleSyntax = model.DefaultRuleSyntax
break
if !router.DefaultRule && router.RuleSyntax == "" {
for _, model := range cfg.HTTP.Models {
router.RuleSyntax = model.DefaultRuleSyntax
break
}
}
}

eps := router.EntryPoints
router.EntryPoints = nil
eps := router.EntryPoints
router.EntryPoints = nil

for _, epName := range eps {
m, ok := cfg.HTTP.Models[epName+"@internal"]
if ok {
cp := router.DeepCopy()
for _, epName := range eps {
m, ok := cfg.HTTP.Models[epName+"@internal"]
if ok {
cp := router.DeepCopy()

cp.EntryPoints = []string{epName}
cp.EntryPoints = []string{epName}

if cp.TLS == nil {
cp.TLS = m.TLS
}
if cp.TLS == nil {
cp.TLS = m.TLS
}

cp.Middlewares = append(m.Middlewares, cp.Middlewares...)
cp.Middlewares = append(m.Middlewares, cp.Middlewares...)

rtName := name
if len(eps) > 1 {
rtName = epName + "-" + name
}
rts[rtName] = cp
} else {
router.EntryPoints = append(router.EntryPoints, epName)
rtName := name
if len(eps) > 1 {
rtName = epName + "-" + name
}
rts[rtName] = cp
} else {
router.EntryPoints = append(router.EntryPoints, epName)

rts[name] = router
rts[name] = router
}
}
}
}

cfg.HTTP.Routers = rts
cfg.HTTP.Routers = rts
}

if cfg.TCP == nil || len(cfg.TCP.Models) == 0 {
return cfg
}

tcpRouters := make(map[string]*dynamic.TCPRouter)

for _, rt := range cfg.TCP.Routers {
for name, rt := range cfg.TCP.Routers {
router := rt.DeepCopy()

if router.RuleSyntax == "" {
Expand All @@ -208,6 +209,8 @@ func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
break
}
}

tcpRouters[name] = router
}

cfg.TCP.Routers = tcpRouters
Expand Down
44 changes: 44 additions & 0 deletions pkg/server/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,50 @@ func Test_applyModel(t *testing.T) {
},
},
},
{
desc: "with TCP model, two entry points",
input: dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"test": {
EntryPoints: []string{"websecure", "web"},
},
"test2": {
EntryPoints: []string{"web"},
RuleSyntax: "barfoo",
},
},
Middlewares: make(map[string]*dynamic.TCPMiddleware),
Services: make(map[string]*dynamic.TCPService),
Models: map[string]*dynamic.TCPModel{
"websecure@internal": {
DefaultRuleSyntax: "foobar",
},
},
},
},
expected: dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"test": {
EntryPoints: []string{"websecure", "web"},
RuleSyntax: "foobar",
},
"test2": {
EntryPoints: []string{"web"},
RuleSyntax: "barfoo",
},
},
Middlewares: make(map[string]*dynamic.TCPMiddleware),
Services: make(map[string]*dynamic.TCPService),
Models: map[string]*dynamic.TCPModel{
"websecure@internal": {
DefaultRuleSyntax: "foobar",
},
},
},
},
},
}

for _, test := range testCases {
Expand Down