forked from aws-controllers-k8s/route53-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
334 lines (293 loc) · 9.52 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package record_set
import (
"context"
"errors"
"strings"
svcapitypes "github.com/aws-controllers-k8s/route53-controller/apis/v1alpha1"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
"github.com/aws/aws-sdk-go-v2/aws"
svcsdk "github.com/aws/aws-sdk-go-v2/service/route53"
svcsdktypes "github.com/aws/aws-sdk-go-v2/service/route53/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// newResourceRecords returns a slice of ResourceRecord pointer objects
// with values set by the resource's corresponding spec field.
func (rm *resourceManager) newResourceRecords(
r *resource,
) []svcsdktypes.ResourceRecord {
if r.ko.Spec.ResourceRecords == nil {
return nil
}
res := make([]svcsdktypes.ResourceRecord, len(r.ko.Spec.ResourceRecords))
for i, rr := range r.ko.Spec.ResourceRecords {
value := *rr.Value
res[i] = svcsdktypes.ResourceRecord{
Value: &value,
}
}
return res
}
// newAliasTarget returns a pointer to an AliasTarget object
// with values set by the resource's corresponding spec field.
func (rm *resourceManager) newAliasTarget(
r *resource,
) *svcsdktypes.AliasTarget {
if r.ko.Spec.AliasTarget == nil {
return nil
}
res := &svcsdktypes.AliasTarget{}
if r.ko.Spec.AliasTarget.DNSName != nil {
res.DNSName = r.ko.Spec.AliasTarget.DNSName
}
if r.ko.Spec.AliasTarget.EvaluateTargetHealth != nil {
res.EvaluateTargetHealth = *r.ko.Spec.AliasTarget.EvaluateTargetHealth
}
if r.ko.Spec.AliasTarget.HostedZoneID != nil {
res.HostedZoneId = r.ko.Spec.AliasTarget.HostedZoneID
}
return res
}
// newCIDRRoutingConfig returns a pointer to a CIDRRoutingConfig object
// with values set by the resource's corresponding spec field.
func (rm *resourceManager) newCIDRRoutingConfig(
r *resource,
) *svcsdktypes.CidrRoutingConfig {
if r.ko.Spec.CIDRRoutingConfig == nil {
return nil
}
res := &svcsdktypes.CidrRoutingConfig{}
if r.ko.Spec.CIDRRoutingConfig.CollectionID != nil {
res.CollectionId = r.ko.Spec.CIDRRoutingConfig.CollectionID
}
if r.ko.Spec.CIDRRoutingConfig.LocationName != nil {
res.LocationName = r.ko.Spec.CIDRRoutingConfig.LocationName
}
return res
}
// newGeoLocation returns a pointer to a GeoLocation object
// with values set by the resource's corresponding spec field.
func (rm *resourceManager) newGeoLocation(
r *resource,
) *svcsdktypes.GeoLocation {
if r.ko.Spec.GeoLocation == nil {
return nil
}
res := &svcsdktypes.GeoLocation{}
if r.ko.Spec.GeoLocation.ContinentCode != nil {
res.ContinentCode = r.ko.Spec.GeoLocation.ContinentCode
}
if r.ko.Spec.GeoLocation.CountryCode != nil {
res.CountryCode = r.ko.Spec.GeoLocation.CountryCode
}
if r.ko.Spec.GeoLocation.SubdivisionCode != nil {
res.SubdivisionCode = r.ko.Spec.GeoLocation.SubdivisionCode
}
return res
}
// newResourceRecordSet returns a pointer to a ResourceRecordSet object
// with each field set by the resource's corresponding spec field.
func (rm *resourceManager) newResourceRecordSet(
ctx context.Context,
r *resource,
) (*svcsdktypes.ResourceRecordSet, error) {
res := &svcsdktypes.ResourceRecordSet{}
domain, err := rm.getHostedZoneDomain(ctx, r)
if err != nil {
return nil, err
}
dnsName := rm.getDNSName(r, domain)
// Set required fields for the ChangeResourceRecordSets API
res.Name = &dnsName
res.Type = svcsdktypes.RRType(*r.ko.Spec.RecordType)
// Set optional fields
if r.ko.Spec.Failover != nil {
res.Failover = svcsdktypes.ResourceRecordSetFailover(*r.ko.Spec.Failover)
}
if r.ko.Spec.HealthCheckID != nil {
res.HealthCheckId = r.ko.Spec.HealthCheckID
}
if r.ko.Spec.MultiValueAnswer != nil {
res.MultiValueAnswer = r.ko.Spec.MultiValueAnswer
}
if r.ko.Spec.Region != nil {
res.Region = svcsdktypes.ResourceRecordSetRegion(*r.ko.Spec.Region)
}
if r.ko.Spec.SetIdentifier != nil {
res.SetIdentifier = r.ko.Spec.SetIdentifier
}
if r.ko.Spec.TTL != nil {
res.TTL = r.ko.Spec.TTL
}
if r.ko.Spec.Weight != nil {
res.Weight = r.ko.Spec.Weight
}
// Set resource records if available
resourceRecords := rm.newResourceRecords(r)
res.ResourceRecords = resourceRecords
// Set alias target if available
aliasTarget := rm.newAliasTarget(r)
res.AliasTarget = aliasTarget
// Set CIDR routing config if available
cidrRoutingConfig := rm.newCIDRRoutingConfig(r)
res.CidrRoutingConfig = cidrRoutingConfig
// Set geolocation if available
geoLocation := rm.newGeoLocation(r)
res.GeoLocation = geoLocation
return res, nil
}
// newChangeBatch returns a pointer to a ChangeBatch object
// with each field set by the resource's corresponding spec field.
func (rm *resourceManager) newChangeBatch(
action svcsdktypes.ChangeAction,
recordSet *svcsdktypes.ResourceRecordSet,
) *svcsdktypes.ChangeBatch {
change := svcsdktypes.Change{}
change.Action = svcsdktypes.ChangeAction(action)
change.ResourceRecordSet = recordSet
return &svcsdktypes.ChangeBatch{
Changes: []svcsdktypes.Change{change},
}
}
// customUpdateRecordSet is the custom implementation for
// RecordSet resource's update operation.
func (rm *resourceManager) customUpdateRecordSet(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (updated *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateRecordSet")
defer func() {
exit(err)
}()
// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := desired.ko.DeepCopy()
input := &svcsdk.ChangeResourceRecordSetsInput{}
input.HostedZoneId = desired.ko.Spec.HostedZoneID
action := svcsdktypes.ChangeActionUpsert
recordSet, err := rm.newResourceRecordSet(ctx, desired)
if err != nil {
return nil, err
}
changeBatch := rm.newChangeBatch(action, recordSet)
input.ChangeBatch = changeBatch
var resp *svcsdk.ChangeResourceRecordSetsOutput
resp, err = rm.sdkapi.ChangeResourceRecordSets(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "ChangeResourceRecordSets", err)
// The previous change batch is no longer representative of the newly applied change.
if err != nil {
ko.Status.ID = nil
ko.Status.Status = nil
ko.Status.SubmittedAt = nil
return &resource{ko}, err
}
if resp.ChangeInfo.Id != nil {
ko.Status.ID = resp.ChangeInfo.Id
} else {
ko.Status.ID = nil
}
if resp.ChangeInfo.Status != "" {
ko.Status.Status = aws.String(string(resp.ChangeInfo.Status))
} else {
ko.Status.Status = nil
}
if resp.ChangeInfo.SubmittedAt != nil {
ko.Status.SubmittedAt = &metav1.Time{*resp.ChangeInfo.SubmittedAt}
} else {
ko.Status.SubmittedAt = nil
}
rm.setStatusDefaults(ko)
// Ensure that the status eventually becomes INSYNC after an update has been detected
err = rm.syncStatus(ctx, ko)
if err != nil {
return nil, err
}
if ko.Status.Status == nil || svcsdktypes.ChangeStatus(*ko.Status.Status) == svcsdktypes.ChangeStatusPending {
ackcondition.SetSynced(&resource{ko}, corev1.ConditionFalse, nil, nil)
}
return &resource{ko}, nil
}
// syncStatus will sync the state of record sets. PENDING indicates that the
// request has not yet been applied to all Route53 DNS servers and INSYNC
// represents that the request has been fully propagated to all DNS servers.
func (rm *resourceManager) syncStatus(
ctx context.Context,
ko *svcapitypes.RecordSet,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncStatus")
defer func() {
exit(err)
}()
// It is possible to hit this condition if the previous change batch was
// invalid (e.g. bad parameter). In such cases, a new change ID will be
// assigned after going through a successful update.
if ko.Status.ID == nil {
ko.Status.Status = nil
return nil
}
changeInput := &svcsdk.GetChangeInput{}
changeInput.Id = ko.Status.ID
resp, err := rm.sdkapi.GetChange(ctx, changeInput)
rm.metrics.RecordAPICall("READ_ONE", "GetChange", err)
if err != nil {
return err
}
status := string(resp.ChangeInfo.Status)
ko.Status.Status = &status
return nil
}
// getHostedZoneDomain gets the domain name of the hosted zone.
func (rm *resourceManager) getHostedZoneDomain(
ctx context.Context,
r *resource,
) (string, error) {
var err error
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.getHostedZoneDomain")
defer func() {
exit(err)
}()
input := &svcsdk.GetHostedZoneInput{}
if r.ko.Spec.HostedZoneID != nil {
input.Id = r.ko.Spec.HostedZoneID
}
resp, err := rm.sdkapi.GetHostedZone(ctx, input)
rm.metrics.RecordAPICall("READ_ONE", "GetHostedZone", err)
if err != nil {
var notFound *svcsdktypes.NoSuchHostedZone
if errors.As(err, ¬Found) {
return "", ackerr.NotFound
}
return "", err
}
return *resp.HostedZone.Name, nil
}
// getDNSName returns the appended value of the user supplied subdomain and the
// domain of the hosted zone. If a subdomain is not supplied, the full DNS name
// will just equate to the hosted zone domain name.
func (rm *resourceManager) getDNSName(
r *resource,
domain string,
) (dnsName string) {
if r.ko.Spec.Name != nil {
dnsName += *r.ko.Spec.Name + "."
}
dnsName += domain
return dnsName
}
// decodeRecordName decodes special characters from the DNSName of a record set.
// ListResourceRecordSets returns the DNS names with an encoded value for "*",
// so the DNSName needs to be decoded before comparing with our spec values.
func decodeRecordName(name string) string {
if strings.Contains(name, "\\052") {
return strings.Replace(name, "\\052", "*", -1)
}
return name
}