mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	* Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * Fix linter errors * More linting fixes * More docs and making members private that dont need to be public * More lint fixes This leaves: ~~~ middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..." middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported ~~~ I plan on reworking the proxy anyway, so I'll leave that be.
		
			
				
	
	
		
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package kubernetes
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 
 | |
| 	"github.com/miekg/coredns/middleware"
 | |
| 	"github.com/miekg/coredns/middleware/pkg/dnsutil"
 | |
| 	"github.com/miekg/coredns/request"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| 	"golang.org/x/net/context"
 | |
| )
 | |
| 
 | |
| // ServeDNS implements the middleware.Handler interface.
 | |
| func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | |
| 	state := request.Request{W: w, Req: r}
 | |
| 	if state.QClass() != dns.ClassINET {
 | |
| 		return dns.RcodeServerFailure, fmt.Errorf("can only deal with ClassINET")
 | |
| 	}
 | |
| 
 | |
| 	m := new(dns.Msg)
 | |
| 	m.SetReply(r)
 | |
| 	m.Authoritative, m.RecursionAvailable, m.Compress = true, true, true
 | |
| 
 | |
| 	// TODO: find an alternative to this block
 | |
| 	ip := dnsutil.ExtractAddressFromReverse(state.Name())
 | |
| 	if ip != "" {
 | |
| 		records := k.getServiceRecordForIP(ip, state.Name())
 | |
| 		if len(records) > 0 {
 | |
| 			srvPTR := &records[0]
 | |
| 			m.Answer = append(m.Answer, srvPTR.NewPTR(state.QName(), ip))
 | |
| 
 | |
| 			m = dnsutil.Dedup(m)
 | |
| 			state.SizeAndDo(m)
 | |
| 			m, _ = state.Scrub(m)
 | |
| 			w.WriteMsg(m)
 | |
| 			return dns.RcodeSuccess, nil
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	// Check that query matches one of the zones served by this middleware,
 | |
| 	// otherwise delegate to the next in the pipeline.
 | |
| 	zone := middleware.Zones(k.Zones).Matches(state.Name())
 | |
| 	if zone == "" {
 | |
| 		if k.Next == nil {
 | |
| 			return dns.RcodeServerFailure, nil
 | |
| 		}
 | |
| 		return k.Next.ServeDNS(ctx, w, r)
 | |
| 	}
 | |
| 
 | |
| 	var (
 | |
| 		records, extra []dns.RR
 | |
| 		err            error
 | |
| 	)
 | |
| 	switch state.Type() {
 | |
| 	case "A":
 | |
| 		records, err = k.A(zone, state, nil)
 | |
| 	case "AAAA":
 | |
| 		records, err = k.AAAA(zone, state, nil)
 | |
| 	case "TXT":
 | |
| 		records, err = k.TXT(zone, state)
 | |
| 		// TODO: change lookup to return appropriate error. Then add code below
 | |
| 		// this switch to check for the error and return not implemented.
 | |
| 		//return dns.RcodeNotImplemented, nil
 | |
| 	case "CNAME":
 | |
| 		records, err = k.CNAME(zone, state)
 | |
| 	case "MX":
 | |
| 		records, extra, err = k.MX(zone, state)
 | |
| 	case "SRV":
 | |
| 		records, extra, err = k.SRV(zone, state)
 | |
| 	case "SOA":
 | |
| 		records = []dns.RR{k.SOA(zone, state)}
 | |
| 	case "NS":
 | |
| 		if state.Name() == zone {
 | |
| 			records, extra, err = k.NS(zone, state)
 | |
| 			break
 | |
| 		}
 | |
| 		fallthrough
 | |
| 	default:
 | |
| 		// Do a fake A lookup, so we can distinguish between NODATA and NXDOMAIN
 | |
| 		_, err = k.A(zone, state, nil)
 | |
| 	}
 | |
| 	if isKubernetesNameError(err) {
 | |
| 		return k.Err(zone, dns.RcodeNameError, state)
 | |
| 	}
 | |
| 	if err != nil {
 | |
| 		return dns.RcodeServerFailure, err
 | |
| 	}
 | |
| 
 | |
| 	if len(records) == 0 {
 | |
| 		return k.Err(zone, dns.RcodeSuccess, state)
 | |
| 	}
 | |
| 
 | |
| 	m.Answer = append(m.Answer, records...)
 | |
| 	m.Extra = append(m.Extra, extra...)
 | |
| 
 | |
| 	m = dnsutil.Dedup(m)
 | |
| 	state.SizeAndDo(m)
 | |
| 	m, _ = state.Scrub(m)
 | |
| 	w.WriteMsg(m)
 | |
| 	return dns.RcodeSuccess, nil
 | |
| }
 | |
| 
 | |
| // Err writes an error response back to the client.
 | |
| func (k Kubernetes) Err(zone string, rcode int, state request.Request) (int, error) {
 | |
| 	m := new(dns.Msg)
 | |
| 	m.SetRcode(state.Req, rcode)
 | |
| 	m.Ns = []dns.RR{k.SOA(zone, state)}
 | |
| 	state.SizeAndDo(m)
 | |
| 	state.W.WriteMsg(m)
 | |
| 	return rcode, nil
 | |
| }
 |