Skip to content

Commit

Permalink
Update DBConnectionTest to only return an error
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Shen <[email protected]>
  • Loading branch information
mjlshen committed Jun 4, 2024
1 parent e66dc55 commit d96a7e6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions frontend/pkg/database/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func NewCache() DBClient {
}
}

func (c *Cache) DBConnectionTest(ctx context.Context) (string, error) {
return "using cache", nil
func (c *Cache) DBConnectionTest(ctx context.Context) error {
return nil
}

func (c *Cache) GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, error) {
Expand Down
25 changes: 13 additions & 12 deletions frontend/pkg/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
Expand All @@ -18,8 +19,11 @@ const (

var ErrNotFound = errors.New("DocumentNotFound")

// DBClient is a document store for frontend to perform required CRUD operations against
type DBClient interface {
DBConnectionTest(ctx context.Context) (string, error)
// DBConnectionTest is used to health check the database. If the database is not reachable or otherwise not ready
// to be used, an error should be returned.
DBConnectionTest(ctx context.Context) error

GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, error)
SetClusterDoc(ctx context.Context, doc *HCPOpenShiftClusterDocument) error
Expand Down Expand Up @@ -76,23 +80,20 @@ func NewCosmosDBClient(config *CosmosDBConfig) (DBClient, error) {
}

// DBConnectionTest checks the async database is accessible on startup
func (d *CosmosDBClient) DBConnectionTest(ctx context.Context) (string, error) {
if d.config.DBName == "none" || d.config.DBName == "" {
return "No database configured, skipping", nil
}

func (d *CosmosDBClient) DBConnectionTest(ctx context.Context) error {
database, err := d.client.NewDatabase(d.config.DBName)
if err != nil {
return "", err
return fmt.Errorf("failed to create Cosmos database client during healthcheck: %v", err)
}
result, err := database.Read(ctx, nil)
if err != nil {
return "", err

if _, err := database.Read(ctx, nil); err != nil {
return fmt.Errorf("failed to read Cosmos database information during healthcheck: %v", err)
}
return result.DatabaseProperties.ID, nil

return nil
}

// GetClusterDoc retreives a cluster document from async DB using resource ID
// GetClusterDoc retrieves a cluster document from async DB using resource ID
func (d *CosmosDBClient) GetClusterDoc(ctx context.Context, resourceID string, partitionKey string) (*HCPOpenShiftClusterDocument, error) {
container, err := d.client.NewContainer(d.config.DBName, clustersContainer)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions frontend/pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,11 @@ func (f *Frontend) Join() {

func (f *Frontend) CheckReady(ctx context.Context) bool {
// Verify the DB is available and accessible
result, err := f.dbClient.DBConnectionTest(ctx)
if err != nil {
f.logger.Error(fmt.Sprintf("Database test failed to fetch properties: %v", err))
if err := f.dbClient.DBConnectionTest(ctx); err != nil {
f.logger.Error(fmt.Sprintf("Database test failed: %v", err))
return false
}
f.logger.Debug(fmt.Sprintf("Database check completed - %s", result))
f.logger.Debug("Database check completed")

return f.ready.Load().(bool)
}
Expand Down

0 comments on commit d96a7e6

Please sign in to comment.