| 
									
										
										
										
											2017-08-09 05:18:46 -07:00
										 |  |  | package kubernetes
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-08-23 07:19:41 +01:00
										 |  |  | 	"strings"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/etcd/msg"
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil"
 | 
					
						
							| 
									
										
										
										
											2017-08-09 05:18:46 -07:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Reverse implements the ServiceBackend interface.
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | func (k *Kubernetes) Reverse(state request.Request, exact bool, opt plugin.Options) ([]msg.Service, error) {
 | 
					
						
							| 
									
										
										
										
											2017-08-09 05:18:46 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ip := dnsutil.ExtractAddressFromReverse(state.Name())
 | 
					
						
							|  |  |  | 	if ip == "" {
 | 
					
						
							| 
									
										
										
										
											2017-09-12 10:52:43 +01:00
										 |  |  | 		return nil, nil
 | 
					
						
							| 
									
										
										
										
											2017-08-09 05:18:46 -07:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:44:42 +01:00
										 |  |  | 	records := k.serviceRecordForIP(ip, state.Name())
 | 
					
						
							| 
									
										
										
										
											2017-09-12 10:52:43 +01:00
										 |  |  | 	return records, nil
 | 
					
						
							| 
									
										
										
										
											2017-08-09 05:18:46 -07:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-08-23 07:19:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // 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
 | 
					
						
							| 
									
										
										
										
											2017-09-29 15:58:50 -04:00
										 |  |  | 	for _, service := range k.APIConn.ServiceList() {
 | 
					
						
							| 
									
										
										
										
											2017-08-23 07:19:41 +01:00
										 |  |  | 		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
 | 
					
						
							| 
									
										
										
										
											2017-10-17 21:30:54 -04:00
										 |  |  | 	for _, ep := range k.APIConn.EpIndexReverse(ip) {
 | 
					
						
							| 
									
										
										
										
											2017-08-23 07:19:41 +01:00
										 |  |  | 		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), ep.ObjectMeta.Name, ep.ObjectMeta.Namespace, Svc, k.primaryZone()}, ".")
 | 
					
						
							|  |  |  | 					return []msg.Service{{Host: domain}}
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |