* 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.
This commit is contained in:
Miek Gieben
2016-09-23 09:14:12 +01:00
committed by GitHub
parent cacbb0e0b1
commit 090d1872e9
61 changed files with 381 additions and 577 deletions

View File

@@ -11,6 +11,7 @@ import (
"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 {
@@ -29,7 +30,7 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
srvPTR := &records[0]
m.Answer = append(m.Answer, srvPTR.NewPTR(state.QName(), ip))
m = dedup(m)
m = dnsutil.Dedup(m)
state.SizeAndDo(m)
m, _ = state.Scrub(m)
w.WriteMsg(m)
@@ -93,14 +94,14 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
m.Answer = append(m.Answer, records...)
m.Extra = append(m.Extra, extra...)
m = dedup(m)
m = dnsutil.Dedup(m)
state.SizeAndDo(m)
m, _ = state.Scrub(m)
w.WriteMsg(m)
return dns.RcodeSuccess, nil
}
// NoData write a nodata response to the client.
// 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)
@@ -109,11 +110,3 @@ func (k Kubernetes) Err(zone string, rcode int, state request.Request) (int, err
state.W.WriteMsg(m)
return rcode, nil
}
func dedup(m *dns.Msg) *dns.Msg {
// TODO(miek): expensive!
m.Answer = dns.Dedup(m.Answer, nil)
m.Ns = dns.Dedup(m.Ns, nil)
m.Extra = dns.Dedup(m.Extra, nil)
return m
}

View File

