discovery: add command line flag for discovery-proxy

This commit is contained in:
Jonathan Boulle 2014-11-06 16:35:24 -08:00
parent ccded6644a
commit 8f1885a398
5 changed files with 37 additions and 42 deletions

View File

@ -22,7 +22,6 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path" "path"
"sort" "sort"
"strconv" "strconv"
@ -46,16 +45,14 @@ var (
) )
const ( const (
// Environment variable used to configure an HTTP proxy for discovery
DiscoveryProxyEnv = "ETCD_DISCOVERY_PROXY"
// Number of retries discovery will attempt before giving up and erroring out. // Number of retries discovery will attempt before giving up and erroring out.
nRetries = uint(3) nRetries = uint(3)
) )
// JoinCluster will connect to the discovery service at the given url, and // JoinCluster will connect to the discovery service at the given url, and
// register the server represented by the given id and config to the cluster // register the server represented by the given id and config to the cluster
func JoinCluster(durl string, id types.ID, config string) (string, error) { func JoinCluster(durl, dproxyurl string, id types.ID, config string) (string, error) {
d, err := newDiscovery(durl, id) d, err := newDiscovery(durl, dproxyurl, id)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -64,8 +61,8 @@ func JoinCluster(durl string, id types.ID, config string) (string, error) {
// GetCluster will connect to the discovery service at the given url and // GetCluster will connect to the discovery service at the given url and
// retrieve a string describing the cluster // retrieve a string describing the cluster
func GetCluster(durl string) (string, error) { func GetCluster(durl, dproxyurl string) (string, error) {
d, err := newDiscovery(durl, 0) d, err := newDiscovery(durl, dproxyurl, 0)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -82,11 +79,10 @@ type discovery struct {
clock clockwork.Clock clock clockwork.Clock
} }
// proxyFuncFromEnv builds a proxy function if the appropriate environment // newProxyFunc builds a proxy function from the given string, which should
// variable is set. It performs basic sanitization of the environment variable // represent a URL that can be used as a proxy. It performs basic
// and returns any error encountered. // sanitization of the URL and returns any error encountered.
func proxyFuncFromEnv() (func(*http.Request) (*url.URL, error), error) { func newProxyFunc(proxy string) (func(*http.Request) (*url.URL, error), error) {
proxy := os.Getenv(DiscoveryProxyEnv)
if proxy == "" { if proxy == "" {
return nil, nil return nil, nil
} }
@ -111,14 +107,14 @@ func proxyFuncFromEnv() (func(*http.Request) (*url.URL, error), error) {
return http.ProxyURL(proxyURL), nil return http.ProxyURL(proxyURL), nil
} }
func newDiscovery(durl string, id types.ID) (*discovery, error) { func newDiscovery(durl, dproxyurl string, id types.ID) (*discovery, error) {
u, err := url.Parse(durl) u, err := url.Parse(durl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
token := u.Path token := u.Path
u.Path = "" u.Path = ""
pf, err := proxyFuncFromEnv() pf, err := newProxyFunc(dproxyurl)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -20,7 +20,6 @@ import (
"errors" "errors"
"math/rand" "math/rand"
"net/http" "net/http"
"os"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
@ -32,9 +31,8 @@ import (
"github.com/coreos/etcd/client" "github.com/coreos/etcd/client"
) )
func TestProxyFuncFromEnvUnset(t *testing.T) { func TestNewProxyFuncUnset(t *testing.T) {
os.Setenv(DiscoveryProxyEnv, "") pf, err := newProxyFunc("")
pf, err := proxyFuncFromEnv()
if pf != nil { if pf != nil {
t.Fatal("unexpected non-nil proxyFunc") t.Fatal("unexpected non-nil proxyFunc")
} }
@ -43,14 +41,13 @@ func TestProxyFuncFromEnvUnset(t *testing.T) {
} }
} }
func TestProxyFuncFromEnvBad(t *testing.T) { func TestNewProxyFuncBad(t *testing.T) {
tests := []string{ tests := []string{
"%%", "%%",
"http://foo.com/%1", "http://foo.com/%1",
} }
for i, in := range tests { for i, in := range tests {
os.Setenv(DiscoveryProxyEnv, in) pf, err := newProxyFunc(in)
pf, err := proxyFuncFromEnv()
if pf != nil { if pf != nil {
t.Errorf("#%d: unexpected non-nil proxyFunc", i) t.Errorf("#%d: unexpected non-nil proxyFunc", i)
} }
@ -60,14 +57,13 @@ func TestProxyFuncFromEnvBad(t *testing.T) {
} }
} }
func TestProxyFuncFromEnv(t *testing.T) { func TestNewProxyFunc(t *testing.T) {
tests := map[string]string{ tests := map[string]string{
"bar.com": "http://bar.com", "bar.com": "http://bar.com",
"http://disco.foo.bar": "http://disco.foo.bar", "http://disco.foo.bar": "http://disco.foo.bar",
} }
for in, w := range tests { for in, w := range tests {
os.Setenv(DiscoveryProxyEnv, in) pf, err := newProxyFunc(in)
pf, err := proxyFuncFromEnv()
if pf == nil { if pf == nil {
t.Errorf("%s: unexpected nil proxyFunc", in) t.Errorf("%s: unexpected nil proxyFunc", in)
continue continue

View File

@ -57,6 +57,7 @@ var (
name = fs.String("name", "default", "Unique human-readable name for this node") name = fs.String("name", "default", "Unique human-readable name for this node")
dir = fs.String("data-dir", "", "Path to the data directory") dir = fs.String("data-dir", "", "Path to the data directory")
durl = fs.String("discovery", "", "Discovery service used to bootstrap the cluster") durl = fs.String("discovery", "", "Discovery service used to bootstrap the cluster")
dproxy = fs.String("discovery-proxy", "", "HTTP proxy to use for traffic to discovery service")
snapCount = fs.Uint64("snapshot-count", etcdserver.DefaultSnapCount, "Number of committed transactions to trigger a snapshot") snapCount = fs.Uint64("snapshot-count", etcdserver.DefaultSnapCount, "Number of committed transactions to trigger a snapshot")
printVersion = fs.Bool("version", false, "Print the version and exit") printVersion = fs.Bool("version", false, "Print the version and exit")
@ -265,6 +266,7 @@ func startEtcd() error {
SnapCount: *snapCount, SnapCount: *snapCount,
Cluster: cls, Cluster: cls,
DiscoveryURL: *durl, DiscoveryURL: *durl,
DiscoveryProxy: *dproxy,
NewCluster: clusterStateFlag.String() == clusterStateFlagNew, NewCluster: clusterStateFlag.String() == clusterStateFlagNew,
Transport: pt, Transport: pt,
} }
@ -303,7 +305,7 @@ func startProxy() error {
} }
if *durl != "" { if *durl != "" {
s, err := discovery.GetCluster(*durl) s, err := discovery.GetCluster(*durl, *dproxy)
if err != nil { if err != nil {
return err return err
} }

View File

@ -29,6 +29,7 @@ import (
type ServerConfig struct { type ServerConfig struct {
Name string Name string
DiscoveryURL string DiscoveryURL string
DiscoveryProxy string
ClientURLs types.URLs ClientURLs types.URLs
DataDir string DataDir string
SnapCount uint64 SnapCount uint64

View File

@ -204,7 +204,7 @@ func NewServer(cfg *ServerConfig) (*EtcdServer, error) {
} }
m := cfg.Cluster.MemberByName(cfg.Name) m := cfg.Cluster.MemberByName(cfg.Name)
if cfg.ShouldDiscover() { if cfg.ShouldDiscover() {
s, err := discovery.JoinCluster(cfg.DiscoveryURL, m.ID, cfg.Cluster.String()) s, err := discovery.JoinCluster(cfg.DiscoveryURL, cfg.DiscoveryProxy, m.ID, cfg.Cluster.String())
if err != nil { if err != nil {
return nil, err return nil, err
} }