/
hooks.go
234 lines (214 loc) · 6.6 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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 db_proxy
import (
"context"
"errors"
"fmt"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackrequeue "github.com/aws-controllers-k8s/runtime/pkg/requeue"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcapitypes "github.com/aws-controllers-k8s/rds-controller/apis/v1alpha1"
svcsdk "github.com/aws/aws-sdk-go/service/rds"
"github.com/aws-controllers-k8s/rds-controller/pkg/util"
)
var (
// TerminalStatuses are the status strings that are terminal states for a
// DB proxy.
TerminalStatuses = []string{
svcsdk.DBProxyStatusIncompatibleNetwork,
svcsdk.DBProxyStatusDeleting,
svcsdk.DBProxyStatusSuspending,
}
)
var (
requeueWaitWhileDeleting = ackrequeue.NeededAfter(
errors.New("DB proxy in 'deleting' state, cannot be modified or deleted."),
ackrequeue.DefaultRequeueAfterDuration,
)
)
// requeueWaitUntilCanModify returns a `ackrequeue.RequeueNeededAfter` struct
// explaining the DB proxy cannot be modified until it reaches an available
// status.
func requeueWaitUntilCanModify(r *resource) *ackrequeue.RequeueNeededAfter {
if r.ko.Status.Status == nil {
return nil
}
status := *r.ko.Status.Status
msg := fmt.Sprintf(
"DB proxy in '%s' state, cannot be modified until '%s'.",
status, svcsdk.DBProxyStatusAvailable,
)
return ackrequeue.NeededAfter(
errors.New(msg),
ackrequeue.DefaultRequeueAfterDuration,
)
}
// proxyHasTerminalStatus returns whether the supplied DB proxy is in a
// terminal state
func proxyHasTerminalStatus(r *resource) bool {
if r.ko.Status.Status == nil {
return false
}
dbis := *r.ko.Status.Status
for _, s := range TerminalStatuses {
if dbis == s {
return true
}
}
return false
}
// proxyAvailable returns true if the supplied DB proxy is in an
// available status
func proxyAvailable(r *resource) bool {
if r.ko.Status.Status == nil {
return false
}
dbis := *r.ko.Status.Status
return dbis == svcsdk.DBProxyStatusAvailable
}
// proxyCreating returns true if the supplied DB proxy is in the process
// of being created
func proxyCreating(r *resource) bool {
if r.ko.Status.Status == nil {
return false
}
dbis := *r.ko.Status.Status
return dbis == svcsdk.DBProxyStatusCreating
}
// proxyDeleting returns true if the supplied DB proxy is in the process
// of being deleted
func proxyDeleting(r *resource) bool {
if r.ko.Status.Status == nil {
return false
}
dbis := *r.ko.Status.Status
return dbis == svcsdk.DBProxyStatusDeleting
}
// syncTags keeps the resource's tags in sync
//
// NOTE(jaypipes): RDS' Tagging APIs differ from other AWS APIs in the
// following ways:
//
// 1. The names of the tagging API operations are different. Other APIs use the
// Tagris `ListTagsForResource`, `TagResource` and `UntagResource` API
// calls. RDS uses `ListTagsForResource`, `AddTagsToResource` and
// `RemoveTagsFromResource`.
//
// 2. Even though the name of the `ListTagsForResource` API call is the same,
// the structure of the input and the output are different from other APIs.
// For the input, instead of a `ResourceArn` field, RDS names the field
// `ResourceName`, but actually expects an ARN, not the proxy
// name. This is the same for the `AddTagsToResource` and
// `RemoveTagsFromResource` input shapes. For the output shape, the field is
// called `TagList` instead of `Tags` but is otherwise the same struct with
// a `Key` and `Value` member field.
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) }()
arn := (*string)(latest.ko.Status.ACKResourceMetadata.ARN)
toAdd, toDelete := util.ComputeTagsDelta(
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
)
if len(toDelete) > 0 {
rlog.Debug("removing tags from proxy", "tags", toDelete)
_, err = rm.sdkapi.RemoveTagsFromResourceWithContext(
ctx,
&svcsdk.RemoveTagsFromResourceInput{
ResourceName: arn,
TagKeys: toDelete,
},
)
rm.metrics.RecordAPICall("UPDATE", "RemoveTagsFromResource", err)
if err != nil {
return err
}
}
// NOTE(jaypipes): According to the RDS API documentation, adding a tag
// with a new value overwrites any existing tag with the same key. So, we
// don't need to do anything to "update" a Tag. Simply including it in the
// AddTagsToResource call is enough.
if len(toAdd) > 0 {
rlog.Debug("adding tags to proxy", "tags", toAdd)
_, err = rm.sdkapi.AddTagsToResourceWithContext(
ctx,
&svcsdk.AddTagsToResourceInput{
ResourceName: arn,
Tags: sdkTagsFromResourceTags(toAdd),
},
)
rm.metrics.RecordAPICall("UPDATE", "AddTagsToResource", err)
if err != nil {
return err
}
}
return nil
}
// getTags retrieves the resource's associated tags
func (rm *resourceManager) getTags(
ctx context.Context,
resourceARN string,
) ([]*svcapitypes.Tag, error) {
resp, err := rm.sdkapi.ListTagsForResourceWithContext(
ctx,
&svcsdk.ListTagsForResourceInput{
ResourceName: &resourceARN,
},
)
rm.metrics.RecordAPICall("GET", "ListTagsForResource", err)
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,
})
}
return tags, nil
}
// compareTags adds a difference to the delta if the supplied resources have
// different tag collections
func compareTags(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
} else if len(a.ko.Spec.Tags) > 0 {
if !util.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
}
}
}
// sdkTagsFromResourceTags transforms a *svcapitypes.Tag array to a *svcsdk.Tag
// array.
func sdkTagsFromResourceTags(
rTags []*svcapitypes.Tag,
) []*svcsdk.Tag {
tags := make([]*svcsdk.Tag, len(rTags))
for i := range rTags {
tags[i] = &svcsdk.Tag{
Key: rTags[i].Key,
Value: rTags[i].Value,
}
}
return tags
}