From ed4f1ad7eda52951ca2d945ae35e71f8202326b9 Mon Sep 17 00:00:00 2001 From: Mike Beaumont Date: Thu, 5 Jan 2023 14:30:08 +0100 Subject: [PATCH] fix: allow user rules to overwrite default passthrough rule Signed-off-by: Mike Beaumont --- .../plugin/v1alpha1/listeners.go | 38 ++++++++++--------- .../plugin/v1alpha1/plugin_test.go | 38 +++++++++++++++++-- .../testdata/basic.listeners.golden.yaml | 10 ++--- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go index d10eb2195105..47958bd6e6a4 100644 --- a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go +++ b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go @@ -85,6 +85,27 @@ func FindRoutes( ) []Route { var unmergedRules []RuleAcc + // Prepend a rule to passthrough unmatched traffic + rules = append([]ToRouteRule{{ + Subset: core_xds.MeshService(serviceName), + Rules: []api.Rule{{ + Matches: []api.Match{{ + Path: api.PathMatch{ + Prefix: "/", + }, + }}, + Default: api.RuleConf{ + BackendRefs: &[]api.BackendRef{{ + TargetRef: common_api.TargetRef{ + Kind: common_api.MeshService, + Name: serviceName, + }, + Weight: 100, + }}, + }, + }}, + }}, rules...) + for _, rule := range rules { if !rule.Subset.IsSubset(core_xds.MeshService(serviceName)) { continue @@ -132,23 +153,6 @@ func FindRoutes( routes = append(routes, route) } - // append the default route - routes = append(routes, Route{ - Matches: []api.Match{{ - Path: api.PathMatch{ - Prefix: "/", - }, - }}, - Filters: nil, - BackendRefs: []api.BackendRef{{ - TargetRef: common_api.TargetRef{ - Kind: common_api.MeshService, - Name: serviceName, - }, - Weight: 100, - }}, - }) - return routes } diff --git a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/plugin_test.go b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/plugin_test.go index 7e16e0a768d6..61853b14ff5b 100644 --- a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/plugin_test.go +++ b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/plugin_test.go @@ -194,6 +194,18 @@ var _ = Describe("MeshHTTPRoute", func() { }}, serviceName: "backend", expectedRoutes: []plugin.Route{{ + Matches: []api.Match{{ + Path: api.PathMatch{ + Prefix: "/", + }, + }}, + BackendRefs: []api.BackendRef{ + { + TargetRef: builders.TargetRefService("backend"), + Weight: 100, + }, + }, + }, { Matches: []api.Match{{ Path: api.PathMatch{ Prefix: "/v1", @@ -217,7 +229,26 @@ var _ = Describe("MeshHTTPRoute", func() { Weight: 100, }, }, - }, { + }}, + }), Entry("overwrite-passthrough-rule", routesTestCase{ + rules: []plugin.ToRouteRule{{ + Subset: core_xds.MeshService("backend"), + Rules: []api.Rule{{ + Matches: []api.Match{{ + Path: api.PathMatch{ + Prefix: "/", + }, + }}, + Default: api.RuleConf{ + BackendRefs: &[]api.BackendRef{{ + TargetRef: builders.TargetRefService("other-service"), + Weight: 100, + }}, + }, + }}, + }}, + serviceName: "backend", + expectedRoutes: []plugin.Route{{ Matches: []api.Match{{ Path: api.PathMatch{ Prefix: "/", @@ -225,13 +256,12 @@ var _ = Describe("MeshHTTPRoute", func() { }}, BackendRefs: []api.BackendRef{ { - TargetRef: builders.TargetRefService("backend"), + TargetRef: builders.TargetRefService("other-service"), Weight: 100, }, }, }}, - }), - ) + })) type outboundsTestCase struct { proxy core_xds.Proxy xdsContext xds_context.Context diff --git a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/testdata/basic.listeners.golden.yaml b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/testdata/basic.listeners.golden.yaml index b5a701bfd444..c64941995cf8 100644 --- a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/testdata/basic.listeners.golden.yaml +++ b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/testdata/basic.listeners.golden.yaml @@ -28,19 +28,19 @@ resources: name: backend routes: - match: - prefix: /v1 + prefix: / route: cluster: backend timeout: 0s - match: - prefix: /v2 + prefix: /v1 route: - cluster: backend-_0_ + cluster: backend timeout: 0s - match: - prefix: / + prefix: /v2 route: - cluster: backend + cluster: backend-_0_ timeout: 0s statPrefix: backend metadata: