| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 04:06:48 -07:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 19:11:28 +08:00
										 |  |  | 	api "k8s.io/api/core/v1"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 04:06:48 -07:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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...)
 | 
					
						
							| 
									
										
										
										
											2017-08-09 04:06:48 -07:00
										 |  |  | 	search = append(search, "") // sentinal
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:26:31 +01:00
										 |  |  | 	return search
 | 
					
						
							| 
									
										
										
										
											2017-08-09 04:06:48 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:26:31 +01:00
										 |  |  | // podWithIP return the api.Pod for source IP ip. It returns nil if nothing can be found.
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | func (k *Kubernetes) podWithIP(ip string) *api.Pod {
 | 
					
						
							|  |  |  | 	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
										 |  |  | }
 |