cleanup: go vet and golint run (#736)

* cleanup: go vet and golint run

Various cleanups trickered by go vet and golint.

* Fix tests and lowercase all errors

Lowercase all errors, some tests in kubernetes use errors from
kubernetes which do start with a capital letter.
This commit is contained in:
Miek Gieben 2017-06-14 09:37:10 -07:00 committed by GitHub
parent 5c10eba31c
commit e49ca86ce4
21 changed files with 64 additions and 62 deletions

View file

@ -16,8 +16,8 @@ import (
"github.com/coredns/coredns/pb"
)
// servergRPC represents an instance of a DNS-over-gRPC server.
type servergRPC struct {
// ServergRPC represents an instance of a DNS-over-gRPC server.
type ServergRPC struct {
*Server
grpcServer *grpc.Server
@ -25,18 +25,18 @@ type servergRPC struct {
}
// NewServergRPC returns a new CoreDNS GRPC server and compiles all middleware in to it.
func NewServergRPC(addr string, group []*Config) (*servergRPC, error) {
func NewServergRPC(addr string, group []*Config) (*ServergRPC, error) {
s, err := NewServer(addr, group)
if err != nil {
return nil, err
}
gs := &servergRPC{Server: s}
gs := &ServergRPC{Server: s}
return gs, nil
}
// Serve implements caddy.TCPServer interface.
func (s *servergRPC) Serve(l net.Listener) error {
func (s *ServergRPC) Serve(l net.Listener) error {
s.m.Lock()
s.listenAddr = l.Addr()
s.m.Unlock()
@ -57,10 +57,10 @@ func (s *servergRPC) Serve(l net.Listener) error {
}
// ServePacket implements caddy.UDPServer interface.
func (s *servergRPC) ServePacket(p net.PacketConn) error { return nil }
func (s *ServergRPC) ServePacket(p net.PacketConn) error { return nil }
// Listen implements caddy.TCPServer interface.
func (s *servergRPC) Listen() (net.Listener, error) {
func (s *ServergRPC) Listen() (net.Listener, error) {
// The *tls* middleware must make sure that multiple conflicting
// TLS configuration return an error: it can only be specified once.
@ -88,11 +88,11 @@ func (s *servergRPC) Listen() (net.Listener, error) {
}
// ListenPacket implements caddy.UDPServer interface.
func (s *servergRPC) ListenPacket() (net.PacketConn, error) { return nil, nil }
func (s *ServergRPC) ListenPacket() (net.PacketConn, error) { return nil, nil }
// OnStartupComplete lists the sites served by this server
// and any relevant information, assuming Quiet is false.
func (s *servergRPC) OnStartupComplete() {
func (s *ServergRPC) OnStartupComplete() {
if Quiet {
return
}
@ -102,7 +102,7 @@ func (s *servergRPC) OnStartupComplete() {
}
}
func (s *servergRPC) Stop() (err error) {
func (s *ServergRPC) Stop() (err error) {
s.m.Lock()
defer s.m.Unlock()
if s.grpcServer != nil {
@ -114,7 +114,7 @@ func (s *servergRPC) Stop() (err error) {
// Query is the main entry-point into the gRPC server. From here we call ServeDNS like
// any normal server. We use a custom responseWriter to pick up the bytes we need to write
// back to the client as a protobuf.
func (s *servergRPC) Query(ctx context.Context, in *pb.DnsPacket) (*pb.DnsPacket, error) {
func (s *ServergRPC) Query(ctx context.Context, in *pb.DnsPacket) (*pb.DnsPacket, error) {
msg := new(dns.Msg)
err := msg.Unpack(in.Msg)
if err != nil {
@ -144,7 +144,7 @@ func (s *servergRPC) Query(ctx context.Context, in *pb.DnsPacket) (*pb.DnsPacket
return &pb.DnsPacket{Msg: packed}, nil
}
func (s *servergRPC) Shutdown() error {
func (s *ServergRPC) Shutdown() error {
if s.grpcServer != nil {
s.grpcServer.Stop()
}

View file

@ -7,6 +7,7 @@ import (
"time"
)
// Freq tracks the frequence of things.
type Freq struct {
// Last time we saw a query for this element.
last time.Time
@ -21,7 +22,7 @@ func New(t time.Time) *Freq {
return &Freq{last: t, hits: 0}
}
// Updates updates the number of hits. Last time seen will be set to now.
// Update updates the number of hits. Last time seen will be set to now.
// If the last time we've seen this entity is within now - d, we increment hits, otherwise
// we reset hits to 1. It returns the number of hits.
func (f *Freq) Update(d time.Duration, now time.Time) int {

View file

@ -43,7 +43,7 @@ func TestDebug(t *testing.T) {
}
}
if cfg.Debug != test.expectedDebug {
t.Fatalf("Test %d: Expected debug to be: %t, but got: %t, input: %s", i, test.expectedDebug, test.input)
t.Fatalf("Test %d: Expected debug to be: %t, but got: %t, input: %s", i, test.expectedDebug, cfg.Debug, test.input)
}
}
}

View file

@ -71,7 +71,7 @@ func notifyAddr(c *dns.Client, m *dns.Msg, s string) (err error) {
}
}
if err != nil {
return fmt.Errorf("Notify for zone %q was not accepted by %q: %q", m.Question[0].Name, s, err)
return fmt.Errorf("notify for zone %q was not accepted by %q: %q", m.Question[0].Name, s, err)
}
return fmt.Errorf("Notify for zone %q was not accepted by %q: rcode was %q", m.Question[0].Name, s, rcode.ToString(code))
return fmt.Errorf("notify for zone %q was not accepted by %q: rcode was %q", m.Question[0].Name, s, rcode.ToString(code))
}

View file

@ -60,7 +60,7 @@ func (z *Zone) Reload() error {
return nil
}
// SOASerialIfDefind returns the SOA's serial if the zone has a SOA record in the Apex, or
// SOASerialIfDefined returns the SOA's serial if the zone has a SOA record in the Apex, or
// -1 otherwise.
func (z *Zone) SOASerialIfDefined() int64 {
z.reloadMu.Lock()

View file

@ -7,6 +7,7 @@ import (
"github.com/coredns/coredns/middleware/etcd/msg"
)
// Federation holds TODO(...).
type Federation struct {
name string
zone string
@ -26,8 +27,8 @@ const (
//
// But importing above breaks coredns with flag collision of 'log_dir'
LabelAvailabilityZone = "failure-domain.beta.kubernetes.io/zone"
LabelRegion = "failure-domain.beta.kubernetes.io/region"
labelAvailabilityZone = "failure-domain.beta.kubernetes.io/zone"
labelRegion = "failure-domain.beta.kubernetes.io/region"
)
// stripFederation removes the federation segment from the segment list, if it
@ -66,12 +67,12 @@ func (k *Kubernetes) federationCNAMERecord(r recordRequest) msg.Service {
if r.endpoint == "" {
return msg.Service{
Key: strings.Join([]string{msg.Path(r.zone, "coredns"), r.typeName, r.federation, r.namespace, r.service}, "/"),
Host: strings.Join([]string{r.service, r.namespace, r.federation, r.typeName, node.Labels[LabelAvailabilityZone], node.Labels[LabelRegion], f.zone}, "."),
Host: strings.Join([]string{r.service, r.namespace, r.federation, r.typeName, node.Labels[labelAvailabilityZone], node.Labels[labelRegion], f.zone}, "."),
}
}
return msg.Service{
Key: strings.Join([]string{msg.Path(r.zone, "coredns"), r.typeName, r.federation, r.namespace, r.service, r.endpoint}, "/"),
Host: strings.Join([]string{r.endpoint, r.service, r.namespace, r.federation, r.typeName, node.Labels[LabelAvailabilityZone], node.Labels[LabelRegion], f.zone}, "."),
Host: strings.Join([]string{r.endpoint, r.service, r.namespace, r.federation, r.typeName, node.Labels[labelAvailabilityZone], node.Labels[labelRegion], f.zone}, "."),
}
}

View file

@ -66,8 +66,8 @@ func (apiConnFedTest) GetNodeByName(name string) (api.Node, error) {
ObjectMeta: api.ObjectMeta{
Name: "test.node.foo.bar",
Labels: map[string]string{
LabelRegion: "fd-r",
LabelAvailabilityZone: "fd-az",
labelRegion: "fd-r",
labelAvailabilityZone: "fd-az",
},
},
}, nil

View file

@ -230,7 +230,7 @@ func (k *Kubernetes) InitKubeCache() (err error) {
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("Failed to create kubernetes notification controller: %v", err)
return fmt.Errorf("failed to create kubernetes notification controller: %v", err)
}
if k.LabelSelector != nil {
@ -238,7 +238,7 @@ func (k *Kubernetes) InitKubeCache() (err error) {
selector, err = unversionedapi.LabelSelectorAsSelector(k.LabelSelector)
k.Selector = &selector
if err != nil {
return fmt.Errorf("Unable to create Selector for LabelSelector '%s'.Error was: %s", k.LabelSelector, err)
return fmt.Errorf("unable to create Selector for LabelSelector '%s'.Error was: %s", k.LabelSelector, err)
}
}

View file

@ -431,8 +431,8 @@ func (APIConnServiceTest) GetNodeByName(name string) (api.Node, error) {
ObjectMeta: api.ObjectMeta{
Name: "test.node.foo.bar",
Labels: map[string]string{
LabelRegion: "fd-r",
LabelAvailabilityZone: "fd-az",
labelRegion: "fd-r",
labelAvailabilityZone: "fd-az",
},
},
}, nil

View file

@ -96,7 +96,7 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
for _, cidrStr := range args {
_, cidr, err := net.ParseCIDR(cidrStr)
if err != nil {
return nil, errors.New("Invalid cidr: " + cidrStr)
return nil, fmt.Errorf("invalid cidr: %s", cidrStr)
}
k8s.ReverseCidrs = append(k8s.ReverseCidrs, *cidr)
@ -111,7 +111,7 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
case PodModeDisabled, PodModeInsecure, PodModeVerified:
k8s.PodMode = args[0]
default:
return nil, errors.New("Value for pods must be one of: disabled, verified, insecure")
return nil, fmt.Errorf("wrong value for pods: %s, must be one of: disabled, verified, insecure", args[0])
}
continue
}
@ -142,7 +142,7 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
if len(args) > 0 {
rp, err := time.ParseDuration(args[0])
if err != nil {
return nil, fmt.Errorf("Unable to parse resync duration value. Value provided was '%v'. Example valid values: '15s', '5m', '1h'. Error was: %v", args[0], err)
return nil, fmt.Errorf("unable to parse resync duration value: '%v': %v", args[0], err)
}
k8s.ResyncPeriod = rp
continue
@ -154,7 +154,7 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
labelSelectorString := strings.Join(args, " ")
ls, err := unversionedapi.ParseToLabelSelector(labelSelectorString)
if err != nil {
return nil, fmt.Errorf("Unable to parse label selector. Value provided was '%v'. Error was: %v", labelSelectorString, err)
return nil, fmt.Errorf("unable to parse label selector value: '%v': %v", labelSelectorString, err)
}
k8s.LabelSelector = ls
continue
@ -185,17 +185,15 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
zone: args[1],
})
continue
} else {
return nil, fmt.Errorf("Incorrect number of arguments for federation. Got %v, expect 2.", len(args))
}
return nil, c.ArgErr()
return nil, fmt.Errorf("incorrect number of arguments for federation, got %v, expected 2", len(args))
}
}
return k8s, nil
}
}
return nil, errors.New("Kubernetes setup called without keyword 'kubernetes' in Corefile")
return nil, errors.New("kubernetes setup called without keyword 'kubernetes' in Corefile")
}
const (

View file

@ -223,7 +223,7 @@ func TestKubernetesParse(t *testing.T) {
"no kubernetes keyword",
"",
true,
"Kubernetes setup called without keyword 'kubernetes' in Corefile",
"kubernetes setup called without keyword 'kubernetes' in Corefile",
-1,
-1,
defaultResyncPeriod,
@ -255,7 +255,7 @@ func TestKubernetesParse(t *testing.T) {
endpoint
}`,
true,
"Wrong argument count or unexpected line ending after 'endpoint'",
"rong argument count or unexpected line ending",
-1,
-1,
defaultResyncPeriod,
@ -272,7 +272,7 @@ func TestKubernetesParse(t *testing.T) {
namespaces
}`,
true,
"Parse error: Wrong argument count or unexpected line ending after 'namespaces'",
"rong argument count or unexpected line ending",
-1,
-1,
defaultResyncPeriod,
@ -289,7 +289,7 @@ func TestKubernetesParse(t *testing.T) {
resyncperiod
}`,
true,
"Wrong argument count or unexpected line ending after 'resyncperiod'",
"rong argument count or unexpected line ending",
-1,
0,
0 * time.Minute,
@ -306,7 +306,7 @@ func TestKubernetesParse(t *testing.T) {
resyncperiod 15
}`,
true,
"Unable to parse resync duration value. Value provided was ",
"unable to parse resync duration value",
-1,
0,
0 * time.Second,
@ -323,7 +323,7 @@ func TestKubernetesParse(t *testing.T) {
resyncperiod abc
}`,
true,
"Unable to parse resync duration value. Value provided was ",
"unable to parse resync duration value",
-1,
0,
0 * time.Second,
@ -340,7 +340,7 @@ func TestKubernetesParse(t *testing.T) {
labels
}`,
true,
"Wrong argument count or unexpected line ending after 'labels'",
"rong argument count or unexpected line ending",
-1,
0,
0 * time.Second,
@ -357,7 +357,7 @@ func TestKubernetesParse(t *testing.T) {
labels environment in (production, qa
}`,
true,
"Unable to parse label selector. Value provided was",
"unable to parse label selector",
-1,
0,
0 * time.Second,
@ -429,7 +429,7 @@ func TestKubernetesParse(t *testing.T) {
pods giant_seed
}`,
true,
"Value for pods must be one of: disabled, verified, insecure",
"rong value for pods",
-1,
0,
defaultResyncPeriod,
@ -460,12 +460,12 @@ func TestKubernetesParse(t *testing.T) {
},
// cidrs ok
{
"Invalid cidr: hard",
"invalid cidr: hard",
`kubernetes coredns.local {
cidrs hard dry
}`,
true,
"Invalid cidr: hard",
"invalid cidr: hard",
-1,
0,
defaultResyncPeriod,
@ -483,7 +483,7 @@ func TestKubernetesParse(t *testing.T) {
fallthrough junk
}`,
true,
"Wrong argument count",
"rong argument count",
-1,
0,
defaultResyncPeriod,
@ -559,7 +559,7 @@ func TestKubernetesParse(t *testing.T) {
federation starship
}`,
true,
`Incorrect number of arguments for federation. Got 1, expect 2.`,
`incorrect number of arguments for federation`,
-1,
0,
defaultResyncPeriod,

View file

@ -108,7 +108,7 @@ func Typify(m *dns.Msg, t time.Time) (Type, *dns.OPT) {
return NameError, opt
}
if ns > 0 && ns > 0 && m.Rcode == dns.RcodeSuccess {
if ns > 0 && m.Rcode == dns.RcodeSuccess {
return Delegation, opt
}

View file

@ -50,7 +50,7 @@ func NewTLSConfigFromArgs(args ...string) (*tls.Config, error) {
// Client cert, use specified CA
c, err = NewTLSConfig(args[0], args[1], args[2])
default:
err = fmt.Errorf("Maximum of three arguments allowed for TLS config, found %d", len(args))
err = fmt.Errorf("maximum of three arguments allowed for TLS config, found %d", len(args))
}
if err != nil {
return nil, err
@ -64,7 +64,7 @@ func NewTLSConfigFromArgs(args ...string) (*tls.Config, error) {
func NewTLSConfig(certPath, keyPath, caPath string) (*tls.Config, error) {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, fmt.Errorf("Could not load TLS cert: %s", err)
return nil, fmt.Errorf("could not load TLS cert: %s", err)
}
roots, err := loadRoots(caPath)
@ -94,11 +94,11 @@ func loadRoots(caPath string) (*x509.CertPool, error) {
roots := x509.NewCertPool()
pem, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, fmt.Errorf("Error reading %s: %s", caPath, err)
return nil, fmt.Errorf("error reading %s: %s", caPath, err)
}
ok := roots.AppendCertsFromPEM(pem)
if !ok {
return nil, fmt.Errorf("Could not read root certs: %s", err)
return nil, fmt.Errorf("could not read root certs: %s", err)
}
return roots, nil
}

View file

@ -15,6 +15,7 @@ type dnsEx struct {
Options
}
// Options define the options understood by dns.Exchange.
type Options struct {
ForceTCP bool // If true use TCP for upstream no matter what
}

View file

@ -17,7 +17,7 @@ func NewLookup(hosts []string) Proxy {
return NewLookupWithOption(hosts, Options{})
}
// NewLookupWithForcedProto process creates a simple round robin forward with potentially forced proto for upstream.
// NewLookupWithOption process creates a simple round robin forward with potentially forced proto for upstream.
func NewLookupWithOption(hosts []string, opts Options) Proxy {
p := Proxy{Next: nil}

View file

@ -23,7 +23,7 @@ const (
)
func operatorError(operator string) error {
return fmt.Errorf("Invalid operator %v", operator)
return fmt.Errorf("invalid operator %v", operator)
}
func newReplacer(r *dns.Msg) replacer.Replacer {

View file

@ -94,7 +94,7 @@ func (rule *edns0LocalRule) Rewrite(r *dns.Msg) Result {
// newEdns0Rule creates an EDNS0 rule of the appropriate type based on the args
func newEdns0Rule(args ...string) (Rule, error) {
if len(args) < 2 {
return nil, fmt.Errorf("Too few arguments for an EDNS0 rule")
return nil, fmt.Errorf("too few arguments for an EDNS0 rule")
}
ruleType := strings.ToLower(args[0])

View file

@ -64,7 +64,7 @@ type Rule interface {
func newRule(args ...string) (Rule, error) {
if len(args) == 0 {
return nil, fmt.Errorf("No rule type specified for rewrite")
return nil, fmt.Errorf("no rule type specified for rewrite")
}
ruleType := strings.ToLower(args[0])

View file

@ -7,11 +7,12 @@ import (
"sync/atomic"
"github.com/coredns/coredns/middleware"
// Plugin the trace package.
_ "github.com/coredns/coredns/middleware/pkg/trace"
"github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
"golang.org/x/net/context"
)
@ -40,7 +41,7 @@ func (t *trace) OnStartup() error {
case "zipkin":
err = t.setupZipkin()
default:
err = fmt.Errorf("Unknown endpoint type: %s", t.EndpointType)
err = fmt.Errorf("unknown endpoint type: %s", t.EndpointType)
}
})
return err

View file

@ -52,10 +52,10 @@ func TestGrpc(t *testing.T) {
}
if d.Rcode != dns.RcodeSuccess {
t.Errorf("Expected success but got %s", d.Rcode)
t.Errorf("Expected success but got %d", d.Rcode)
}
if len(d.Extra) != 2 {
t.Errorf("Expected 2 RRs in additional section, but got %s", len(d.Extra))
t.Errorf("Expected 2 RRs in additional section, but got %d", len(d.Extra))
}
}

View file

@ -45,6 +45,6 @@ func send(t *testing.T, server string) {
t.Fatalf("Expected successful reply, got %s", dns.RcodeToString[r.Rcode])
}
if len(r.Extra) != 2 {
t.Fatalf("Expected 2 RRs in additional, got %s", len(r.Extra))
t.Fatalf("Expected 2 RRs in additional, got %d", len(r.Extra))
}
}