Skip to content

Commit

Permalink
Fixes flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrie Chen committed Jan 31, 2023
1 parent d2483d2 commit 6a8b7ed
Show file tree
Hide file tree
Showing 22 changed files with 291 additions and 298 deletions.
2 changes: 1 addition & 1 deletion pkg/resource/acl/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func computeTagsDelta(
if !equalStrings(latestElement.Value, desiredElement.Value) {
addedOrUpdated = append(addedOrUpdated, desiredElement)
}
continue
}
continue
}
removed = append(removed, latestElement.Key)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/resource/cluster/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ func computeTagsDelta(
if !equalStrings(latestElement.Value, desiredElement.Value) {
addedOrUpdated = append(addedOrUpdated, desiredElement)
}
continue
}
continue
}
removed = append(removed, latestElement.Key)
}
Expand Down
68 changes: 38 additions & 30 deletions pkg/resource/parameter_group/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package parameter_group

import (
"context"
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"

ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
Expand Down Expand Up @@ -173,13 +174,7 @@ func (rm *resourceManager) getTags(
if err != nil {
return nil, err
}
tags := make([]*svcapitypes.Tag, 0, len(resp.TagList))
for _, tag := range resp.TagList {
tags = append(tags, &svcapitypes.Tag{
Key: tag.Key,
Value: tag.Value,
})
}
tags := resourceTagsFromSDKTags(resp.TagList)
return tags, nil
}

Expand Down Expand Up @@ -235,34 +230,27 @@ func (rm *resourceManager) updateTags(
func computeTagsDelta(
desired []*svcapitypes.Tag,
latest []*svcapitypes.Tag,
) (added []*svcapitypes.Tag, removed []*string) {
toDelete := []*string{}
toAdd := []*svcapitypes.Tag{}

desiredTags := map[string]string{}
key := ""
value := ""
for _, tag := range desired {
if tag.Key != nil {
key = *tag.Key
value = ""
if tag.Value != nil {
value = *tag.Value
) (addedOrUpdated []*svcapitypes.Tag, removed []*string) {
var visitedIndexes []string

for _, latestElement := range latest {
visitedIndexes = append(visitedIndexes, *latestElement.Key)
for _, desiredElement := range desired {
if equalStrings(latestElement.Key, desiredElement.Key) {
if !equalStrings(latestElement.Value, desiredElement.Value) {
addedOrUpdated = append(addedOrUpdated, desiredElement)
}
}
desiredTags[key] = value
continue
}
removed = append(removed, latestElement.Key)
}

for _, tag := range desired {
toAdd = append(toAdd, tag)
}
for _, tag := range latest {
_, ok := desiredTags[*tag.Key]
if !ok {
toDelete = append(toDelete, tag.Key)
for _, desiredElement := range desired {
if !ackutil.InStrings(*desiredElement.Key, visitedIndexes) {
addedOrUpdated = append(addedOrUpdated, desiredElement)
}
}
return toAdd, toDelete
return addedOrUpdated, removed
}

func sdkTagsFromResourceTags(
Expand All @@ -277,3 +265,23 @@ func sdkTagsFromResourceTags(
}
return tags
}

func resourceTagsFromSDKTags(
sdkTags []*svcsdk.Tag,
) []*svcapitypes.Tag {
tags := make([]*svcapitypes.Tag, len(sdkTags))
for i := range sdkTags {
tags[i] = &svcapitypes.Tag{
Key: sdkTags[i].Key,
Value: sdkTags[i].Value,
}
}
return tags
}

func equalStrings(a, b *string) bool {
if a == nil {
return b == nil || *b == ""
}
return (*a == "" && b == nil) || *a == *b
}
150 changes: 144 additions & 6 deletions pkg/resource/snapshot/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,29 @@ import (
"context"
"errors"

svcsdk "github.com/aws/aws-sdk-go/service/memorydb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

svcapitypes "github.com/aws-controllers-k8s/memorydb-controller/apis/v1alpha1"
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
svcsdk "github.com/aws/aws-sdk-go/service/memorydb"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

svcapitypes "github.com/aws-controllers-k8s/memorydb-controller/apis/v1alpha1"
)

var (
condMsgCurrentlyDeleting string = "snapshot currently being deleted"
deleteStatus string = "deleting"
)

var (
requeueWaitWhileDeleting = ackrequeue.NeededAfter(
errors.New("delete is in progress"),
ackrequeue.DefaultRequeueAfterDuration,
)
)

func (rm *resourceManager) customDescribeSnapshotSetOutput(
Expand Down Expand Up @@ -248,6 +261,131 @@ func (rm *resourceManager) newCopySnapshotPayload(
return res, nil
}

// isDeleting returns true if supplied snapshot resource state is 'deleting'
func isDeleting(r *resource) bool {
if r == nil || r.ko.Status.Status == nil {
return false
}
status := *r.ko.Status.Status
return status == deleteStatus
}

func (rm *resourceManager) setSnapshotOutput(
r *resource,
obj *svcsdk.Snapshot,
) (*resource, error) {
if obj == nil ||
r == nil ||
r.ko == nil {
return nil, nil
}
resp := &svcsdk.DeleteSnapshotOutput{Snapshot: obj}

// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := r.ko.DeepCopy()

if ko.Status.ACKResourceMetadata == nil {
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
}
if resp.Snapshot.ARN != nil {
arn := ackv1alpha1.AWSResourceName(*resp.Snapshot.ARN)
ko.Status.ACKResourceMetadata.ARN = &arn
}
if resp.Snapshot.ClusterConfiguration != nil {
f1 := &svcapitypes.ClusterConfiguration{}
if resp.Snapshot.ClusterConfiguration.Description != nil {
f1.Description = resp.Snapshot.ClusterConfiguration.Description
}
if resp.Snapshot.ClusterConfiguration.EngineVersion != nil {
f1.EngineVersion = resp.Snapshot.ClusterConfiguration.EngineVersion
}
if resp.Snapshot.ClusterConfiguration.MaintenanceWindow != nil {
f1.MaintenanceWindow = resp.Snapshot.ClusterConfiguration.MaintenanceWindow
}
if resp.Snapshot.ClusterConfiguration.Name != nil {
f1.Name = resp.Snapshot.ClusterConfiguration.Name
}
if resp.Snapshot.ClusterConfiguration.NodeType != nil {
f1.NodeType = resp.Snapshot.ClusterConfiguration.NodeType
}
if resp.Snapshot.ClusterConfiguration.NumShards != nil {
f1.NumShards = resp.Snapshot.ClusterConfiguration.NumShards
}
if resp.Snapshot.ClusterConfiguration.ParameterGroupName != nil {
f1.ParameterGroupName = resp.Snapshot.ClusterConfiguration.ParameterGroupName
}
if resp.Snapshot.ClusterConfiguration.Port != nil {
f1.Port = resp.Snapshot.ClusterConfiguration.Port
}
if resp.Snapshot.ClusterConfiguration.Shards != nil {
f1f8 := []*svcapitypes.ShardDetail{}
for _, f1f8iter := range resp.Snapshot.ClusterConfiguration.Shards {
f1f8elem := &svcapitypes.ShardDetail{}
if f1f8iter.Configuration != nil {
f1f8elemf0 := &svcapitypes.ShardConfiguration{}
if f1f8iter.Configuration.ReplicaCount != nil {
f1f8elemf0.ReplicaCount = f1f8iter.Configuration.ReplicaCount
}
if f1f8iter.Configuration.Slots != nil {
f1f8elemf0.Slots = f1f8iter.Configuration.Slots
}
f1f8elem.Configuration = f1f8elemf0
}
if f1f8iter.Name != nil {
f1f8elem.Name = f1f8iter.Name
}
if f1f8iter.Size != nil {
f1f8elem.Size = f1f8iter.Size
}
if f1f8iter.SnapshotCreationTime != nil {
f1f8elem.SnapshotCreationTime = &metav1.Time{*f1f8iter.SnapshotCreationTime}
}
f1f8 = append(f1f8, f1f8elem)
}
f1.Shards = f1f8
}
if resp.Snapshot.ClusterConfiguration.SnapshotRetentionLimit != nil {
f1.SnapshotRetentionLimit = resp.Snapshot.ClusterConfiguration.SnapshotRetentionLimit
}
if resp.Snapshot.ClusterConfiguration.SnapshotWindow != nil {
f1.SnapshotWindow = resp.Snapshot.ClusterConfiguration.SnapshotWindow
}
if resp.Snapshot.ClusterConfiguration.SubnetGroupName != nil {
f1.SubnetGroupName = resp.Snapshot.ClusterConfiguration.SubnetGroupName
}
if resp.Snapshot.ClusterConfiguration.TopicArn != nil {
f1.TopicARN = resp.Snapshot.ClusterConfiguration.TopicArn
}
if resp.Snapshot.ClusterConfiguration.VpcId != nil {
f1.VPCID = resp.Snapshot.ClusterConfiguration.VpcId
}
ko.Status.ClusterConfiguration = f1
} else {
ko.Status.ClusterConfiguration = nil
}
if resp.Snapshot.KmsKeyId != nil {
ko.Spec.KMSKeyID = resp.Snapshot.KmsKeyId
} else {
ko.Spec.KMSKeyID = nil
}
if resp.Snapshot.Source != nil {
ko.Status.Source = resp.Snapshot.Source
} else {
ko.Status.Source = nil
}
if resp.Snapshot.Status != nil {
ko.Status.Status = resp.Snapshot.Status
} else {
ko.Status.Status = nil
}

rm.setStatusDefaults(ko)
// custom set output from response
rm.customSetOutput(obj, ko)
return &resource{ko}, nil
}

// getTags gets tags from given ParameterGroup.
func (rm *resourceManager) getTags(
ctx context.Context,
Expand Down Expand Up @@ -346,8 +484,8 @@ func computeTagsDelta(
if !equalStrings(latestElement.Value, desiredElement.Value) {
addedOrUpdated = append(addedOrUpdated, desiredElement)
}
continue
}
continue
}
removed = append(removed, latestElement.Key)
}
Expand Down
Loading

0 comments on commit 6a8b7ed

Please sign in to comment.