forked from aws-controllers-k8s/ec2-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
189 lines (163 loc) · 5.05 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
// 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 flow_log
import (
"context"
svcapitypes "github.com/aws-controllers-k8s/ec2-controller/apis/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
)
func (rm *resourceManager) customUpdateFlowLog(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (updated *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateFlowLog")
defer exit(err)
// Default `updated` to `desired` because it is likely
// EC2 `modify` APIs do NOT return output, only errors.
// If the `modify` calls (i.e. `sync`) do NOT return
// an error, then the update was successful and desired.Spec
// (now updated.Spec) reflects the latest resource state.
updated = rm.concreteResource(desired.DeepCopy())
if delta.DifferentAt("Spec.Tags") {
if err := rm.syncTags(ctx, desired, latest); err != nil {
return nil, err
}
}
return updated, nil
}
// syncTags used to keep tags in sync by calling Create and Delete API's
func (rm *resourceManager) syncTags(
ctx context.Context,
desired *resource,
latest *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncTags")
defer func(err error) {
exit(err)
}(err)
resourceId := []*string{latest.ko.Status.FlowLogID}
toAdd, toDelete := computeTagsDelta(
desired.ko.Spec.Tags, latest.ko.Spec.Tags,
)
if len(toDelete) > 0 {
rlog.Debug("removing tags from FlowLog resource", "tags", toDelete)
_, err = rm.sdkapi.DeleteTagsWithContext(
ctx,
&svcsdk.DeleteTagsInput{
Resources: resourceId,
Tags: rm.sdkTags(toDelete),
},
)
rm.metrics.RecordAPICall("UPDATE", "DeleteTags", err)
if err != nil {
return err
}
}
if len(toAdd) > 0 {
rlog.Debug("adding tags to FlowLog resource", "tags", toAdd)
_, err = rm.sdkapi.CreateTagsWithContext(
ctx,
&svcsdk.CreateTagsInput{
Resources: resourceId,
Tags: rm.sdkTags(toAdd),
},
)
rm.metrics.RecordAPICall("UPDATE", "CreateTags", err)
if err != nil {
return err
}
}
return nil
}
// sdkTags converts *svcapitypes.Tag array to a *svcsdk.Tag array
func (rm *resourceManager) sdkTags(
tags []*svcapitypes.Tag,
) (sdktags []*svcsdk.Tag) {
for _, i := range tags {
sdktag := rm.newTag(*i)
sdktags = append(sdktags, sdktag)
}
return sdktags
}
// computeTagsDelta returns tags to be added and removed from the resource
func computeTagsDelta(
desired []*svcapitypes.Tag,
latest []*svcapitypes.Tag,
) (toAdd []*svcapitypes.Tag, toDelete []*svcapitypes.Tag) {
desiredTags := map[string]string{}
for _, tag := range desired {
desiredTags[*tag.Key] = *tag.Value
}
latestTags := map[string]string{}
for _, tag := range latest {
latestTags[*tag.Key] = *tag.Value
}
for _, tag := range desired {
val, ok := latestTags[*tag.Key]
if !ok || val != *tag.Value {
toAdd = append(toAdd, tag)
}
}
for _, tag := range latest {
_, ok := desiredTags[*tag.Key]
if !ok {
toDelete = append(toDelete, tag)
}
}
return toAdd, toDelete
}
// 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 {
addedOrUpdated, removed := computeTagsDelta(a.ko.Spec.Tags, b.ko.Spec.Tags)
if len(addedOrUpdated) != 0 || len(removed) != 0 {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
}
}
}
// updateTagSpecificationsInCreateRequest adds
// Tags defined in the Spec to CreateFlowLogsInput.TagSpecification
// and ensures the ResourceType is always set to 'FlowLog'
func updateTagSpecificationsInCreateRequest(r *resource,
input *svcsdk.CreateFlowLogsInput) {
input.TagSpecifications = nil
desiredTagSpecs := svcsdk.TagSpecification{}
if r.ko.Spec.Tags != nil {
requestedTags := []*svcsdk.Tag{}
for _, desiredTag := range r.ko.Spec.Tags {
// Add in tags defined in the Spec
tag := &svcsdk.Tag{}
if desiredTag.Key != nil && desiredTag.Value != nil {
tag.SetKey(*desiredTag.Key)
tag.SetValue(*desiredTag.Value)
}
requestedTags = append(requestedTags, tag)
}
desiredTagSpecs.SetResourceType("vpc-flow-log")
desiredTagSpecs.SetTags(requestedTags)
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
}
}