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

SRV record headless #553

Merged
merged 2 commits into from
Jun 11, 2021
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
2 changes: 1 addition & 1 deletion pkg/endpointslice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *Controller) IsHealthy(name, namespace, clusterID string) bool {
if endpointInfo != nil && endpointInfo.clusterInfo != nil {
info := endpointInfo.clusterInfo[clusterID]
if info != nil {
return len(info.ipList) > 0
return len(info.recordList) > 0
}
}

Expand Down
57 changes: 43 additions & 14 deletions pkg/endpointslice/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (

"github.com/submariner-io/admiral/pkg/log"
"github.com/submariner-io/lighthouse/pkg/constants"
"github.com/submariner-io/lighthouse/pkg/serviceimport"
discovery "k8s.io/api/discovery/v1beta1"
"k8s.io/klog"
mcsv1a1 "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
)

type endpointInfo struct {
Expand All @@ -32,16 +34,16 @@ type endpointInfo struct {
}

type clusterInfo struct {
hostIPs map[string][]string
ipList []string
hostRecords map[string][]serviceimport.DNSRecord
recordList []serviceimport.DNSRecord
}

type Map struct {
epMap map[string]*endpointInfo
sync.RWMutex
}

func (m *Map) GetIPs(hostname, cluster, namespace, name string, checkCluster func(string) bool) ([]string, bool) {
func (m *Map) GetDNSRecords(hostname, cluster, namespace, name string, checkCluster func(string) bool) ([]serviceimport.DNSRecord, bool) {
key := keyFunc(name, namespace)

clusterInfos := func() map[string]*clusterInfo {
Expand All @@ -62,24 +64,24 @@ func (m *Map) GetIPs(hostname, cluster, namespace, name string, checkCluster fun

switch {
case cluster == "":
ips := make([]string, 0)
records := make([]serviceimport.DNSRecord, 0)

for clusterID, info := range clusterInfos {
if checkCluster == nil || checkCluster(clusterID) {
ips = append(ips, info.ipList...)
records = append(records, info.recordList...)
}
}

return ips, true
return records, true
case clusterInfos[cluster] == nil:
return nil, false
case hostname == "":
return clusterInfos[cluster].ipList, true
case clusterInfos[cluster].hostIPs == nil:
return clusterInfos[cluster].recordList, true
case clusterInfos[cluster].hostRecords == nil:
return nil, false
default:
ips, ok := clusterInfos[cluster].hostIPs[hostname]
return ips, ok
records, ok := clusterInfos[cluster].hostRecords[hostname]
return records, ok
}
}

Expand Down Expand Up @@ -115,16 +117,43 @@ func (m *Map) Put(es *discovery.EndpointSlice) {
}

epInfo.clusterInfo[cluster] = &clusterInfo{
ipList: make([]string, 0),
hostIPs: make(map[string][]string),
recordList: make([]serviceimport.DNSRecord, 0),
hostRecords: make(map[string][]serviceimport.DNSRecord),
}

mcsPorts := make([]mcsv1a1.ServicePort, len(es.Ports))

for i, port := range es.Ports {
mcsPort := mcsv1a1.ServicePort{
Name: *port.Name,
Protocol: *port.Protocol,
AppProtocol: port.AppProtocol,
Port: *port.Port,
}
mcsPorts[i] = mcsPort
}

for _, endpoint := range es.Endpoints {
var records []serviceimport.DNSRecord

for _, address := range endpoint.Addresses {
record := serviceimport.DNSRecord{
IP: address,
Ports: mcsPorts,
}

if endpoint.Hostname != nil {
record.HostName = *endpoint.Hostname
}

records = append(records, record)
}

if endpoint.Hostname != nil {
epInfo.clusterInfo[cluster].hostIPs[*endpoint.Hostname] = endpoint.Addresses
epInfo.clusterInfo[cluster].hostRecords[*endpoint.Hostname] = records
}

epInfo.clusterInfo[cluster].ipList = append(epInfo.clusterInfo[cluster].ipList, endpoint.Addresses...)
epInfo.clusterInfo[cluster].recordList = append(epInfo.clusterInfo[cluster].recordList, records...)
}

klog.V(log.DEBUG).Infof("Adding clusterInfo %#v for EndpointSlice %q in %q", epInfo.clusterInfo[cluster], es.Name, cluster)
Expand Down
12 changes: 9 additions & 3 deletions pkg/endpointslice/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package endpointslice_test
import (
"sort"

"github.com/submariner-io/lighthouse/pkg/serviceimport"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/submariner-io/lighthouse/pkg/endpointslice"
Expand Down Expand Up @@ -55,16 +57,20 @@ var _ = Describe("EndpointSlice Map", func() {
return clusterStatusMap[id]
}

getIPs := func(hostname, cluster, ns, name string) []string {
ips, found := endpointSliceMap.GetIPs(hostname, cluster, ns, name, checkCluster)
getRecords := func(hostname, cluster, ns, name string) []serviceimport.DNSRecord {
ips, found := endpointSliceMap.GetDNSRecords(hostname, cluster, ns, name, checkCluster)
Expect(found).To(BeTrue())
return ips
}

expectIPs := func(hostname, cluster, ns, name string, expIPs []string) {
sort.Strings(expIPs)
for i := 0; i < 5; i++ {
ips := getIPs(hostname, cluster, namespace1, service1)
var ips []string
records := getRecords(hostname, cluster, namespace1, service1)
for _, record := range records {
ips = append(ips, record.IP)
}
sort.Strings(ips)
Expect(ips).To(Equal(expIPs))
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/serviceimport/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (
)

type DNSRecord struct {
IP string
Ports []mcsv1a1.ServicePort
IP string
Ports []mcsv1a1.ServicePort
HostName string
}

type clusterInfo struct {
Expand Down
22 changes: 13 additions & 9 deletions plugin/lighthouse/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,28 @@ func (lh *Lighthouse) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns

func (lh *Lighthouse) getDNSRecord(zone string, state request.Request, ctx context.Context, w dns.ResponseWriter,
r *dns.Msg, pReq recordRequest) (int, error) {
var isHeadless bool
var (
ips []string
found bool
record *serviceimport.DNSRecord
dnsRecords []serviceimport.DNSRecord
found bool
record *serviceimport.DNSRecord
)

record, found = lh.getClusterIPForSvc(pReq)
if !found {
ips, found = lh.endpointSlices.GetIPs(pReq.hostname, pReq.cluster, pReq.namespace, pReq.service, lh.clusterStatus.IsConnected)
dnsRecords, found = lh.endpointSlices.GetDNSRecords(pReq.hostname, pReq.cluster, pReq.namespace,
pReq.service, lh.clusterStatus.IsConnected)
if !found {
log.Debugf("No record found for %q", state.QName())
return lh.nextOrFailure(state.Name(), ctx, w, r, dns.RcodeNameError, "record not found")
}

isHeadless = true
} else if record != nil && record.IP != "" {
ips = []string{record.IP}
dnsRecords = append(dnsRecords, *record)
}

if len(ips) == 0 {
if len(dnsRecords) == 0 {
log.Debugf("Couldn't find a connected cluster or valid IPs for %q", state.QName())
return lh.emptyResponse(state)
}
Expand All @@ -95,12 +99,12 @@ func (lh *Lighthouse) getDNSRecord(zone string, state request.Request, ctx conte
return lh.emptyResponse(state)
}

var records []dns.RR
records := make([]dns.RR, 0)

if state.QType() == dns.TypeA {
records = lh.createARecords(ips, state)
records = lh.createARecords(dnsRecords, state)
} else if state.QType() == dns.TypeSRV {
records = lh.createSRVRecords(record, state, pReq, zone)
records = lh.createSRVRecords(dnsRecords, state, pReq, zone, isHeadless)
}

if len(records) == 0 {
Expand Down
Loading