Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-duplicate EndpointSlice endpoint addresses in the resolver #1482

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions coredns/resolver/endpoint_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"

"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/lighthouse/coredns/constants"
Expand Down Expand Up @@ -142,6 +143,8 @@ func (i *Interface) putHeadlessEndpointSlices(key, clusterID string, endpointSli

serviceInfo.clusters[clusterID] = clusterInfo

allAddresses := sets.New[string]()

for _, endpointSlice := range endpointSlices {
mcsPorts := mcsServicePortsFrom(endpointSlice.Ports)
publishNotReadyAddresses := endpointSlice.Annotations[constants.PublishNotReadyAddresses] == strconv.FormatBool(true)
Expand Down Expand Up @@ -169,15 +172,19 @@ func (i *Interface) putHeadlessEndpointSlices(key, clusterID string, endpointSli
}

for _, address := range endpoint.Addresses {
if allAddresses.Has(address) {
continue
}

allAddresses.Insert(address)

record := DNSRecord{
IP: address,
Ports: mcsPorts,
ClusterName: clusterID,
HostName: hostname,
}

record.HostName = hostname

records = append(records, record)
}

Expand Down
61 changes: 61 additions & 0 deletions coredns/resolver/headless_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,67 @@ func testHeadlessService() {
})
})
})

When("a service has multiple EndpointSlices with duplicate addresses", func() {
BeforeEach(func() {
endpointSlice = newEndpointSlice(namespace1, service1, clusterID1, []mcsv1a1.ServicePort{port1},
discovery.Endpoint{
Addresses: []string{endpointIP1},
Conditions: discovery.EndpointConditions{Ready: &ready},
},
discovery.Endpoint{
Addresses: []string{endpointIP2},
Conditions: discovery.EndpointConditions{Ready: &ready},
},
discovery.Endpoint{
Addresses: []string{endpointIP3},
Conditions: discovery.EndpointConditions{Ready: &ready},
},
)
})

JustBeforeEach(func() {
Expect(t.resolver.PutEndpointSlices(endpointSlice, newEndpointSlice(namespace1, service1, clusterID1, []mcsv1a1.ServicePort{port1},
discovery.Endpoint{
Addresses: []string{endpointIP1},
Conditions: discovery.EndpointConditions{Ready: &ready},
},
discovery.Endpoint{
Addresses: []string{endpointIP3},
Conditions: discovery.EndpointConditions{Ready: &ready},
},
discovery.Endpoint{
Addresses: []string{endpointIP4},
Conditions: discovery.EndpointConditions{Ready: &ready},
},
))).To(BeFalse())
})

It("should return DNS records with unique addresses", func() {
t.assertDNSRecordsFound(namespace1, service1, "", "", true,
resolver.DNSRecord{
IP: endpointIP1,
Ports: []mcsv1a1.ServicePort{port1},
ClusterName: clusterID1,
},
resolver.DNSRecord{
IP: endpointIP2,
Ports: []mcsv1a1.ServicePort{port1},
ClusterName: clusterID1,
},
resolver.DNSRecord{
IP: endpointIP3,
Ports: []mcsv1a1.ServicePort{port1},
ClusterName: clusterID1,
},
resolver.DNSRecord{
IP: endpointIP4,
Ports: []mcsv1a1.ServicePort{port1},
ClusterName: clusterID1,
},
)
})
})
}

func testHeadlessServiceInMultipleClusters() {
Expand Down
Loading