From d31890f12d2ed4824432cdb1bcc3f9648b0182d1 Mon Sep 17 00:00:00 2001 From: Thomas Vendetta Date: Thu, 13 Feb 2020 09:47:18 -0500 Subject: [PATCH] Fix issue where headless services could return duplicate pod IP's --- source/service.go | 15 ++++++++++++++- source/service_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/source/service.go b/source/service.go index 11323eae3e..46e65d1e7f 100644 --- a/source/service.go +++ b/source/service.go @@ -260,7 +260,20 @@ func (sc *serviceSource) extractHeadlessEndpoints(svc *v1.Service, hostname stri } sort.Strings(headlessDomains) for _, headlessDomain := range headlessDomains { - targets := targetsByHeadlessDomain[headlessDomain] + allTargets := targetsByHeadlessDomain[headlessDomain] + targets := []string{} + + deduppedTargets := map[string]bool{} + for _, target := range allTargets { + if _, ok := deduppedTargets[target]; ok { + log.Debugf("Removing duplicate target %s", target) + continue + } + + deduppedTargets[target] = true + targets = append(targets, target) + } + if ttl.IsConfigured() { endpoints = append(endpoints, endpoint.NewEndpointWithTTL(headlessDomain, endpoint.RecordTypeA, ttl, targets...)) } else { diff --git a/source/service_test.go b/source/service_test.go index 7acdbbc604..f687ce7683 100644 --- a/source/service_test.go +++ b/source/service_test.go @@ -1788,6 +1788,33 @@ func TestHeadlessServices(t *testing.T) { }, false, }, + { + "annotated Headless services return only a unique set of targets", + "", + "testing", + "foo", + v1.ServiceTypeClusterIP, + "", + "", + false, + map[string]string{"component": "foo"}, + map[string]string{ + hostnameAnnotationKey: "service.example.org", + }, + v1.ClusterIPNone, + []string{"1.1.1.1", "1.1.1.1", "1.1.1.2"}, + map[string]string{ + "component": "foo", + }, + []string{}, + []string{"foo-0", "foo-1", "foo-3"}, + []string{"", "", ""}, + []v1.PodPhase{v1.PodRunning, v1.PodRunning, v1.PodRunning}, + []*endpoint.Endpoint{ + {DNSName: "service.example.org", Targets: endpoint.Targets{"1.1.1.1", "1.1.1.2"}}, + }, + false, + }, } { t.Run(tc.title, func(t *testing.T) { // Create a Kubernetes testing client