Fix issue where headless services could return duplicate pod IP's

This commit is contained in:
Thomas Vendetta 2020-02-13 09:47:18 -05:00
parent b0a3f3784f
commit d31890f12d
No known key found for this signature in database
GPG Key ID: 8366FB06892B4447
2 changed files with 41 additions and 1 deletions

View File

@ -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 {

View File

@ -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