Skip to content

Commit

Permalink
reduce db connection test timeout (#49384)
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinFrazar authored Nov 25, 2024
1 parent cc07d7c commit 2a107d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
8 changes: 4 additions & 4 deletions integration/conntest/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ func TestDiagnoseConnectionForPostgresDatabases(t *testing.T) {
{
Type: types.ConnectionDiagnosticTrace_RBAC_DATABASE,
Status: types.ConnectionDiagnosticTrace_SUCCESS,
Details: "A Database Agent is available to proxy the connection to the Database.",
Details: "A Teleport Database Service is available to proxy the connection to the Database.",
},
{
Type: types.ConnectionDiagnosticTrace_CONNECTIVITY,
Status: types.ConnectionDiagnosticTrace_SUCCESS,
Details: "Database is accessible from the Database Agent.",
Details: "Database is accessible from the Teleport Database Service.",
},
{
Type: types.ConnectionDiagnosticTrace_RBAC_DATABASE_LOGIN,
Expand Down Expand Up @@ -172,8 +172,8 @@ func TestDiagnoseConnectionForPostgresDatabases(t *testing.T) {
Status: types.ConnectionDiagnosticTrace_FAILED,
Details: "Database not found. " +
"Ensure your role grants access by adding it to the 'db_labels' property. " +
"This can also happen when you don't have a Database Agent proxying the database - " +
"you can fix that by adding the database labels to the 'db_service.resources.labels' in 'teleport.yaml' file of the database agent.",
"This can also happen when you don't have a Teleport Database Service proxying the database - " +
"you can fix that by adding the database labels to the 'db_service.resources.labels' in 'teleport.yaml' file of the Database Service.",
},
},
},
Expand Down
19 changes: 11 additions & 8 deletions lib/client/conntest/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

apiclient "github.com/gravitational/teleport/api/client"
"github.com/gravitational/teleport/api/client/proto"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/client/conntest/database"
Expand Down Expand Up @@ -136,8 +137,8 @@ func (s *DatabaseConnectionTester) TestConnection(ctx context.Context, req TestC
types.ConnectionDiagnosticTrace_RBAC_DATABASE,
"Database not found. "+
"Ensure your role grants access by adding it to the 'db_labels' property. "+
"This can also happen when you don't have a Database Agent proxying the database - "+
"you can fix that by adding the database labels to the 'db_service.resources.labels' in 'teleport.yaml' file of the database agent.",
"This can also happen when you don't have a Teleport Database Service proxying the database - "+
"you can fix that by adding the database labels to the 'db_service.resources.labels' in 'teleport.yaml' file of the Database Service.",
trace.NotFound("%s not found", req.ResourceName),
)
if err != nil {
Expand Down Expand Up @@ -167,7 +168,7 @@ func (s *DatabaseConnectionTester) TestConnection(ctx context.Context, req TestC
if _, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_RBAC_DATABASE,
"A Database Agent is available to proxy the connection to the Database.",
"A Teleport Database Service is available to proxy the connection to the Database.",
nil,
); err != nil {
return nil, trace.Wrap(err)
Expand All @@ -184,7 +185,9 @@ func (s *DatabaseConnectionTester) TestConnection(ctx context.Context, req TestC
return nil, trace.Wrap(err)
}

if pingErr := databasePinger.Ping(ctx, ping); pingErr != nil {
pingCtx, cancel := context.WithTimeout(ctx, apidefaults.DefaultIOTimeout)
defer cancel()
if pingErr := databasePinger.Ping(pingCtx, ping); pingErr != nil {
connDiag, err := s.handlePingError(ctx, connectionDiagnosticID, pingErr, databasePinger)
return connDiag, trace.Wrap(err)
}
Expand Down Expand Up @@ -273,7 +276,7 @@ func newPing(alpnProxyAddr, databaseUser, databaseName string) (database.PingPar
func (s DatabaseConnectionTester) handlePingSuccess(ctx context.Context, connectionDiagnosticID string) (types.ConnectionDiagnostic, error) {
if _, err := s.appendDiagnosticTrace(ctx, connectionDiagnosticID,
types.ConnectionDiagnosticTrace_CONNECTIVITY,
"Database is accessible from the Database Agent.",
"Database is accessible from the Teleport Database Service.",
nil,
); err != nil {
return nil, trace.Wrap(err)
Expand Down Expand Up @@ -341,12 +344,12 @@ func (s DatabaseConnectionTester) handlePingError(ctx context.Context, connectio
return connDiag, nil
}

if databasePinger.IsConnectionRefusedError(pingErr) {
if databasePinger.IsConnectionRefusedError(pingErr) || strings.Contains(pingErr.Error(), "context deadline exceeded") {
connDiag, err := s.appendDiagnosticTrace(ctx,
connectionDiagnosticID,
types.ConnectionDiagnosticTrace_CONNECTIVITY,
"There was a connection problem between the Database Agent and the Database. "+
"Ensure the Database is running and accessible from the Database Agent.",
"There was a connection problem between the Teleport Database Service and the database. "+
"Ensure the database is running and accessible from the Database Service over the network.",
pingErr,
)
if err != nil {
Expand Down

0 comments on commit 2a107d8

Please sign in to comment.