coredns/plugin/etcd/setup.go
Miek Gieben dbd1c047cb
Run gostaticheck (#3325)
* Run gostaticheck

Run gostaticcheck on the codebase and fix almost all flagged items.

Only keep

* coremain/run.go:192:2: var appVersion is unused (U1000)
* plugin/chaos/setup.go:54:3: the surrounding loop is unconditionally terminated (SA4004)
* plugin/etcd/setup.go:103:3: the surrounding loop is unconditionally terminated (SA4004)
* plugin/pkg/replacer/replacer.go:274:13: argument should be pointer-like to avoid allocations (SA6002)
* plugin/route53/setup.go:124:28: session.New is deprecated: Use NewSession functions to create sessions instead. NewSession has the same functionality as New except an error can be returned when the func is called instead of waiting to receive an error until a request is made.  (SA1019)
* test/grpc_test.go:25:69: grpc.WithTimeout is deprecated: use DialContext and context.WithTimeout instead.  Will be supported throughout 1.x.  (SA1019)

The first one isn't true, as this is set via ldflags. The rest is
minor. The deprecation should be fixed at some point; I'll file some
issues.

Signed-off-by: Miek Gieben <miek@miek.nl>

* Make sure to plug in the plugins

import the plugins, that file that did this was removed, put it in the
reload test as this requires an almost complete coredns server.

Signed-off-by: Miek Gieben <miek@miek.nl>
2019-10-01 07:41:29 +01:00

124 lines
2.7 KiB
Go

package etcd
import (
"crypto/tls"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
mwtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/caddyserver/caddy"
etcdcv3 "go.etcd.io/etcd/clientv3"
)
func init() { plugin.Register("etcd", setup) }
func setup(c *caddy.Controller) error {
e, err := etcdParse(c)
if err != nil {
return plugin.Error("etcd", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
e.Next = next
return e
})
return nil
}
func etcdParse(c *caddy.Controller) (*Etcd, error) {
etc := Etcd{PathPrefix: "skydns"}
var (
tlsConfig *tls.Config
err error
endpoints = []string{defaultEndpoint}
username string
password string
)
etc.Upstream = upstream.New()
for c.Next() {
etc.Zones = c.RemainingArgs()
if len(etc.Zones) == 0 {
etc.Zones = make([]string, len(c.ServerBlockKeys))
copy(etc.Zones, c.ServerBlockKeys)
}
for i, str := range etc.Zones {
etc.Zones[i] = plugin.Host(str).Normalize()
}
for c.NextBlock() {
switch c.Val() {
case "stubzones":
// ignored, remove later.
case "fallthrough":
etc.Fall.SetZonesFromArgs(c.RemainingArgs())
case "debug":
/* it is a noop now */
case "path":
if !c.NextArg() {
return &Etcd{}, c.ArgErr()
}
etc.PathPrefix = c.Val()
case "endpoint":
args := c.RemainingArgs()
if len(args) == 0 {
return &Etcd{}, c.ArgErr()
}
endpoints = args
case "upstream":
// remove soon
c.RemainingArgs()
case "tls": // cert key cacertfile
args := c.RemainingArgs()
tlsConfig, err = mwtls.NewTLSConfigFromArgs(args...)
if err != nil {
return &Etcd{}, err
}
case "credentials":
args := c.RemainingArgs()
if len(args) == 0 {
return &Etcd{}, c.ArgErr()
}
if len(args) != 2 {
return &Etcd{}, c.Errf("credentials requires 2 arguments, username and password")
}
username, password = args[0], args[1]
default:
if c.Val() != "}" {
return &Etcd{}, c.Errf("unknown property '%s'", c.Val())
}
}
}
client, err := newEtcdClient(endpoints, tlsConfig, username, password)
if err != nil {
return &Etcd{}, err
}
etc.Client = client
etc.endpoints = endpoints
return &etc, nil
}
return &Etcd{}, nil
}
func newEtcdClient(endpoints []string, cc *tls.Config, username, password string) (*etcdcv3.Client, error) {
etcdCfg := etcdcv3.Config{
Endpoints: endpoints,
TLS: cc,
}
if username != "" && password != "" {
etcdCfg.Username = username
etcdCfg.Password = password
}
cli, err := etcdcv3.New(etcdCfg)
if err != nil {
return nil, err
}
return cli, nil
}
const defaultEndpoint = "http://localhost:2379"