-
Notifications
You must be signed in to change notification settings - Fork 2.8k
chore(fqdn-template): fqdn templating move to specific folder and update documentation #5354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2ab176
chore(fqdn): fqdn move to specific folder and update documentation
ivankatliarchuk d273fd1
chore(fqdn): fqdn move to specific folder and update documentation
ivankatliarchuk add6a6b
chore(fqdn): fqdn move to specific folder and update documentation
ivankatliarchuk e9576a2
chore(fqdn): fqdn move to specific folder and update documentation
ivankatliarchuk 13debc1
chore(fqdn): fqdn move to specific folder and update documentation
ivankatliarchuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,4 +164,3 @@ tests: | |
asserts: | ||
- failedTemplate: | ||
errorMessage: "'txtPrefix' and 'txtSuffix' are mutually exclusive" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
# FQDN Templating Guide | ||
|
||
## What is FQDN Templating? | ||
|
||
**FQDN templating** is a feature that allows to dynamically construct Fully Qualified Domain Names (FQDNs) using a Go templating engine. | ||
Instead of relying solely on annotations or static names, you can use metadata from Kubernetes objects—such as service names, namespaces, and labels—to generate DNS records programmatically and dynamically. | ||
|
||
This is useful for: | ||
|
||
- Creating consistent naming conventions across environments. | ||
- Reducing boilerplate annotations. | ||
- Supporting multi-tenant or dynamic environments. | ||
- Migrating from one DNS scheme to another | ||
- Supporting multiple variants, such as a regional one and then one that doesn't or similar. | ||
|
||
## How It Works | ||
|
||
ExternalDNS has a flag: `--fqdn-template`, which defines a Go template for rendering the desired DNS names. | ||
|
||
The template uses the following data from the source object (e.g., a `Service` or `Ingress`): | ||
|
||
| Field | Description | | ||
|:--------------|:------------------------------------------------------------------| | ||
| `Name` | Name of the object (e.g., service) | | ||
| `Namespace` | Namespace of the object | | ||
| `Labels` | Map of labels applied to the object | | ||
| `Annotations` | Map of annotations | | ||
| `TargetName` | For `Service`, it's the service name; for `Ingress`, the hostname | | ||
| `Endpoint` | Contains more contextual endpoint info, such as IP/target | | ||
| `Controller` | Controller type (optional) | | ||
|
||
## Supported Sources | ||
|
||
<!-- TODO: generate from code --> | ||
|
||
| Source | Description | FQDN Supported | | ||
|:-----------------------|:----------------------------------------------------------------|:--------------:| | ||
| `ambassador-host` | Queries Ambassador Host resources for endpoints. | ❌ | | ||
| `cloudfoundry` | Queries Cloud Foundry resources for endpoints. | ❌ | | ||
| `connector` | Queries a custom connector source for endpoints. | ❌ | | ||
| `contour-httpproxy` | Queries Contour HTTPProxy resources for endpoints. | ✅ | | ||
| `crd` | Queries Custom Resource Definitions (CRDs) for endpoints. | ❌ | | ||
| `empty` | Uses an empty source, typically for testing or no-op scenarios. | ❌ | | ||
| `f5-transportserver` | Queries F5 TransportServer resources for endpoints. | ❌ | | ||
| `f5-virtualserver` | Queries F5 VirtualServer resources for endpoints. | ❌ | | ||
| `fake` | Uses a fake source for testing purposes. | ❌ | | ||
| `gateway-grpcroute` | Queries GRPCRoute resources from the Gateway API. | ✅ | | ||
| `gateway-httproute` | Queries HTTPRoute resources from the Gateway API. | ✅ | | ||
| `gateway-tcproute` | Queries TCPRoute resources from the Gateway API. | ✅ | | ||
| `gateway-tlsroute` | Queries TLSRoute resources from the Gateway API. | ❌ | | ||
| `gateway-udproute` | Queries UDPRoute resources from the Gateway API. | ❌ | | ||
| `gloo-proxy` | Queries Gloo Proxy resources for endpoints. | ❌ | | ||
| `ingress` | Queries Kubernetes Ingress resources for endpoints. | ✅ | | ||
| `istio-gateway` | Queries Istio Gateway resources for endpoints. | ✅ | | ||
| `istio-virtualservice` | Queries Istio VirtualService resources for endpoints. | ✅ | | ||
| `kong-tcpingress` | Queries Kong TCPIngress resources for endpoints. | ❌ | | ||
| `node` | Queries Kubernetes Node resources for endpoints. | ✅ | | ||
| `openshift-route` | Queries OpenShift Route resources for endpoints. | ✅ | | ||
| `pod` | Queries Kubernetes Pod resources for endpoints. | ❌ | | ||
| `service` | Queries Kubernetes Service resources for endpoints. | ✅ | | ||
| `skipper-routegroup` | Queries Skipper RouteGroup resources for endpoints. | ✅ | | ||
| `traefik-proxy` | Queries Traefik Proxy resources for endpoints. | ❌ | | ||
|
||
## Custom Functions | ||
|
||
<!-- TODO: generate from code --> | ||
|
||
| Function | Description | | ||
|:-------------|:--------------------------------------------------------------------------------------| | ||
| `trimPrefix` | Function from the `strings` package. Returns `s` without the provided leading prefix. | | ||
|
||
--- | ||
|
||
## Example Usage | ||
|
||
> These examples should provide a solid foundation for implementing FQDN templating in your ExternalDNS setup. | ||
> If you have specific requirements or encounter issues, feel free to explore the issues or update this guide. | ||
|
||
### Basic Usage | ||
|
||
```sh | ||
external-dns \ | ||
--provider=aws \ | ||
--source=service \ | ||
--fqdn-template="{{ .Name }}.example.com,{{ .Name }}.{{ .Namespace }}.example.tld" | ||
|
||
# This will result in DNS entries like | ||
>route53> my-service.example.com | ||
>route53> another-service.example.com | ||
mloiseleur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
### With Namespace | ||
|
||
```yml | ||
args: | ||
--fqdn-template="{{.Name}}.{{.Namespace}}.example.com" | ||
|
||
# This will result in DNS entries like | ||
# route53> my-service.default.example.com | ||
# route53> other-service.kube-system.example.com | ||
``` | ||
|
||
### Using Labels in Templates | ||
|
||
You can also utilize labels in your FQDN templates to create more dynamic DNS entries. Assuming your service has: | ||
|
||
```yml | ||
metadata: | ||
labels: | ||
environment: staging | ||
``` | ||
|
||
```yml | ||
args: | ||
--fqdn-template="{{.Labels.environment}}.example.com" | ||
|
||
# This will result in DNS entries like | ||
# route53> staging.my-service.example.com | ||
mloiseleur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
### Multiple FQDN Templates | ||
|
||
ExternalDNS allows specifying multiple FQDN templates, which can be useful when you want to create multiple DNS entries for a single service or ingress. | ||
|
||
> Be cautious, as this will create multiple DNS records per resource, potentially increasing the number of API calls to your DNS provider. | ||
|
||
```yml | ||
args: | ||
--fqdn-template={{.Name}}.example.com,{{.Name}}.svc.example.com | ||
``` | ||
|
||
### Conditional Templating combined with Annotations processing | ||
|
||
In scenarios where you want to conditionally generate FQDNs based on annotations, you can use Go template functions like or to provide defaults. | ||
|
||
```yml | ||
args: | ||
- --combine-fqdn-annotation # this is required to combine FQDN templating and annotation processing | ||
- --fqdn-template={{ or .Annotations.dns "invalid" }}.example.com | ||
- --exclude-domains=invalid.example.com | ||
``` | ||
|
||
### Using Annotations for FQDN Templating] | ||
mloiseleur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
This example demonstrates how to use annotations in Kubernetes objects to dynamically generate Fully Qualified Domain Names (FQDNs) using the --fqdn-template flag in ExternalDNS. | ||
|
||
The Service object includes an annotation dns.company.com/label with the value my-org-tld-v2. This annotation is used as part of the FQDN template to construct the DNS name. | ||
|
||
```yml | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: nginx-v2 | ||
namespace: my-namespace | ||
annotations: | ||
dns.company.com/label: my-org-tld-v2 | ||
spec: | ||
type: ClusterIP | ||
clusterIP: None | ||
``` | ||
|
||
The --fqdn-template flag is configured to use the annotation value (dns.company.com/label) and append the namespace and a custom domain (company.local) to generate the FQDN. | ||
|
||
```yml | ||
args: | ||
--source=service | ||
--fqdn-template='{{index .ObjectMeta.Annotations "dns.company.com/label"}}.{{ .Namespace }}.company.local' | ||
|
||
# For the given Service object, the resulting FQDN will be: | ||
# route53> my-org-tld-v2.my-namespace.company.local | ||
``` | ||
|
||
### DNS Scheme Migration | ||
|
||
If you're transitioning from one naming convention to another (e.g., from svc.cluster.local to svc.example.com), --fqdn-template allows you to generate the new records alongside or in place of the old ones — without requiring changes to your Kubernetes manifests. | ||
|
||
```yml | ||
args: | ||
- --fqdn-template='{{.Name}}.new-dns.example.com' | ||
``` | ||
|
||
This helps automate DNS record migration while maintaining service continuity. | ||
|
||
### Multi-Variant Domain Support | ||
|
||
You can also support regional variants or multi-tenant architectures, where the same service is deployed to different regions or environments: | ||
|
||
```yaml | ||
--fqdn-template='{{ .Name }}.{{ .Environment }}.{{.Region}}.example.com, {{ if eq .Environment "prod" }}{{ .Name }}.my-company.tld{{ end }}' | ||
``` | ||
|
||
With additional context (e.g., annotations), this can produce FQDNs like: | ||
|
||
```yml | ||
api.dev.us-east-1.example.com | ||
api.my-company.tld | ||
mloiseleur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
``` | ||
|
||
This is helpful in scenarios such as: | ||
|
||
- Blue/green deployments across domains | ||
- Staging vs. production resolution | ||
- Multi-cloud or multi-region failover strategies | ||
|
||
## Tips | ||
|
||
- If `--fqdn-template` is specified, ExternalDNS ignores any `external-dns.alpha.kubernetes.io/hostname` annotations. | ||
- You must still ensure the resulting FQDN is valid and unique. | ||
- Since Go templates can be error-prone, test your template with simple examples before deploying. Mismatched field names or nil values (e.g., missing labels) will result in errors or skipped entries. | ||
|
||
## FaQ | ||
|
||
### Can I specify multiple global FQDN templates? | ||
|
||
Yes, you can. Pass in a comma separated list to --fqdn-template. Beware this will double (triple, etc) the amount of DNS entries based on how many services, ingresses and so on you have and will get you faster towards the API request limit of your DNS provider. | ||
|
||
### Where to find template syntax | ||
|
||
- [Go template syntax](https://pkg.go.dev/text/template) | ||
- [Go func builtins](https://github.com/golang/go/blob/master/src/text/template/funcs.go#L39-L63) | ||
|
||
### FQDN Templating, Helm and improper templating syntax | ||
|
||
The user encountered errors due to improper templating syntax: | ||
|
||
```yml | ||
extraArgs: | ||
- --fqdn-template={{name}}.uat.example.com | ||
``` | ||
|
||
The correct syntax should include a dot prefix: `{{.Name}}`. | ||
Additionally, when using Helm's `tpl` function, it's necessary to escape the braces to prevent premature evaluation: | ||
|
||
```yml | ||
extraArgs: | ||
- --fqdn-template={{ `{{.Name}}.uat.example.com` }} | ||
``` | ||
|
||
### Handling Subdomain-Only Hostnames | ||
|
||
In [Issue #1872](https://github.com/kubernetes-sigs/external-dns/issues/1872), it was observed that ExternalDNS ignores the `--fqdn-template` when the ingress host field is set to a subdomain (e.g., foo) without a full domain. | ||
The expectation was that the template would still apply, generating entries like `foo.bar.example.com.` | ||
This highlights a limitation to be aware of when designing FQDN templates. | ||
|
||
> :warning: This is currently not supported ! User would expect external-dns to generate a dns record according to the fqdnTemplate | ||
> e.g. if the ingress name: foo and host: foo is created while fqdnTemplate={{.Name}}.bar.example.com then a dns record foo.bar.example.com should be created | ||
|
||
```yml | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: foo | ||
spec: | ||
rules: | ||
- host: foo | ||
http: | ||
paths: | ||
- backend: | ||
serviceName: foo | ||
servicePort: 80 | ||
path: / | ||
``` | ||
|
||
### Combining FQDN Template with Annotations | ||
|
||
In [Issue #3318](https://github.com/kubernetes-sigs/external-dns/issues/3318), a question was raised about the interaction between --fqdn-template and --combine-fqdn-annotation. | ||
The discussion clarified that when both flags are used, ExternalDNS combines the FQDN generated from the template with the annotation value, providing flexibility in DNS name construction. | ||
|
||
### Using Annotations for Dynamic FQDNs | ||
|
||
In [Issue #2627](https://github.com/kubernetes-sigs/external-dns/issues/2627), a user aimed to generate DNS entries based on ingress annotations: | ||
|
||
```yml | ||
args: | ||
- --fqdn-template={{.Annotations.hostname}}.example.com | ||
- --combine-fqdn-annotation | ||
- --domain-filter=example.com | ||
``` | ||
|
||
By setting the hostname annotation in the ingress resource, ExternalDNS constructs the FQDN accordingly. This approach allows for dynamic DNS entries without hardcoding hostnames. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2025 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package fqdn | ||
|
||
import ( | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
func ParseTemplate(fqdnTemplate string) (tmpl *template.Template, err error) { | ||
if fqdnTemplate == "" { | ||
return nil, nil | ||
} | ||
funcs := template.FuncMap{ | ||
"trimPrefix": strings.TrimPrefix, | ||
} | ||
return template.New("endpoint").Funcs(funcs).Parse(fqdnTemplate) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.