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,7 +26,12 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M | |||||||
| 	// otherwise delegate to the next in the pipeline. | 	// otherwise delegate to the next in the pipeline. | ||||||
| 	zone := middleware.Zones(k.Zones).Matches(state.Name()) | 	zone := middleware.Zones(k.Zones).Matches(state.Name()) | ||||||
| 	if zone == "" { | 	if zone == "" { | ||||||
| 		return middleware.NextOrFailure(k.Name(), k.Next, ctx, w, r) | 		// 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 ( | 	var ( | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"log" | 	"log" | ||||||
|  | 	"net" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| @@ -41,6 +42,7 @@ type Kubernetes struct { | |||||||
| 	LabelSelector *unversionedapi.LabelSelector | 	LabelSelector *unversionedapi.LabelSelector | ||||||
| 	Selector      *labels.Selector | 	Selector      *labels.Selector | ||||||
| 	PodMode       string | 	PodMode       string | ||||||
|  | 	ReverseCidrs  []net.IPNet | ||||||
| } | } | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| @@ -128,6 +130,16 @@ func (k *Kubernetes) Reverse(state request.Request, exact bool, opt middleware.O | |||||||
| 	return records, nil, nil | 	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. | // Lookup implements the ServiceBackend interface. | ||||||
| func (k *Kubernetes) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { | func (k *Kubernetes) Lookup(state request.Request, name string, typ uint16) (*dns.Msg, error) { | ||||||
| 	return k.Proxy.Lookup(state, name, typ) | 	return k.Proxy.Lookup(state, name, typ) | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package kubernetes | |||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"net" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| @@ -84,6 +85,20 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) { | |||||||
|  |  | ||||||
| 			for c.NextBlock() { | 			for c.NextBlock() { | ||||||
| 				switch c.Val() { | 				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": | 				case "pods": | ||||||
| 					args := c.RemainingArgs() | 					args := c.RemainingArgs() | ||||||
| 					if len(args) == 1 { | 					if len(args) == 1 { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user