mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-30 17:53:21 -04:00 
			
		
		
		
	Allow cidr based reverse zone config (#500)
* add cidrs opt * remove state data from middleware object
This commit is contained in:
		
				
					committed by
					
						 John Belamaric
						John Belamaric
					
				
			
			
				
	
			
			
			
						parent
						
							3a04d2a306
						
					
				
				
					commit
					8beb1b2166
				
			| @@ -26,8 +26,13 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M | ||||
| 	// otherwise delegate to the next in the pipeline. | ||||
| 	zone := middleware.Zones(k.Zones).Matches(state.Name()) | ||||
| 	if zone == "" { | ||||
| 		// If this is a PTR request, and a the request is in a defined | ||||
| 		// pod/service cidr range, process the request in this middleware, | ||||
| 		// otherwise pass to next middleware. | ||||
| 		if state.Type() != "PTR" || !k.IsRequestInReverseRange(state) { | ||||
| 			return middleware.NextOrFailure(k.Name(), k.Next, ctx, w, r) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	var ( | ||||
| 		records, extra []dns.RR | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"net" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| @@ -41,6 +42,7 @@ type Kubernetes struct { | ||||
| 	LabelSelector *unversionedapi.LabelSelector | ||||
| 	Selector      *labels.Selector | ||||
| 	PodMode       string | ||||
| 	ReverseCidrs  []net.IPNet | ||||
| } | ||||
|  | ||||
| const ( | ||||
| @@ -128,6 +130,16 @@ func (k *Kubernetes) Reverse(state request.Request, exact bool, opt middleware.O | ||||
| 	return records, nil, nil | ||||
| } | ||||
|  | ||||
| func (k *Kubernetes) IsRequestInReverseRange(state request.Request) bool { | ||||
| 	ip := dnsutil.ExtractAddressFromReverse(state.Name()) | ||||
| 	for _, c := range k.ReverseCidrs { | ||||
| 		if c.Contains(net.ParseIP(ip)) { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|  | ||||
| // Lookup implements the ServiceBackend interface. | ||||
| func (k *Kubernetes) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { | ||||
| 	return k.Proxy.Lookup(state, name, typ) | ||||
|   | ||||
| @@ -3,6 +3,7 @@ package kubernetes | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"net" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| @@ -84,6 +85,20 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) { | ||||
|  | ||||
| 			for c.NextBlock() { | ||||
| 				switch c.Val() { | ||||
| 				case "cidrs": | ||||
| 					args := c.RemainingArgs() | ||||
| 					if len(args) > 0 { | ||||
| 						for _, cidrStr := range args { | ||||
| 							_, cidr, err := net.ParseCIDR(cidrStr) | ||||
| 							if err != nil { | ||||
| 								return nil, errors.New(c.Val() + " contains an invalid cidr: " + cidrStr) | ||||
| 							} | ||||
| 							k8s.ReverseCidrs = append(k8s.ReverseCidrs, *cidr) | ||||
|  | ||||
| 						} | ||||
| 						continue | ||||
| 					} | ||||
| 					return nil, c.ArgErr() | ||||
| 				case "pods": | ||||
| 					args := c.RemainingArgs() | ||||
| 					if len(args) == 1 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user