Combining Services from different HTTPRoutes

Related Documentation
Related Resources

Kong Ingress Controller can consolidate rules from multiple HTTPRoute resources that target the same backend services into a single Kong Gateway service.

Enable the feature

Use the CombinedServicesFromDifferentHTTPRoutes feature gate to enable this functionality:

kubectl set env -n kong deployment/kong-controller \
  CONTROLLER_FEATURE_GATES="CombinedServicesFromDifferentHTTPRoutes=true" \
  -c ingress-controller

You can refer to the feature gate reference to learn more.

How does CombinedServicesFromDifferentHTTPRoutes 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:

  • namespace is the namespace of the HTTPRoutes.
  • backend_ns is the namespace of the first backend service.
  • backend_name is the name of the first backend service.
  • backend_port is the port number of the first backend service.
  • backend_weight is the weight of the first backend service, if specified.
  • next_backends are 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, where hash is 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.

Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!