Skip to content

Commit

Permalink
fix(rds/dbcluster): Add check for ServerlessV2ScalingConfiguration
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Blatt (external expert on behalf of DB Netz) <[email protected]>
  • Loading branch information
MisterMX committed Dec 11, 2023
1 parent 4124fb7 commit 46f4fe3
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 27 deletions.
6 changes: 6 additions & 0 deletions pkg/controller/rds/dbcluster/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,8 @@ func generateRestoreDBClusterToPointInTimeInput(cr *svcapitypes.DBCluster) *svcs
}

func (e *custom) isUpToDate(ctx context.Context, cr *svcapitypes.DBCluster, out *svcsdk.DescribeDBClustersOutput) (bool, string, error) { //nolint:gocyclo
current := GenerateDBCluster(out)

status := pointer.StringValue(out.DBClusters[0].Status)
if status == "modifying" || status == "upgrading" || status == "configuring-iam-database-auth" || status == "migrating" || status == "prepairing-data-migration" || status == "creating" {
return true, "", nil
Expand Down Expand Up @@ -619,6 +621,10 @@ func (e *custom) isUpToDate(ctx context.Context, cr *svcapitypes.DBCluster, out
return false, "", err
}

if diff := cmp.Diff(cr.Spec.ForProvider.ServerlessV2ScalingConfiguration, current.Spec.ForProvider.ServerlessV2ScalingConfiguration); diff != "" {
return false, "ServerlessV2ScalingConfiguration: " + diff, nil
}

add, remove := DiffTags(cr.Spec.ForProvider.Tags, out.DBClusters[0].TagList)
if len(add) > 0 || len(remove) > 0 {
return false, "", nil
Expand Down
177 changes: 150 additions & 27 deletions pkg/controller/rds/dbcluster/setup_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package dbcluster

import (
"context"
_ "embed"
"testing"

svcsdk "github.com/aws/aws-sdk-go/service/rds"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"

svcapitypes "github.com/crossplane-contrib/provider-aws/apis/rds/v1alpha1"
)

func ptr(str string) *string {
return &str
}

func TestIsVPCSecurityGroupIDsUpToDate(t *testing.T) {
type args struct {
cr *svcapitypes.DBCluster
Expand Down Expand Up @@ -43,13 +44,13 @@ func TestIsVPCSecurityGroupIDsUpToDate(t *testing.T) {
{
VpcSecurityGroups: []*svcsdk.VpcSecurityGroupMembership{
{
VpcSecurityGroupId: ptr("sg-123"),
VpcSecurityGroupId: ptr.To("sg-123"),
},
{
VpcSecurityGroupId: ptr("sg-456"),
VpcSecurityGroupId: ptr.To("sg-456"),
},
{
VpcSecurityGroupId: ptr("sg-789"),
VpcSecurityGroupId: ptr.To("sg-789"),
},
},
},
Expand All @@ -76,10 +77,10 @@ func TestIsVPCSecurityGroupIDsUpToDate(t *testing.T) {
{
VpcSecurityGroups: []*svcsdk.VpcSecurityGroupMembership{
{
VpcSecurityGroupId: ptr("sg-456"),
VpcSecurityGroupId: ptr.To("sg-456"),
},
{
VpcSecurityGroupId: ptr("sg-123"),
VpcSecurityGroupId: ptr.To("sg-123"),
},
},
},
Expand Down Expand Up @@ -129,10 +130,10 @@ func TestIsVPCSecurityGroupIDsUpToDate(t *testing.T) {
{
VpcSecurityGroups: []*svcsdk.VpcSecurityGroupMembership{
{
VpcSecurityGroupId: ptr("sg-456"),
VpcSecurityGroupId: ptr.To("sg-456"),
},
{
VpcSecurityGroupId: ptr("sg-123"),
VpcSecurityGroupId: ptr.To("sg-123"),
},
},
},
Expand All @@ -159,10 +160,10 @@ func TestIsVPCSecurityGroupIDsUpToDate(t *testing.T) {
{
VpcSecurityGroups: []*svcsdk.VpcSecurityGroupMembership{
{
VpcSecurityGroupId: ptr("sg-123"),
VpcSecurityGroupId: ptr.To("sg-123"),
},
{
VpcSecurityGroupId: ptr("sg-456"),
VpcSecurityGroupId: ptr.To("sg-456"),
},
},
},
Expand Down Expand Up @@ -237,7 +238,7 @@ func TestIsEngineVersionUpToDate(t *testing.T) {
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
EngineVersion: ptr("12.3"), // some AWS "default" value
EngineVersion: ptr.To("12.3"), // some AWS "default" value
},
},
},
Expand All @@ -252,15 +253,15 @@ func TestIsEngineVersionUpToDate(t *testing.T) {
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
CustomDBClusterParameters: svcapitypes.CustomDBClusterParameters{
EngineVersion: ptr("12.3"),
EngineVersion: ptr.To("12.3"),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
EngineVersion: ptr("12.3"), // some AWS "default" value
EngineVersion: ptr.To("12.3"), // some AWS "default" value
},
},
},
Expand All @@ -275,15 +276,15 @@ func TestIsEngineVersionUpToDate(t *testing.T) {
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
CustomDBClusterParameters: svcapitypes.CustomDBClusterParameters{
EngineVersion: ptr("12"),
EngineVersion: ptr.To("12"),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
EngineVersion: ptr("12.3"), // some AWS "default" value
EngineVersion: ptr.To("12.3"), // some AWS "default" value
},
},
},
Expand All @@ -298,15 +299,15 @@ func TestIsEngineVersionUpToDate(t *testing.T) {
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
CustomDBClusterParameters: svcapitypes.CustomDBClusterParameters{
EngineVersion: ptr("12.1"),
EngineVersion: ptr.To("12.1"),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
EngineVersion: ptr("12.3"), // some AWS "default" value
EngineVersion: ptr.To("12.3"), // some AWS "default" value
},
},
},
Expand All @@ -321,15 +322,15 @@ func TestIsEngineVersionUpToDate(t *testing.T) {
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
CustomDBClusterParameters: svcapitypes.CustomDBClusterParameters{
EngineVersion: ptr("13"),
EngineVersion: ptr.To("13"),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
EngineVersion: ptr("12.3"),
EngineVersion: ptr.To("12.3"),
},
},
},
Expand Down Expand Up @@ -398,7 +399,7 @@ func TestIsDBClusterParameterGroupNameUpToDate(t *testing.T) {
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
DBClusterParameterGroup: ptr("default.aurora-postgresql14"), // some AWS "default" value
DBClusterParameterGroup: ptr.To("default.aurora-postgresql14"), // some AWS "default" value
},
},
},
Expand All @@ -412,14 +413,14 @@ func TestIsDBClusterParameterGroupNameUpToDate(t *testing.T) {
cr: &svcapitypes.DBCluster{
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
DBClusterParameterGroupName: ptr("default.aurora-postgresql14"),
DBClusterParameterGroupName: ptr.To("default.aurora-postgresql14"),
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
DBClusterParameterGroup: ptr("default.aurora-postgresql14"), // some AWS "default" value
DBClusterParameterGroup: ptr.To("default.aurora-postgresql14"), // some AWS "default" value
},
},
},
Expand All @@ -433,14 +434,14 @@ func TestIsDBClusterParameterGroupNameUpToDate(t *testing.T) {
cr: &svcapitypes.DBCluster{
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
DBClusterParameterGroupName: ptr("default.aurora-postgresql15"),
DBClusterParameterGroupName: ptr.To("default.aurora-postgresql15"),
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
DBClusterParameterGroup: ptr("default.aurora-postgresql14"),
DBClusterParameterGroup: ptr.To("default.aurora-postgresql14"),
},
},
},
Expand All @@ -461,3 +462,125 @@ func TestIsDBClusterParameterGroupNameUpToDate(t *testing.T) {
})
}
}

