Delegation via labels
Use labels to delegate traffic from a parent HTTPRoute to different child HTTPRoutes.
Configuration overview
In this example, you learn how to use labels to delegate traffic. The parent HTTPRoute defines the labels that must be present on the child HTTPRoute to allow traffic to be forwarded.
You typically configure the parent to find an HTTPRoute with a specific label in a specific namespace. However, you can also use a wildcard for the namespace when you have multiple HTTPRoutes in different namespaces that can all receive delegated traffic. This configuration can significantly simplify your route delegation setup as it allows you to quickly add new child HTTPRoutes to the delegation chain without changing the parent HTTPRoute configuration.
Before you begin
Create the namespaces for
team1andteam2.kubectl create namespace team1 kubectl create namespace team2Deploy the httpbin app into both namespaces.
kubectl -n team1 apply -f https://raw.githubusercontent.com/solo-io/gloo-mesh-use-cases/main/policy-demo/httpbin.yaml kubectl -n team2 apply -f https://raw.githubusercontent.com/solo-io/gloo-mesh-use-cases/main/policy-demo/httpbin.yamlVerify that the httpbin apps are up and running.
kubectl get pods -n team1 kubectl get pods -n team2Example output:
NAME READY STATUS RESTARTS AGE httpbin-f46cc8b9b-bzl9z 3/3 Running 0 7s NAME READY STATUS RESTARTS AGE httpbin-f46cc8b9b-nhtmg 3/3 Running 0 6s
HTTPRoutes in specific namespaces
Create the parent HTTPRoute resource that matches incoming traffic on the
delegation.exampledomain. The HTTPRoute resource specifies two routes:- Route 1 matches traffic on the path prefix
/anything/team1and delegates traffic to the HTTPRoute with thedelegation.gateway.solo.io/label: team1label. - Route 2 matches traffic on the path prefix
/anything/team2and delegates traffic to the HTTPRoute with thedelegation.gateway.solo.io/label: team2label.
kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: parent namespace: gloo-system spec: parentRefs: - name: http hostnames: - delegation.example rules: - matches: - path: type: PathPrefix value: /anything/team1 backendRefs: # Delegate to routes with the label delegation.gateway.solo.io/label:team1 # in the team1 namespace - group: delegation.gateway.solo.io kind: label name: team1 namespace: team1 - matches: - path: type: PathPrefix value: /anything/team2 backendRefs: # Delegate to routes with the label delegation.gateway.solo.io/label:team2 # in the team2 namespace - group: delegation.gateway.solo.io kind: label name: team2 namespace: team2 EOF- Route 1 matches traffic on the path prefix
Create the
child-team1HTTPRoute resource in theteam1namespace that matches traffic on the/anything/team1/foopath prefix. To delegate traffic to this HTTPRoute, you must label the route with thedelegation.gateway.solo.io/label: team1label that you defined on theparentHTTPRoute.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: child-team1 namespace: team1 labels: delegation.gateway.solo.io/label: team1 spec: rules: - matches: - path: type: PathPrefix value: /anything/team1/foo backendRefs: - name: httpbin port: 8000 EOFCreate the
child-team2HTTPRoute resource in theteam2namespace that matches traffic on the/anything/team2/barexact prefix. To delegate traffic to this HTTPRoute, you must label the route with thedelegation.gateway.solo.io/label: team2label that you defined on theparentHTTPRoute.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: child-team2 namespace: team2 labels: delegation.gateway.solo.io/label: team2 spec: rules: - matches: - path: type: Exact value: /anything/team2/bar backendRefs: - name: httpbin port: 8000 EOFSend a request to the
delegation.exampledomain along the/anything/team1/foopath. Verify that you get back a 200 HTTP response code.Example output:
HTTP/1.1 200 OK access-control-allow-credentials: true access-control-allow-origin: * content-type: application/json; encoding=utf-8 content-length: 509 x-envoy-upstream-service-time: 0 server: envoy { "args": {}, "headers": { "Accept": [ "*/*" ], "Host": [ "delegation.example:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "65927858-2c6b-42ae-9278-8ff9d8bba3f8" ] }, "origin": "10.0.64.27:49526", "url": "http://delegation.example:8080/anything/team1/foo", "data": "", "files": null, "form": null, "json": null }Send a request to the
delegation.exampledomain along the/anything/team2/barpath. Verify that you also get back a 200 HTTP response code.Example output:
HTTP/1.1 200 OK access-control-allow-credentials: true access-control-allow-origin: * content-type: application/json; encoding=utf-8 content-length: 509 x-envoy-upstream-service-time: 1 server: envoy { "args": {}, "headers": { "Accept": [ "*/*" ], "Host": [ "delegation.example:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "d645dc37-5326-4b69-8c2c-4060e12ca4ff" ] }, "origin": "10.0.64.27:53026", "url": "http://delegation.example:8080/anything/team2/bar", "data": "", "files": null, "form": null, "json": null }
Use wildcard namespaces
Instead of routing to an HTTPRoute with a specific label in a specific namespace, you can use a wildcard for the namespace. This configuration can streamline your route delegation setup as it allows you to easily add new child HTTPRoutes to the delegation chain.
Update the
parentHTTPRoute to delegate traffic to all child HTTPRoutes with thewildcardlabel.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: parent namespace: gloo-system spec: parentRefs: - name: http hostnames: - delegation.example rules: - matches: - path: type: PathPrefix value: / backendRefs: - group: delegation.gateway.solo.io kind: label name: wildcard namespace: all EOFUpdate the
child-team1HTTPRoute to add thedelegation.gateway.solo.io/label: wildcardlabel so that theparentHTTPRoute can delegate traffic to this route.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: child-team1 namespace: team1 labels: delegation.gateway.solo.io/label: wildcard spec: rules: - matches: - path: type: PathPrefix value: /anything/team1/foo backendRefs: - name: httpbin port: 8000 EOFUpdate the
child-team2HTTPRoute to also add thedelegation.gateway.solo.io/label: wildcardlabel.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: child-team2 namespace: team2 labels: delegation.gateway.solo.io/label: wildcard spec: rules: - matches: - path: type: Exact value: /anything/team2/bar backendRefs: - name: httpbin port: 8000 EOFSend a request to the
delegation.exampledomain along the/anything/team1/foopath. Verify that you get back a 200 HTTP response code.Example output:
HTTP/1.1 200 OK access-control-allow-credentials: true access-control-allow-origin: * content-type: application/json; encoding=utf-8 content-length: 509 x-envoy-upstream-service-time: 0 server: envoy { "args": {}, "headers": { "Accept": [ "*/*" ], "Host": [ "delegation.example:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "65927858-2c6b-42ae-9278-8ff9d8bba3f8" ] }, "origin": "10.0.64.27:49526", "url": "http://delegation.example:8080/anything/team1/foo", "data": "", "files": null, "form": null, "json": null }Send a request to the
delegation.exampledomain along the/anything/team2/barpath. Verify that you also get back a 200 HTTP response code.Example output:
HTTP/1.1 200 OK access-control-allow-credentials: true access-control-allow-origin: * content-type: application/json; encoding=utf-8 content-length: 509 x-envoy-upstream-service-time: 1 server: envoy { "args": {}, "headers": { "Accept": [ "*/*" ], "Host": [ "delegation.example:8080" ], "User-Agent": [ "curl/8.7.1" ], "X-Envoy-Expected-Rq-Timeout-Ms": [ "15000" ], "X-Forwarded-Proto": [ "http" ], "X-Request-Id": [ "d645dc37-5326-4b69-8c2c-4060e12ca4ff" ] }, "origin": "10.0.64.27:53026", "url": "http://delegation.example:8080/anything/team2/bar", "data": "", "files": null, "form": null, "json": null }
Cleanup
You can optionally remove the resources that you set up as part of this guide.
kubectl delete httproute parent -n gloo-system
kubectl delete httproute child-team1 -n team1
kubectl delete httproute child-team2 -n team2
kubectl delete -n team1 -f https://raw.githubusercontent.com/solo-io/gloo-mesh-use-cases/main/policy-demo/httpbin.yaml
kubectl delete -n team2 -f https://raw.githubusercontent.com/solo-io/gloo-mesh-use-cases/main/policy-demo/httpbin.yaml
kubectl delete namespaces team1 team2