Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Aurora instances per cluster #31846

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 41 additions & 36 deletions pkg/databasemonitoring/aws/aurora.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,50 @@ func (c *Client) GetAuroraClusterEndpoints(ctx context.Context, dbClusterIdentif
if len(dbClusterIdentifiers) == 0 {
return nil, fmt.Errorf("at least one database cluster identifier is required")
}
clusterInstances, err := c.client.DescribeDBInstances(ctx,
&rds.DescribeDBInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("db-cluster-id"),
Values: dbClusterIdentifiers,
},
},
})
if err != nil {
return nil, fmt.Errorf("error running GetAuroraClusterEndpoints %v", err)
}
clusters := make(map[string]*AuroraCluster, 0)
for _, db := range clusterInstances.DBInstances {
if db.Endpoint != nil && db.DBClusterIdentifier != nil {
if db.Endpoint.Address == nil || db.DBInstanceStatus == nil || strings.ToLower(*db.DBInstanceStatus) != "available" {
continue
}
// Add to list of instances for the cluster
instance := &Instance{
Endpoint: *db.Endpoint.Address,
}
// Set if IAM is configured for the endpoint
if db.IAMDatabaseAuthenticationEnabled != nil {
instance.IamEnabled = *db.IAMDatabaseAuthenticationEnabled
}
// Set the port, if it is known
if db.Endpoint.Port != nil {
instance.Port = *db.Endpoint.Port
}
if db.Engine != nil {
instance.Engine = *db.Engine
}
if _, ok := clusters[*db.DBClusterIdentifier]; !ok {
clusters[*db.DBClusterIdentifier] = &AuroraCluster{
Instances: make([]*Instance, 0),

for _, clusterId := range dbClusterIdentifiers {
// TODO: Seth Samuel: This method is not paginated, so if there are more than 100 instances in a cluster, we will only get the first 100
// We should add pagination support to this method at some point
clusterInstances, err := c.client.DescribeDBInstances(ctx,
&rds.DescribeDBInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("db-cluster-id"),
Values: []string{clusterId},
},
},
})
if err != nil {
return nil, fmt.Errorf("error running GetAuroraClusterEndpoints %v", err)
}
for _, db := range clusterInstances.DBInstances {
if db.Endpoint != nil && db.DBClusterIdentifier != nil {
if db.Endpoint.Address == nil || db.DBInstanceStatus == nil || strings.ToLower(*db.DBInstanceStatus) != "available" {
continue
}
// Add to list of instances for the cluster
instance := &Instance{
Endpoint: *db.Endpoint.Address,
}
// Set if IAM is configured for the endpoint
if db.IAMDatabaseAuthenticationEnabled != nil {
instance.IamEnabled = *db.IAMDatabaseAuthenticationEnabled
}
// Set the port, if it is known
if db.Endpoint.Port != nil {
instance.Port = *db.Endpoint.Port
}
if db.Engine != nil {
instance.Engine = *db.Engine
}
if _, ok := clusters[*db.DBClusterIdentifier]; !ok {
clusters[*db.DBClusterIdentifier] = &AuroraCluster{
Instances: make([]*Instance, 0),
}
}
clusters[*db.DBClusterIdentifier].Instances = append(clusters[*db.DBClusterIdentifier].Instances, instance)
}
clusters[*db.DBClusterIdentifier].Instances = append(clusters[*db.DBClusterIdentifier].Instances, instance)
}
}
if len(clusters) == 0 {
Expand Down
12 changes: 10 additions & 2 deletions pkg/databasemonitoring/aws/aurora_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
{
name: "multiple cluster ids returns single endpoint from API",
configureClient: func(k *MockrdsService) {
k.EXPECT().DescribeDBInstances(gomock.Any(), createDescribeDBInstancesRequest([]string{"test-cluster", "test-cluster-2"})).Return(&rds.DescribeDBInstancesOutput{
k.EXPECT().DescribeDBInstances(gomock.Any(), createDescribeDBInstancesRequest([]string{"test-cluster"})).Return(&rds.DescribeDBInstancesOutput{
DBInstances: []types.DBInstance{
{
Endpoint: &types.Endpoint{
Expand All @@ -226,6 +226,9 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
},
},
}, nil).Times(1)
k.EXPECT().DescribeDBInstances(gomock.Any(), createDescribeDBInstancesRequest([]string{"test-cluster-2"})).Return(&rds.DescribeDBInstancesOutput{
DBInstances: []types.DBInstance{},
}, nil).Times(1)
},
clusterIDs: []string{"test-cluster", "test-cluster-2"},
expectedAuroraClusterEndpoints: map[string]*AuroraCluster{
Expand All @@ -244,7 +247,7 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
{
name: "multiple cluster ids returns many endpoints from API",
configureClient: func(k *MockrdsService) {
k.EXPECT().DescribeDBInstances(gomock.Any(), createDescribeDBInstancesRequest([]string{"test-cluster", "test-cluster-2"})).Return(&rds.DescribeDBInstancesOutput{
k.EXPECT().DescribeDBInstances(gomock.Any(), createDescribeDBInstancesRequest([]string{"test-cluster"})).Return(&rds.DescribeDBInstancesOutput{
DBInstances: []types.DBInstance{
{
Endpoint: &types.Endpoint{
Expand All @@ -268,6 +271,11 @@ func TestGetAuroraClusterEndpoints(t *testing.T) {
DBInstanceStatus: aws.String("available"),
Engine: aws.String("aurora-postgresql"),
},
},
}, nil).Times(1)

k.EXPECT().DescribeDBInstances(gomock.Any(), createDescribeDBInstancesRequest([]string{"test-cluster-2"})).Return(&rds.DescribeDBInstancesOutput{
DBInstances: []types.DBInstance{
{
Endpoint: &types.Endpoint{
Address: aws.String("test-endpoint-3"),
Expand Down
Loading