diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index ce0df96aa..e10fe348b 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -307,7 +307,7 @@ func dialOptions(c Config, i InstanceConnConfig) []cloudsqlconn.DialOption { if i.IAMAuthN != nil { opts = append(opts, cloudsqlconn.WithDialIAMAuthN(*i.IAMAuthN)) } - if i.SQLDataEnabled != nil && *i.SQLDataEnabled || c.SQLDataEnabled { + if sqlDataEnabled(&c, i) { opts = append(opts, cloudsqlconn.WithSQLData()) } @@ -641,7 +641,7 @@ func NewClient(ctx context.Context, d cloudsql.Dialer, l cloudsql.Logger, conf * // unless the proxy is in SqlDataEnabled mode, initiate a refresh operation to warm the cache for _, inst := range conf.Instances { // Skip instances with SqlDataEnabled - if conf.SQLDataEnabled || inst.SQLDataEnabled != nil && *inst.SQLDataEnabled { + if sqlDataEnabled(conf, inst) { continue } go func(name string) { _, _ = d.EngineVersion(ctx, name) }(inst.Name) @@ -904,6 +904,15 @@ type socketMount struct { dialOpts []cloudsqlconn.DialOption } +// sqlDataEnabled reports whether the instance connects through the SQL Data +// API. A value set on the instance takes precedence over the global setting. +func sqlDataEnabled(conf *Config, inst InstanceConnConfig) bool { + if inst.SQLDataEnabled != nil { + return *inst.SQLDataEnabled + } + return conf.SQLDataEnabled +} + func networkType(conf *Config, inst InstanceConnConfig) string { if (conf.UnixSocket == "" && inst.UnixSocket == "" && inst.UnixSocketPath == "") || (inst.Addr != "" || inst.Port != 0) { @@ -951,7 +960,7 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi np = inst.Port case conf.Port != 0: np = pc.nextPort() - case conf.SQLDataEnabled || inst.SQLDataEnabled != nil && *inst.SQLDataEnabled: + case sqlDataEnabled(conf, inst): // TODO: Only Postgres is supported by the SqlDataService // when more engines are supported, this code will need to change. np = pc.nextDBPort("POSTGRES") @@ -971,7 +980,7 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi var version string switch { - case conf.SQLDataEnabled || inst.SQLDataEnabled != nil && *inst.SQLDataEnabled: + case sqlDataEnabled(conf, inst): version = "POSTGRES" default: var err error diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index f0dd14b19..258bd253c 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -265,6 +265,21 @@ func TestClientInitialization(t *testing.T) { "127.0.0.1:60000", }, }, + { + desc: "with SQL Data enabled globally but disabled per-instance", + in: &proxy.Config{ + Addr: "127.0.0.1", + SQLDataEnabled: true, + Instances: []proxy.InstanceConnConfig{ + {Name: sd}, + {Name: mysql, SQLDataEnabled: pointer(false)}, + }, + }, + wantTCPAddrs: []string{ + "127.0.0.1:5432", + "127.0.0.1:3306", + }, + }, { desc: "with a Unix socket", in: &proxy.Config{ @@ -735,6 +750,18 @@ func TestSQLDataWarmup(t *testing.T) { }, want: 0, }, + { + desc: "warmup not skipped when disabled per-instance", + in: &proxy.Config{ + SQLDataEnabled: true, + Instances: []proxy.InstanceConnConfig{{ + Name: "proj:reg:inst", + Port: 30107, + SQLDataEnabled: pointer(false), + }}, + }, + want: 1, + }, } for _, tc := range tcs {