From 7bdc8182ddcc0034d32a6662f9f57a99f67e2b5a Mon Sep 17 00:00:00 2001 From: Jason Hung Date: Mon, 7 Mar 2016 11:02:46 -0800 Subject: [PATCH] Added option to indicate which host to listen In addition to being able to use ./cloud_sql_proxy -dir=/cloudsql -instances=PROJECT:REGION:INSTANCE=tcp:PORT functionality, I think it is important to be able to indicate which ADDRESS to listen with a -instances=PROJECT:REGION:INSTANCE=tcp:PORT:ADDRESS option. The main use case is using with Docker and GKE. I'd like to be able to use the TCP to simplify Docker Google Container Engine deployments and simply have my existing services look for the proxy service. --- cmd/cloud_sql_proxy/proxy.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/cloud_sql_proxy/proxy.go b/cmd/cloud_sql_proxy/proxy.go index 8a52e57ba..5c202c944 100644 --- a/cmd/cloud_sql_proxy/proxy.go +++ b/cmd/cloud_sql_proxy/proxy.go @@ -122,18 +122,24 @@ func listenInstance(dst chan<- proxy.Conn, dir, instance string) (net.Listener, var path string var l net.Listener if eq := strings.Index(instance, "="); eq != -1 { - spl := strings.SplitN(instance[eq+1:], ":", 2) + spl := strings.SplitN(instance[eq+1:], ":", 3) if len(spl) == 1 { return nil, fmt.Errorf("invalid format in %q; expected 'project:instance=tcp:port'", instance) } instance = instance[:eq] + host := "127.0.0.1" + if len(spl) == 3 { + host = spl[2] + } + var err error - if l, err = net.Listen(spl[0], "127.0.0.1:"+spl[1]); err != nil { + if l, err = net.Listen(spl[0], host+":"+spl[1]); err != nil { return nil, err } - path = "localhost:" + spl[1] + // path = "localhost:" + spl[1] + path = host + ":" + spl[1] } else { path = filepath.Join(dir, instance) remove(path)