Skip to content

Commit

Permalink
Make consistent error logging and fail the inactive instance without …
Browse files Browse the repository at this point in the history
…port config
  • Loading branch information
annafang-google committed Sep 19, 2023
1 parent a77165f commit 2c7bb3f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
26 changes: 15 additions & 11 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,20 +510,22 @@ func NewClient(ctx context.Context, d cloudsql.Dialer, l cloudsql.Logger, conf *
for _, inst := range conf.Instances {
m, err := c.newSocketMount(ctx, conf, pc, inst)
if err != nil {
c.logger.Errorf("[%v] Unable to mount socket: %v", inst.Name, err)
if networkType(conf, inst) == "unix" {
continue
}
switch err.(type) {
// this is not the inactive instance case. Fail the proxy
// so that the user could fix the proxy command error.
case *net.OpError:
for _, m := range mnts {
mErr := m.Close()
if mErr != nil {
l.Errorf("[%v] failed to close mount: %v", m.inst, mErr)
}
}
return nil, fmt.Errorf("[%v] Unable to mount socket: %v", inst.Name, err)
}
} else {
l.Infof("[%s] Listening on %s", inst.Name, m.Addr())
mnts = append(mnts, m)
}

l.Infof("[%s] Listening on %s", inst.Name, m.Addr())
mnts = append(mnts, m)
}
c.mnts = mnts
if len(mnts) == 0 {
Expand Down Expand Up @@ -795,8 +797,10 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi
np = pc.nextPort()
default:
version, err := c.dialer.EngineVersion(ctx, inst.Name)
// Exit if the port is not specified for inactive instance
if err != nil {
c.logger.Errorf("could not resolve version for %q: %v", inst.Name, err)
c.logger.Errorf("[%v] could not resolve instance version: %v", inst.Name, err)
return nil, err
}
np = pc.nextDBPort(version)
}
Expand All @@ -807,21 +811,21 @@ func (c *Client) newSocketMount(ctx context.Context, conf *Config, pc *portConfi

version, err := c.dialer.EngineVersion(ctx, inst.Name)
if err != nil {
c.logger.Errorf("could not resolve version for %q: %v", inst.Name, err)
c.logger.Errorf("[%v] could not resolve instance version: %v", inst.Name, err)
return nil, err
}

address, err = newUnixSocketMount(inst, conf.UnixSocket, strings.HasPrefix(version, "POSTGRES"))
if err != nil {
c.logger.Errorf("could not mount unix socket %q for %q: %v",
conf.UnixSocket, inst.Name, err)
c.logger.Errorf("[%v] could not mount unix socket %q: %v", inst.Name, conf.UnixSocket, err)
return nil, err
}
}

lc := net.ListenConfig{KeepAlive: 30 * time.Second}
ln, err := lc.Listen(ctx, network, address)
if err != nil {
c.logger.Errorf("[%v] could not listen to address %v: %v", inst.Name, address, err)
return nil, err
}
// Change file permissions to allow access for user, group, and other.
Expand Down
16 changes: 8 additions & 8 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,6 @@ func TestClientInitialization(t *testing.T) {
filepath.Join(testUnixSocketPathPg),
},
},
{
desc: "without TCP port or unix socket for non functional instance",
in: &proxy.Config{
Instances: []proxy.InstanceConnConfig{
{Name: "proj:region:fakeserver"},
},
},
},
{
desc: "with TCP port for non functional instance",
in: &proxy.Config{
Expand Down Expand Up @@ -754,6 +746,14 @@ func TestProxyInitializationWithFailedUnixSocket(t *testing.T) {
},
},
},
{
desc: "without TCP port or unix socket for non functional instance",
in: &proxy.Config{
Instances: []proxy.InstanceConnConfig{
{Name: "proj:region:fakeserver"},
},
},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
Expand Down

0 comments on commit 2c7bb3f

Please sign in to comment.