add support for hostname annotation in ingress resource (#545)

* add support for hostname annotation in ingress resource

* implement getHostnamesFromAnnotations and use that in ingress/service
This commit is contained in:
Rajat Jindal 2018-05-08 07:13:19 -07:00 committed by Henning Jacobs
parent 0e99625e9a
commit c2751f81cf
4 changed files with 104 additions and 7 deletions

View File

@ -232,6 +232,12 @@ func endpointsFromIngress(ing *v1beta1.Ingress) []*endpoint.Endpoint {
}
endpoints = append(endpoints, endpointsForHostname(rule.Host, targets, ttl)...)
}
hostnameList := getHostnamesFromAnnotations(ing.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl)...)
}
return endpoints
}

View File

@ -615,6 +615,93 @@ func testIngressEndpoints(t *testing.T) {
},
},
},
{
title: "ingress rules with hostname annotation",
targetNamespace: "",
ingressItems: []fakeIngress{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
hostnameAnnotationKey: "dns-through-hostname.com",
},
dnsnames: []string{"example.org"},
ips: []string{"1.2.3.4"},
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "dns-through-hostname.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
},
},
{
title: "ingress rules with hostname annotation having multiple hostnames",
targetNamespace: "",
ingressItems: []fakeIngress{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
hostnameAnnotationKey: "dns-through-hostname.com, another-dns-through-hostname.com",
},
dnsnames: []string{"example.org"},
ips: []string{"1.2.3.4"},
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "dns-through-hostname.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
{
DNSName: "another-dns-through-hostname.com",
Targets: endpoint.Targets{"1.2.3.4"},
RecordType: endpoint.RecordTypeA,
},
},
},
{
title: "ingress rules with hostname and target annotation",
targetNamespace: "",
ingressItems: []fakeIngress{
{
name: "fake1",
namespace: namespace,
annotations: map[string]string{
hostnameAnnotationKey: "dns-through-hostname.com",
targetAnnotationKey: "ingress-target.com",
},
dnsnames: []string{"example.org"},
ips: []string{},
},
},
expected: []*endpoint.Endpoint{
{
DNSName: "example.org",
Targets: endpoint.Targets{"ingress-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
{
DNSName: "dns-through-hostname.com",
Targets: endpoint.Targets{"ingress-target.com"},
RecordType: endpoint.RecordTypeCNAME,
},
},
},
{
title: "ingress rules with annotation and custom TTL",
targetNamespace: "",

View File

@ -191,13 +191,7 @@ func (sc *serviceSource) endpointsFromTemplate(svc *v1.Service) ([]*endpoint.End
func (sc *serviceSource) endpoints(svc *v1.Service) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint
// Get the desired hostname of the service from the annotation.
hostnameAnnotation, exists := svc.Annotations[hostnameAnnotationKey]
if !exists {
return nil
}
hostnameList := strings.Split(strings.Replace(hostnameAnnotation, " ", "", -1), ",")
hostnameList := getHostnamesFromAnnotations(svc.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, sc.generateEndpoints(svc, hostname)...)
}

View File

@ -21,6 +21,7 @@ import (
"math"
"net"
"strconv"
"strings"
"github.com/kubernetes-incubator/external-dns/endpoint"
)
@ -64,6 +65,15 @@ func getTTLFromAnnotations(annotations map[string]string) (endpoint.TTL, error)
return endpoint.TTL(ttlValue), nil
}
func getHostnamesFromAnnotations(annotations map[string]string) []string {
hostnameAnnotation, exists := annotations[hostnameAnnotationKey]
if !exists {
return nil
}
return strings.Split(strings.Replace(hostnameAnnotation, " ", "", -1), ",")
}
// suitableType returns the DNS resource record type suitable for the target.
// In this case type A for IPs and type CNAME for everything else.
func suitableType(target string) string {