/
hooks.go
248 lines (218 loc) · 6.85 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package instance_profile
import (
"context"
"fmt"
"strings"
svcapitypes "github.com/aws-controllers-k8s/iam-controller/apis/v1alpha1"
commonutil "github.com/aws-controllers-k8s/iam-controller/pkg/util"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcsdk "github.com/aws/aws-sdk-go/service/iam"
)
// customUpdateInstanceProfile is the custom implementation for
// InstanceProfile resource's update operation.
func (rm *resourceManager) customUpdateInstanceProfile(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (updated *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateInstanceProfile")
defer func() { exit(err) }()
// Do not proceed with update if an immutable field was updated
if immutableFieldChanges := rm.getImmutableFieldChanges(delta); len(immutableFieldChanges) > 0 {
msg := fmt.Sprintf("Immutable Spec fields have been modified: %s", strings.Join(immutableFieldChanges, ","))
return nil, ackerr.NewTerminalError(fmt.Errorf(msg))
}
ko := desired.ko.DeepCopy()
if delta.DifferentAt("Spec.Tags") {
if err = rm.syncTags(ctx, desired, latest); err != nil {
return nil, err
}
}
if delta.DifferentAt("Spec.Role") {
if err = rm.syncRole(ctx, desired, latest); err != nil {
return nil, err
}
}
rm.setStatusDefaults(ko)
return &resource{ko}, nil
}
// syncRole takes the delta between the desired role for the instance
// profile and the currently attached role. If a difference is found,
// the role will be synced to the desired value
func (rm *resourceManager) syncRole(
ctx context.Context,
desired *resource,
latest *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncRole")
defer func() { exit(err) }()
// If no role is desired, detach any existing roles
if desired.ko.Spec.Role == nil {
if latest.ko.Spec.Role == nil {
return nil
}
if err = rm.detachRole(ctx, latest); err != nil {
return err
}
// Don't continue, nothing left to do
return nil
}
// If the currently attached role and the desired role are different,
// detach the existing role
if latest.ko.Spec.Role != nil {
if *desired.ko.Spec.Role == *latest.ko.Spec.Role {
return nil
}
if err = rm.detachRole(ctx, latest); err != nil {
return err
}
}
err = rm.attachRole(ctx, desired)
return err
}
// attachRole will attach a new IAM role to the instance profile
func (rm *resourceManager) attachRole(
ctx context.Context,
desired *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.attachRole")
defer func() { exit(err) }()
input := &svcsdk.AddRoleToInstanceProfileInput{}
input.SetInstanceProfileName(*desired.ko.Spec.Name)
input.SetRoleName(*desired.ko.Spec.Role)
_, err = rm.sdkapi.AddRoleToInstanceProfileWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "AddRoleToInstanceProfile", err)
return err
}
// detachRole will detach an existing IAM role from the instance profile
func (rm *resourceManager) detachRole(
ctx context.Context,
latest *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.detachRole")
defer func() { exit(err) }()
input := &svcsdk.RemoveRoleFromInstanceProfileInput{}
input.SetInstanceProfileName(*latest.ko.Spec.Name)
input.SetRoleName(*latest.ko.Spec.Role)
_, err = rm.sdkapi.RemoveRoleFromInstanceProfileWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "RemoveRoleFromInstanceProfile", err)
return err
}
// syncTags examines the Tags in the supplied InstanceProfile and calls TagInstanceProfile
// and UntagInstanceProfile APIs to ensure that the set of associated Tags stays in sync
// with InstanceProfile.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 := []*svcapitypes.Tag{}
toDelete := []*svcapitypes.Tag{}
existingTags := latest.ko.Spec.Tags
for _, t := range existingTags {
if !inTags(*t.Key, *t.Value, desired.ko.Spec.Tags) {
toDelete = append(toDelete, t)
}
}
for _, t := range desired.ko.Spec.Tags {
if !inTags(*t.Key, *t.Value, existingTags) {
toAdd = append(toAdd, t)
}
}
if len(toDelete) > 0 {
for _, t := range toDelete {
rlog.Debug("removing tag from instance profile", "key", *t.Key, "value", *t.Value)
}
if err = rm.removeTags(ctx, desired, toDelete); err != nil {
return err
}
}
if len(toAdd) > 0 {
for _, t := range toAdd {
rlog.Debug("adding tag to instance profile", "key", *t.Key, "value", *t.Value)
}
if err = rm.addTags(ctx, desired, toAdd); err != nil {
return err
}
}
return nil
}
// inTags returns true if the supplied key and value can be found in the
// supplied list of Tag structs.
func inTags(
key string,
value string,
tags []*svcapitypes.Tag,
) bool {
for _, t := range tags {
if *t.Key == key && *t.Value == value {
return true
}
}
return false
}
// addTags adds the supplied Tags to the supplied InstanceProfile resource
func (rm *resourceManager) addTags(
ctx context.Context,
r *resource,
tags []*svcapitypes.Tag,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.addTag")
defer func() { exit(err) }()
input := &svcsdk.TagInstanceProfileInput{}
input.InstanceProfileName = r.ko.Spec.Name
inTags := []*svcsdk.Tag{}
for _, t := range tags {
inTags = append(inTags, &svcsdk.Tag{Key: t.Key, Value: t.Value})
}
input.Tags = inTags
_, err = rm.sdkapi.TagInstanceProfileWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "TagInstanceProfile", err)
return err
}
// removeTags removes the supplied Tags from the supplied InstanceProfile resource
func (rm *resourceManager) removeTags(
ctx context.Context,
r *resource,
tags []*svcapitypes.Tag,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.removeTag")
defer func() { exit(err) }()
input := &svcsdk.UntagInstanceProfileInput{}
input.InstanceProfileName = r.ko.Spec.Name
inTagKeys := []*string{}
for _, t := range tags {
inTagKeys = append(inTagKeys, t.Key)
}
input.TagKeys = inTagKeys
_, err = rm.sdkapi.UntagInstanceProfileWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UntagInstanceProfile", err)
return err
}
// compareTags is a custom comparison function for comparing lists of Tag
// structs where the order of the structs in the list is not important.
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 !commonutil.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
}
}
}