Skip to content

Commit

Permalink
Added support for Extern-DNS Annotations for Services
Browse files Browse the repository at this point in the history
Signed-off-by: Nikoleta Verbeck <nerdynick@gmail.comn>
  • Loading branch information
Nikoleta Verbeck committed Dec 1, 2022
1 parent cb857da commit b4d0deb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This plugin relies on its own connection to the k8s API server and doesn't share
| ---- | ---------------- | -------- |
| HTTPRoute<sup>[1](#foot1)</sup> | all FQDNs from `spec.hostnames` matching configured zones | `gateway.status.addresses`<sup>[2](#foot2)</sup> |
| Ingress | all FQDNs from `spec.rules[*].host` matching configured zones | `.status.loadBalancer.ingress` |
| Service<sup>[3](#foot3)</sup> | `name.namespace` + any of the configured zones OR any string consisting of lower case alphanumeric characters, '-' or '.', specified in the `coredns.io/hostname` annotation (see [this](https://github.com/ori-edge/k8s_gateway/blob/master/test/service-annotation.yml#L8) for an example) | `.status.loadBalancer.ingress` |
| Service<sup>[3](#foot3)</sup> | `name.namespace` + any of the configured zones OR any string consisting of lower case alphanumeric characters, '-' or '.', specified in the `coredns.io/hostname` or `external-dns.alpha.kubernetes.io/hostname` annotations (see [this](https://github.com/ori-edge/k8s_gateway/blob/master/test/service-annotation.yml#L8) for an example) | `.status.loadBalancer.ingress` |
| VirtualServer<sup>[4](#foot4)</sup> | `spec.host` | `.status.externalEnpoints.ip` |


Expand Down
43 changes: 27 additions & 16 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (
)

const (
defaultResyncPeriod = 0
ingressHostnameIndex = "ingressHostname"
serviceHostnameIndex = "serviceHostname"
gatewayUniqueIndex = "gatewayIndex"
httpRouteHostnameIndex = "httpRouteHostname"
virtualServerHostnameIndex = "virtualServerHostname"
hostnameAnnotationKey = "coredns.io/hostname"
defaultResyncPeriod = 0
ingressHostnameIndex = "ingressHostname"
serviceHostnameIndex = "serviceHostname"
gatewayUniqueIndex = "gatewayIndex"
httpRouteHostnameIndex = "httpRouteHostname"
virtualServerHostnameIndex = "virtualServerHostname"
hostnameAnnotationKey = "coredns.io/hostname"
externalDnsHostnameAnnotationKey = "external-dns.alpha.kubernetes.io/hostname"
)

// KubeController stores the current runtime configuration and cache
Expand Down Expand Up @@ -336,23 +337,33 @@ func serviceHostnameIndexFunc(obj interface{}) ([]string, error) {
}

hostname := service.Name + "." + service.Namespace
if annotation, exists := service.Annotations[hostnameAnnotationKey]; exists {
if annotation, exists := checkServiceAnnotation(hostnameAnnotationKey, service); exists {
hostname = annotation
} else if annotation, exists := checkServiceAnnotation(externalDnsHostnameAnnotationKey, service); exists {
hostname = annotation
}

log.Debugf("Adding index %s for service %s", hostname, service.Name)

return []string{hostname}, nil
}

func checkServiceAnnotation(annotation string, service *core.Service) (string, bool) {
if annotationValue, exists := service.Annotations[annotation]; exists {
// checking the hostname length limits
if _, ok := dns.IsDomainName(annotation); ok {
if _, ok := dns.IsDomainName(annotationValue); ok {
// checking RFC 1123 conformance (same as metadata labels)
if valid := isdns1123Hostname(annotation); valid {
hostname = strings.ToLower(annotation)
if valid := isdns1123Hostname(annotationValue); valid {
return strings.ToLower(annotationValue), true
} else {
log.Infof("RFC 1123 conformance failed for FQDN: %s", annotation)
log.Infof("RFC 1123 conformance failed for FQDN: %s", annotationValue)
}
} else {
log.Infof("Invalid FQDN length: %s", annotation)
log.Infof("Invalid FQDN length: %s", annotationValue)
}
}

log.Debugf("Adding index %s for service %s", hostname, service.Name)

return []string{hostname}, nil
return "", false
}

func virtualServerHostnameIndexFunc(obj interface{}) ([]string, error) {
Expand Down

0 comments on commit b4d0deb

Please sign in to comment.