diff --git a/integration/testdata/rawdata-crd.json b/integration/testdata/rawdata-crd.json index 5bf6512a4..2cd67af47 100644 --- a/integration/testdata/rawdata-crd.json +++ b/integration/testdata/rawdata-crd.json @@ -20,7 +20,7 @@ "web" ], "middlewares": [ - "default/mychain" + "default/mychain@kubernetescrd" ], "service": "default/test2.route-23c7f4c450289ee29016", "rule": "Host(`foo.com`) \u0026\u0026 PathPrefix(`/tobestripped`)", @@ -34,7 +34,7 @@ "default/mychain@kubernetescrd": { "chain": { "middlewares": [ - "default/stripprefix" + "default/stripprefix@kubernetescrd" ] }, "status": "enabled", @@ -56,10 +56,10 @@ "loadBalancer": { "servers": [ { - "url": "http://10.42.0.3:80" + "url": "http://10.42.0.2:80" }, { - "url": "http://10.42.0.5:80" + "url": "http://10.42.0.6:80" } ], "passHostHeader": true @@ -69,18 +69,18 @@ "default/test.route-6b204d94623b3df4370c@kubernetescrd" ], "serverStatus": { - "http://10.42.0.3:80": "UP", - "http://10.42.0.5:80": "UP" + "http://10.42.0.2:80": "UP", + "http://10.42.0.6:80": "UP" } }, "default/test2.route-23c7f4c450289ee29016@kubernetescrd": { "loadBalancer": { "servers": [ { - "url": "http://10.42.0.3:80" + "url": "http://10.42.0.2:80" }, { - "url": "http://10.42.0.5:80" + "url": "http://10.42.0.6:80" } ], "passHostHeader": true @@ -90,8 +90,8 @@ "default/test2.route-23c7f4c450289ee29016@kubernetescrd" ], "serverStatus": { - "http://10.42.0.3:80": "UP", - "http://10.42.0.5:80": "UP" + "http://10.42.0.2:80": "UP", + "http://10.42.0.6:80": "UP" } } }, @@ -117,10 +117,10 @@ "loadBalancer": { "servers": [ { - "address": "10.42.0.6:8080" + "address": "10.42.0.4:8080" }, { - "address": "10.42.0.7:8080" + "address": "10.42.0.5:8080" } ] }, diff --git a/pkg/server/middleware/middlewares.go b/pkg/server/middleware/middlewares.go index e93dee542..e13bee412 100644 --- a/pkg/server/middleware/middlewares.go +++ b/pkg/server/middleware/middlewares.go @@ -141,6 +141,12 @@ func (b *Builder) buildConstructor(ctx context.Context, middlewareName string) ( if middleware != nil { return nil, badConf } + + qualifiedNames := make([]string, len(config.Chain.Middlewares)) + for i, name := range config.Chain.Middlewares { + qualifiedNames[i] = internal.GetQualifiedName(ctx, name) + } + config.Chain.Middlewares = qualifiedNames middleware = func(next http.Handler) (http.Handler, error) { return chain.New(ctx, next, *config.Chain, b, middlewareName) } diff --git a/pkg/server/router/router.go b/pkg/server/router/router.go index 9b08cff95..c27d42229 100644 --- a/pkg/server/router/router.go +++ b/pkg/server/router/router.go @@ -147,6 +147,7 @@ func (m *Manager) buildHTTPHandler(ctx context.Context, router *runtime.RouterIn for i, name := range router.Middlewares { qualifiedNames[i] = internal.GetQualifiedName(ctx, name) } + router.Middlewares = qualifiedNames rm := m.modifierBuilder.Build(ctx, qualifiedNames) if router.Service == "" { diff --git a/pkg/server/router/router_test.go b/pkg/server/router/router_test.go index b113d5c36..36ee65a6e 100644 --- a/pkg/server/router/router_test.go +++ b/pkg/server/router/router_test.go @@ -725,6 +725,57 @@ func TestRuntimeConfiguration(t *testing.T) { } +func TestProviderOnMiddlewares(t *testing.T) { + entryPoints := []string{"web"} + + rtConf := runtime.NewConfig(dynamic.Configuration{ + HTTP: &dynamic.HTTPConfiguration{ + Services: map[string]*dynamic.Service{ + "test@file": { + LoadBalancer: &dynamic.ServersLoadBalancer{ + Servers: []dynamic.Server{}, + }, + }, + }, + Routers: map[string]*dynamic.Router{ + "router@file": { + Rule: "Host(`test`)", + Service: "test@file", + Middlewares: []string{"chain@file", "m1"}, + }, + "router@docker": { + Rule: "Host(`test`)", + Service: "test@file", + Middlewares: []string{"chain", "m1@file"}, + }, + }, + Middlewares: map[string]*dynamic.Middleware{ + "chain@file": { + Chain: &dynamic.Chain{Middlewares: []string{"m1", "m2", "m1@file"}}, + }, + "chain@docker": { + Chain: &dynamic.Chain{Middlewares: []string{"m1", "m2", "m1@file"}}, + }, + "m1@file": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m1"}}, + "m2@file": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m2"}}, + "m1@docker": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m1"}}, + "m2@docker": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m2"}}, + }, + }, + }) + serviceManager := service.NewManager(rtConf.Services, http.DefaultTransport, nil, nil, nil, nil) + middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager) + responseModifierFactory := responsemodifiers.NewBuilder(map[string]*runtime.MiddlewareInfo{}) + routerManager := NewManager(rtConf, serviceManager, middlewaresBuilder, responseModifierFactory) + + _ = routerManager.BuildHandlers(context.Background(), entryPoints, false) + + assert.Equal(t, []string{"chain@file", "m1@file"}, rtConf.Routers["router@file"].Middlewares) + assert.Equal(t, []string{"m1@file", "m2@file", "m1@file"}, rtConf.Middlewares["chain@file"].Chain.Middlewares) + assert.Equal(t, []string{"chain@docker", "m1@file"}, rtConf.Routers["router@docker"].Middlewares) + assert.Equal(t, []string{"m1@docker", "m2@docker", "m1@file"}, rtConf.Middlewares["chain@docker"].Chain.Middlewares) +} + type staticTransport struct { res *http.Response }