coredns/plugin/kubernetes/reverse.go
Miek Gieben 830e97f800
plugin/kubernetes: allow trimming down of cached items. (#2128)
* Convert to runtime.Object to smaller structs

This adds conversion for all the objects we want to keep in the cache.
It keeps the minimum for CoreDNS to function and throws away the rest.

The conversion:
api.Endpoints -> object.Endpoints
api.Pod       -> object.Pod
api.Serivce   -> object.Service

We needed to copy some client-go stuff to insert a conversion function
into NewIndexInformers.

Some unrelated cleanups in the watch functionality as that needed to be
touched because of the above translation of objects.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Reduce test line-count

Signed-off-by: Miek Gieben <miek@miek.nl>

* ....and fix test

Signed-off-by: Miek Gieben <miek@miek.nl>

* Drop use of append

Signed-off-by: Miek Gieben <miek@miek.nl>

* cosmetic changes

Signed-off-by: Miek Gieben <miek@miek.nl>

* that was a typo

Signed-off-by: Miek Gieben <miek@miek.nl>

* re-introduce append here

We can't really use len() here because we don't know the number before
hand.

Signed-off-by: Miek Gieben <miek@miek.nl>

* comment in better place

Signed-off-by: Miek Gieben <miek@miek.nl>

* Make the timestamp a bool; thats where it is used for

Signed-off-by: Miek Gieben <miek@miek.nl>

* Set incoming object to nil

Explicataliy discard the converted object; we did a deep copy it's
not needed anymore.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Per Chris's comment

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-10-09 21:56:09 +01:00

54 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 == "" {
_, e := k.Records(state, exact)
return nil, e
}
records := k.serviceRecordForIP(ip, state.Name())
if len(records) == 0 {
return records, errNoItems
}
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.SvcIndexReverse(ip) {
if len(k.Namespaces) > 0 && !k.namespaceExposed(service.Namespace) {
continue
}
domain := strings.Join([]string{service.Name, service.Namespace, Svc, k.primaryZone()}, ".")
return []msg.Service{{Host: domain, TTL: k.ttl}}
}
// If no cluster ips match, search endpoints
for _, ep := range k.APIConn.EpIndexReverse(ip) {
if len(k.Namespaces) > 0 && !k.namespaceExposed(ep.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.Name, ep.Namespace, Svc, k.primaryZone()}, ".")
return []msg.Service{{Host: domain, TTL: k.ttl}}
}
}
}
}
return nil
}