mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	mw/kubernetes: remove kService and kPod
Remove the intermediate step of gathering everyhing in a kPod and kService and extracting the msg.Service from there. Now findPods and findServices return []msg.Service. This cuts down on the code and also removed the double looping of finding the data we need, so it should be faster.
This commit is contained in:
		| @@ -1,6 +1,8 @@ | ||||
| package kubernetes | ||||
|  | ||||
| import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/coredns/coredns/middleware" | ||||
| 	"github.com/coredns/coredns/middleware/etcd/msg" | ||||
| 	"github.com/coredns/coredns/middleware/pkg/dnsutil" | ||||
| @@ -18,3 +20,36 @@ func (k *Kubernetes) Reverse(state request.Request, exact bool, opt middleware.O | ||||
| 	records := k.serviceRecordForIP(ip, state.Name()) | ||||
| 	return records, nil, 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 | ||||
| 	svcList := k.APIConn.ServiceList() | ||||
|  | ||||
| 	for _, service := range svcList { | ||||
| 		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 | ||||
| 	epList := k.APIConn.EndpointsList() | ||||
| 	for _, ep := range epList.Items { | ||||
| 		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 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user