Networking
Serve a Function at a path and a host of its own with a Route, ask its callers to authenticate, and allow a Function's outbound connections with an EgressPolicy.
Usage
A Route tells funcd's edge, which receives the calls from outside funcd, where to answer: each rule sends the requests on a path to a Function, a Bucket's files or a Site. An EgressPolicy lists where a Function's workers may connect, on a server that controls outbound connections.
The TypeScript example http-api serves the Function devices-api at /api. Install the
dev build of funcdctl, Git and curl, and put Node.js on the PATH.
Clone the repository and start the example:
$ git clone https://github.com/pyvvo/funcd-typescript
$ cd funcd-typescript
$ funcdctl dev examples/http-api --gport 8080 --cport 8081Until the edge serves the Route, a call gets 404 no route for "/api/devices". In another terminal, repeat until the
Route's phase is Ready, with the reason Programmed:
$ funcdctl --server http://127.0.0.1:8081 --token funcd-dev-token get route api -n default -o jsonThe Route, in route.yaml; Serve HTTP requests explains the
Function:
# The devices API at /api: the edge strips the prefix and tells the app with X-Forwarded-Prefix.
apiVersion: funcd.io/v1alpha1
kind: Route
metadata:
name: api
namespace: default
resourceGroup: rg1
spec:
rules:
- path: /api
backend:
function: devices-apiA rule without a pathType is a Prefix: /api matches /api/devices but not /apix.
backend.function names a Function of the Route's namespace. The edge removes the
rule's path, so the app gets /devices, with /api in X-Forwarded-Prefix; the Function's own address,
/function/devices-api, keeps answering. Create a device and list them:
$ curl -s -X POST -H 'content-type: application/json' -d '{"name":"truck-7"}' http://127.0.0.1:8080/api/devices
{"id":"1","name":"truck-7"}
$ curl -s http://127.0.0.1:8080/api/devices
[{"id":"1","name":"truck-7"}]Press Ctrl-C in the first terminal to stop.
Choose the host of a Route
A Route answers on the host that its host sets, compared in lower case without a port or a
trailing dot. Without a host, it answers on its namespace's host, <namespace>.<server.baseDomain>, such as
shop.localhost; a Route of default, which has no host, answers on every host except another namespace's while that
namespace has a Route there.
A Route is not served, and not Ready:
- on another namespace's host, or a name under it, while that namespace has a Route there, or with a rule under
/functionon such a host (HostReserved); - in
defaultwithout a host, with a rule under/function(ReservedPath), or when the NamespacedefaultsetsdefaultExposure: explicit(HostRequired).
This Route serves devices-api at the root of devices.example.com
(complete file); locally,
send that host with -H 'Host: devices.example.com':
apiVersion: funcd.io/v1alpha1
kind: Route
metadata:
name: devices
namespace: default
resourceGroup: rg1
spec:
host: devices.example.com
rules:
- path: /
backend:
function: devices-apistatus.host names the host that a Route claims. A Function's or a
Site's spec.ingress host or path makes a Route that it owns, by the
same rules.
Choose which rule serves a request
- A
Prefixrule matches its path and every path under it by whole segments,/matching all; anExactrule only its path, and its backend gets/. A rule withmethodsmatches only those. - Among the matching rules, the longest path wins, whatever Route holds it; at equal length, a Route with a host wins.
- On a namespace's host, a path under
/functiongoes to that namespace's Function, unless a rule of that namespace under/functionmatches. - No match gets 404
no route for "<path>". A path under/health/after the rule's path gets 404, reserved for funcd's worker checks.
Two rules conflict on the same claimed host, the same path and a common method. The first Route by namespace name, then
by name, with owned Routes after the others, keeps the path, whatever the apply order; the other serves nothing, with
RouteConflict and a message such as conflicts with Route default/a-conflict on (host, path, method), until the
winner is deleted. A hostless Route of default conflicts only with other hostless Routes of default.
Ask callers to authenticate
A Route's auth mode applies to its rules, and a rule's own auth
wins; without either, the Namespace's
edgeDefaults.auth applies, else open.
An authenticated rule:
- answers 401
authentication required but no authenticator is configuredwhileserver.auth.edgeisfalse, its default, and underfuncdctl dev; - otherwise needs a server credential's token in
Authorization: BearerorX-Api-Key, whose role may get the namespace's Functions: else 401, or 403. The Function gets the call without those headers.
The oidc mode signs callers in with an OpenID Connect provider instead: OIDC sign-in.
A Route's mode covers only its rules: a Function's own address takes its
spec.ingress.auth, else the Namespace's default, else
open, and answers only while its address is on.
The env-echo example's Route secure-echo, in secure-route.yaml:
# secure-echo: a Route that serves env-echo at /secure and asks its callers to authenticate. A request needs a
# bearer token or an X-Api-Key header of an identity that may get Functions in the namespace: 401 without a valid
# one, 403 when that identity may not. While server.auth.edge is off, every caller gets 401. The mode covers the
# rules of this Route only.
apiVersion: funcd.io/v1alpha1
kind: Route
metadata:
name: secure-echo
namespace: default
resourceGroup: rg1
spec:
auth:
mode: authenticated
rules:
- path: /secure
backend:
function: env-echoFrom the funcd-typescript repository, start the example:
$ funcdctl dev examples/env-echo --gport 8080 --cport 8081In another terminal, wait until the Route is Ready:
$ funcdctl --server http://127.0.0.1:8081 --token funcd-dev-token get route secure-echo -n default -o jsonThen call through the Route, and at the Function's own address:
$ curl -s -X POST http://127.0.0.1:8080/secure
{"type":"urn:funcd:problem:unauthorized","title":"Unauthorized","status":401,"detail":"dataplane.ServeHTTP: authentication required but no authenticator is configured"}
$ curl -s -X POST http://127.0.0.1:8080/function/env-echo
{"config":"prod","secret":"s3cr3t","shared":"from-secret"}Press Ctrl-C in the first terminal to stop.
Allow a Function's outbound connections
Egress control works on Linux with the containerd runtime. Turn it on with these
server settings, as the egress-probe example's
funcdconfig.yaml does:
server.network.egress: true, server.network.egressGatewayPort, server.network.dnsForwarderPort and
server.network.dnsResolver. With it on, a Function or an add-on engine reaches nothing outside funcd unless an
EgressPolicy or a Policy allows it. Without it on containerd, a Function's workers connect anywhere and an add-on
engine nowhere. Under funcdctl dev, workers and engines both connect anywhere.
An EgressPolicy's rules allow TCP connections to their to
on their ports, or on every port:
domains: exact names, or wildcards such as*.example.com, which do not matchexample.comitself;cidrs: address ranges, such as10.0.0.0/8.
appliesTo names the Functions, Jobs and add-on objects it applies to, each by its kind and
name, such as {kind: Job, name: tofu}, or {kind: Tunnel, name: public} for the engine of an add-on object, its kind
spelled as its Addon spells it. funcd refuses with 400 a kind that no Addon registers for objects that run an engine,
such as a settings kind. Without appliesTo, the rules apply to all of the namespace's Functions and to no Job or
engine. The rules of
all its EgressPolicies add up. A worker can look up only the names that a domain rule allows, and a domain rule allows only the
addresses that the lookup returned, never a private one. So a CIDR host reached by name also needs a domain rule, and a
private address needs a CIDR rule.
A new worker can connect about 2 seconds after it starts. A refused connection on the ports 80 or 8080 gets 403
egress denied by funcd EgressPolicy, and is closed on others. funcd's own log records each decision as
egress decision, with allowed.
The egress-probe Function fetches the url of its input and returns ok. With egress control on and no
EgressPolicy, https://example.com returns "ok":false. This EgressPolicy allows port 443 to every name under
example.com:
# allow-example-subdomains: an EgressPolicy for every Function of the namespace default, since it sets no
# appliesTo. It allows connections on port 443 to every name under example.com.
apiVersion: funcd.io/v1alpha1
kind: EgressPolicy
metadata:
name: allow-example-subdomains
namespace: default
resourceGroup: rg1
spec:
rules:
- to:
domains:
- "*.example.com"
ports:
- 443Applied, https://www.example.com returns "ok":true, and a lookup of www.example.org is refused. This one allows
port 443 at 1.1.1.1, which a URL can name without a lookup; https://1.0.0.1 stays refused:
# allow-one-one-one-one: an EgressPolicy for every Function of the namespace default that allows connections on
# port 443 to the address 1.1.1.1. A connection to an address needs no lookup, so a CIDR rule allows it by the
# address alone; an address that no rule holds, such as 1.0.0.1, stays refused. Both EgressPolicies of this folder
# apply together: a connection that either allows goes through.
apiVersion: funcd.io/v1alpha1
kind: EgressPolicy
metadata:
name: allow-one-one-one-one
namespace: default
resourceGroup: rg1
spec:
rules:
- to:
cidrs:
- 1.1.1.1/32
ports:
- 443Access
- Outbound connections. A Function or an add-on engine gets none by itself. Each connection needs a
permitofegress::connect: funcd makes one from each EgressPolicy rule, and a Policy can hold one, but a lookup still needs a domain rule. A Policy'sforbidstops a connection, not the lookup. - A workload's calls to the edge. With egress control on, a worker or an add-on engine reaches funcd's edge
directly at funcd's bridge address,
10.63.0.1by default, on the data plane's port; a configured address needs a fixed port inserver.dataPlaneAddr. There it gets only its own namespace's hosts, Functions by name and sign-in callback: another namespace's, or anX-Funcd-Namespaceheader that names one, gets 403outside the caller's namespace. Any other way to it, such as a node address at that port, is refused whatever an EgressPolicy allows. - Callers of an authenticated rule. With
server.auth.edge: true, the token's credential decides, fromauth.credentials:vieweris the smallest role that passes in its namespaces, and another namespace gets 403, such asnot authorized to invoke default/env-echo: viewer not scoped to namespace "default".
Limits and quotas
- An EgressPolicy holds 1 to 64 rules, each with at most 64 domains and CIDRs and 32 ports.
- A call through the edge carries at most 1 MiB, or gets 413.
server.limitssets the edge's rate, in-flight and body limits, off by default; see also the Function's limits.
Status
funcdctl get route <name> -n <namespace> -o json prints a Route's status. A Route serves all its rules or none:
| Phase | Reason | Meaning |
|---|---|---|
Ready | Programmed | The edge serves every rule of the Route. |
Pending | BackendNotFound | The Function that a rule names does not exist in the Route's namespace, with a message such as backend function "nope" not found. funcd checks again every controller.referentPollInterval, 2 seconds by default, so the Route serves soon after the Function exists. |
Pending | BucketNotFound | The Bucket that a rule names does not exist in the Route's namespace. funcd checks again every controller.referentPollInterval. |
Pending | SiteNotFound | The Site that a rule names does not exist in the Route's namespace. funcd checks again every controller.referentPollInterval. |
Pending | SiteNotReady | The Site that a rule names serves nothing yet. funcd checks again every controller.referentPollInterval. |
Pending | Disabled | The Route's enabled is false, or the Route is one that a Function or a Site owns in a namespace whose defaultExposure is explicit, and its enabled is not set. It leaves its paths to the other Routes. |
Pending | HostReserved, ReservedPath or HostRequired | The Route's host or a rule's path breaks a rule of Choose the host of a Route. |
Pending | RouteConflict | Another Route keeps one of the Route's paths, as Choose which rule serves a request explains. |
Pending | Another reason | Read the condition's reason and message in the same output. |
An EgressPolicy has no status; funcdctl get egresspolicy -n <namespace> lists them.
Changes and deletion
- A Route change is live once
Ready'sobservedGenerationshows the new generation. - Deleting a Route removes its rules at once, and a Route that lost a conflict to it serves again.
- Deleting a Function deletes its
spec.ingressRoute; other Routes to it turnBackendNotFoundwithincontroller.routeResyncInterval, 10 seconds by default, and serve again when the Function returns. - An EgressPolicy change applies to the next connection and lookup.
CLI errors
These errors stop a command before any server answers:
| Error | Action |
|---|---|
no node interpreter found for the node handler | funcdctl dev found no Node.js for the examples' Functions. Put Node.js on the PATH, or set FUNCD_NODE to the path of node. |
manifest is invalid (<kind> "<name>"), followed by the field and the rule it breaks, such as spec.rules[0].path "api" must begin with '/' | funcdctl apply checks each Route and EgressPolicy before it sends it. Correct the field that the message names, as the Spec tab describes it. |
manifest is invalid, followed by spec.rules[0].backend must set exactly one of function, static or site | Give the rule one backend. |
manifest is invalid, followed by spec.rules[0].to must set at least one of domains/cidrs | List a domain or a CIDR in the rule's to. |
manifest is invalid, followed by spec.rules[0].to.domains "*" is not a valid domain or "*."-wildcard | Write an exact host name, or a wildcard that starts with *.. |
<file>: <kind> "<name>" is in namespace "<namespace>"; funcdctl dev runs namespace default only | Remove metadata.namespace from the object, set it to default, or apply the file to a funcd server. |
Run it locally
funcdctl devserves its folder's Routes on--gport, in the namespacedefaultonly, so a hostless Route answers on every host.- Its edge has no authenticator, so
authenticatedrules answer 401. - It controls no outbound connection: an EgressPolicy is applied but changes nothing, and the banner warns
⚠ egress is NOT isolated in dev. - Its state stays in memory unless
--persistkeeps it in.funcd-dev.
Examples
- http-api in TypeScript: a Route in front of a Function that serves HTTP.
- env-echo in TypeScript: an open and an authenticated Route to one Function.
- egress-probe in TypeScript: a Function that tests outbound connections, with two EgressPolicies.
OIDC sign-in
Sign users in at the edge with an OpenID Connect provider before their requests reach a Route, a workload's address or a namespace, and read who signed in.
Namespaces and resource groups
Delete the resources of a resource group as one, set the defaults of a namespace with a Namespace, and reach a namespace at its own host.