Skip to content

Commit 1972693

Browse files
tsawadaenocom
andauthored
feat: allow multiple -instances flags (GoogleCloudPlatform#1046)
This PR adds support for specifying the `instances` flag repeatedly, as a convenience in templating contexts. Fixes GoogleCloudPlatform#1030 Co-authored-by: Eno Compton <enocom@google.com>
1 parent 4b84c6c commit 1972693

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

cmd/cloud_sql_proxy/cloud_sql_proxy.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,7 @@ Unix socket-based connections.`)
7373
`Open sockets for each Cloud SQL Instance in the projects specified
7474
(comma-separated list)`,
7575
)
76-
instances = flag.String("instances", "",
77-
`Comma-separated list of fully qualified instances (project:region:name)
78-
to connect to. If the name has the suffix '=tcp:port', a TCP server is opened
79-
on the specified port on localhost to proxy to that instance. It is also possible
80-
to listen on a custom address by providing a host, e.g., '=tcp:0.0.0.0:port'. If
81-
no value is provided for 'tcp', one socket file per instance is opened in 'dir'.
82-
You may use INSTANCES environment variable for the same effect. Using both will
83-
use value from flag, Not compatible with -fuse.`,
84-
)
76+
instances stringListValue // -instances flag is defined in runProxy()
8577
instanceSrc = flag.String("instances_metadata", "", `If provided, it is treated as a path to a metadata value which
8678
is polled for a comma-separated list of instances to connect to. For example,
8779
to use the instance metadata value named 'cloud-sql-instances' you would
@@ -198,6 +190,8 @@ Connection:
198190
199191
-instances=my-project:my-region:my-instance
200192
193+
For convenience, this flag may be specified multiple times.
194+
201195
For connectivity over TCP, you must specify a tcp port as part of the
202196
instance string. For example, the following example opens a loopback TCP
203197
socket on port 3306, which will be proxied to connect to the instance
@@ -295,6 +289,17 @@ func userAgentFromVersionString() string {
295289

296290
const accountErrorSuffix = `Please create a new VM with Cloud SQL access (scope) enabled under "Identity and API access". Alternatively, create a new "service account key" and specify it using the -credential_file parameter`
297291

292+
type stringListValue []string
293+
294+
func (i *stringListValue) String() string {
295+
return strings.Join(*i, ",")
296+
}
297+
298+
func (i *stringListValue) Set(s string) error {
299+
*i = append(*i, stringList(s)...)
300+
return nil
301+
}
302+
298303
func checkFlags(onGCE bool) error {
299304
if !onGCE {
300305
if *instanceSrc != "" {
@@ -481,6 +486,17 @@ func gcloudProject() ([]string, error) {
481486
}
482487

483488
func runProxy() int {
489+
flag.Var(&instances, "instances",
490+
`Comma-separated list of fully qualified instances (project:region:name)
491+
to connect to. If the name has the suffix '=tcp:port', a TCP server is opened
492+
on the specified port on localhost to proxy to that instance. It is also possible
493+
to listen on a custom address by providing a host, e.g., '=tcp:0.0.0.0:port'. If
494+
no value is provided for 'tcp', one socket file per instance is opened in 'dir'.
495+
For convenience, this flag may be specified multiple times.
496+
You may use the INSTANCES environment variable for the same effect. Using both will
497+
use the value from the flag, Not compatible with -fuse.`,
498+
)
499+
484500
flag.Parse()
485501

486502
if *version {
@@ -527,14 +543,13 @@ func runProxy() int {
527543

528544
// TODO: needs a better place for consolidation
529545
// if instances is blank and env var INSTANCES is supplied use it
530-
if envInstances := os.Getenv("INSTANCES"); *instances == "" && envInstances != "" {
531-
*instances = envInstances
546+
if envInstances := os.Getenv("INSTANCES"); len(instances) == 0 && envInstances != "" {
547+
instances.Set(envInstances)
532548
}
533549

534-
instList := stringList(*instances)
535550
projList := stringList(*projects)
536551
// TODO: it'd be really great to consolidate flag verification in one place.
537-
if len(instList) == 0 && *instanceSrc == "" && len(projList) == 0 && !*useFuse {
552+
if len(instances) == 0 && *instanceSrc == "" && len(projList) == 0 && !*useFuse {
538553
var err error
539554
projList, err = gcloudProject()
540555
if err == nil {
@@ -571,8 +586,8 @@ func runProxy() int {
571586
logging.Errorf(err.Error())
572587
return 1
573588
}
574-
instList = append(instList, ins...)
575-
cfgs, err := CreateInstanceConfigs(*dir, *useFuse, instList, *instanceSrc, client, *skipInvalidInstanceConfigs)
589+
instances = append(instances, ins...)
590+
cfgs, err := CreateInstanceConfigs(*dir, *useFuse, instances, *instanceSrc, client, *skipInvalidInstanceConfigs)
576591
if err != nil {
577592
logging.Errorf(err.Error())
578593
return 1

0 commit comments

Comments
 (0)