Files
coredns/core/setup/etcd.go

198 lines
4.7 KiB
Go
Raw Normal View History

2016-03-20 17:54:21 +00:00
package setup
import (
2016-03-20 18:17:07 +00:00
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net"
"net/http"
"time"
2016-03-20 17:54:21 +00:00
"github.com/miekg/coredns/middleware"
2016-03-20 21:36:55 +00:00
"github.com/miekg/coredns/middleware/etcd"
2016-03-22 22:44:50 +00:00
"github.com/miekg/coredns/middleware/proxy"
"github.com/miekg/coredns/middleware/singleflight"
2016-03-20 21:36:55 +00:00
etcdc "github.com/coreos/etcd/client"
2016-03-22 22:44:50 +00:00
"golang.org/x/net/context"
2016-03-20 17:54:21 +00:00
)
const defaultEndpoint = "http://localhost:2379"
2016-03-20 18:17:07 +00:00
// Etcd sets up the etcd middleware.
func Etcd(c *Controller) (middleware.Middleware, error) {
etcd, stubzones, err := etcdParse(c)
2016-03-20 17:54:21 +00:00
if err != nil {
return nil, err
}
if stubzones {
c.Startup = append(c.Startup, func() error {
etcd.UpdateStubZones()
return nil
})
}
2016-03-20 18:17:07 +00:00
2016-03-20 17:54:21 +00:00
return func(next middleware.Handler) middleware.Handler {
2016-03-22 22:44:50 +00:00
etcd.Next = next
return etcd
2016-03-20 17:54:21 +00:00
}, nil
}
func etcdParse(c *Controller) (etcd.Etcd, bool, error) {
stub := make(map[string]proxy.Proxy)
2016-03-22 22:44:50 +00:00
etc := etcd.Etcd{
Proxy: proxy.New([]string{"8.8.8.8:53", "8.8.4.4:53"}),
2016-03-22 22:44:50 +00:00
PathPrefix: "skydns",
Ctx: context.Background(),
Inflight: &singleflight.Group{},
Stubmap: &stub,
2016-03-22 22:44:50 +00:00
}
var (
client etcdc.KeysAPI
tlsCertFile = ""
tlsKeyFile = ""
tlsCAcertFile = ""
endpoints = []string{defaultEndpoint}
stubzones = false
)
2016-03-20 17:54:21 +00:00
for c.Next() {
2016-03-20 18:17:07 +00:00
if c.Val() == "etcd" {
2016-03-22 22:44:50 +00:00
etc.Client = client
etc.Zones = c.RemainingArgs()
if len(etc.Zones) == 0 {
etc.Zones = c.ServerBlockHosts
2016-03-20 17:54:21 +00:00
}
2016-03-22 22:44:50 +00:00
middleware.Zones(etc.Zones).FullyQualify()
if c.NextBlock() {
// TODO(miek): 2 switches?
switch c.Val() {
case "stubzones":
stubzones = true
case "debug":
etc.Debug = true
case "path":
if !c.NextArg() {
return etcd.Etcd{}, false, c.ArgErr()
}
etc.PathPrefix = c.Val()
case "endpoint":
args := c.RemainingArgs()
if len(args) == 0 {
return etcd.Etcd{}, false, c.ArgErr()
}
endpoints = args
case "upstream":
args := c.RemainingArgs()
if len(args) == 0 {
return etcd.Etcd{}, false, c.ArgErr()
}
for i := 0; i < len(args); i++ {
h, p, e := net.SplitHostPort(args[i])
if e != nil && p == "" {
args[i] = h + ":53"
}
}
endpoints = args
etc.Proxy = proxy.New(args)
case "tls": // cert key cacertfile
args := c.RemainingArgs()
if len(args) != 3 {
return etcd.Etcd{}, false, c.ArgErr()
}
tlsCertFile, tlsKeyFile, tlsCAcertFile = args[0], args[1], args[2]
}
for c.Next() {
switch c.Val() {
case "stubzones":
stubzones = true
case "debug":
etc.Debug = true
case "path":
if !c.NextArg() {
return etcd.Etcd{}, false, c.ArgErr()
}
etc.PathPrefix = c.Val()
case "endpoint":
args := c.RemainingArgs()
if len(args) == 0 {
return etcd.Etcd{}, false, c.ArgErr()
}
endpoints = args
case "upstream":
args := c.RemainingArgs()
if len(args) == 0 {
return etcd.Etcd{}, false, c.ArgErr()
}
for i := 0; i < len(args); i++ {
h, p, e := net.SplitHostPort(args[i])
if e != nil && p == "" {
args[i] = h + ":53"
}
}
etc.Proxy = proxy.New(args)
case "tls": // cert key cacertfile
args := c.RemainingArgs()
if len(args) != 3 {
return etcd.Etcd{}, false, c.ArgErr()
}
tlsCertFile, tlsKeyFile, tlsCAcertFile = args[0], args[1], args[2]
}
}
}
client, err := newEtcdClient(endpoints, tlsCertFile, tlsKeyFile, tlsCAcertFile)
if err != nil {
return etcd.Etcd{}, false, err
}
etc.Client = client
return etc, stubzones, nil
2016-03-20 17:54:21 +00:00
}
}
return etcd.Etcd{}, false, nil
2016-03-20 17:54:21 +00:00
}
2016-03-20 21:36:55 +00:00
func newEtcdClient(endpoints []string, tlsCert, tlsKey, tlsCACert string) (etcdc.KeysAPI, error) {
etcdCfg := etcdc.Config{
Endpoints: endpoints,
2016-03-20 18:17:07 +00:00
Transport: newHTTPSTransport(tlsCert, tlsKey, tlsCACert),
}
2016-03-20 21:36:55 +00:00
cli, err := etcdc.New(etcdCfg)
2016-03-20 17:54:21 +00:00
if err != nil {
return nil, err
}
2016-03-20 21:36:55 +00:00
return etcdc.NewKeysAPI(cli), nil
2016-03-20 18:17:07 +00:00
}
2016-03-20 21:36:55 +00:00
func newHTTPSTransport(tlsCertFile, tlsKeyFile, tlsCACertFile string) etcdc.CancelableTransport {
2016-03-20 18:17:07 +00:00
var cc *tls.Config = nil
if tlsCertFile != "" && tlsKeyFile != "" {
var rpool *x509.CertPool
if tlsCACertFile != "" {
if pemBytes, err := ioutil.ReadFile(tlsCACertFile); err == nil {
rpool = x509.NewCertPool()
rpool.AppendCertsFromPEM(pemBytes)
}
}
if tlsCert, err := tls.LoadX509KeyPair(tlsCertFile, tlsKeyFile); err == nil {
cc = &tls.Config{
RootCAs: rpool,
Certificates: []tls.Certificate{tlsCert},
InsecureSkipVerify: true,
}
2016-03-20 17:54:21 +00:00
}
}
2016-03-20 18:17:07 +00:00
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: cc,
}
return tr
}