[proxy] Added proxy.Config struct for initialization

This commit is contained in:
Paul Bellamy committed 2015-06-05 13:23:14 +01:00
1 parent c31358259f
commit c0cf9e509c
2 files changed
+70 -80

No files matched your search

+20 -30
View File
@@ -10,33 +10,30 @@ import (
) )
var ( var (
version = "(unreleased version)" version = "(unreleased version)"
defaultTarget = "unix:///var/run/docker.sock" defaultDockerAddr = "unix:///var/run/docker.sock"
defaultListen = ":12375" defaultListenAddr = ":12375"
) )
func main() { func main() {
var ( var (
debug bool debug bool
justVersion bool justVersion bool
listen string c proxy.Config
target string
tlsConfig proxy.TLSConfig
withDNS bool
withIPAM bool
) )
c.Version = version
getopt.BoolVarLong(&debug, "debug", 'd', "log debugging information") getopt.BoolVarLong(&debug, "debug", 'd', "log debugging information")
getopt.BoolVarLong(&justVersion, "version", 0, "print version and exit") getopt.BoolVarLong(&justVersion, "version", 0, "print version and exit")
getopt.StringVar(&listen, 'L', fmt.Sprintf("address on which to listen (default %s)", defaultListen)) getopt.StringVar(&c.ListenAddr, 'L', fmt.Sprintf("address on which to listen (default %s)", defaultListenAddr))
getopt.StringVar(&target, 'H', fmt.Sprintf("docker daemon URL to proxy (default %s)", defaultTarget)) getopt.StringVar(&c.DockerAddr, 'H', fmt.Sprintf("docker daemon URL to proxy (default %s)", defaultDockerAddr))
getopt.StringVarLong(&tlsConfig.CACert, "tlscacert", 0, "Trust certs signed only by this CA") getopt.StringVarLong(&c.TLSConfig.CACert, "tlscacert", 0, "Trust certs signed only by this CA")
getopt.StringVarLong(&tlsConfig.Cert, "tlscert", 0, "Path to TLS certificate file") getopt.StringVarLong(&c.TLSConfig.Cert, "tlscert", 0, "Path to TLS certificate file")
getopt.BoolVarLong(&tlsConfig.Enabled, "tls", 0, "Use TLS; implied by --tlsverify") getopt.BoolVarLong(&c.TLSConfig.Enabled, "tls", 0, "Use TLS; implied by --tlsverify")
getopt.StringVarLong(&tlsConfig.Key, "tlskey", 0, "Path to TLS key file") getopt.StringVarLong(&c.TLSConfig.Key, "tlskey", 0, "Path to TLS key file")
getopt.BoolVarLong(&tlsConfig.Verify, "tlsverify", 0, "Use TLS and verify the remote") getopt.BoolVarLong(&c.TLSConfig.Verify, "tlsverify", 0, "Use TLS and verify the remote")
getopt.BoolVarLong(&withDNS, "with-dns", 'w', "instruct created containers to use weaveDNS as their nameserver") getopt.BoolVarLong(&c.WithDNS, "with-dns", 'w', "instruct created containers to use weaveDNS as their nameserver")
getopt.BoolVarLong(&withIPAM, "with-ipam", 'i', "automatically allocate addresses for containers without a WEAVE_CIDR") getopt.BoolVarLong(&c.WithIPAM, "with-ipam", 'i', "automatically allocate addresses for containers without a WEAVE_CIDR")
getopt.Parse() getopt.Parse()
if justVersion { if justVersion {
@@ -48,27 +45,20 @@ func main() {
InitDefaultLogging(true) InitDefaultLogging(true)
} }
if target == "" { if c.DockerAddr == "" {
target = defaultTarget c.DockerAddr = defaultDockerAddr
} }
if listen == "" { if c.ListenAddr == "" {
listen = defaultListen c.ListenAddr = defaultListenAddr
} }
p, err := proxy.NewProxy( p, err := proxy.NewProxy(c)
version,
target,
listen,
withDNS,
withIPAM,
&tlsConfig,
)
if err != nil { if err != nil {
Error.Fatalf("Could not start proxy: %s", err) Error.Fatalf("Could not start proxy: %s", err)
} }
if err := p.ListenAndServe(); err != nil { if err := p.ListenAndServe(); err != nil {
Error.Fatalf("Could not listen on %s: %s", listen, err) Error.Fatalf("Could not listen on %s: %s", p.ListenAddr, err)
} }
} }
+50 -50
View File
@@ -27,36 +27,24 @@ var (
) )
type Proxy struct { type Proxy struct {
Dial func() (net.Conn, error) Config
version string
dial func() (net.Conn, error)
client *docker.Client client *docker.Client
dockerAddr string
dockerBridgeIP string dockerBridgeIP string
listenAddr string
tlsConfig *TLSConfig
withDNS bool
withIPAM bool
} }
func NewProxy(version, dockerAddr, listenAddr string, withDNS, withIPAM bool, tlsConfig *TLSConfig) (*Proxy, error) { type Config struct {
u, err := url.Parse(dockerAddr) DockerAddr string
if err != nil { ListenAddr string
return nil, err TLSConfig TLSConfig
} Version string
WithDNS bool
WithIPAM bool
}
var dockerBridgeIP []byte func NewProxy(c Config) (*Proxy, error) {
if withDNS { u, err := url.Parse(c.DockerAddr)
dockerBridgeIP, err = callWeave("docker-bridge-ip")
if err != nil {
return nil, err
}
}
if err := tlsConfig.loadCerts(); err != nil {
Error.Fatalf("Could not configure tls for proxy: %s", err)
}
client, err := docker.NewClient(dockerAddr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -69,19 +57,31 @@ func NewProxy(version, dockerAddr, listenAddr string, withDNS, withIPAM bool, tl
targetAddr = u.Path targetAddr = u.Path
} }
return &Proxy{ p := &Proxy{
Dial: func() (net.Conn, error) { Config: c,
dial: func() (net.Conn, error) {
return net.Dial(u.Scheme, targetAddr) return net.Dial(u.Scheme, targetAddr)
}, },
version: version, }
client: client,
dockerAddr: dockerAddr, if p.WithDNS {
listenAddr: listenAddr, dockerBridgeIP, err := callWeave("docker-bridge-ip")
dockerBridgeIP: string(dockerBridgeIP), if err != nil {
tlsConfig: tlsConfig, return nil, err
withDNS: withDNS, }
withIPAM: withIPAM, p.dockerBridgeIP = string(dockerBridgeIP)
}, nil }
if err := p.TLSConfig.loadCerts(); err != nil {
Error.Fatalf("Could not configure tls for proxy: %s", err)
}
p.client, err = docker.NewClient(p.DockerAddr)
if err != nil {
return nil, err
}
return p, nil
} }
func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -89,13 +89,13 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
switch { switch {
case containerCreateRegexp.MatchString(path): case containerCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createContainerInterceptor{proxy.client, proxy.withDNS, proxy.dockerBridgeIP, proxy.withIPAM}, w, r) proxy.serveWithInterceptor(&createContainerInterceptor{proxy.client, proxy.WithDNS, proxy.dockerBridgeIP, proxy.WithIPAM}, w, r)
case containerStartRegexp.MatchString(path): case containerStartRegexp.MatchString(path):
proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.withDNS, proxy.withIPAM}, w, r) proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.WithDNS, proxy.WithIPAM}, w, r)
case execCreateRegexp.MatchString(path): case execCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.withIPAM}, w, r) proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.WithIPAM}, w, r)
case strings.HasPrefix(path, "/status"): case strings.HasPrefix(path, "/status"):
fmt.Fprintln(w, "weave proxy", proxy.version) fmt.Fprintln(w, "weave proxy", proxy.Version)
fmt.Fprintln(w, proxy.Status()) fmt.Fprintln(w, proxy.Status())
default: default:
proxy.serveWithInterceptor(&nullInterceptor{}, w, r) proxy.serveWithInterceptor(&nullInterceptor{}, w, r)
@@ -103,20 +103,20 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (proxy *Proxy) serveWithInterceptor(i interceptor, w http.ResponseWriter, r *http.Request) { func (proxy *Proxy) serveWithInterceptor(i interceptor, w http.ResponseWriter, r *http.Request) {
newClient(proxy.Dial, i).ServeHTTP(w, r) newClient(proxy.dial, i).ServeHTTP(w, r)
} }
// Return status string // Return status string
func (proxy *Proxy) Status() string { func (proxy *Proxy) Status() string {
var buf bytes.Buffer var buf bytes.Buffer
fmt.Fprintln(&buf, "Listen address is", proxy.listenAddr) fmt.Fprintln(&buf, "Listen address is", proxy.ListenAddr)
fmt.Fprintln(&buf, "Docker address is", proxy.dockerAddr) fmt.Fprintln(&buf, "Docker address is", proxy.DockerAddr)
if proxy.withDNS { if proxy.WithDNS {
fmt.Fprintln(&buf, "DNS on") fmt.Fprintln(&buf, "DNS on")
} else { } else {
fmt.Fprintln(&buf, "DNS off") fmt.Fprintln(&buf, "DNS off")
} }
if proxy.withIPAM { if proxy.WithIPAM {
fmt.Fprintln(&buf, "IPAM on") fmt.Fprintln(&buf, "IPAM on")
} else { } else {
fmt.Fprintln(&buf, "IPAM off") fmt.Fprintln(&buf, "IPAM off")
@@ -125,18 +125,18 @@ func (proxy *Proxy) Status() string {
} }
func (proxy *Proxy) ListenAndServe() error { func (proxy *Proxy) ListenAndServe() error {
listener, err := net.Listen("tcp", proxy.listenAddr) listener, err := net.Listen("tcp", proxy.ListenAddr)
if err != nil { if err != nil {
return err return err
} }
if proxy.tlsConfig.enabled() { if proxy.TLSConfig.enabled() {
listener = tls.NewListener(listener, proxy.tlsConfig.Config) listener = tls.NewListener(listener, proxy.TLSConfig.Config)
Info.Println("TLS Enabled") Info.Println("TLS Enabled")
} }
Info.Printf("Listening on %s", proxy.listenAddr) Info.Printf("Listening on %s", proxy.ListenAddr)
Info.Printf("Proxying %s", proxy.dockerAddr) Info.Printf("Proxying %s", proxy.DockerAddr)
return (&http.Server{Handler: proxy}).Serve(listener) return (&http.Server{Handler: proxy}).Serve(listener)
} }