Skip to content

Commit cdaf96b

Browse files
committed
add plugin code from coredns/plugin/kubernetes
1 parent 1894f03 commit cdaf96b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+6647
-0
lines changed

Diff for: autopath.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package kubernetes
2+
3+
import (
4+
"github.com/coredns/coredns/plugin"
5+
"github.com/coredns/coredns/plugin/kubernetes/object"
6+
"github.com/coredns/coredns/request"
7+
)
8+
9+
// AutoPath implements the AutoPathFunc call from the autopath plugin.
10+
// It returns a per-query search path or nil indicating no searchpathing should happen.
11+
func (k *Kubernetes) AutoPath(state request.Request) []string {
12+
// Check if the query falls in a zone we are actually authoritative for and thus if we want autopath.
13+
zone := plugin.Zones(k.Zones).Matches(state.Name())
14+
if zone == "" {
15+
return nil
16+
}
17+
18+
// cluster.local {
19+
// autopath @kubernetes
20+
// kubernetes {
21+
// pods verified #
22+
// }
23+
// }
24+
// if pods != verified will cause panic and return SERVFAIL, expect worked as normal without autopath function
25+
if !k.opts.initPodCache {
26+
return nil
27+
}
28+
29+
ip := state.IP()
30+
31+
pod := k.podWithIP(ip)
32+
if pod == nil {
33+
return nil
34+
}
35+
36+
search := make([]string, 3)
37+
if zone == "." {
38+
search[0] = pod.Namespace + ".svc."
39+
search[1] = "svc."
40+
search[2] = "."
41+
} else {
42+
search[0] = pod.Namespace + ".svc." + zone
43+
search[1] = "svc." + zone
44+
search[2] = zone
45+
}
46+
47+
search = append(search, k.autoPathSearch...)
48+
search = append(search, "") // sentinel
49+
return search
50+
}
51+
52+
// podWithIP return the api.Pod for source IP. It returns nil if nothing can be found.
53+
func (k *Kubernetes) podWithIP(ip string) *object.Pod {
54+
if k.podMode != podModeVerified {
55+
return nil
56+
}
57+
ps := k.APIConn.PodIndex(ip)
58+
if len(ps) == 0 {
59+
return nil
60+
}
61+
return ps[0]
62+
}

0 commit comments

Comments
 (0)