-
Notifications
You must be signed in to change notification settings - Fork 13
/
hooks.go
145 lines (126 loc) · 4 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package queue
import (
"context"
"fmt"
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
"github.com/aws/aws-sdk-go/aws/arn"
svcsdk "github.com/aws/aws-sdk-go/service/sqs"
)
// syncTags examines the Tags in the supplied Queue and calls the
// ListQueueTags, TagQueue and UntagQueue APIs to ensure that the set of
// associated Tags stays in sync with the Queue.Spec.Tags
func (rm *resourceManager) syncTags(
ctx context.Context,
desired *resource,
latest *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncTags")
defer func() { exit(err) }()
toAdd := map[string]*string{}
toDelete := []*string{}
existingTags := latest.ko.Spec.Tags
for k, v := range desired.ko.Spec.Tags {
if ev, found := existingTags[k]; !found || *ev != *v {
toAdd[k] = v
}
}
for k, _ := range existingTags {
if _, found := desired.ko.Spec.Tags[k]; !found {
deleteKey := k
toDelete = append(toDelete, &deleteKey)
}
}
if len(toAdd) > 0 {
for k, v := range toAdd {
rlog.Debug("adding tag to queue", "key", k, "value", *v)
}
if err = rm.addTags(ctx, desired, toAdd); err != nil {
return err
}
}
if len(toDelete) > 0 {
for _, k := range toDelete {
rlog.Debug("removing tag from queue", "key", *k)
}
if err = rm.removeTags(ctx, desired, toDelete); err != nil {
return err
}
}
return nil
}
// getTags returns the list of tags to the Queue
func (rm *resourceManager) getTags(
ctx context.Context,
r *resource,
) (map[string]*string, error) {
var err error
var resp *svcsdk.ListQueueTagsOutput
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.getTags")
defer func() { exit(err) }()
input := &svcsdk.ListQueueTagsInput{}
input.QueueUrl = r.ko.Status.QueueURL
// NOTE(jaypipes): Unlike many other ListTags APIs, SQS's is not
// paginated...
resp, err = rm.sdkapi.ListQueueTagsWithContext(ctx, input)
rm.metrics.RecordAPICall("READ_MANY", "ListQueueTags", err)
if err != nil || resp == nil {
return nil, err
}
// and the output's Tags field is actually a map[string]*string... go
// figure :)
return resp.Tags, err
}
// addTags adds the supplied Tags to the supplied Queue resource
func (rm *resourceManager) addTags(
ctx context.Context,
r *resource,
tags map[string]*string,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.addTag")
defer func() { exit(err) }()
input := &svcsdk.TagQueueInput{}
input.QueueUrl = r.ko.Status.QueueURL
input.Tags = tags
_, err = rm.sdkapi.TagQueueWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "TagQueue", err)
return err
}
// removeTags removes the supplied Tags from the supplied Queue resource
func (rm *resourceManager) removeTags(
ctx context.Context,
r *resource,
tagKeys []*string, // the set of tag keys to delete
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.removeTag")
defer func() { exit(err) }()
input := &svcsdk.UntagQueueInput{}
input.QueueUrl = r.ko.Status.QueueURL
input.TagKeys = tagKeys
_, err = rm.sdkapi.UntagQueueWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UntagQueue", err)
return err
}
func (rm *resourceManager) getQueueNameFromARN(tmpARN ackv1alpha1.AWSResourceName) (string, error) {
queueARN, err := arn.Parse(string(tmpARN))
if err != nil {
return "", fmt.Errorf("error parsing queue ARN: %s, error: %w", tmpARN, err)
}
return queueARN.Resource, nil
}