Kong Ingress Controller can consolidate rules from multiple HTTPRoute resources that target the same backend services into a single Kong Gateway service.
Combining Services from different HTTPRoutes
Enable the feature
Use the --combined-services-from-different-httproutes CLI flag to enable this functionality, or enable via Helm:
ingressController:
enabled: true
installCRDs: true
env:
# this will inject CONTROLLER_COMBINED_SERVICES_FROM_DIFFERENT_HTTPROUTES="true"
combined_services_from_different_httproutes: "true"
How does Combined Services From Different HTTPRoutes work?
Any rules with same combination of backend services (combination of namespace, name, port, and weight in backendRefs of rules) in all HTTPRoutes within the same namespace are translated to one Kong Gateway Service.
How is the translation done?
The names of the translated Kong Gateway Service are changed when the feature is enabled. Instead of generating names from source HTTPRoute
and rules, the Kong Gateway Service names are generated from the consolidated backends.
Compute Service name
Names of Kong Gateway Services are computed from the namespace, name, port, and weight (if specified). The pattern of names is:
httproute.NAMESPACE.svc.BACKEND_NS.BACKEND_NAME.BACKEND_PORT.[BACKEND_WEIGHT]_[NEXT_BACKENDs]...
Where:
-
namespaceis the namespace of theHTTPRoutes. -
backend_nsis the namespace of the first backend service. -
backend_nameis the name of the first backend service. -
backend_portis the port number of the first backend service. -
backend_weightis the weight of the first backend service, if specified. -
next_backendsare sections computed from other backend services. Backend services are sorted by the namespace and name.
When the computed name is longer than 512 characters (the limit of Service name length in Konnect), the backend service name is trimmed using the following rules:
- Only use
backend_ns,backend_name,backend_port,backend_weight, - Append the
_combined.<hash>to make sure that the name is unique, wherehashis the SHA256 digest of the computed service name.
For example, here are two HTTPRoutes with rules pointing to the same backends, with the same ports and weights:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: httproute-consolidated-1
namespace: default
spec:
parentRefs:
- name: kong
rules:
- matches:
- path:
type: PathPrefix
value: /httproute-testing
backendRefs:
- name: echo-1
kind: Service
port: 80
weight: 75
- name: echo-2
kind: Service
port: 8080
weight: 25
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: httproute-consolidated-2
namespace: default
spec:
parentRefs:
- name: kong
rules:
- matches:
- path:
type: PathPrefix
value: /httproute-testing
backendRefs:
- name: echo-1
kind: Service
port: 80
weight: 75
- name: echo-2
kind: Service
port: 8080
weight: 25
When the feature is disabled (which is the default), the rules in the two HTTPRoutes are translated to two distinct Kong Gateway Services:
httproute.default.httproute-consolidated-1.0 and httproute.default.httproute-consolidated-2.0.
When the feature is enabled, the rules from the two HTTPRoutes, httproute-consolidated-1 and httproute-consolidated-2, result in a single Kong Gateway Service named httproute.default.svc.default.echo-1.80.75.default.echo-2.80.25.