@@ -7,7 +7,7 @@ import (
"time"
"github.com/miekg/coredns/middleware"
"github.com/miekg/coredns/middleware/kubernetes/msg"
"github.com/miekg/coredns/middleware/etcd/msg"
"github.com/miekg/coredns/middleware/kubernetes/nametemplate"
"github.com/miekg/coredns/middleware/kubernetes/util"
"github.com/miekg/coredns/middleware/pkg/dnsutil"
@@ -22,6 +22,7 @@ import (
"k8s.io/kubernetes/pkg/labels"
)
// Kubernetes implements a middleware that connects to a Kubernetes cluster.
type Kubernetes struct {
Next middleware.Handler
Zones []string
@@ -35,6 +36,8 @@ type Kubernetes struct {
Selector *labels.Selector
}
// InitKubeCache initializes a new Kubernetes cache.
// TODO(miek): is this correct?
func (k *Kubernetes) InitKubeCache() error {
// For a custom api server or running outside a k8s cluster
// set URL in env.KUBERNETES_MASTER or set endpoint in Corefile
@@ -232,7 +235,7 @@ func (k *Kubernetes) getServiceRecordForIP(ip, name string) []msg.Service {
const (
priority = 10 // default priority when nothing is set
ttl = 300 // default ttl when nothing is set
minTtl = 60
minTTL = 60
hostmaster = "hostmaster"
k8sTimeout = 5 * time.Second
)

View File

@@ -7,7 +7,7 @@ import (
"time"
"github.com/miekg/coredns/middleware"
"github.com/miekg/coredns/middleware/kubernetes/msg"
"github.com/miekg/coredns/middleware/etcd/msg"
"github.com/miekg/coredns/middleware/pkg/dnsutil"
"github.com/miekg/coredns/request"
@@ -24,6 +24,7 @@ func (k Kubernetes) records(state request.Request, exact bool) ([]msg.Service, e
return services, nil
}
// A returns A records from kubernetes or an error.
func (k Kubernetes) A(zone string, state request.Request, previousRecords []dns.RR) (records []dns.RR, err error) {
services, err := k.records(state, false)
if err != nil {
@@ -83,6 +84,7 @@ func (k Kubernetes) A(zone string, state request.Request, previousRecords []dns.
return records, nil
}
// AAAA returns AAAA records from kubernetes or an error.
func (k Kubernetes) AAAA(zone string, state request.Request, previousRecords []dns.RR) (records []dns.RR, err error) {
services, err := k.records(state, false)
if err != nil {
@@ -144,7 +146,8 @@ func (k Kubernetes) AAAA(zone string, state request.Request, previousRecords []d
}
// SRV returns SRV records from kubernetes.
// If the Target is not a name but an IP address, a name is created on the fly.
// If the Target is not a name but an IP address, a name is created on the fly and the IP address is put in
// the additional section.
func (k Kubernetes) SRV(zone string, state request.Request) (records []dns.RR, extra []dns.RR, err error) {
services, err := k.records(state, false)
if err != nil {
@@ -226,21 +229,22 @@ func (k Kubernetes) SRV(zone string, state request.Request) (records []dns.RR, e
return records, extra, nil
}
// Returning MX records from kubernetes not implemented.
// MX returns MX records from kubernetes. Not implemented!
func (k Kubernetes) MX(zone string, state request.Request) (records []dns.RR, extra []dns.RR, err error) {
return nil, nil, err
}
// Returning CNAME records from kubernetes not implemented.
// CNAME returns CNAME records from kubernetes. Not implemented!
func (k Kubernetes) CNAME(zone string, state request.Request) (records []dns.RR, err error) {
return nil, err
}
// Returning TXT records from kubernetes not implemented.
// TXT returns TXT records from kubernetes. Not implemented!
func (k Kubernetes) TXT(zone string, state request.Request) (records []dns.RR, err error) {
return nil, err
}
// NS returns NS records from kubernetes.
func (k Kubernetes) NS(zone string, state request.Request) (records, extra []dns.RR, err error) {
// NS record for this zone live in a special place, ns.dns.<zone>. Fake our lookup.
// only a tad bit fishy...
@@ -273,7 +277,7 @@ func (k Kubernetes) NS(zone string, state request.Request) (records, extra []dns
return records, extra, nil
}
// SOA Record returns a SOA record.
// SOA Record returns a SOA record from kubernetes.
func (k Kubernetes) SOA(zone string, state request.Request) *dns.SOA {
header := dns.RR_Header{Name: zone, Rrtype: dns.TypeSOA, Ttl: 300, Class: dns.ClassINET}
return &dns.SOA{Hdr: header,
@@ -287,6 +291,7 @@ func (k Kubernetes) SOA(zone string, state request.Request) *dns.SOA {
}
}
// PTR Record returns PTR records from kubernetes.
func (k Kubernetes) PTR(zone string, state request.Request) ([]dns.RR, error) {
reverseIP := dnsutil.ExtractAddressFromReverse(state.Name())
if reverseIP == "" {

View File

@@ -1,171 +0,0 @@
package msg
import (
"net"
"strings"
"github.com/miekg/dns"
)
// This *is* the rdata from a SRV record, but with a twist.
// Host (Target in SRV) must be a domain name, but if it looks like an IP
// address (4/6), we will treat it like an IP address.
type Service struct {
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Priority int `json:"priority,omitempty"`
Weight int `json:"weight,omitempty"`
Text string `json:"text,omitempty"`
Mail bool `json:"mail,omitempty"` // Be an MX record. Priority becomes Preference.
Ttl uint32 `json:"ttl,omitempty"`
// When a SRV record with a "Host: IP-address" is added, we synthesize
// a srv.Target domain name. Normally we convert the full Key where
// the record lives to a DNS name and use this as the srv.Target. When
// TargetStrip > 0 we strip the left most TargetStrip labels from the
// DNS name.
TargetStrip int `json:"targetstrip,omitempty"`
// Group is used to group (or *not* to group) different services
// together. Services with an identical Group are returned in the same
// answer.
Group string `json:"group,omitempty"`
// Etcd key where we found this service and ignored from json un-/marshalling
Key string `json:"-"`
}
// NewSRV returns a new SRV record based on the Service.
func (s *Service) NewSRV(name string, weight uint16) *dns.SRV {
host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip)
return &dns.SRV{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeSRV, Class: dns.ClassINET, Ttl: s.Ttl},
Priority: uint16(s.Priority), Weight: weight, Port: uint16(s.Port), Target: dns.Fqdn(host)}
}
// NewMX returns a new MX record based on the Service.
func (s *Service) NewMX(name string) *dns.MX {
host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip)
return &dns.MX{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeMX, Class: dns.ClassINET, Ttl: s.Ttl},
Preference: uint16(s.Priority), Mx: host}
}
// NewA returns a new A record based on the Service.
func (s *Service) NewA(name string, ip net.IP) *dns.A {
return &dns.A{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: s.Ttl}, A: ip}
}
// NewAAAA returns a new AAAA record based on the Service.
func (s *Service) NewAAAA(name string, ip net.IP) *dns.AAAA {
return &dns.AAAA{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: s.Ttl}, AAAA: ip}
}
// NewCNAME returns a new CNAME record based on the Service.
func (s *Service) NewCNAME(name string, target string) *dns.CNAME {
return &dns.CNAME{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: s.Ttl}, Target: dns.Fqdn(target)}
}
// NewTXT returns a new TXT record based on the Service.
func (s *Service) NewTXT(name string) *dns.TXT {
return &dns.TXT{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeTXT, Class: dns.ClassINET, Ttl: s.Ttl}, Txt: split255(s.Text)}
}
// NewNS returns a new NS record based on the Service.
func (s *Service) NewNS(name string) *dns.NS {
host := targetStrip(dns.Fqdn(s.Host), s.TargetStrip)
return &dns.NS{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: s.Ttl}, Ns: host}
}
// NewPTR returns a new PTR record based on the Service.
func (s *Service) NewPTR(name string, target string) *dns.PTR {
return &dns.PTR{Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypePTR, Class: dns.ClassINET, Ttl: s.Ttl}, Ptr: dns.Fqdn(target)}
}
// Group checks the services in sx, it looks for a Group attribute on the shortest
// keys. If there are multiple shortest keys *and* the group attribute disagrees (and
// is not empty), we don't consider it a group.
// If a group is found, only services with *that* group (or no group) will be returned.
func Group(sx []Service) []Service {
if len(sx) == 0 {
return sx
}
// Shortest key with group attribute sets the group for this set.
group := sx[0].Group
slashes := strings.Count(sx[0].Key, "/")
length := make([]int, len(sx))
for i, s := range sx {
x := strings.Count(s.Key, "/")
length[i] = x
if x < slashes {
if s.Group == "" {
break
}
slashes = x
group = s.Group
}
}
if group == "" {
return sx
}
ret := []Service{} // with slice-tricks in sx we can prolly save this allocation (TODO)
for i, s := range sx {
if s.Group == "" {
ret = append(ret, s)
continue
}
// Disagreement on the same level
if length[i] == slashes && s.Group != group {
return sx
}
if s.Group == group {
ret = append(ret, s)
}
}
return ret
}
// Split255 splits a string into 255 byte chunks.
func split255(s string) []string {
if len(s) < 255 {
return []string{s}
}
sx := []string{}
p, i := 0, 255
for {
if i <= len(s) {
sx = append(sx, s[p:i])
} else {
sx = append(sx, s[p:])
break
}
p, i = p+255, i+255
}
return sx
}
// targetStrip strips "targetstrip" labels from the left side of the fully qualified name.
func targetStrip(name string, targetStrip int) string {
if targetStrip == 0 {
return name
}
offset, end := 0, false
for i := 0; i < targetStrip; i++ {
offset, end = dns.NextLabel(name, offset)
}
if end {
// We overshot the name, use the orignal one.
offset = 0
}
name = name[offset:]
return name
}

View File

@@ -1,125 +0,0 @@
package msg
import "testing"
func TestSplit255(t *testing.T) {
xs := split255("abc")
if len(xs) != 1 && xs[0] != "abc" {
t.Errorf("Failure to split abc")
}
s := ""
for i := 0; i < 255; i++ {
s += "a"
}
xs = split255(s)
if len(xs) != 1 && xs[0] != s {
t.Errorf("failure to split 255 char long string")
}
s += "b"
xs = split255(s)
if len(xs) != 2 || xs[1] != "b" {
t.Errorf("failure to split 256 char long string: %d", len(xs))
}
for i := 0; i < 255; i++ {
s += "a"
}
xs = split255(s)
if len(xs) != 3 || xs[2] != "a" {
t.Errorf("failure to split 510 char long string: %d", len(xs))
}
}
func TestGroup(t *testing.T) {
// Key are in the wrong order, but for this test it does not matter.
sx := Group(
[]Service{
{Host: "127.0.0.1", Group: "g1", Key: "b/sub/dom1/skydns/test"},
{Host: "127.0.0.2", Group: "g2", Key: "a/dom1/skydns/test"},
},
)
// Expecting to return the shortest key with a Group attribute.
if len(sx) != 1 {
t.Fatalf("failure to group zeroth set: %v", sx)
}
if sx[0].Key != "a/dom1/skydns/test" {
t.Fatalf("failure to group zeroth set: %v, wrong Key", sx)
}
// Groups disagree, so we will not do anything.
sx = Group(
[]Service{
{Host: "server1", Group: "g1", Key: "region1/skydns/test"},
{Host: "server2", Group: "g2", Key: "region1/skydns/test"},
},
)
if len(sx) != 2 {
t.Fatalf("failure to group first set: %v", sx)
}
// Group is g1, include only the top-level one.
sx = Group(
[]Service{
{Host: "server1", Group: "g1", Key: "a/dom/region1/skydns/test"},
{Host: "server2", Group: "g2", Key: "a/subdom/dom/region1/skydns/test"},
},
)
if len(sx) != 1 {
t.Fatalf("failure to group second set: %v", sx)
}
// Groupless services must be included.
sx = Group(
[]Service{
{Host: "server1", Group: "g1", Key: "a/dom/region1/skydns/test"},
{Host: "server2", Group: "g2", Key: "a/subdom/dom/region1/skydns/test"},
{Host: "server2", Group: "", Key: "b/subdom/dom/region1/skydns/test"},
},
)
if len(sx) != 2 {
t.Fatalf("failure to group third set: %v", sx)
}
// Empty group on the highest level: include that one also.
sx = Group(
[]Service{
{Host: "server1", Group: "g1", Key: "a/dom/region1/skydns/test"},
{Host: "server1", Group: "", Key: "b/dom/region1/skydns/test"},
{Host: "server2", Group: "g2", Key: "a/subdom/dom/region1/skydns/test"},
},
)
if len(sx) != 2 {
t.Fatalf("failure to group fourth set: %v", sx)
}
// Empty group on the highest level: include that one also, and the rest.
sx = Group(
[]Service{
{Host: "server1", Group: "g5", Key: "a/dom/region1/skydns/test"},
{Host: "server1", Group: "", Key: "b/dom/region1/skydns/test"},
{Host: "server2", Group: "g5", Key: "a/subdom/dom/region1/skydns/test"},
},
)
if len(sx) != 3 {
t.Fatalf("failure to group fith set: %v", sx)
}
// One group.
sx = Group(
[]Service{
{Host: "server1", Group: "g6", Key: "a/dom/region1/skydns/test"},
},
)
if len(sx) != 1 {
t.Fatalf("failure to group sixth set: %v", sx)
}
// No group, once service
sx = Group(
[]Service{
{Host: "server1", Key: "a/dom/region1/skydns/test"},
},
)
if len(sx) != 1 {
t.Fatalf("failure to group seventh set: %v", sx)
}
}

View File

@@ -60,6 +60,7 @@ var requiredSymbols = []string{
// Where the query string is longer than the template, need to define which
// symbol consumes the other segments. Most likely this would be the servicename.
// Also consider how to handle static strings in the format template.
type NameTemplate struct {
formatString string
splitFormat []string
@@ -105,11 +106,11 @@ func (t *NameTemplate) SetTemplate(s string) error {
// step down the stack to find the right element.
func (t *NameTemplate) GetZoneFromSegmentArray(segments []string) string {
if index, ok := t.Element["zone"]; !ok {
index, ok := t.Element["zone"]
if !ok {
return ""
} else {
return strings.Join(segments[index:len(segments)], ".")
}
return strings.Join(segments[index:len(segments)], ".")
}
func (t *NameTemplate) GetNamespaceFromSegmentArray(segments []string) string {
@@ -132,11 +133,11 @@ func (t *NameTemplate) GetTypeFromSegmentArray(segments []string) string {
}
func (t *NameTemplate) GetSymbolFromSegmentArray(symbol string, segments []string) string {
if index, ok := t.Element[symbol]; !ok {
index, ok := t.Element[symbol]
if !ok {
return ""
} else {
return segments[index]
}
return segments[index]
}
// GetRecordNameFromNameValues returns the string produced by applying the
@@ -164,6 +165,7 @@ func (t *NameTemplate) GetRecordNameFromNameValues(values NameValues) string {
}
func (t *NameTemplate) IsValid() bool {
// This is *only* used in a test, for the test this should be a private method.
result := true
// Ensure that all requiredSymbols are found in NameTemplate