func TestIsUpToDate(t *testing.T) {
type args struct {
cr *svcapitypes.DBCluster
out *svcsdk.DescribeDBClustersOutput
kube client.Client
}

type want struct {
isUpToDate bool
err error
}

cases := map[string]struct {
args
want
}{
"DifferentServerlessV2ScalingConfigurationMinimum": {
args: args{
kube: test.NewMockClient(),
cr: &svcapitypes.DBCluster{
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
DBClusterParameterGroupName: ptr.To("default.aurora-postgresql15"),
ServerlessV2ScalingConfiguration: &svcapitypes.ServerlessV2ScalingConfiguration{
MaxCapacity: ptr.To[float64](5.0),
MinCapacity: ptr.To[float64](1.0),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
DBClusterParameterGroup: ptr.To("default.aurora-postgresql14"),
ServerlessV2ScalingConfiguration: &svcsdk.ServerlessV2ScalingConfigurationInfo{
MaxCapacity: ptr.To[float64](5.0),
MinCapacity: ptr.To[float64](3.0),
},
},
},
},
},
want: want{
isUpToDate: false,
},
},
"DifferentServerlessV2ScalingConfigurationMaximum": {
args: args{
kube: test.NewMockClient(),
cr: &svcapitypes.DBCluster{
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
DBClusterParameterGroupName: ptr.To("default.aurora-postgresql15"),
ServerlessV2ScalingConfiguration: &svcapitypes.ServerlessV2ScalingConfiguration{
MaxCapacity: ptr.To[float64](5.0),
MinCapacity: ptr.To[float64](1.0),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
DBClusterParameterGroup: ptr.To("default.aurora-postgresql14"),
ServerlessV2ScalingConfiguration: &svcsdk.ServerlessV2ScalingConfigurationInfo{
MaxCapacity: ptr.To[float64](10.0),
MinCapacity: ptr.To[float64](1.0),
},
},
},
},
},
want: want{
isUpToDate: false,
},
},
"SameServerlessV2ScalingConfiguration": {
args: args{
kube: test.NewMockClient(),
cr: &svcapitypes.DBCluster{
Spec: svcapitypes.DBClusterSpec{
ForProvider: svcapitypes.DBClusterParameters{
DBClusterParameterGroupName: ptr.To("default.aurora-postgresql15"),
ServerlessV2ScalingConfiguration: &svcapitypes.ServerlessV2ScalingConfiguration{
MaxCapacity: ptr.To[float64](5.0),
MinCapacity: ptr.To[float64](1.0),
},
},
},
},
out: &svcsdk.DescribeDBClustersOutput{
DBClusters: []*svcsdk.DBCluster{
{
DBClusterParameterGroup: ptr.To("default.aurora-postgresql14"),
ServerlessV2ScalingConfiguration: &svcsdk.ServerlessV2ScalingConfigurationInfo{
MaxCapacity: ptr.To[float64](5.0),
MinCapacity: ptr.To[float64](1.0),
},
},
},
},
},
want: want{
isUpToDate: false,
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
custom := custom{kube: tc.args.kube}
isUpToDate, _, err := custom.isUpToDate(context.Background(), tc.args.cr, tc.args.out)
if diff := cmp.Diff(tc.want.isUpToDate, isUpToDate); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
if diff := cmp.Diff(tc.want.err, err); diff != "" {
t.Errorf("error: -want, +got:\n%s", diff)
}
})
}
}

0 comments on commit 46f4fe3

Please sign in to comment.