[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

+50 -50
View File
@@ -27,36 +27,24 @@ var (
)
type Proxy struct {
Dial func() (net.Conn, error)
version string
Config
dial func() (net.Conn, error)
client *docker.Client
dockerAddr string
dockerBridgeIP string
listenAddr string
tlsConfig *TLSConfig
withDNS bool
withIPAM bool
}
func NewProxy(version, dockerAddr, listenAddr string, withDNS, withIPAM bool, tlsConfig *TLSConfig) (*Proxy, error) {
u, err := url.Parse(dockerAddr)
if err != nil {
return nil, err
}
type Config struct {
DockerAddr string
ListenAddr string
TLSConfig TLSConfig
Version string
WithDNS bool
WithIPAM bool
}
var dockerBridgeIP []byte
if withDNS {
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)
func NewProxy(c Config) (*Proxy, error) {
u, err := url.Parse(c.DockerAddr)
if err != nil {
return nil, err
}
@@ -69,19 +57,31 @@ func NewProxy(version, dockerAddr, listenAddr string, withDNS, withIPAM bool, tl
targetAddr = u.Path
}
return &Proxy{
Dial: func() (net.Conn, error) {
p := &Proxy{
Config: c,
dial: func() (net.Conn, error) {
return net.Dial(u.Scheme, targetAddr)
},
version: version,
client: client,
dockerAddr: dockerAddr,
listenAddr: listenAddr,
dockerBridgeIP: string(dockerBridgeIP),
tlsConfig: tlsConfig,
withDNS: withDNS,
withIPAM: withIPAM,
}, nil
}
if p.WithDNS {
dockerBridgeIP, err := callWeave("docker-bridge-ip")
if err != nil {
return nil, err
}
p.dockerBridgeIP = string(dockerBridgeIP)
}
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) {
@@ -89,13 +89,13 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
switch {
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):
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):
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.withIPAM}, w, r)
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.WithIPAM}, w, r)
case strings.HasPrefix(path, "/status"):
fmt.Fprintln(w, "weave proxy", proxy.version)
fmt.Fprintln(w, "weave proxy", proxy.Version)
fmt.Fprintln(w, proxy.Status())
default:
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) {
newClient(proxy.Dial, i).ServeHTTP(w, r)
newClient(proxy.dial, i).ServeHTTP(w, r)
}
// Return status string
func (proxy *Proxy) Status() string {
var buf bytes.Buffer
fmt.Fprintln(&buf, "Listen address is", proxy.listenAddr)
fmt.Fprintln(&buf, "Docker address is", proxy.dockerAddr)
if proxy.withDNS {
fmt.Fprintln(&buf, "Listen address is", proxy.ListenAddr)
fmt.Fprintln(&buf, "Docker address is", proxy.DockerAddr)
if proxy.WithDNS {
fmt.Fprintln(&buf, "DNS on")
} else {
fmt.Fprintln(&buf, "DNS off")
}
if proxy.withIPAM {
if proxy.WithIPAM {
fmt.Fprintln(&buf, "IPAM on")
} else {
fmt.Fprintln(&buf, "IPAM off")
@@ -125,18 +125,18 @@ func (proxy *Proxy) Status() string {
}
func (proxy *Proxy) ListenAndServe() error {
listener, err := net.Listen("tcp", proxy.listenAddr)
listener, err := net.Listen("tcp", proxy.ListenAddr)
if err != nil {
return err
}
if proxy.tlsConfig.enabled() {
listener = tls.NewListener(listener, proxy.tlsConfig.Config)
if proxy.TLSConfig.enabled() {
listener = tls.NewListener(listener, proxy.TLSConfig.Config)
Info.Println("TLS Enabled")
}
Info.Printf("Listening on %s", proxy.listenAddr)
Info.Printf("Proxying %s", proxy.dockerAddr)
Info.Printf("Listening on %s", proxy.ListenAddr)
Info.Printf("Proxying %s", proxy.DockerAddr)
return (&http.Server{Handler: proxy}).Serve(listener)
}