Skip to content

Commit 6a8b7ed

Browse files
author
Kyrie Chen
committed
Fixes flaky tests
1 parent d2483d2 commit 6a8b7ed

22 files changed

+291
-298
lines changed

pkg/resource/acl/hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ func computeTagsDelta(
122122
if !equalStrings(latestElement.Value, desiredElement.Value) {
123123
addedOrUpdated = append(addedOrUpdated, desiredElement)
124124
}
125-
continue
126125
}
126+
continue
127127
}
128128
removed = append(removed, latestElement.Key)
129129
}

pkg/resource/cluster/hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ func computeTagsDelta(
345345
if !equalStrings(latestElement.Value, desiredElement.Value) {
346346
addedOrUpdated = append(addedOrUpdated, desiredElement)
347347
}
348-
continue
349348
}
349+
continue
350350
}
351351
removed = append(removed, latestElement.Key)
352352
}

pkg/resource/parameter_group/hooks.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package parameter_group
1515

1616
import (
1717
"context"
18+
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
1819

1920
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2021
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
@@ -173,13 +174,7 @@ func (rm *resourceManager) getTags(
173174
if err != nil {
174175
return nil, err
175176
}
176-
tags := make([]*svcapitypes.Tag, 0, len(resp.TagList))
177-
for _, tag := range resp.TagList {
178-
tags = append(tags, &svcapitypes.Tag{
179-
Key: tag.Key,
180-
Value: tag.Value,
181-
})
182-
}
177+
tags := resourceTagsFromSDKTags(resp.TagList)
183178
return tags, nil
184179
}
185180

@@ -235,34 +230,27 @@ func (rm *resourceManager) updateTags(
235230
func computeTagsDelta(
236231
desired []*svcapitypes.Tag,
237232
latest []*svcapitypes.Tag,
238-
) (added []*svcapitypes.Tag, removed []*string) {
239-
toDelete := []*string{}
240-
toAdd := []*svcapitypes.Tag{}
241-
242-
desiredTags := map[string]string{}
243-
key := ""
244-
value := ""
245-
for _, tag := range desired {
246-
if tag.Key != nil {
247-
key = *tag.Key
248-
value = ""
249-
if tag.Value != nil {
250-
value = *tag.Value
233+
) (addedOrUpdated []*svcapitypes.Tag, removed []*string) {
234+
var visitedIndexes []string
235+
236+
for _, latestElement := range latest {
237+
visitedIndexes = append(visitedIndexes, *latestElement.Key)
238+
for _, desiredElement := range desired {
239+
if equalStrings(latestElement.Key, desiredElement.Key) {
240+
if !equalStrings(latestElement.Value, desiredElement.Value) {
241+
addedOrUpdated = append(addedOrUpdated, desiredElement)
242+
}
251243
}
252-
desiredTags[key] = value
244+
continue
253245
}
246+
removed = append(removed, latestElement.Key)
254247
}
255-
256-
for _, tag := range desired {
257-
toAdd = append(toAdd, tag)
258-
}
259-
for _, tag := range latest {
260-
_, ok := desiredTags[*tag.Key]
261-
if !ok {
262-
toDelete = append(toDelete, tag.Key)
248+
for _, desiredElement := range desired {
249+
if !ackutil.InStrings(*desiredElement.Key, visitedIndexes) {
250+
addedOrUpdated = append(addedOrUpdated, desiredElement)
263251
}
264252
}
265-
return toAdd, toDelete
253+
return addedOrUpdated, removed
266254
}
267255

268256
func sdkTagsFromResourceTags(
@@ -277,3 +265,23 @@ func sdkTagsFromResourceTags(
277265
}
278266
return tags
279267
}
268+
269+
func resourceTagsFromSDKTags(
270+
sdkTags []*svcsdk.Tag,
271+
) []*svcapitypes.Tag {
272+
tags := make([]*svcapitypes.Tag, len(sdkTags))
273+
for i := range sdkTags {
274+
tags[i] = &svcapitypes.Tag{
275+
Key: sdkTags[i].Key,
276+
Value: sdkTags[i].Value,
277+
}
278+
}
279+
return tags
280+
}
281+
282+
func equalStrings(a, b *string) bool {
283+
if a == nil {
284+
return b == nil || *b == ""
285+
}
286+
return (*a == "" && b == nil) || *a == *b
287+
}

pkg/resource/snapshot/hooks.go

Lines changed: 144 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,29 @@ import (
1717
"context"
1818
"errors"
1919

20-
svcsdk "github.com/aws/aws-sdk-go/service/memorydb"
21-
corev1 "k8s.io/api/core/v1"
22-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23-
24-
svcapitypes "github.com/aws-controllers-k8s/memorydb-controller/apis/v1alpha1"
2520
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
2621
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2722
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
23+
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
2824
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2925
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
26+
svcsdk "github.com/aws/aws-sdk-go/service/memorydb"
27+
corev1 "k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
30+
svcapitypes "github.com/aws-controllers-k8s/memorydb-controller/apis/v1alpha1"
31+
)
32+
33+
var (
34+
condMsgCurrentlyDeleting string = "snapshot currently being deleted"
35+
deleteStatus string = "deleting"
36+
)
37+
38+
var (
39+
requeueWaitWhileDeleting = ackrequeue.NeededAfter(
40+
errors.New("delete is in progress"),
41+
ackrequeue.DefaultRequeueAfterDuration,
42+
)
3043
)
3144

3245
func (rm *resourceManager) customDescribeSnapshotSetOutput(
@@ -248,6 +261,131 @@ func (rm *resourceManager) newCopySnapshotPayload(
248261
return res, nil
249262
}
250263

264+
// isDeleting returns true if supplied snapshot resource state is 'deleting'
265+
func isDeleting(r *resource) bool {
266+
if r == nil || r.ko.Status.Status == nil {
267+
return false
268+
}
269+
status := *r.ko.Status.Status
270+
return status == deleteStatus
271+
}
272+
273+
func (rm *resourceManager) setSnapshotOutput(
274+
r *resource,
275+
obj *svcsdk.Snapshot,
276+
) (*resource, error) {
277+
if obj == nil ||
278+
r == nil ||
279+
r.ko == nil {
280+
return nil, nil
281+
}
282+
resp := &svcsdk.DeleteSnapshotOutput{Snapshot: obj}
283+
284+
// Merge in the information we read from the API call above to the copy of
285+
// the original Kubernetes object we passed to the function
286+
ko := r.ko.DeepCopy()
287+
288+
if ko.Status.ACKResourceMetadata == nil {
289+
ko.Status.ACKResourceMetadata = &ackv1alpha1.ResourceMetadata{}
290+
}
291+
if resp.Snapshot.ARN != nil {
292+
arn := ackv1alpha1.AWSResourceName(*resp.Snapshot.ARN)
293+
ko.Status.ACKResourceMetadata.ARN = &arn
294+
}
295+
if resp.Snapshot.ClusterConfiguration != nil {
296+
f1 := &svcapitypes.ClusterConfiguration{}
297+
if resp.Snapshot.ClusterConfiguration.Description != nil {
298+
f1.Description = resp.Snapshot.ClusterConfiguration.Description
299+
}
300+
if resp.Snapshot.ClusterConfiguration.EngineVersion != nil {
301+
f1.EngineVersion = resp.Snapshot.ClusterConfiguration.EngineVersion
302+
}
303+
if resp.Snapshot.ClusterConfiguration.MaintenanceWindow != nil {
304+
f1.MaintenanceWindow = resp.Snapshot.ClusterConfiguration.MaintenanceWindow
305+
}
306+
if resp.Snapshot.ClusterConfiguration.Name != nil {
307+
f1.Name = resp.Snapshot.ClusterConfiguration.Name
308+
}
309+
if resp.Snapshot.ClusterConfiguration.NodeType != nil {
310+
f1.NodeType = resp.Snapshot.ClusterConfiguration.NodeType
311+
}
312+
if resp.Snapshot.ClusterConfiguration.NumShards != nil {
313+
f1.NumShards = resp.Snapshot.ClusterConfiguration.NumShards
314+
}
315+
if resp.Snapshot.ClusterConfiguration.ParameterGroupName != nil {
316+
f1.ParameterGroupName = resp.Snapshot.ClusterConfiguration.ParameterGroupName
317+
}
318+
if resp.Snapshot.ClusterConfiguration.Port != nil {
319+
f1.Port = resp.Snapshot.ClusterConfiguration.Port
320+
}
321+
if resp.Snapshot.ClusterConfiguration.Shards != nil {
322+
f1f8 := []*svcapitypes.ShardDetail{}
323+
for _, f1f8iter := range resp.Snapshot.ClusterConfiguration.Shards {
324+
f1f8elem := &svcapitypes.ShardDetail{}
325+
if f1f8iter.Configuration != nil {
326+
f1f8elemf0 := &svcapitypes.ShardConfiguration{}
327+
if f1f8iter.Configuration.ReplicaCount != nil {
328+
f1f8elemf0.ReplicaCount = f1f8iter.Configuration.ReplicaCount
329+
}
330+
if f1f8iter.Configuration.Slots != nil {
331+
f1f8elemf0.Slots = f1f8iter.Configuration.Slots
332+
}
333+
f1f8elem.Configuration = f1f8elemf0
334+
}
335+
if f1f8iter.Name != nil {
336+
f1f8elem.Name = f1f8iter.Name
337+
}
338+
if f1f8iter.Size != nil {
339+
f1f8elem.Size = f1f8iter.Size
340+
}
341+
if f1f8iter.SnapshotCreationTime != nil {
342+
f1f8elem.SnapshotCreationTime = &metav1.Time{*f1f8iter.SnapshotCreationTime}
343+
}
344+
f1f8 = append(f1f8, f1f8elem)
345+
}
346+
f1.Shards = f1f8
347+
}
348+
if resp.Snapshot.ClusterConfiguration.SnapshotRetentionLimit != nil {
349+
f1.SnapshotRetentionLimit = resp.Snapshot.ClusterConfiguration.SnapshotRetentionLimit
350+
}
351+
if resp.Snapshot.ClusterConfiguration.SnapshotWindow != nil {
352+
f1.SnapshotWindow = resp.Snapshot.ClusterConfiguration.SnapshotWindow
353+
}
354+
if resp.Snapshot.ClusterConfiguration.SubnetGroupName != nil {
355+
f1.SubnetGroupName = resp.Snapshot.ClusterConfiguration.SubnetGroupName
356+
}
357+
if resp.Snapshot.ClusterConfiguration.TopicArn != nil {
358+
f1.TopicARN = resp.Snapshot.ClusterConfiguration.TopicArn
359+
}
360+
if resp.Snapshot.ClusterConfiguration.VpcId != nil {
361+
f1.VPCID = resp.Snapshot.ClusterConfiguration.VpcId
362+
}
363+
ko.Status.ClusterConfiguration = f1
364+
} else {
365+
ko.Status.ClusterConfiguration = nil
366+
}
367+
if resp.Snapshot.KmsKeyId != nil {
368+
ko.Spec.KMSKeyID = resp.Snapshot.KmsKeyId
369+
} else {
370+
ko.Spec.KMSKeyID = nil
371+
}
372+
if resp.Snapshot.Source != nil {
373+
ko.Status.Source = resp.Snapshot.Source
374+
} else {
375+
ko.Status.Source = nil
376+
}
377+
if resp.Snapshot.Status != nil {
378+
ko.Status.Status = resp.Snapshot.Status
379+
} else {
380+
ko.Status.Status = nil
381+
}
382+
383+
rm.setStatusDefaults(ko)
384+
// custom set output from response
385+
rm.customSetOutput(obj, ko)
386+
return &resource{ko}, nil
387+
}
388+
251389
// getTags gets tags from given ParameterGroup.
252390
func (rm *resourceManager) getTags(
253391
ctx context.Context,
@@ -346,8 +484,8 @@ func computeTagsDelta(
346484
if !equalStrings(latestElement.Value, desiredElement.Value) {
347485
addedOrUpdated = append(addedOrUpdated, desiredElement)
348486
}
349-
continue
350487
}
488+
continue
351489
}
352490
removed = append(removed, latestElement.Key)
353491
}

0 commit comments

Comments
 (0)