2017-08-09 03:13:38 -07:00
|
|
|
package kubernetes
|
|
|
|
|
|
2017-08-09 04:06:48 -07:00
|
|
|
import (
|
2017-09-14 09:36:06 +01:00
|
|
|
"github.com/coredns/coredns/plugin"
|
2018-10-09 21:56:09 +01:00
|
|
|
"github.com/coredns/coredns/plugin/kubernetes/object"
|
2017-08-09 04:06:48 -07:00
|
|
|
"github.com/coredns/coredns/request"
|
|
|
|
|
)
|
|
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// AutoPath implements the AutoPathFunc call from the autopath plugin.
|
2017-08-10 19:26:31 +01:00
|
|
|
// It returns a per-query search path or nil indicating no searchpathing should happen.
|
|
|
|
|
func (k *Kubernetes) AutoPath(state request.Request) []string {
|
2018-02-12 14:27:16 -05:00
|
|
|
// Check if the query falls in a zone we are actually authoritative for and thus if we want autopath.
|
2017-09-14 09:36:06 +01:00
|
|
|
zone := plugin.Zones(k.Zones).Matches(state.Name())
|
2017-08-10 19:26:31 +01:00
|
|
|
if zone == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 04:53:05 +08:00
|
|
|
// cluster.local {
|
|
|
|
|
// autopath @kubernetes
|
|
|
|
|
// kubernetes {
|
|
|
|
|
// pods verified #
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// if pods != verified will cause panic and return SERVFAIL, expect worked as normal without autopath function
|
|
|
|
|
if !k.opts.initPodCache {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 04:06:48 -07:00
|
|
|
ip := state.IP()
|
|
|
|
|
|
2017-08-10 19:26:31 +01:00
|
|
|
pod := k.podWithIP(ip)
|
2017-08-09 04:06:48 -07:00
|
|
|
if pod == nil {
|
2017-08-10 19:26:31 +01:00
|
|
|
return nil
|
2017-08-09 04:06:48 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-10 19:26:31 +01:00
|
|
|
search := make([]string, 3)
|
|
|
|
|
if zone == "." {
|
|
|
|
|
search[0] = pod.Namespace + ".svc."
|
|
|
|
|
search[1] = "svc."
|
|
|
|
|
search[2] = "."
|
|
|
|
|
} else {
|
|
|
|
|
search[0] = pod.Namespace + ".svc." + zone
|
|
|
|
|
search[1] = "svc." + zone
|
|
|
|
|
search[2] = zone
|
|
|
|
|
}
|
2017-08-09 04:06:48 -07:00
|
|
|
|
2017-08-10 19:26:31 +01:00
|
|
|
search = append(search, k.autoPathSearch...)
|
2018-08-14 17:55:55 +02:00
|
|
|
search = append(search, "") // sentinel
|
2017-08-10 19:26:31 +01:00
|
|
|
return search
|
2017-08-09 04:06:48 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-01 15:10:45 +08:00
|
|
|
// podWithIP returns the api.Pod for source IP. It returns nil if nothing can be found.
|
2018-10-09 21:56:09 +01:00
|
|
|
func (k *Kubernetes) podWithIP(ip string) *object.Pod {
|
2020-05-04 04:17:26 -04:00
|
|
|
if k.podMode != podModeVerified {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-09-29 15:58:50 -04:00
|
|
|
ps := k.APIConn.PodIndex(ip)
|
|
|
|
|
if len(ps) == 0 {
|
|
|
|
|
return nil
|
2017-08-09 03:13:38 -07:00
|
|
|
}
|
2017-09-29 15:58:50 -04:00
|
|
|
return ps[0]
|
2017-08-09 03:13:38 -07:00
|
|
|
}
|