* initial commit Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * convert endpointslices to object.endpoints Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add opt hard coded for now Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * check that server supports endpointslice Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * fix import grouping Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * dont use endpoint slice in 1.17 or 1.18 Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * bump kind/k8s in circle ci to latest Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * drop k8s to latest supported by kind Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * use endpointslice name as endoint Name; index by Service name Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * use index key comparison in nsAddrs() Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add Index to object.Endpoint fixtures; fix direct endpoint name compares Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add slice dup check and test Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * todo Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add ep-slice skew dup test for reverse Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * nsaddrs: de-dup ep-slice skew dups; add test Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * remove todo Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * address various feedback Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * consolidate endpoint/slice informer code Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * fix endpoint informer consolidation; use clearer func name Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * log info; use major/minor fields Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * fix nsAddr and unit test Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * add latency tracking for endpointslices Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * endpointslice latency unit test & fix Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * code shuffling Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * rename endpointslices in tests Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * remove de-dup from nsAddrs and test Signed-off-by: Chris O'Haver <cohaver@infoblox.com> * remove de-dup from findServices / test Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/miekg/dns"
|
|
api "k8s.io/api/core/v1"
|
|
)
|
|
|
|
func isDefaultNS(name, zone string) bool {
|
|
return strings.Index(name, defaultNSName) == 0 && strings.Index(name, zone) == len(defaultNSName)
|
|
}
|
|
|
|
// nsAddrs returns the A or AAAA records for the CoreDNS service in the cluster. If the service cannot be found,
|
|
// it returns a record for the local address of the machine we're running on.
|
|
func (k *Kubernetes) nsAddrs(external bool, zone string) []dns.RR {
|
|
var (
|
|
svcNames []string
|
|
svcIPs []net.IP
|
|
)
|
|
|
|
// Find the CoreDNS Endpoints
|
|
for _, localIP := range k.localIPs {
|
|
endpoints := k.APIConn.EpIndexReverse(localIP.String())
|
|
|
|
// Collect IPs for all Services of the Endpoints
|
|
for _, endpoint := range endpoints {
|
|
svcs := k.APIConn.SvcIndex(endpoint.Index)
|
|
for _, svc := range svcs {
|
|
if external {
|
|
svcName := strings.Join([]string{svc.Name, svc.Namespace, zone}, ".")
|
|
for _, exIP := range svc.ExternalIPs {
|
|
svcNames = append(svcNames, svcName)
|
|
svcIPs = append(svcIPs, net.ParseIP(exIP))
|
|
}
|
|
continue
|
|
}
|
|
svcName := strings.Join([]string{svc.Name, svc.Namespace, Svc, zone}, ".")
|
|
if svc.ClusterIP == api.ClusterIPNone {
|
|
// For a headless service, use the endpoints IPs
|
|
for _, s := range endpoint.Subsets {
|
|
for _, a := range s.Addresses {
|
|
svcNames = append(svcNames, endpointHostname(a, k.endpointNameMode)+"."+svcName)
|
|
svcIPs = append(svcIPs, net.ParseIP(a.IP))
|
|
}
|
|
}
|
|
} else {
|
|
svcNames = append(svcNames, svcName)
|
|
svcIPs = append(svcIPs, net.ParseIP(svc.ClusterIP))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// If no local IPs matched any endpoints, use the localIPs directly
|
|
if len(svcIPs) == 0 {
|
|
svcIPs = make([]net.IP, len(k.localIPs))
|
|
svcNames = make([]string, len(k.localIPs))
|
|
for i, localIP := range k.localIPs {
|
|
svcNames[i] = defaultNSName + zone
|
|
svcIPs[i] = localIP
|
|
}
|
|
}
|
|
|
|
// Create an RR slice of collected IPs
|
|
rrs := make([]dns.RR, len(svcIPs))
|
|
for i, ip := range svcIPs {
|
|
if ip.To4() == nil {
|
|
rr := new(dns.AAAA)
|
|
rr.Hdr.Class = dns.ClassINET
|
|
rr.Hdr.Rrtype = dns.TypeAAAA
|
|
rr.Hdr.Name = svcNames[i]
|
|
rr.AAAA = ip
|
|
rrs[i] = rr
|
|
continue
|
|
}
|
|
rr := new(dns.A)
|
|
rr.Hdr.Class = dns.ClassINET
|
|
rr.Hdr.Rrtype = dns.TypeA
|
|
rr.Hdr.Name = svcNames[i]
|
|
rr.A = ip
|
|
rrs[i] = rr
|
|
}
|
|
|
|
return rrs
|
|
}
|
|
|
|
const defaultNSName = "ns.dns."
|