# Ingress and DNS

Your applications and users reach a self-managed region through its [gateway](./system-architecture.md#gateway). This page explains why the gateway needs a wildcard domain, how to serve the region on one hostname instead, the other ways to run a region without your own public wildcard DNS record, and how to keep calls from apps in the same cluster inside the cluster. For the full list of hostnames the gateway serves, see [Inbound network connections](./system-architecture.md#inbound-network-connections).

## Why the region needs a wildcard domain

The gateway serves every project in the region on one address and one port. It picks the project from the hostname of each request. Each project gets two hostnames, made from a project ID that Diagrid assigns when the project is created:

```text
http-prj<project-id>.<domain>
grpc-prj<project-id>.<domain>
```

Anyone with access to the region can create a project from the CLI, the console or the API, at any time, without touching your DNS or your certificates. So the region's DNS and TLS must already cover names that don't exist yet. When you bring your own domain, that takes three things:

- **A wildcard DNS record**, `*.<domain>`, that points at the gateway's load balancer.
- **A certificate for `*.<domain>`**, given to the gateway in `gateway.tls`.
- **A wildcard ingress on the region**, set with `diagrid region create --ingress "https://*.<domain>:443"`.

Calling the gateway by its load balancer address or by the `gateway-envoy` Service name doesn't work: the hostname matches no project, and the request fails. The region's own hostnames, such as `management.` and `trust.`, are one label under the same domain, so the wildcard covers them too.

A wildcard matches one label only. A record or certificate for `*.example.com` doesn't cover `http-prj123.catalyst.example.com`, so create them for the region's own domain, `*.catalyst.example.com`.

If you can't create a wildcard record or certificate, [serve the region on one hostname](#serve-the-region-on-one-hostname) instead. That needs one DNS record and one certificate.

## Serve the region on one hostname

Instead of a wildcard domain, a region can be served on one hostname that you choose, such as `catalyst-api.mycompany.com`. That hostname serves the whole region: every project, the region's management API and app tunnels. You create one DNS record and one certificate for it, and nothing changes in DNS when someone adds a project. If Dapr sidecars run outside the region's cluster, they also need a few [DNS records without a certificate](#create-the-record-and-the-certificate). Choose this if your policy forbids wildcard DNS records, or if you get certificates through a ticket.

This works for self-managed regions without a [managed domain](#let-diagrid-manage-the-domain). Choose it when you create the region, because [switching an existing region](#switch-an-existing-region) moves every app to a new address.

### Turn it on

Give the region's ingress as one full hostname, with no `*.`. The port is optional, for example `https://catalyst-api.mycompany.com:8443`.

- **CLI.** Pass `--shared-project-endpoint` and `--ingress` to `diagrid region create`, or to `diagrid region deploy` with `--managed-domain=false` when it creates the region. These flags need Diagrid CLI 1.123.0 or later.
- **Console.** Turn on **Serve the region on one hostname** in the region's form, then enter the hostname in the **Hostname** box below it.

```bash
diagrid region create my-region --shared-project-endpoint --ingress https://catalyst-api.mycompany.com
```

Diagrid refuses a wildcard ingress in this mode, and refuses the mode together with a managed domain.

Every cluster in the region must run a Diagrid version that supports this mode. Diagrid won't turn it on while an older cluster is connected, and won't let an older cluster join a region that has it on. The region's `status.supportsSharedProjectEndpoint` is `true` when every cluster is new enough:

```bash
diagrid region get my-region -o json | jq .status.supportsSharedProjectEndpoint
```

### What the hostname serves

The gateway serves all of these on the hostname and its one port:

- **Your projects' HTTP and gRPC APIs.** The App ID's API token, sent in the `dapr-api-token` header, tells the gateway which project and App ID to use. Requests with `content-type: application/grpc` go to the App ID's gRPC API. Everything else goes to its HTTP API. If the token is missing or doesn't match an App ID, the gateway returns HTTP `401` or gRPC `UNAUTHENTICATED`. App ID path restrictions still apply. The Temporal API works too; send the token as `authorization: Bearer <token>`.
- **The region's own services**, under `/diagrid/`: the management API, the trust bundle, OIDC discovery and app tunnels. These paths only match requests without a `dapr-api-token`, so a call to your App ID always reaches it. In a [region group](https://docs.diagrid.io/operate/platform-operations/multi-region), the health check at `/diagrid/region/writable` works on the hostname too.
- **Sentry and the region's Dapr services.** These services present their own certificates, so the gateway passes their connections through without opening them. Clients connect to the hostname, but send the service's own name, such as `sentry.catalyst-api.mycompany.com`, in the TLS handshake. The gateway routes the connection by that name, so those names need no certificate.

Diagrid gives out the matching addresses. Every project's HTTP and gRPC endpoint is `https://catalyst-api.mycompany.com`, with the port if the ingress has one. The region's management endpoint is `https://catalyst-api.mycompany.com/diagrid/management`.

### Create the record and the certificate

Create one DNS record for the hostname, pointing at the gateway's load balancer. Give the gateway a certificate for the hostname in `gateway.tls`. If a private CA signs it, see [Private gateway CA](./private-gateway-ca.mdx).

Because the gateway passes Sentry and the Dapr services through, anything in front of it must forward TCP without terminating TLS, just as for a wildcard domain. See [Inbound network connections](./system-architecture.md#inbound-network-connections).

Dapr sidecars that run outside the region's cluster, and some older clients, look up a passed-through service's name in DNS instead of connecting to the hostname. If you use them, add DNS records for those names too, at the same address. They need no certificate. For a region on `catalyst-api.mycompany.com`:

| DNS record | Needed for |
|------------|------------|
| `dapr-api.catalyst-api.mycompany.com`, `dapr-placement.catalyst-api.mycompany.com`, `dapr-scheduler.catalyst-api.mycompany.com` | Every Dapr sidecar that runs outside the region's cluster |
| `dapr-scheduler-server-0.catalyst-api.mycompany.com` to `dapr-scheduler-server-2.catalyst-api.mycompany.com`, one per scheduler replica | Every Dapr sidecar that runs outside the region's cluster |
| `sentry.catalyst-api.mycompany.com` | `diagrid dev` and `diagrid listen` from a Diagrid CLI older than 1.123.0, and Dapr sidecars outside the region's cluster that are older than the release that added this mode |
| `xrs.catalyst-api.mycompany.com` | Dapr sidecars outside the region's cluster that are older than the release that added this mode |

A region whose Dapr sidecars all run in its own cluster, used with a current Diagrid CLI, needs none of these records: the region's own sidecars reach these services inside the cluster. `diagrid region deploy` prints these records for your hostname.

### Switch an existing region

Switching changes the region's domain to the new hostname. Every name under the old wildcard domain stops working at once, including the project hostnames that running apps use. To switch:

1. Create the DNS record and install the certificate for the new hostname, at the gateway's current address. If Dapr sidecars run outside the region's cluster, also create [their DNS records](#create-the-record-and-the-certificate) under the new hostname.

2. Update the region:

   ```bash
   diagrid region update my-region --shared-project-endpoint --ingress https://catalyst-api.mycompany.com
   ```

3. Point your apps at the new endpoint. Re-run `diagrid dev`, or set `DAPR_HTTP_ENDPOINT` and `DAPR_GRPC_ENDPOINT` to `https://catalyst-api.mycompany.com`, with the port if the ingress has one. Set up any Dapr sidecars that run outside the region's cluster again, so they get the new address.

Apps can't reach the region until they use the new endpoint. Switching back works the same way in reverse, with a wildcard ingress:

```bash
diagrid region update my-region --shared-project-endpoint=false --ingress "https://*.catalyst.example.com:443"
```

### Check the hostname

Test the hostname with an App ID's API token:

```bash
curl -i -H "dapr-api-token: <app-id-api-token>" https://catalyst-api.mycompany.com/v1.0/healthz
```

A `2xx` response means the record, the certificate and the routing all work. A `401` means the gateway serves the hostname, but the token doesn't match any App ID.

## Run a region without your own public wildcard record

If your DNS policy doesn't allow a public wildcard record in your domain, [serve the region on one hostname](#serve-the-region-on-one-hostname), or keep the wildcard domain and use one of these three options.

### Let Diagrid manage the domain

With a managed domain, Diagrid owns the region's domain, creates its DNS record and issues its certificate. You create neither.

```bash
diagrid region create my-region --enable-managed-domain --ingress "https://my-gateway-lb.internal.example.com:443"
```

Diagrid publishes `*.<hash>.r1.privatediagrid.net` in public DNS, pointing at the address you give in `--ingress`: a `CNAME` for a hostname, an `A` record for an IP address. Without `--ingress`, it uses the address of the gateway's load balancer Service. The gateway gets the certificate from the control plane. [Quick deployment](./quick-deployment.mdx) uses a managed domain by default and configures the data plane for it; if you install with Helm yourself, see the [Helm reference](./reference.md) for the matching values.

The address can be private. The public record only reveals it, and clients can only reach it from your network. If your policy forbids public records that point at private addresses, use one of the other options.

### Create a record for each hostname

Keep the wildcard ingress on the region, but create individual DNS records instead of a wildcard record:

1. At install time, create records for the region's fixed hostnames that you use: at least `management.` and `trust.`, plus any others from [Inbound network connections](./system-architecture.md#inbound-network-connections).
2. After you create each project, read its two hostnames and create a record for each:

   ```bash
   diagrid project create my-project --region my-region
   diagrid project get my-project -o json | jq -r '.status.endpoints.http.url, .status.endpoints.grpc.url'
   ```

A project can't be reached until its records exist, so create them in the same pipeline that creates the project. If you'd rather not create records for every project, [serve the region on one hostname](#serve-the-region-on-one-hostname) instead.

The certificate must still cover every hostname. A wildcard certificate doesn't need a wildcard DNS record: cert-manager issues `*.<domain>` with a DNS-01 challenge, which needs only a `TXT` record at `_acme-challenge.<domain>`. If you can't use a wildcard certificate, list every hostname in the certificate and reissue it with the new names before anyone uses a new project. To avoid reissuing it, [serve the region on one hostname](#serve-the-region-on-one-hostname).

### Resolve the domain only inside your network

Put the wildcard record in DNS that only your network can see, so nothing is published:

- **A private DNS zone**, such as an Amazon Route 53 private hosted zone, an Azure Private DNS zone or your internal DNS server, with `*.<domain>` pointing at the gateway's internal load balancer.
- **The cluster's CoreDNS**, for apps that run in the same cluster as the region. See the next section.

The gateway still needs a certificate for `*.<domain>`. If a private CA signs it, follow [Private gateway CA](./private-gateway-ca.mdx).

## Resolve project hostnames to the gateway with CoreDNS

An app in the same cluster as the region can call its project without leaving the cluster. It still has to call the project's hostname, or the region's hostname if the region is [served on one hostname](#serve-the-region-on-one-hostname), because the gateway only routes requests that use the region's hostnames. What changes is where the hostname resolves: to the `gateway-envoy` Service in `cra-agent`, not to the load balancer. A CoreDNS rewrite does that for every project in the region and every pod in the cluster, with one rule.

### Choose the port

Inside the cluster, callers reach the gateway on the Service's ports, not the load balancer's. Both are set under `gateway.envoy.service` in your Helm values:

| Value | Carries |
|-------|---------|
| `port` (default `8080`) | Plain HTTP and gRPC. |
| `httpsPort` (`443` in the install guides) | HTTPS and gRPC over TLS, with the gateway's certificate. Only when `gateway.tls` is set. |

The rewrite applies to every pod, including any Dapr sidecars in this cluster that run outside the region and call `sentry.`, `xrs.` and the `dapr-*` hostnames on the region's ingress port. Use it only when `httpsPort` equals that port, as it does in the install guides (`443`). Otherwise those callers stop connecting. Set `httpsPort` to match, or use [host aliases](#use-host-aliases-instead), which affect only the pods you set them on.

With `httpsPort: 443`, apps keep the project endpoints that `diagrid project get` prints. To skip TLS inside the cluster, use the plain port instead:

```bash
DAPR_HTTP_ENDPOINT=http://http-prj<project-id>.<domain>:8080
DAPR_GRPC_ENDPOINT=http://grpc-prj<project-id>.<domain>:8080
```

In a region served on one hostname, use that hostname for both, for example `http://catalyst-api.mycompany.com:8080`.

### Add the rewrite rule

Adapt this rule: replace the domain, escape its dots, and keep the trailing `\.$`. `answer auto` needs CoreDNS 1.8.4 or later.

```text
rewrite stop {
  name regex .*\.catalyst\.example\.com\.$ gateway-envoy.cra-agent.svc.cluster.local. answer auto
}
```

In a region served on one hostname, the rule must also match the hostname itself. For `catalyst-api.mycompany.com`, use `name regex (.*\.)?catalyst-api\.mycompany\.com\.$` in the rule.

If the Corefile already has a rewrite that matches the region's domain, replace it with this rule instead of adding this one next to it. Pods look up the name with the cluster's search suffixes first, such as `http-prj123.catalyst.example.com.svc.cluster.local`, and a rule that matches those names answers before this one.

Don't use the two-line pattern from the CoreDNS examples, a `name regex` line followed by `answer name`. It only works when the target keeps part of the original name. Here every hostname maps to the one gateway name, so CoreDNS answers under the gateway's name instead of the one the app asked for. Apps built on glibc then fail to resolve the name. `answer auto` writes the requested name back into the answer.

Where the rule goes depends on the cluster:

- **AKS.** Don't edit the `coredns` ConfigMap, because AKS resets it. Put the rule in the `coredns-custom` ConfigMap in `kube-system`, under a key that ends in `.override`, then restart CoreDNS with `kubectl -n kube-system rollout restart deployment coredns`.
- **EKS with the CoreDNS add-on.** Add the rule to the Corefile through the add-on's configuration. An edit to the `coredns` ConfigMap can be overwritten when the add-on updates.
- **GKE with kube-dns, OpenShift and EKS Auto Mode.** You can't add a rewrite. Use [host aliases](#use-host-aliases-instead).
- **Any other cluster that runs CoreDNS.** Add the rule to the server block for `.` in the `coredns` ConfigMap in `kube-system`.

Check the CoreDNS version first. After CoreDNS restarts, check the rule from one of your app's pods. The second command prints the cluster IP of the `gateway-envoy` Service:

```bash
kubectl -n kube-system get deploy coredns -o jsonpath='{.spec.template.spec.containers[0].image}'
kubectl exec <app-pod> -- getent hosts http-prj<project-id>.<domain>
```

### Use host aliases instead

Host aliases need nothing from DNS, and they change only the pods you set them on. Get the gateway's cluster IP:

```bash
kubectl get svc -n cra-agent gateway-envoy -o jsonpath='{.spec.clusterIP}'
```

Then add the project's two hostnames to your pod spec. If the region is served on one hostname, add that hostname instead:

```yaml
spec:
  hostAliases:
    - ip: "<gateway cluster IP>"
      hostnames:
        - http-prj<project-id>.<domain>
        - grpc-prj<project-id>.<domain>
```

The cluster IP stays the same while the Service exists. It changes if the Service is deleted and created again, for example when you uninstall and reinstall the region.

### Allow the traffic

Your app runs in your own namespace, so the region's [network policies](./system-architecture.md#network-policies) don't apply to it. If you restrict egress from that namespace, allow traffic to the `cra-agent` namespace. Some network plugins, Cilium among them, don't apply IP-based rules to traffic between pods. On those, also allow ingress to the pods labelled `app.kubernetes.io/component: gateway-envoy` in `cra-agent` from your app's namespace.
