Skip to content

Commit

Permalink
Fix issue where headless services could return duplicate pod IP's
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Vendetta committed Feb 13, 2020
1 parent b0a3f37 commit d31890f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
15 changes: 14 additions & 1 deletion source/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions source/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d31890f

Please sign in to comment.