Skip to content

Commit

Permalink
Merge pull request #1404 from sparebank1utvikling/replicationgroup-diff
Browse files Browse the repository at this point in the history
replicationgroup: Add reason for diff on update
  • Loading branch information
haarchri authored Aug 8, 2022
2 parents 220409e + faa67e8 commit cda22ce
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
39 changes: 21 additions & 18 deletions pkg/clients/elasticache/elasticache.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,28 @@ func ReplicationGroupShardConfigurationNeedsUpdate(kube v1beta1.ReplicationGroup

// ReplicationGroupNeedsUpdate returns true if the supplied ReplicationGroup and
// the configuration of its member clusters differ from given desired state.
func ReplicationGroupNeedsUpdate(kube v1beta1.ReplicationGroupParameters, rg elasticachetypes.ReplicationGroup, ccList []elasticachetypes.CacheCluster) bool {
func ReplicationGroupNeedsUpdate(kube v1beta1.ReplicationGroupParameters, rg elasticachetypes.ReplicationGroup, ccList []elasticachetypes.CacheCluster) string {
switch {
case !reflect.DeepEqual(kube.AutomaticFailoverEnabled, automaticFailoverEnabled(rg.AutomaticFailover)):
return true
return "AutomaticFailover"
case !reflect.DeepEqual(&kube.CacheNodeType, rg.CacheNodeType):
return true
return "CacheNotType"
case !reflect.DeepEqual(kube.SnapshotRetentionLimit, clients.IntFrom32Address(rg.SnapshotRetentionLimit)):
return true
return "SnapshotRetentionLimit"
case !reflect.DeepEqual(kube.SnapshotWindow, rg.SnapshotWindow):
return true
return "SnapshotWindow"
case aws.ToBool(kube.MultiAZEnabled) != aws.ToBool(multiAZEnabled(rg.MultiAZ)):
return true
return "MultiAZ"
case ReplicationGroupNumCacheClustersNeedsUpdate(kube, ccList):
return true
return "NumCacheClusters"
}

for _, cc := range ccList {
if cacheClusterNeedsUpdate(kube, cc) {
return true
if reason := cacheClusterNeedsUpdate(kube, cc); reason != "" {
return reason
}
}
return false
return ""
}

func automaticFailoverEnabled(af elasticachetypes.AutomaticFailoverStatus) *bool {
Expand Down Expand Up @@ -403,30 +403,33 @@ func versionMatches(kubeVersion *string, awsVersion *string) bool { //nolint: go
return true
}

func cacheClusterNeedsUpdate(kube v1beta1.ReplicationGroupParameters, cc elasticachetypes.CacheCluster) bool { // nolint:gocyclo
func cacheClusterNeedsUpdate(kube v1beta1.ReplicationGroupParameters, cc elasticachetypes.CacheCluster) string { // nolint:gocyclo
// AWS will set and return a default version if we don't specify one.
if !versionMatches(kube.EngineVersion, cc.EngineVersion) {
return true
return "EngineVersion"
}
if pg, name := cc.CacheParameterGroup, kube.CacheParameterGroupName; pg != nil && !reflect.DeepEqual(name, pg.CacheParameterGroupName) {
return true
return "CacheParameterGroup"
}
if cc.NotificationConfiguration != nil {
if !reflect.DeepEqual(kube.NotificationTopicARN, cc.NotificationConfiguration.TopicArn) {
return true
return "NoticationTopicARN"
}
if !reflect.DeepEqual(cc.NotificationConfiguration.TopicStatus, kube.NotificationTopicStatus) {
return true
return "TopicStatus"
}
} else if clients.StringValue(kube.NotificationTopicARN) != "" {
return true
return "NotificationTopicARN"
}
// AWS will normalize preferred maintenance windows to lowercase
if !strings.EqualFold(clients.StringValue(kube.PreferredMaintenanceWindow),
clients.StringValue(cc.PreferredMaintenanceWindow)) {
return true
return "PreferredMaintainenceWindow"
}
if sgIDsNeedUpdate(kube.SecurityGroupIDs, cc.SecurityGroups) || sgNamesNeedUpdate(kube.CacheSecurityGroupNames, cc.CacheSecurityGroups) {
return "SecurityGroups"
}
return sgIDsNeedUpdate(kube.SecurityGroupIDs, cc.SecurityGroups) || sgNamesNeedUpdate(kube.CacheSecurityGroupNames, cc.CacheSecurityGroups)
return ""
}

func sgIDsNeedUpdate(kube []string, cc []elasticachetypes.SecurityGroupMembership) bool {
Expand Down
4 changes: 2 additions & 2 deletions pkg/clients/elasticache/elasticache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ func TestReplicationGroupNeedsUpdate(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := ReplicationGroupNeedsUpdate(*tc.kube, tc.rg, tc.ccList)
got := ReplicationGroupNeedsUpdate(*tc.kube, tc.rg, tc.ccList) != ""
if got != tc.want {
t.Errorf("ReplicationGroupNeedsUpdate(...): want %t, got %t", tc.want, got)
}
Expand Down Expand Up @@ -1019,7 +1019,7 @@ func TestCacheClusterNeedsUpdate(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := cacheClusterNeedsUpdate(*tc.kube, tc.cc)
got := cacheClusterNeedsUpdate(*tc.kube, tc.cc) != ""
if got != tc.want {
t.Errorf("cacheClusterNeedsUpdate(...): want %t, got %t", tc.want, got)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/cache/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
tagsNeedUpdate = elasticache.ReplicationGroupTagsNeedsUpdate(cr.Spec.ForProvider.Tags, tags.TagList)
}

rgDiff := elasticache.ReplicationGroupNeedsUpdate(cr.Spec.ForProvider, rg, ccList)
return managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: !elasticache.ReplicationGroupNeedsUpdate(cr.Spec.ForProvider, rg, ccList) &&
ResourceUpToDate: rgDiff == "" &&
!elasticache.ReplicationGroupShardConfigurationNeedsUpdate(cr.Spec.ForProvider, rg) &&
!tagsNeedUpdate,
ConnectionDetails: elasticache.ConnectionEndpoint(rg),
Diff: rgDiff,
}, nil
}

Expand Down Expand Up @@ -241,7 +243,7 @@ func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalUpdate{}, nil
}

if elasticache.ReplicationGroupNeedsUpdate(cr.Spec.ForProvider, rg, ccList) {
if diff := elasticache.ReplicationGroupNeedsUpdate(cr.Spec.ForProvider, rg, ccList); diff != "" {
_, err = e.client.ModifyReplicationGroup(ctx, elasticache.NewModifyReplicationGroupInput(cr.Spec.ForProvider, meta.GetExternalName(cr)))
if err != nil {
return managed.ExternalUpdate{}, awsclient.Wrap(err, errModifyReplicationGroup)
Expand Down

0 comments on commit cda22ce

Please sign in to comment.