-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
skip databases that are not available during auto discovery #10699
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
85b763f
skip databases that are not available during auto discovery
greedy52 fa5d7b0
allow more status for connections
greedy52 14fe0b6
remove empty line
greedy52 736fde7
use ClusterStatus for redshift
greedy52 523a48d
fix redshift debug print
greedy52 21ff983
fix lint S1008
greedy52 fded9a7
review comments
greedy52 100ded9
Merge branch 'master' into STeve/10647_db_auto_discover_skip_stopped
greedy52 673b7d8
Merge branch 'master' into STeve/10647_db_auto_discover_skip_stopped
greedy52 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,14 +44,17 @@ func TestWatcher(t *testing.T) { | |
rdsInstance2, _ := makeRDSInstance(t, "instance-2", "us-east-2", map[string]string{"env": "prod"}) | ||
rdsInstance3, _ := makeRDSInstance(t, "instance-3", "us-east-1", map[string]string{"env": "dev"}) | ||
rdsInstance4, rdsDatabase4 := makeRDSInstance(t, "instance-4", "us-west-1", nil) | ||
rdsInstanceUnavailable, _ := makeRDSInstance(t, "instance-5", "us-west-1", nil, rdsInstanceStatus("stopped")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding a test that explicitly verifies the behavior against unrecognized status, like "xxx-this-status-does-not-exist"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added
|
||
|
||
auroraCluster1, auroraDatabase1 := makeRDSCluster(t, "cluster-1", "us-east-1", services.RDSEngineModeProvisioned, map[string]string{"env": "prod"}) | ||
auroraCluster1, auroraDatabase1 := makeRDSCluster(t, "cluster-1", "us-east-1", map[string]string{"env": "prod"}) | ||
auroraCluster2, auroraDatabases2 := makeRDSClusterWithExtraEndpoints(t, "cluster-2", "us-east-2", map[string]string{"env": "dev"}) | ||
auroraCluster3, _ := makeRDSCluster(t, "cluster-3", "us-east-2", services.RDSEngineModeProvisioned, map[string]string{"env": "prod"}) | ||
auroraClusterUnsupported, _ := makeRDSCluster(t, "serverless", "us-east-1", services.RDSEngineModeServerless, map[string]string{"env": "prod"}) | ||
auroraCluster3, _ := makeRDSCluster(t, "cluster-3", "us-east-2", map[string]string{"env": "prod"}) | ||
auroraClusterUnsupported, _ := makeRDSCluster(t, "serverless", "us-east-1", nil, rdsClusterEngineMode("serverless")) | ||
auroraClusterUnavailable, _ := makeRDSCluster(t, "serverless", "us-east-1", nil, rdsClusterStatus("creating")) | ||
|
||
redshiftUse1Prod, redshiftDatabaseUse1Prod := makeRedshiftCluster(t, "us-east-1", "prod") | ||
redshiftUse1Dev, _ := makeRedshiftCluster(t, "us-east-1", "dev") | ||
redshiftUse1Unavailable, _ := makeRedshiftCluster(t, "us-east-1", "qa", redshiftClusterStatus("paused")) | ||
|
||
tests := []struct { | ||
name string | ||
|
@@ -60,7 +63,7 @@ func TestWatcher(t *testing.T) { | |
expectedDatabases types.Databases | ||
}{ | ||
{ | ||
name: "rds labels matching", | ||
name: "RDS labels matching", | ||
awsMatchers: []services.AWSMatcher{ | ||
{ | ||
Types: []string{services.AWSMatcherRDS}, | ||
|
@@ -88,7 +91,7 @@ func TestWatcher(t *testing.T) { | |
expectedDatabases: append(types.Databases{rdsDatabase1, auroraDatabase1}, auroraDatabases2...), | ||
}, | ||
{ | ||
name: "rds aurora unsupported", | ||
name: "RDS unsupported databases are skipped", | ||
awsMatchers: []services.AWSMatcher{{ | ||
Types: []string{services.AWSMatcherRDS}, | ||
Regions: []string{"us-east-1"}, | ||
|
@@ -103,6 +106,21 @@ func TestWatcher(t *testing.T) { | |
}, | ||
expectedDatabases: types.Databases{auroraDatabase1}, | ||
}, | ||
{ | ||
name: "RDS unavailable databases are skipped", | ||
awsMatchers: []services.AWSMatcher{{ | ||
Types: []string{services.AWSMatcherRDS}, | ||
Regions: []string{"us-east-1"}, | ||
Tags: types.Labels{"*": []string{"*"}}, | ||
}}, | ||
clients: &common.TestCloudClients{ | ||
RDS: &cloud.RDSMock{ | ||
DBInstances: []*rds.DBInstance{rdsInstance1, rdsInstanceUnavailable}, | ||
DBClusters: []*rds.DBCluster{auroraCluster1, auroraClusterUnavailable}, | ||
}, | ||
}, | ||
expectedDatabases: types.Databases{rdsDatabase1, auroraDatabase1}, | ||
}, | ||
{ | ||
name: "skip access denied errors", | ||
awsMatchers: []services.AWSMatcher{{ | ||
|
@@ -126,7 +144,7 @@ func TestWatcher(t *testing.T) { | |
expectedDatabases: types.Databases{rdsDatabase4, auroraDatabase1}, | ||
}, | ||
{ | ||
name: "redshift", | ||
name: "Redshift labels matching", | ||
awsMatchers: []services.AWSMatcher{ | ||
{ | ||
Types: []string{services.AWSMatcherRedshift}, | ||
|
@@ -141,6 +159,22 @@ func TestWatcher(t *testing.T) { | |
}, | ||
expectedDatabases: types.Databases{redshiftDatabaseUse1Prod}, | ||
}, | ||
{ | ||
name: "Redshift unavailable databases are skipped", | ||
awsMatchers: []services.AWSMatcher{ | ||
{ | ||
Types: []string{services.AWSMatcherRedshift}, | ||
Regions: []string{"us-east-1"}, | ||
Tags: types.Labels{"*": []string{"*"}}, | ||
}, | ||
}, | ||
clients: &common.TestCloudClients{ | ||
Redshift: &cloud.RedshiftMock{ | ||
Clusters: []*redshift.Cluster{redshiftUse1Prod, redshiftUse1Unavailable}, | ||
}, | ||
}, | ||
expectedDatabases: types.Databases{redshiftDatabaseUse1Prod}, | ||
}, | ||
{ | ||
name: "matcher with multiple types", | ||
awsMatchers: []services.AWSMatcher{ | ||
|
@@ -178,43 +212,54 @@ func TestWatcher(t *testing.T) { | |
} | ||
} | ||
|
||
func makeRDSInstance(t *testing.T, name, region string, labels map[string]string) (*rds.DBInstance, types.Database) { | ||
func makeRDSInstance(t *testing.T, name, region string, labels map[string]string, opts ...func(*rds.DBInstance)) (*rds.DBInstance, types.Database) { | ||
instance := &rds.DBInstance{ | ||
DBInstanceArn: aws.String(fmt.Sprintf("arn:aws:rds:%v:1234567890:db:%v", region, name)), | ||
DBInstanceIdentifier: aws.String(name), | ||
DbiResourceId: aws.String(uuid.New().String()), | ||
Engine: aws.String(services.RDSEnginePostgres), | ||
DBInstanceStatus: aws.String("available"), | ||
Endpoint: &rds.Endpoint{ | ||
Address: aws.String("localhost"), | ||
Port: aws.Int64(5432), | ||
}, | ||
TagList: labelsToTags(labels), | ||
} | ||
for _, opt := range opts { | ||
opt(instance) | ||
} | ||
|
||
database, err := services.NewDatabaseFromRDSInstance(instance) | ||
require.NoError(t, err) | ||
return instance, database | ||
} | ||
|
||
func makeRDSCluster(t *testing.T, name, region, engineMode string, labels map[string]string) (*rds.DBCluster, types.Database) { | ||
func makeRDSCluster(t *testing.T, name, region string, labels map[string]string, opts ...func(*rds.DBCluster)) (*rds.DBCluster, types.Database) { | ||
cluster := &rds.DBCluster{ | ||
DBClusterArn: aws.String(fmt.Sprintf("arn:aws:rds:%v:1234567890:cluster:%v", region, name)), | ||
DBClusterIdentifier: aws.String(name), | ||
DbClusterResourceId: aws.String(uuid.New().String()), | ||
Engine: aws.String(services.RDSEngineAuroraMySQL), | ||
EngineMode: aws.String(engineMode), | ||
EngineMode: aws.String(services.RDSEngineModeProvisioned), | ||
Status: aws.String("available"), | ||
Endpoint: aws.String("localhost"), | ||
Port: aws.Int64(3306), | ||
TagList: labelsToTags(labels), | ||
} | ||
for _, opt := range opts { | ||
opt(cluster) | ||
} | ||
|
||
database, err := services.NewDatabaseFromRDSCluster(cluster) | ||
require.NoError(t, err) | ||
return cluster, database | ||
} | ||
|
||
func makeRedshiftCluster(t *testing.T, region, env string) (*redshift.Cluster, types.Database) { | ||
func makeRedshiftCluster(t *testing.T, region, env string, opts ...func(*redshift.Cluster)) (*redshift.Cluster, types.Database) { | ||
cluster := &redshift.Cluster{ | ||
ClusterIdentifier: aws.String(env), | ||
ClusterNamespaceArn: aws.String(fmt.Sprintf("arn:aws:redshift:%s:1234567890:namespace:%s", region, env)), | ||
ClusterStatus: aws.String("available"), | ||
Endpoint: &redshift.Endpoint{ | ||
Address: aws.String("localhost"), | ||
Port: aws.Int64(5439), | ||
|
@@ -224,6 +269,10 @@ func makeRedshiftCluster(t *testing.T, region, env string) (*redshift.Cluster, t | |
Value: aws.String(env), | ||
}}, | ||
} | ||
for _, opt := range opts { | ||
opt(cluster) | ||
} | ||
|
||
database, err := services.NewDatabaseFromRedshiftCluster(cluster) | ||
require.NoError(t, err) | ||
return cluster, database | ||
|
@@ -236,6 +285,7 @@ func makeRDSClusterWithExtraEndpoints(t *testing.T, name, region string, labels | |
DbClusterResourceId: aws.String(uuid.New().String()), | ||
Engine: aws.String(services.RDSEngineAuroraMySQL), | ||
EngineMode: aws.String(services.RDSEngineModeProvisioned), | ||
Status: aws.String("available"), | ||
Endpoint: aws.String("localhost"), | ||
ReaderEndpoint: aws.String("reader.host"), | ||
Port: aws.Int64(3306), | ||
|
@@ -259,6 +309,34 @@ func makeRDSClusterWithExtraEndpoints(t *testing.T, name, region string, labels | |
return cluster, append(types.Databases{primaryDatabase, readerDatabase}, customDatabases...) | ||
} | ||
|
||
// rdsInstanceStatus returns an option function for makeRDSInstance to overwrite status. | ||
func rdsInstanceStatus(status string) func(*rds.DBInstance) { | ||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(instance *rds.DBInstance) { | ||
instance.DBInstanceStatus = aws.String(status) | ||
} | ||
} | ||
|
||
// rdsClusterEngineMode returns an option function for makeRDSCluster to overwrite engine mode. | ||
func rdsClusterEngineMode(mode string) func(*rds.DBCluster) { | ||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return func(cluster *rds.DBCluster) { | ||
cluster.EngineMode = aws.String(mode) | ||
} | ||
} | ||
|
||
// rdsClusterStatus returns an option function for makeRDSCluster to overwrite status. | ||
func rdsClusterStatus(status string) func(*rds.DBCluster) { | ||
return func(cluster *rds.DBCluster) { | ||
cluster.Status = aws.String(status) | ||
} | ||
} | ||
|
||
// redshiftClusterStatus returns an option function for makeRedshiftCluster to overwrite status. | ||
func redshiftClusterStatus(status string) func(*redshift.Cluster) { | ||
return func(cluster *redshift.Cluster) { | ||
cluster.ClusterStatus = aws.String(status) | ||
} | ||
} | ||
|
||
func labelsToTags(labels map[string]string) (tags []*rds.Tag) { | ||
for key, val := range labels { | ||
tags = append(tags, &rds.Tag{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.