Skip to content

Commit 9ef0375

Browse files
jrsmrozrainest
andauthored
feature(parser): consolidation of multiple HTTPRouteMatch objects into single kong.Route (#3060)
* feat(parser): consolidation of multiple HTTPRouteMatch objects into single kong.Route Co-authored-by: Travis Raines <[email protected]>
1 parent 7220e7b commit 9ef0375

File tree

5 files changed

+770
-120
lines changed

5 files changed

+770
-120
lines changed

CHANGELOG.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,31 @@ Adding a new version? You'll need three changes:
6868
### Added
6969

7070
- Added `HTTPRoute` support for `CombinedRoutes` feature. When enabled,
71-
this changes how `HTTPRoute` resources are translated so that `HTTPRouteRule`
72-
resources are combined when they have same backends references. This change
73-
does not functionally impact routing: requests that went to a given Service
74-
using the original method still go to the same Service in the new method.
71+
`HTTPRoute.HTTPRouteRule` objects with identical `backendRefs` generate a
72+
single Kong service instead of a service per rule, and
73+
`HTTPRouteRule.HTTPRouteMatche` objects using the same `backendRefs` can be
74+
consolidated into a single Kong route instead of always creating a route per
75+
match, reducing configuration size.
76+
The following limitations apply:
77+
- `HTTPRouteRule` objects cannot be consolidated into a single Kong Service
78+
if they belong to different `HTTPRoute`.
79+
- `HTTPRouteRule` objects cannot be consolidated into a single Kong Service
80+
if they have different `HTTPRouteRule.HTTPBackendRef[]` objects. The order
81+
of the backend references is not important.
82+
- `HTTPRouteMatch` objects cannot be consolidated into a single Kong Route
83+
if parent `HTTPRouteRule` objects cannot be consolidated into a single Kong Service.
84+
- `HTTPRouteMatch` objects cannot be consolidated into a single Kong Route
85+
if parent `HTTPRouteRule` objects have different `HTTPRouteRule.HTTPRouteFilter[]` filters.
86+
- `HTTPRouteMatch` objects cannot be consolidated into a single Kong Route
87+
if they have different matching spec (`HTTPHeaderMatch.Headers`, `HTTPHeaderMatch.QueryParams`,
88+
`HTTPHeaderMatch.Method`). Different `HTTPHeaderMatch.Path` paths between
89+
`HTTPRouteMatch[]` objects does not prevent consolidation.
90+
This change does not functionally impact routing: requests that went to a given Service
91+
using the original method still go to the same Service when `CombinedRoutes` is enabled.
7592
[#3008](https://github.com/Kong/kubernetes-ingress-controller/pull/3008)
76-
- Added `--cache-sync-timeout` flag allowing to change the default controllers'
77-
cache synchronisation timeout.
93+
[#3060]https://github.com/Kong/kubernetes-ingress-controller/pull/3060)
94+
- Added `--cache-sync-timeout` flag allowing to change the default controllers'
95+
cache synchronisation timeout.
7896
[#3013](https://github.com/Kong/kubernetes-ingress-controller/pull/3013)
7997
- Secrets validation introduced: CA certificates won't be synchronized
8098
to Kong if the certificate is expired.

internal/dataplane/parser/translate_httproute.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,25 @@ func (p *Parser) ingressRulesFromHTTPRoute(result *ingressRules, httproute *gate
7878
// ingressRulesFromHTTPRouteWithCombinedServiceRoutes generates a set of proto-Kong routes (ingress rules) from an HTTPRoute.
7979
// If multiple rules in the HTTPRoute use the same Service, it combines them into a single Kong route.
8080
func (p *Parser) ingressRulesFromHTTPRouteWithCombinedServiceRoutes(httproute *gatewayv1beta1.HTTPRoute, result *ingressRules) error {
81-
for _, translationMeta := range translators.TranslateHTTPRoute(httproute) {
81+
for _, kongServiceTranslation := range translators.TranslateHTTPRoute(httproute) {
8282
// HTTPRoute uses a wrapper HTTPBackendRef to add optional filters to its BackendRefs
83-
backendRefs := httpBackendRefsToBackendRefs(translationMeta.BackendRefs)
83+
backendRefs := httpBackendRefsToBackendRefs(kongServiceTranslation.BackendRefs)
8484

85-
// use the original index of the first rule that uses this service as the rule number
86-
firstCombinedRuleNum := translationMeta.RulesNumbers[0]
85+
serviceName := kongServiceTranslation.Name
8786

8887
// create a service and attach the routes to it
89-
service, err := generateKongServiceFromBackendRefWithRuleNumber(p.logger, p.storer, result, httproute, firstCombinedRuleNum, "http", backendRefs...)
88+
service, err := generateKongServiceFromBackendRefWithName(p.logger, p.storer, result, serviceName, httproute, "http", backendRefs...)
9089
if err != nil {
9190
return err
9291
}
9392

9493
// generate the routes for the service and attach them to the service
95-
for j, rule := range translationMeta.Rules {
96-
ruleNumber := translationMeta.RulesNumbers[j]
97-
routes, err := generateKongRoutesFromHTTPRouteRule(httproute, ruleNumber, rule, p.flagEnabledRegexPathPrefix)
94+
for _, kongRouteTranslation := range kongServiceTranslation.KongRoutes {
95+
route, err := generateKongRouteFromTranslation(httproute, kongRouteTranslation, p.flagEnabledRegexPathPrefix)
9896
if err != nil {
9997
return err
10098
}
101-
service.Routes = append(service.Routes, routes...)
99+
service.Routes = append(service.Routes, route)
102100
}
103101

104102
// cache the service to avoid duplicates in further loop iterations
@@ -219,6 +217,30 @@ func generateKongRoutesFromHTTPRouteRule(
219217
return routes, nil
220218
}
221219

220+
func generateKongRouteFromTranslation(
221+
httproute *gatewayv1beta1.HTTPRoute,
222+
translation translators.KongRouteTranslation,
223+
addRegexPrefix bool,
224+
) (kongstate.Route, error) {
225+
// gather the k8s object information and hostnames from the httproute
226+
objectInfo := util.FromK8sObject(httproute)
227+
228+
// get the hostnames from the HTTPRoute
229+
hostnames := getHTTPRouteHostnamesAsSliceOfStringPointers(httproute)
230+
231+
// generate kong plugins from rule.filters
232+
plugins := generatePluginsFromHTTPRouteFilters(translation.Filters)
233+
234+
return generateKongRouteFromHTTPRouteMatches(
235+
translation.Name,
236+
translation.Matches,
237+
objectInfo,
238+
hostnames,
239+
plugins,
240+
addRegexPrefix,
241+
)
242+
}
243+
222244
// generateKongRouteFromHTTPRouteMatches converts an HTTPRouteMatches to a Kong Route object.
223245
// This function assumes that the HTTPRouteMatches share the query params, headers and methods.
224246
func generateKongRouteFromHTTPRouteMatches(

0 commit comments

Comments
 (0)