Route

What is Route?

Route is part of the Function definition. Route defines how traffic from the Gateway listener is routed to a function.

A Route specifies the Gateway to which it will attach in GatewayRef that allows it to receive traffic from the Gateway.

Once a sync Function is created, the function controller will:

  • Look for theGateway called openfunction in openfunction namespace, then attach to this Gateway if route.gatewayRef is not defined in the function.
  • Automatically generate route.hostnames based on Gateway.spec.hostTemplate, if route.hostnames is not defined in function.
  • Automatically generate route.rules based on Gateway.spec.pathTemplate or path of /, if route.rules is not defined in function.
  • a HTTPRoute custom resource will be created based on Route. BackendRefs will be automatically link to the corresponding Knative service revision and label HTTPRouteLabelKey will be added to this HTTPRoute.
  • Create service {{.Name}}.{{.Namespace}}.svc.cluster.local, this service defines an entry for the function to access within the cluster.
  • If the Gateway referenced by route.gatewayRef changed, will update the HTTPRoute.

After a sync Function is deployed, you’ll be able to find Function addresses and Route status in Function’s status field, e.g:

  1. status:
  2. addresses:
  3. - type: External
  4. value: http://function-sample-serving-only.default.ofn.io/
  5. - type: Internal
  6. value: http://function-sample-serving-only.default.svc.cluster.local/
  7. build:
  8. resourceHash: "14903236521345556383"
  9. state: Skipped
  10. route:
  11. conditions:
  12. - message: Valid HTTPRoute
  13. reason: Valid
  14. status: "True"
  15. type: Accepted
  16. hosts:
  17. - function-sample-serving-only.default.ofn.io
  18. - function-sample-serving-only.default.svc.cluster.local
  19. paths:
  20. - type: PathPrefix
  21. value: /
  22. serving:
  23. lastSuccessfulResourceRef: serving-znk54
  24. resourceHash: "10715302888241374768"
  25. resourceRef: serving-znk54
  26. service: serving-znk54-ksvc-nbg6f
  27. state: Running

Note

The Address of type Internal in Funtion.status provides the default method for accessing functions from within the cluster. This internal address is not affected by the Gateway referenced by route.gatewayRef and it’s suitable for use as sink.url of EventSource.

The Address of type External in Funtion.status provides methods for accessing functions from outside the cluster (You can choose to configure Magic DNS or real DNS, please refer to access functions by the external address for more details). This external address is generated based on route.gatewayRef, router.hostnames and route.rules. The routing mode only takes effect on this external address, The following documentation will explain how it works.

For more information about how to access functions, please refer to Function Entrypoints.

Host Based Routing

Host-based is the default routing mode. When route.hostnames is not defined, route.hostnames will be generated based on gateway.spec.hostTemplate. If route.rules is not defined, route.rules will be generated based on path of /.

  1. kubectl apply -f - <<EOF
  2. apiVersion: core.openfunction.io/v1beta2
  3. kind: Function
  4. metadata:
  5. name: function-sample
  6. spec:
  7. version: "v1.0.0"
  8. image: "openfunctiondev/v1beta1-http:latest"
  9. serving:
  10. template:
  11. containers:
  12. - name: function
  13. imagePullPolicy: Always
  14. triggers:
  15. http:
  16. route:
  17. gatewayRef:
  18. name: openfunction
  19. namespace: openfunction
  20. EOF

If you are using the default OpenFunction Gateway, the function external address will be as below:

  1. http://function-sample.default.ofn.io/

Path Based Routing

If you define route.hostnames in a function, route.rules will be generated based on gateway.spec.pathTemplate.

  1. kubectl apply -f - <<EOF
  2. apiVersion: core.openfunction.io/v1beta2
  3. kind: Function
  4. metadata:
  5. name: function-sample
  6. spec:
  7. version: "v1.0.0"
  8. image: "openfunctiondev/v1beta1-http:latest"
  9. serving:
  10. template:
  11. containers:
  12. - name: function
  13. imagePullPolicy: Always
  14. triggers:
  15. http:
  16. route:
  17. gatewayRef:
  18. name: openfunction
  19. namespace: openfunction
  20. hostnames:
  21. - "sample.ofn.io"
  22. EOF

If you are using the default OpenFunction Gateway, the function external address will be as below:

  1. http://sample.default.ofn.io/default/function-sample/

Host and Path based routing

You can define hostname and path at the same time to customize how traffic should be routed to your function.

Note

In this mode, you’ll need to resolve possible conflicts between HTTPRoutes by yourself.

  1. kubectl apply -f - <<EOF
  2. apiVersion: core.openfunction.io/v1beta2
  3. kind: Function
  4. metadata:
  5. name: function-sample
  6. spec:
  7. version: "v1.0.0"
  8. image: "openfunctiondev/v1beta1-http:latest"
  9. serving:
  10. template:
  11. containers:
  12. - name: function
  13. imagePullPolicy: Always
  14. triggers:
  15. http:
  16. route:
  17. gatewayRef:
  18. name: openfunction
  19. namespace: openfunction
  20. rules:
  21. - matches:
  22. - path:
  23. type: PathPrefix
  24. value: /v2/foo
  25. hostnames:
  26. - "sample.ofn.io"
  27. EOF

If you are using the default OpenFunction Gateway, the function external address will be as below:

  1. http://sample.default.ofn.io/v2/foo/