/
route53.go
346 lines (312 loc) · 9.68 KB
/
route53.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
335
336
337
338
339
340
341
342
343
344
345
346
package aws
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
awsutil "github.com/openshift/hypershift/cmd/infra/aws/util"
"github.com/openshift/hypershift/cmd/log"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
)
func (o *CreateInfraOptions) LookupPublicZone(ctx context.Context, client route53iface.Route53API) (string, error) {
name := o.BaseDomain
id, err := lookupZone(ctx, client, name, false)
if err != nil {
log.Log.Error(err, "Public zone not found", "name", name)
return "", err
}
log.Log.Info("Found existing public zone", "name", name, "id", id)
return id, nil
}
func lookupZone(ctx context.Context, client route53iface.Route53API, name string, isPrivateZone bool) (string, error) {
var res *route53.HostedZone
f := func(resp *route53.ListHostedZonesOutput, lastPage bool) (shouldContinue bool) {
for idx, zone := range resp.HostedZones {
if zone.Config != nil && isPrivateZone == aws.BoolValue(zone.Config.PrivateZone) && strings.TrimSuffix(aws.StringValue(zone.Name), ".") == strings.TrimSuffix(name, ".") {
res = resp.HostedZones[idx]
return false
}
}
return !lastPage
}
if err := retryRoute53WithBackoff(ctx, func() error {
if err := client.ListHostedZonesPagesWithContext(ctx, &route53.ListHostedZonesInput{}, f); err != nil {
return err
}
return nil
}); err != nil {
return "", fmt.Errorf("failed to list hosted zones: %w", err)
}
if res == nil {
return "", fmt.Errorf("hosted zone %s not found", name)
}
return cleanZoneID(*res.Id), nil
}
func (o *CreateInfraOptions) CreatePrivateZone(ctx context.Context, client route53iface.Route53API, name, vpcID string) (string, error) {
id, err := lookupZone(ctx, client, name, true)
if err == nil {
log.Log.Info("Found existing private zone", "name", name, "id", id)
err := setSOAMinimum(ctx, client, id, name)
if err != nil {
return "", err
}
return id, err
}
var res *route53.CreateHostedZoneOutput
if err := retryRoute53WithBackoff(ctx, func() error {
callRef := fmt.Sprintf("%d", time.Now().Unix())
if output, err := client.CreateHostedZoneWithContext(ctx, &route53.CreateHostedZoneInput{
CallerReference: aws.String(callRef),
Name: aws.String(name),
HostedZoneConfig: &route53.HostedZoneConfig{
PrivateZone: aws.Bool(true),
},
VPC: &route53.VPC{
VPCId: aws.String(vpcID),
VPCRegion: aws.String(o.Region),
},
}); err != nil {
return err
} else {
res = output
return nil
}
}); err != nil {
return "", fmt.Errorf("failed to create hosted zone: %w", err)
}
if res == nil {
return "", fmt.Errorf("unexpected output from hosted zone creation")
}
id = cleanZoneID(*res.HostedZone.Id)
log.Log.Info("Created private zone", "name", name, "id", id)
err = setSOAMinimum(ctx, client, id, name)
if err != nil {
return "", err
}
return id, nil
}
func (o *DestroyInfraOptions) DestroyDNS(ctx context.Context, client route53iface.Route53API) []error {
var errs []error
errs = append(errs, o.CleanupPublicZone(ctx, client))
return errs
}
func (o *DestroyInfraOptions) DestroyPrivateZones(ctx context.Context, client route53iface.Route53API, vpcID *string) []error {
var output *route53.ListHostedZonesByVPCOutput
if err := retryRoute53WithBackoff(ctx, func() (err error) {
output, err = client.ListHostedZonesByVPCWithContext(ctx, &route53.ListHostedZonesByVPCInput{VPCId: vpcID, VPCRegion: aws.String(o.Region)})
return err
}); err != nil {
return []error{fmt.Errorf("failed to list hosted zones for vpc %s: %w", *vpcID, err)}
}
var errs []error
for _, zone := range output.HostedZoneSummaries {
id := cleanZoneID(*zone.HostedZoneId)
if err := deleteZone(ctx, id, client); err != nil {
return []error{fmt.Errorf("failed to delete private hosted zones for vpc %s: %w", *vpcID, err)}
}
log.Log.Info("Deleted private hosted zone", "id", id, "name", *zone.Name)
}
return errs
}
func (o *DestroyInfraOptions) CleanupPublicZone(ctx context.Context, client route53iface.Route53API) error {
name := o.BaseDomain
id, err := lookupZone(ctx, client, name, false)
if err != nil {
return nil
}
recordName := fmt.Sprintf("*.apps.%s.%s", o.Name, o.BaseDomain)
err = deleteRecord(ctx, client, id, recordName)
if err != nil {
if !isRoute53RecordNotFoundErr(err) {
return fmt.Errorf("failed to delete wildcard record from public zone %s: %w", id, err)
}
} else {
o.Log.Info("Deleted wildcard record from public hosted zone", "id", id, "name", recordName)
}
return nil
}
func setSOAMinimum(ctx context.Context, client route53iface.Route53API, id, name string) error {
recordSet, err := findRecord(ctx, client, id, name, "SOA")
if err != nil {
return err
}
if recordSet == nil || recordSet.ResourceRecords[0] == nil || recordSet.ResourceRecords[0].Value == nil {
return fmt.Errorf("SOA record for private zone %s not found: %w", name, err)
}
record := recordSet.ResourceRecords[0]
fields := strings.Split(*record.Value, " ")
if len(fields) != 7 {
return fmt.Errorf("SOA record value has %d fields, expected 7", len(fields))
}
fields[6] = "60"
record.Value = aws.String(strings.Join(fields, " "))
input := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(id),
ChangeBatch: &route53.ChangeBatch{
Changes: []*route53.Change{
{
Action: aws.String("UPSERT"),
ResourceRecordSet: recordSet,
},
},
},
}
_, err = client.ChangeResourceRecordSetsWithContext(ctx, input)
return err
}
func deleteZone(ctx context.Context, id string, client route53iface.Route53API) error {
err := deleteRecords(ctx, client, id)
if err != nil {
return fmt.Errorf("failed to delete hosted zone records: %v", err)
}
if _, err = client.DeleteHostedZoneWithContext(ctx, &route53.DeleteHostedZoneInput{
Id: aws.String(id),
}); err != nil {
return fmt.Errorf("failed to delete hosted zone: %v", err)
}
return nil
}
func deleteRecords(ctx context.Context, client route53iface.Route53API, id string) error {
lrrsi := &route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(id),
}
output, err := client.ListResourceRecordSetsWithContext(ctx, lrrsi)
if err != nil {
return err
}
if len(output.ResourceRecordSets) == 0 {
return nil
}
var changeBatch route53.ChangeBatch
var deleteRequired bool
for _, rrs := range output.ResourceRecordSets {
if *rrs.Type == "NS" || *rrs.Type == "SOA" {
continue
}
deleteRequired = true
changeBatch.Changes = append(changeBatch.Changes, &route53.Change{
Action: aws.String("DELETE"),
ResourceRecordSet: rrs,
})
}
if !deleteRequired {
return nil
}
crrsi := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(id),
ChangeBatch: &changeBatch,
}
if _, err := client.ChangeResourceRecordSetsWithContext(ctx, crrsi); err != nil {
return err
}
var deletedRecordNames []string
for _, changeBatch := range changeBatch.Changes {
deletedRecordNames = append(deletedRecordNames, *changeBatch.ResourceRecordSet.Name)
}
log.Log.Info("Deleted records from private hosted zone", "id", id, "names", deletedRecordNames)
return nil
}
func deleteRecord(ctx context.Context, client route53iface.Route53API, id, recordName string) error {
record, err := findRecord(ctx, client, id, recordName, "A")
if err != nil {
return err
}
// Change batch for deleting
changeBatch := &route53.ChangeBatch{
Changes: []*route53.Change{
{
Action: aws.String("DELETE"),
ResourceRecordSet: record,
},
},
}
input := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(id),
ChangeBatch: changeBatch,
}
_, err = client.ChangeResourceRecordSetsWithContext(ctx, input)
return err
}
func findRecord(ctx context.Context, client route53iface.Route53API, id, name string, recordType string) (*route53.ResourceRecordSet, error) {
recordName := fqdn(strings.ToLower(name))
input := &route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(id),
StartRecordName: aws.String(recordName),
StartRecordType: aws.String(recordType),
MaxItems: aws.String("1"),
}
var record *route53.ResourceRecordSet
err := client.ListResourceRecordSetsPagesWithContext(ctx, input, func(resp *route53.ListResourceRecordSetsOutput, lastPage bool) bool {
if len(resp.ResourceRecordSets) == 0 {
return false
}
recordSet := resp.ResourceRecordSets[0]
responseName := strings.ToLower(cleanRecordName(*recordSet.Name))
responseType := strings.ToUpper(*recordSet.Type)
if recordName != responseName {
return false
}
if recordType != responseType {
return false
}
record = recordSet
return false
})
if err != nil {
return nil, err
}
if record == nil {
return nil, fmt.Errorf("record not found")
}
return record, nil
}
func fqdn(name string) string {
n := len(name)
if n == 0 || name[n-1] == '.' {
return name
} else {
return name + "."
}
}
func cleanZoneID(ID string) string {
return strings.TrimPrefix(ID, "/hostedzone/")
}
func cleanRecordName(name string) string {
str := name
s, err := strconv.Unquote(`"` + str + `"`)
if err != nil {
return str
}
return s
}
func retryRoute53WithBackoff(ctx context.Context, fn func() error) error {
backoff := wait.Backoff{
Duration: 1 * time.Second,
Steps: 10,
Factor: 1.5,
}
retriable := func(e error) bool {
if !awsutil.IsErrorRetryable(e) {
return false
}
select {
case <-ctx.Done():
return false
default:
return true
}
}
// TODO: inspect the error for throttling details?
return retry.OnError(backoff, retriable, fn)
}
func isRoute53RecordNotFoundErr(err error) bool {
if err != nil && strings.Contains(err.Error(), "record not found") {
return true
}
return false
}