Fix the rule syntax mechanism for TCP

This commit is contained in:
Landry Benguigui 2024-05-14 09:42:04 +02:00 committed by GitHub
parent d8a778b5cd
commit c2c1c3e09e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 87 additions and 40 deletions

View file

@ -84,6 +84,9 @@ func mergeConfiguration(configurations dynamic.Configurations, defaultEntryPoint
for serviceName, service := range configuration.TCP.Services { for serviceName, service := range configuration.TCP.Services {
conf.TCP.Services[provider.MakeQualifiedName(pvd, serviceName)] = service 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 { for serversTransportName, serversTransport := range configuration.TCP.ServersTransports {
conf.TCP.ServersTransports[provider.MakeQualifiedName(pvd, serversTransportName)] = serversTransport conf.TCP.ServersTransports[provider.MakeQualifiedName(pvd, serversTransportName)] = serversTransport
} }
@ -146,60 +149,58 @@ func mergeConfiguration(configurations dynamic.Configurations, defaultEntryPoint
} }
func applyModel(cfg dynamic.Configuration) dynamic.Configuration { func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
if cfg.HTTP == nil || len(cfg.HTTP.Models) == 0 { if cfg.HTTP != nil && len(cfg.HTTP.Models) > 0 {
return cfg rts := make(map[string]*dynamic.Router)
}
rts := make(map[string]*dynamic.Router) for name, rt := range cfg.HTTP.Routers {
router := rt.DeepCopy()
for name, rt := range cfg.HTTP.Routers { if !router.DefaultRule && router.RuleSyntax == "" {
router := rt.DeepCopy() for _, model := range cfg.HTTP.Models {
router.RuleSyntax = model.DefaultRuleSyntax
break
}
}
if !router.DefaultRule && router.RuleSyntax == "" { eps := router.EntryPoints
for _, model := range cfg.HTTP.Models { router.EntryPoints = nil
router.RuleSyntax = model.DefaultRuleSyntax
break for _, epName := range eps {
m, ok := cfg.HTTP.Models[epName+"@internal"]
if ok {
cp := router.DeepCopy()
cp.EntryPoints = []string{epName}
if cp.TLS == nil {
cp.TLS = m.TLS
}
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)
rts[name] = router
}
} }
} }
eps := router.EntryPoints cfg.HTTP.Routers = rts
router.EntryPoints = nil
for _, epName := range eps {
m, ok := cfg.HTTP.Models[epName+"@internal"]
if ok {
cp := router.DeepCopy()
cp.EntryPoints = []string{epName}
if cp.TLS == nil {
cp.TLS = m.TLS
}
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)
rts[name] = router
}
}
} }
cfg.HTTP.Routers = rts
if cfg.TCP == nil || len(cfg.TCP.Models) == 0 { if cfg.TCP == nil || len(cfg.TCP.Models) == 0 {
return cfg return cfg
} }
tcpRouters := make(map[string]*dynamic.TCPRouter) tcpRouters := make(map[string]*dynamic.TCPRouter)
for _, rt := range cfg.TCP.Routers { for name, rt := range cfg.TCP.Routers {
router := rt.DeepCopy() router := rt.DeepCopy()
if router.RuleSyntax == "" { if router.RuleSyntax == "" {
@ -208,6 +209,8 @@ func applyModel(cfg dynamic.Configuration) dynamic.Configuration {
break break
} }
} }
tcpRouters[name] = router
} }
cfg.TCP.Routers = tcpRouters cfg.TCP.Routers = tcpRouters

View file

@ -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 { for _, test := range testCases {