forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle.go
460 lines (370 loc) · 13.7 KB
/
google.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 google
import (
"context"
"fmt"
"sort"
"strings"
"time"
"cloud.google.com/go/compute/metadata"
"github.com/linki/instrumented_http"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2/google"
dns "google.golang.org/api/dns/v1"
googleapi "google.golang.org/api/googleapi"
"google.golang.org/api/option"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
"sigs.k8s.io/external-dns/provider"
)
const (
googleRecordTTL = 300
)
type managedZonesCreateCallInterface interface {
Do(opts ...googleapi.CallOption) (*dns.ManagedZone, error)
}
type managedZonesListCallInterface interface {
Pages(ctx context.Context, f func(*dns.ManagedZonesListResponse) error) error
}
type managedZonesServiceInterface interface {
Create(project string, managedzone *dns.ManagedZone) managedZonesCreateCallInterface
List(project string) managedZonesListCallInterface
}
type resourceRecordSetsListCallInterface interface {
Pages(ctx context.Context, f func(*dns.ResourceRecordSetsListResponse) error) error
}
type resourceRecordSetsClientInterface interface {
List(project string, managedZone string) resourceRecordSetsListCallInterface
}
type changesCreateCallInterface interface {
Do(opts ...googleapi.CallOption) (*dns.Change, error)
}
type changesServiceInterface interface {
Create(project string, managedZone string, change *dns.Change) changesCreateCallInterface
}
type resourceRecordSetsService struct {
service *dns.ResourceRecordSetsService
}
func (r resourceRecordSetsService) List(project string, managedZone string) resourceRecordSetsListCallInterface {
return r.service.List(project, managedZone)
}
type managedZonesService struct {
service *dns.ManagedZonesService
}
func (m managedZonesService) Create(project string, managedzone *dns.ManagedZone) managedZonesCreateCallInterface {
return m.service.Create(project, managedzone)
}
func (m managedZonesService) List(project string) managedZonesListCallInterface {
return m.service.List(project)
}
type changesService struct {
service *dns.ChangesService
}
func (c changesService) Create(project string, managedZone string, change *dns.Change) changesCreateCallInterface {
return c.service.Create(project, managedZone, change)
}
// GoogleProvider is an implementation of Provider for Google CloudDNS.
type GoogleProvider struct {
provider.BaseProvider
// The Google project to work in
project string
// Enabled dry-run will print any modifying actions rather than execute them.
dryRun bool
// Max batch size to submit to Google Cloud DNS per transaction.
batchChangeSize int
// Interval between batch updates.
batchChangeInterval time.Duration
// only consider hosted zones managing domains ending in this suffix
domainFilter endpoint.DomainFilter
// only consider hosted zones ending with this zone id
zoneIDFilter provider.ZoneIDFilter
// A client for managing resource record sets
resourceRecordSetsClient resourceRecordSetsClientInterface
// A client for managing hosted zones
managedZonesClient managedZonesServiceInterface
// A client for managing change sets
changesClient changesServiceInterface
// The context parameter to be passed for gcloud API calls.
ctx context.Context
}
// NewGoogleProvider initializes a new Google CloudDNS based Provider.
func NewGoogleProvider(ctx context.Context, project string, domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter, batchChangeSize int, batchChangeInterval time.Duration, dryRun bool) (*GoogleProvider, error) {
gcloud, err := google.DefaultClient(ctx, dns.NdevClouddnsReadwriteScope)
if err != nil {
return nil, err
}
gcloud = instrumented_http.NewClient(gcloud, &instrumented_http.Callbacks{
PathProcessor: func(path string) string {
parts := strings.Split(path, "/")
return parts[len(parts)-1]
},
})
dnsClient, err := dns.NewService(ctx, option.WithHTTPClient(gcloud))
if err != nil {
return nil, err
}
if project == "" {
mProject, mErr := metadata.ProjectID()
if mErr == nil {
log.Infof("Google project auto-detected: %s", mProject)
project = mProject
}
}
provider := &GoogleProvider{
project: project,
dryRun: dryRun,
batchChangeSize: batchChangeSize,
batchChangeInterval: batchChangeInterval,
domainFilter: domainFilter,
zoneIDFilter: zoneIDFilter,
resourceRecordSetsClient: resourceRecordSetsService{dnsClient.ResourceRecordSets},
managedZonesClient: managedZonesService{dnsClient.ManagedZones},
changesClient: changesService{dnsClient.Changes},
ctx: ctx,
}
return provider, nil
}
// Zones returns the list of hosted zones.
func (p *GoogleProvider) Zones(ctx context.Context) (map[string]*dns.ManagedZone, error) {
zones := make(map[string]*dns.ManagedZone)
f := func(resp *dns.ManagedZonesListResponse) error {
for _, zone := range resp.ManagedZones {
if p.domainFilter.Match(zone.DnsName) && (p.zoneIDFilter.Match(fmt.Sprintf("%v", zone.Id)) || p.zoneIDFilter.Match(fmt.Sprintf("%v", zone.Name))) {
zones[zone.Name] = zone
log.Debugf("Matched %s (zone: %s)", zone.DnsName, zone.Name)
} else {
log.Debugf("Filtered %s (zone: %s)", zone.DnsName, zone.Name)
}
}
return nil
}
log.Debugf("Matching zones against domain filters: %v", p.domainFilter.Filters)
if err := p.managedZonesClient.List(p.project).Pages(ctx, f); err != nil {
return nil, err
}
if len(zones) == 0 {
if p.domainFilter.IsConfigured() {
log.Warnf("No zones in the project, %s, match domain filters: %v", p.project, p.domainFilter.Filters)
} else {
log.Warnf("No zones found in the project, %s", p.project)
}
}
for _, zone := range zones {
log.Debugf("Considering zone: %s (domain: %s)", zone.Name, zone.DnsName)
}
return zones, nil
}
// Records returns the list of records in all relevant zones.
func (p *GoogleProvider) Records(ctx context.Context) (endpoints []*endpoint.Endpoint, _ error) {
zones, err := p.Zones(ctx)
if err != nil {
return nil, err
}
f := func(resp *dns.ResourceRecordSetsListResponse) error {
for _, r := range resp.Rrsets {
if !provider.SupportedRecordType(r.Type) {
continue
}
endpoints = append(endpoints, endpoint.NewEndpointWithTTL(r.Name, r.Type, endpoint.TTL(r.Ttl), r.Rrdatas...))
}
return nil
}
for _, z := range zones {
if err := p.resourceRecordSetsClient.List(p.project, z.Name).Pages(ctx, f); err != nil {
return nil, err
}
}
return endpoints, nil
}
// CreateRecords creates a given set of DNS records in the given hosted zone.
func (p *GoogleProvider) CreateRecords(endpoints []*endpoint.Endpoint) error {
change := &dns.Change{}
change.Additions = append(change.Additions, p.newFilteredRecords(endpoints)...)
return p.submitChange(p.ctx, change)
}
// UpdateRecords updates a given set of old records to a new set of records in a given hosted zone.
func (p *GoogleProvider) UpdateRecords(records, oldRecords []*endpoint.Endpoint) error {
change := &dns.Change{}
change.Additions = append(change.Additions, p.newFilteredRecords(records)...)
change.Deletions = append(change.Deletions, p.newFilteredRecords(oldRecords)...)
return p.submitChange(p.ctx, change)
}
// DeleteRecords deletes a given set of DNS records in a given zone.
func (p *GoogleProvider) DeleteRecords(endpoints []*endpoint.Endpoint) error {
change := &dns.Change{}
change.Deletions = append(change.Deletions, p.newFilteredRecords(endpoints)...)
return p.submitChange(p.ctx, change)
}
// ApplyChanges applies a given set of changes in a given zone.
func (p *GoogleProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
change := &dns.Change{}
change.Additions = append(change.Additions, p.newFilteredRecords(changes.Create)...)
change.Additions = append(change.Additions, p.newFilteredRecords(changes.UpdateNew)...)
change.Deletions = append(change.Deletions, p.newFilteredRecords(changes.UpdateOld)...)
change.Deletions = append(change.Deletions, p.newFilteredRecords(changes.Delete)...)
return p.submitChange(ctx, change)
}
// newFilteredRecords returns a collection of RecordSets based on the given endpoints and domainFilter.
func (p *GoogleProvider) newFilteredRecords(endpoints []*endpoint.Endpoint) []*dns.ResourceRecordSet {
records := []*dns.ResourceRecordSet{}
for _, endpoint := range endpoints {
if p.domainFilter.Match(endpoint.DNSName) {
records = append(records, newRecord(endpoint))
}
}
return records
}
// submitChange takes a zone and a Change and sends it to Google.
func (p *GoogleProvider) submitChange(ctx context.Context, change *dns.Change) error {
if len(change.Additions) == 0 && len(change.Deletions) == 0 {
log.Info("All records are already up to date")
return nil
}
zones, err := p.Zones(ctx)
if err != nil {
return err
}
// separate into per-zone change sets to be passed to the API.
changes := separateChange(zones, change)
for zone, change := range changes {
for batch, c := range batchChange(change, p.batchChangeSize) {
log.Infof("Change zone: %v batch #%d", zone, batch)
for _, del := range c.Deletions {
log.Infof("Del records: %s %s %s %d", del.Name, del.Type, del.Rrdatas, del.Ttl)
}
for _, add := range c.Additions {
log.Infof("Add records: %s %s %s %d", add.Name, add.Type, add.Rrdatas, add.Ttl)
}
if p.dryRun {
continue
}
if _, err := p.changesClient.Create(p.project, zone, c).Do(); err != nil {
return err
}
time.Sleep(p.batchChangeInterval)
}
}
return nil
}
// batchChange separates a zone in multiple transaction.
func batchChange(change *dns.Change, batchSize int) []*dns.Change {
changes := []*dns.Change{}
if batchSize == 0 {
return append(changes, change)
}
type dnsChange struct {
additions []*dns.ResourceRecordSet
deletions []*dns.ResourceRecordSet
}
changesByName := map[string]*dnsChange{}
for _, a := range change.Additions {
change, ok := changesByName[a.Name]
if !ok {
change = &dnsChange{}
changesByName[a.Name] = change
}
change.additions = append(change.additions, a)
}
for _, a := range change.Deletions {
change, ok := changesByName[a.Name]
if !ok {
change = &dnsChange{}
changesByName[a.Name] = change
}
change.deletions = append(change.deletions, a)
}
names := make([]string, 0)
for v := range changesByName {
names = append(names, v)
}
sort.Strings(names)
currentChange := &dns.Change{}
var totalChanges int
for _, name := range names {
c := changesByName[name]
totalChangesByName := len(c.additions) + len(c.deletions)
if totalChangesByName > batchSize {
log.Warnf("Total changes for %s exceeds max batch size of %d, total changes: %d", name,
batchSize, totalChangesByName)
continue
}
if totalChanges+totalChangesByName > batchSize {
totalChanges = 0
changes = append(changes, currentChange)
currentChange = &dns.Change{}
}
currentChange.Additions = append(currentChange.Additions, c.additions...)
currentChange.Deletions = append(currentChange.Deletions, c.deletions...)
totalChanges += totalChangesByName
}
if totalChanges > 0 {
changes = append(changes, currentChange)
}
return changes
}
// separateChange separates a multi-zone change into a single change per zone.
func separateChange(zones map[string]*dns.ManagedZone, change *dns.Change) map[string]*dns.Change {
changes := make(map[string]*dns.Change)
zoneNameIDMapper := provider.ZoneIDName{}
for _, z := range zones {
zoneNameIDMapper[z.Name] = z.DnsName
changes[z.Name] = &dns.Change{
Additions: []*dns.ResourceRecordSet{},
Deletions: []*dns.ResourceRecordSet{},
}
}
for _, a := range change.Additions {
if zoneName, _ := zoneNameIDMapper.FindZone(provider.EnsureTrailingDot(a.Name)); zoneName != "" {
changes[zoneName].Additions = append(changes[zoneName].Additions, a)
} else {
log.Warnf("No matching zone for record addition: %s %s %s %d", a.Name, a.Type, a.Rrdatas, a.Ttl)
}
}
for _, d := range change.Deletions {
if zoneName, _ := zoneNameIDMapper.FindZone(provider.EnsureTrailingDot(d.Name)); zoneName != "" {
changes[zoneName].Deletions = append(changes[zoneName].Deletions, d)
} else {
log.Warnf("No matching zone for record deletion: %s %s %s %d", d.Name, d.Type, d.Rrdatas, d.Ttl)
}
}
// separating a change could lead to empty sub changes, remove them here.
for zone, change := range changes {
if len(change.Additions) == 0 && len(change.Deletions) == 0 {
delete(changes, zone)
}
}
return changes
}
// newRecord returns a RecordSet based on the given endpoint.
func newRecord(ep *endpoint.Endpoint) *dns.ResourceRecordSet {
// TODO(linki): works around appending a trailing dot to TXT records. I think
// we should go back to storing DNS names with a trailing dot internally. This
// way we can use it has is here and trim it off if it exists when necessary.
targets := make([]string, len(ep.Targets))
copy(targets, []string(ep.Targets))
if ep.RecordType == endpoint.RecordTypeCNAME {
targets[0] = provider.EnsureTrailingDot(targets[0])
}
// no annotation results in a Ttl of 0, default to 300 for backwards-compatibility
var ttl int64 = googleRecordTTL
if ep.RecordTTL.IsConfigured() {
ttl = int64(ep.RecordTTL)
}
return &dns.ResourceRecordSet{
Name: provider.EnsureTrailingDot(ep.DNSName),
Rrdatas: targets,
Ttl: ttl,
Type: ep.RecordType,
}
}