Skip to content

Commit

Permalink
MGDAPI-5078 - feat: allow applying redis node size changes immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
KevFan committed Jan 23, 2023
1 parent caafba3 commit 695b58e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
6 changes: 4 additions & 2 deletions pkg/client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

// define a function that allows us to perform modification logic on the
// custom resource (e.g. setting owner refs) before creating or updating it
// custom resource (e.g. setting owner refs) before creating it
type modifyResourceFunc func(cr metav1.Object) error

// ReconcileBlobStorage creates or updates a blob storage custom resource
Expand Down Expand Up @@ -93,7 +93,7 @@ func ReconcilePostgres(ctx context.Context, client client.Client, productName, d
}

// ReconcileRedis creates or updates a redis custom resource
func ReconcileRedis(ctx context.Context, client client.Client, productName, deploymentType, tier, name, ns, secretName, secretNs string, applyImmediately bool, modifyFunc modifyResourceFunc) (*v1alpha1.Redis, error) {
func ReconcileRedis(ctx context.Context, client client.Client, productName, deploymentType, tier, name, ns, secretName, secretNs, size string, applyImmediately, maintenanceWindow bool, modifyFunc modifyResourceFunc) (*v1alpha1.Redis, error) {
r := &v1alpha1.Redis{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down Expand Up @@ -121,7 +121,9 @@ func ReconcileRedis(ctx context.Context, client client.Client, productName, depl
Name: secretName,
Namespace: secretNs,
}
r.Spec.Size = size
r.Spec.ApplyImmediately = applyImmediately
r.Spec.MaintenanceWindow = maintenanceWindow

return nil
})
Expand Down
53 changes: 31 additions & 22 deletions pkg/client/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package client
import (
"context"
"errors"
croType "github.com/integr8ly/cloud-resource-operator/apis/integreatly/v1alpha1/types"
"reflect"
"testing"

croType "github.com/integr8ly/cloud-resource-operator/apis/integreatly/v1alpha1/types"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -416,17 +417,19 @@ func TestReconcileRedis(t *testing.T) {
}

type args struct {
ctx context.Context
client client.Client
deploymentType string
tier string
productName string
name string
ns string
secretName string
secretNs string
applyImmediately bool
modifyFunc modifyResourceFunc
ctx context.Context
client client.Client
deploymentType string
tier string
productName string
name string
ns string
secretName string
secretNs string
size string
applyImmediately bool
maintenanceWindow bool
modifyFunc modifyResourceFunc
}
tests := []struct {
name string
Expand Down Expand Up @@ -471,15 +474,18 @@ func TestReconcileRedis(t *testing.T) {
{
name: "test modification function",
args: args{
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme),
deploymentType: "managed",
tier: "production",
productName: "test",
name: "test",
ns: "test",
secretName: "test",
secretNs: "test",
ctx: context.TODO(),
client: fake.NewFakeClientWithScheme(scheme),
deploymentType: "managed",
tier: "production",
productName: "test",
name: "test",
ns: "test",
secretName: "test",
secretNs: "test",
size: "test",
applyImmediately: true,
maintenanceWindow: true,
modifyFunc: func(cr v1.Object) error {
cr.SetLabels(map[string]string{
"cro": "test",
Expand All @@ -503,6 +509,9 @@ func TestReconcileRedis(t *testing.T) {
Name: "test",
Namespace: "test",
},
Size: "test",
ApplyImmediately: true,
MaintenanceWindow: true,
},
},
wantErr: false,
Expand All @@ -529,7 +538,7 @@ func TestReconcileRedis(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ReconcileRedis(tt.args.ctx, tt.args.client, tt.args.productName, tt.args.deploymentType, tt.args.tier, tt.args.name, tt.args.ns, tt.args.secretName, tt.args.secretNs, tt.args.applyImmediately, tt.args.modifyFunc)
got, err := ReconcileRedis(tt.args.ctx, tt.args.client, tt.args.productName, tt.args.deploymentType, tt.args.tier, tt.args.name, tt.args.ns, tt.args.secretName, tt.args.secretNs, tt.args.size, tt.args.applyImmediately, tt.args.maintenanceWindow, tt.args.modifyFunc)
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileRedis() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
5 changes: 3 additions & 2 deletions pkg/providers/aws/provider_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (p *RedisProvider) createElasticacheCluster(ctx context.Context, r *v1alpha

if maintenanceWindow {
// check if any modifications are required to bring the elasticache instance up to date with the strategy map.
modifyInput, err := buildElasticacheUpdateStrategy(ec2Svc, elasticacheConfig, foundCache, replicationGroupClusters, logger)
modifyInput, err := buildElasticacheUpdateStrategy(ec2Svc, elasticacheConfig, foundCache, replicationGroupClusters, logger, r)
if err != nil {
errMsg := "failed to build elasticache modify strategy"
return nil, croType.StatusMessage(errMsg), errorUtil.Wrap(err, errMsg)
Expand Down Expand Up @@ -733,7 +733,7 @@ func (p *RedisProvider) isLastResource(ctx context.Context) (bool, error) {
// if modifications are required, a modify input struct will be returned with all proposed changes.
//
// if no modifications are required, nil will be returned.
func buildElasticacheUpdateStrategy(ec2Client ec2iface.EC2API, elasticacheConfig *elasticache.CreateReplicationGroupInput, foundConfig *elasticache.ReplicationGroup, replicationGroupClusters []elasticache.CacheCluster, logger *logrus.Entry) (*elasticache.ModifyReplicationGroupInput, error) {
func buildElasticacheUpdateStrategy(ec2Client ec2iface.EC2API, elasticacheConfig *elasticache.CreateReplicationGroupInput, foundConfig *elasticache.ReplicationGroup, replicationGroupClusters []elasticache.CacheCluster, logger *logrus.Entry, r *v1alpha1.Redis) (*elasticache.ModifyReplicationGroupInput, error) {
// setup logger.
actionLogger := resources.NewActionLogger(logger, "buildElasticacheUpdateStrategy")
actionLogger.Infof("verifying that %s configuration is as expected", *foundConfig.ReplicationGroupId)
Expand Down Expand Up @@ -788,6 +788,7 @@ func buildElasticacheUpdateStrategy(ec2Client ec2iface.EC2API, elasticacheConfig
// the instance type is supported, go ahead with the modification.
if instanceTypeSupported {
modifyInput.CacheNodeType = elasticacheConfig.CacheNodeType
modifyInput.ApplyImmediately = aws.Bool(r.Spec.ApplyImmediately)
updateFound = true
} else {
// the instance type isn't supported, log and skip.
Expand Down
4 changes: 3 additions & 1 deletion pkg/providers/aws/provider_redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,7 @@ func Test_buildElasticacheUpdateStrategy(t *testing.T) {
foundConfig *elasticache.ReplicationGroup
replicationGroupClusters []elasticache.CacheCluster
logger *logrus.Entry
redis *v1alpha1.Redis
}
tests := []struct {
name string
Expand Down Expand Up @@ -2402,6 +2403,7 @@ func Test_buildElasticacheUpdateStrategy(t *testing.T) {
},
},
logger: testLogger,
redis: &v1alpha1.Redis{},
},
want: &elasticache.ModifyReplicationGroupInput{
CacheNodeType: aws.String("cache.newValue"),
Expand Down Expand Up @@ -2484,7 +2486,7 @@ func Test_buildElasticacheUpdateStrategy(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildElasticacheUpdateStrategy(tt.args.ec2Client, tt.args.elasticacheConfig, tt.args.foundConfig, tt.args.replicationGroupClusters, tt.args.logger)
got, err := buildElasticacheUpdateStrategy(tt.args.ec2Client, tt.args.elasticacheConfig, tt.args.foundConfig, tt.args.replicationGroupClusters, tt.args.logger, tt.args.redis)
if tt.wantErr != "" && err.Error() != tt.wantErr {
t.Errorf("createElasticacheCluster() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 695b58e

Please sign in to comment.