Skip to content

Commit

Permalink
fix: allow user rules to overwrite default passthrough rule
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Beaumont <mjboamail@gmail.com>
  • Loading branch information
michaelbeaumont committed Jan 5, 2023
1 parent ff06e06 commit ed4f1ad
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
38 changes: 21 additions & 17 deletions pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
38 changes: 34 additions & 4 deletions pkg/plugins/policies/meshhttproute/plugin/v1alpha1/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -217,21 +229,39 @@ 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: "/",
},
}},
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit ed4f1ad

Please sign in to comment.