mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 10:43:17 -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.
77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package loadbalance
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// RoundRobinResponseWriter is a response writer that shuffles A and AAAA records.
|
|
type RoundRobinResponseWriter struct {
|
|
dns.ResponseWriter
|
|
}
|
|
|
|
// WriteMsg implements the dns.ResponseWriter interface.
|
|
func (r *RoundRobinResponseWriter) WriteMsg(res *dns.Msg) error {
|
|
if res.Rcode != dns.RcodeSuccess {
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
}
|
|
|
|
res.Answer = roundRobin(res.Answer)
|
|
res.Ns = roundRobin(res.Ns)
|
|
res.Extra = roundRobin(res.Extra)
|
|
|
|
return r.ResponseWriter.WriteMsg(res)
|
|
}
|
|
|
|
func roundRobin(in []dns.RR) []dns.RR {
|
|
cname := []dns.RR{}
|
|
address := []dns.RR{}
|
|
rest := []dns.RR{}
|
|
for _, r := range in {
|
|
switch r.Header().Rrtype {
|
|
case dns.TypeCNAME:
|
|
cname = append(cname, r)
|
|
case dns.TypeA, dns.TypeAAAA:
|
|
address = append(address, r)
|
|
default:
|
|
rest = append(rest, r)
|
|
}
|
|
}
|
|
|
|
switch l := len(address); l {
|
|
case 0, 1:
|
|
break
|
|
case 2:
|
|
if dns.Id()%2 == 0 {
|
|
address[0], address[1] = address[1], address[0]
|
|
}
|
|
default:
|
|
for j := 0; j < l*(int(dns.Id())%4+1); j++ {
|
|
q := int(dns.Id()) % l
|
|
p := int(dns.Id()) % l
|
|
if q == p {
|
|
p = (p + 1) % l
|
|
}
|
|
address[q], address[p] = address[p], address[q]
|
|
}
|
|
}
|
|
out := append(cname, rest...)
|
|
out = append(out, address...)
|
|
return out
|
|
}
|
|
|
|
// Write implements the dns.ResponseWriter interface.
|
|
func (r *RoundRobinResponseWriter) Write(buf []byte) (int, error) {
|
|
// Should we pack and unpack here to fiddle with the packet... Not likely.
|
|
log.Printf("[WARNING] RoundRobin called with Write: no shuffling records")
|
|
n, err := r.ResponseWriter.Write(buf)
|
|
return n, err
|
|
}
|
|
|
|
// Hijack implements the dns.ResponseWriter interface.
|
|
func (r *RoundRobinResponseWriter) Hijack() {
|
|
r.ResponseWriter.Hijack()
|
|
return
|
|
}
|