Change to use a new 'endpoints' directive and use a constant Add initial docs for 'endpoints' directive Add tests to Kubernetes setup for endpoints Changes based on PR feedback endpoint_pod_names is a boolean config option. Chahanged docs to reflect this. Add a test when endpoints_pod_names is not set Update README.md Remove endpointNameModeName as it is no longer used
52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
|
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
|
"github.com/coredns/coredns/request"
|
|
)
|
|
|
|
// Reverse implements the ServiceBackend interface.
|
|
func (k *Kubernetes) Reverse(state request.Request, exact bool, opt plugin.Options) ([]msg.Service, error) {
|
|
|
|
ip := dnsutil.ExtractAddressFromReverse(state.Name())
|
|
if ip == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
records := k.serviceRecordForIP(ip, state.Name())
|
|
return records, nil
|
|
}
|
|
|
|
// serviceRecordForIP gets a service record with a cluster ip matching the ip argument
|
|
// If a service cluster ip does not match, it checks all endpoints
|
|
func (k *Kubernetes) serviceRecordForIP(ip, name string) []msg.Service {
|
|
// First check services with cluster ips
|
|
for _, service := range k.APIConn.ServiceList() {
|
|
if (len(k.Namespaces) > 0) && !k.namespaceExposed(service.Namespace) {
|
|
continue
|
|
}
|
|
if service.Spec.ClusterIP == ip {
|
|
domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()}, ".")
|
|
return []msg.Service{{Host: domain}}
|
|
}
|
|
}
|
|
// If no cluster ips match, search endpoints
|
|
for _, ep := range k.APIConn.EpIndexReverse(ip) {
|
|
if (len(k.Namespaces) > 0) && !k.namespaceExposed(ep.ObjectMeta.Namespace) {
|
|
continue
|
|
}
|
|
for _, eps := range ep.Subsets {
|
|
for _, addr := range eps.Addresses {
|
|
if addr.IP == ip {
|
|
domain := strings.Join([]string{endpointHostname(addr, k.endpointNameMode), ep.ObjectMeta.Name, ep.ObjectMeta.Namespace, Svc, k.primaryZone()}, ".")
|
|
return []msg.Service{{Host: domain}}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|