From c2751f81cf10666dccec13abccff9038c4aa161e Mon Sep 17 00:00:00 2001 From: Rajat Jindal Date: Tue, 8 May 2018 07:13:19 -0700 Subject: [PATCH] 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 --- source/ingress.go | 6 +++ source/ingress_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++ source/service.go | 8 +--- source/source.go | 10 +++++ 4 files changed, 104 insertions(+), 7 deletions(-) diff --git a/source/ingress.go b/source/ingress.go index 098c53894..2fe82e8b3 100644 --- a/source/ingress.go +++ b/source/ingress.go @@ -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 } diff --git a/source/ingress_test.go b/source/ingress_test.go index 710195e7e..359c6560f 100644 --- a/source/ingress_test.go +++ b/source/ingress_test.go @@ -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: "", diff --git a/source/service.go b/source/service.go index 5920af1a3..ce4710371 100644 --- a/source/service.go +++ b/source/service.go @@ -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)...) } diff --git a/source/source.go b/source/source.go index 6a7496e7d..95eca52bc 100644 --- a/source/source.go +++ b/source/source.go @@ -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 {