mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 02:03:20 -04:00 
			
		
		
		
	* For caddy v1 in our org This RP changes all imports for caddyserver/caddy to coredns/caddy. This is the v1 code of caddy. For the coredns/caddy repo the following changes have been made: * anything not needed by us is deleted * all `telemetry` stuff is deleted * all its import paths are also changed to point to coredns/caddy * the v1 branch has been moved to the master branch * a v1.1.0 tag has been added to signal the latest release Signed-off-by: Miek Gieben <miek@miek.nl> * Fix imports Signed-off-by: Miek Gieben <miek@miek.nl> * Group coredns/caddy with out plugins Signed-off-by: Miek Gieben <miek@miek.nl> * remove this file Signed-off-by: Miek Gieben <miek@miek.nl> * Relax import ordering github.com/coredns is now also a coredns dep, this makes github.com/coredns/caddy fit more natural in the list. Signed-off-by: Miek Gieben <miek@miek.nl> * Fix final import Signed-off-by: Miek Gieben <miek@miek.nl>
		
			
				
	
	
		
			125 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			125 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package etcd
 | |
| 
 | |
| import (
 | |
| 	"crypto/tls"
 | |
| 
 | |
| 	"github.com/coredns/caddy"
 | |
| 	"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"
 | |
| 
 | |
| 	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"
 |