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"
"net/http"
"net/url"
"os"
"path"
"sort"
"strconv"
@ -46,16 +45,14 @@ var (
)
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.
nRetries = uint(3)
)
// 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
func JoinCluster(durl string, id types.ID, config string) (string, error) {
d, err := newDiscovery(durl, id)
func JoinCluster(durl, dproxyurl string, id types.ID, config string) (string, error) {
d, err := newDiscovery(durl, dproxyurl, id)
if err != nil {
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
// retrieve a string describing the cluster
func GetCluster(durl string) (string, error) {
d, err := newDiscovery(durl, 0)
func GetCluster(durl, dproxyurl string) (string, error) {
d, err := newDiscovery(durl, dproxyurl, 0)
if err != nil {
return "", err
}
@ -82,11 +79,10 @@ type discovery struct {
clock clockwork.Clock
}
// proxyFuncFromEnv builds a proxy function if the appropriate environment
// variable is set. It performs basic sanitization of the environment variable
// and returns any error encountered.
func proxyFuncFromEnv() (func(*http.Request) (*url.URL, error), error) {
proxy := os.Getenv(DiscoveryProxyEnv)
// newProxyFunc builds a proxy function from the given string, which should
// represent a URL that can be used as a proxy. It performs basic
// sanitization of the URL and returns any error encountered.
func newProxyFunc(proxy string) (func(*http.Request) (*url.URL, error), error) {
if proxy == "" {
return nil, nil
}
@ -111,14 +107,14 @@ func proxyFuncFromEnv() (func(*http.Request) (*url.URL, error), error) {
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)
if err != nil {
return nil, err
}
token := u.Path
u.Path = ""
pf, err := proxyFuncFromEnv()
pf, err := newProxyFunc(dproxyurl)
if err != nil {
return nil, err
}

View File

@ -20,7 +20,6 @@ import (
"errors"
"math/rand"
"net/http"
"os"
"reflect"
"sort"
"strconv"
@ -32,9 +31,8 @@ import (
"github.com/coreos/etcd/client"
)
func TestProxyFuncFromEnvUnset(t *testing.T) {
os.Setenv(DiscoveryProxyEnv, "")
pf, err := proxyFuncFromEnv()
func TestNewProxyFuncUnset(t *testing.T) {
pf, err := newProxyFunc("")
if pf != nil {
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{
"%%",
"http://foo.com/%1",
}
for i, in := range tests {
os.Setenv(DiscoveryProxyEnv, in)
pf, err := proxyFuncFromEnv()
pf, err := newProxyFunc(in)
if pf != nil {
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{
"bar.com": "http://bar.com",
"http://disco.foo.bar": "http://disco.foo.bar",
}
for in, w := range tests {
os.Setenv(DiscoveryProxyEnv, in)
pf, err := proxyFuncFromEnv()
pf, err := newProxyFunc(in)
if pf == nil {
t.Errorf("%s: unexpected nil proxyFunc", in)
continue

View File

@ -57,6 +57,7 @@ var (
name = fs.String("name", "default", "Unique human-readable name for this node")
dir = fs.String("data-dir", "", "Path to the data directory")
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")
printVersion = fs.Bool("version", false, "Print the version and exit")
@ -259,14 +260,15 @@ func startEtcd() error {
}
cfg := &etcdserver.ServerConfig{
Name: *name,
ClientURLs: acurls,
DataDir: *dir,
SnapCount: *snapCount,
Cluster: cls,
DiscoveryURL: *durl,
NewCluster: clusterStateFlag.String() == clusterStateFlagNew,
Transport: pt,
Name: *name,
ClientURLs: acurls,
DataDir: *dir,
SnapCount: *snapCount,
Cluster: cls,
DiscoveryURL: *durl,
DiscoveryProxy: *dproxy,
NewCluster: clusterStateFlag.String() == clusterStateFlagNew,
Transport: pt,
}
var s *etcdserver.EtcdServer
s, err = etcdserver.NewServer(cfg)
@ -303,7 +305,7 @@ func startProxy() error {
}
if *durl != "" {
s, err := discovery.GetCluster(*durl)
s, err := discovery.GetCluster(*durl, *dproxy)
if err != nil {
return err
}

View File

@ -27,14 +27,15 @@ import (
// ServerConfig holds the configuration of etcd as taken from the command line or discovery.
type ServerConfig struct {
Name string
DiscoveryURL string
ClientURLs types.URLs
DataDir string
SnapCount uint64
Cluster *Cluster
NewCluster bool
Transport *http.Transport
Name string
DiscoveryURL string
DiscoveryProxy string
ClientURLs types.URLs
DataDir string
SnapCount uint64
Cluster *Cluster
NewCluster bool
Transport *http.Transport
}
// VerifyBootstrapConfig sanity-checks the initial config and returns an error

View File

@ -204,7 +204,7 @@ func NewServer(cfg *ServerConfig) (*EtcdServer, error) {
}
m := cfg.Cluster.MemberByName(cfg.Name)
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 {
return nil, err
}