forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
293 lines (228 loc) · 8.64 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
/*
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 provider
import (
log "github.com/Sirupsen/logrus"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/dns/v1"
googleapi "google.golang.org/api/googleapi"
"github.com/kubernetes-incubator/external-dns/endpoint"
"github.com/kubernetes-incubator/external-dns/plan"
)
type managedZonesCreateCallInterface interface {
Do(opts ...googleapi.CallOption) (*dns.ManagedZone, error)
}
type managedZonesDeleteCallInterface interface {
Do(opts ...googleapi.CallOption) error
}
type managedZonesListCallInterface interface {
Pages(ctx context.Context, f func(*dns.ManagedZonesListResponse) error) error
}
type managedZonesServiceInterface interface {
Create(project string, managedzone *dns.ManagedZone) managedZonesCreateCallInterface
Delete(project string, managedZone string) managedZonesDeleteCallInterface
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) Delete(project string, managedZone string) managedZonesDeleteCallInterface {
return m.service.Delete(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 {
// The Google project to work in
project string
// Enabled dry-run will print any modifying actions rather than execute them.
dryRun bool
// 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
}
// NewGoogleProvider initializes a new Google CloudDNS based Provider.
func NewGoogleProvider(project string, dryRun bool) (Provider, error) {
gcloud, err := google.DefaultClient(context.TODO(), dns.NdevClouddnsReadwriteScope)
if err != nil {
return nil, err
}
dnsClient, err := dns.New(gcloud)
if err != nil {
return nil, err
}
provider := &googleProvider{
project: project,
dryRun: dryRun,
resourceRecordSetsClient: resourceRecordSetsService{dnsClient.ResourceRecordSets},
managedZonesClient: managedZonesService{dnsClient.ManagedZones},
changesClient: changesService{dnsClient.Changes},
}
return provider, nil
}
// Zones returns the list of hosted zones.
func (p *googleProvider) Zones() (zones []*dns.ManagedZone, _ error) {
f := func(resp *dns.ManagedZonesListResponse) error {
// each page is processed sequentially, no need for a mutex here.
zones = append(zones, resp.ManagedZones...)
return nil
}
err := p.managedZonesClient.List(p.project).Pages(context.TODO(), f)
if err != nil {
return nil, err
}
return zones, nil
}
// CreateZone creates a hosted zone given a name.
func (p *googleProvider) CreateZone(name, domain string) error {
zone := &dns.ManagedZone{
Name: name,
DnsName: domain,
Description: "Automatically managed zone by kubernetes.io/external-dns",
}
_, err := p.managedZonesClient.Create(p.project, zone).Do()
if err != nil {
return err
}
return nil
}
// DeleteZone deletes a hosted zone given a name.
func (p *googleProvider) DeleteZone(name string) error {
err := p.managedZonesClient.Delete(p.project, name).Do()
if err != nil {
return err
}
return nil
}
// Records returns the list of A records in a given hosted zone.
func (p *googleProvider) Records(zone string) (endpoints []*endpoint.Endpoint, _ error) {
f := func(resp *dns.ResourceRecordSetsListResponse) error {
for _, r := range resp.Rrsets {
// TODO(linki, ownership): Remove once ownership system is in place.
// See: https://github.com/kubernetes-incubator/external-dns/pull/122/files/74e2c3d3e237411e619aefc5aab694742001cdec#r109863370
switch r.Type {
case "A", "CNAME", "TXT":
break
default:
continue
}
for _, rr := range r.Rrdatas {
// each page is processed sequentially, no need for a mutex here.
endpoints = append(endpoints, endpoint.NewEndpoint(r.Name, rr, r.Type))
}
}
return nil
}
err := p.resourceRecordSetsClient.List(p.project, zone).Pages(context.TODO(), f)
if 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(zone string, endpoints []*endpoint.Endpoint) error {
change := &dns.Change{}
change.Additions = append(change.Additions, newRecords(endpoints)...)
return p.submitChange(zone, change)
}
// UpdateRecords updates a given set of old records to a new set of records in a given hosted zone.
func (p *googleProvider) UpdateRecords(zone string, records, oldRecords []*endpoint.Endpoint) error {
change := &dns.Change{}
change.Additions = append(change.Additions, newRecords(records)...)
change.Deletions = append(change.Deletions, newRecords(oldRecords)...)
return p.submitChange(zone, change)
}
// DeleteRecords deletes a given set of DNS records in a given zone.
func (p *googleProvider) DeleteRecords(zone string, endpoints []*endpoint.Endpoint) error {
change := &dns.Change{}
change.Deletions = append(change.Deletions, newRecords(endpoints)...)
return p.submitChange(zone, change)
}
// ApplyChanges applies a given set of changes in a given zone.
func (p *googleProvider) ApplyChanges(zone string, changes *plan.Changes) error {
change := &dns.Change{}
change.Additions = append(change.Additions, newRecords(changes.Create)...)
change.Additions = append(change.Additions, newRecords(changes.UpdateNew)...)
change.Deletions = append(change.Deletions, newRecords(changes.UpdateOld)...)
change.Deletions = append(change.Deletions, newRecords(changes.Delete)...)
return p.submitChange(zone, change)
}
// submitChange takes a zone and a Change and sends it to Google.
func (p *googleProvider) submitChange(zone string, change *dns.Change) error {
if p.dryRun {
for _, del := range change.Deletions {
log.Infof("Del records: %s %s %s", del.Name, del.Type, del.Rrdatas)
}
for _, add := range change.Additions {
log.Infof("Add records: %s %s %s", add.Name, add.Type, add.Rrdatas)
}
return nil
}
if len(change.Additions) == 0 && len(change.Deletions) == 0 {
return nil
}
_, err := p.changesClient.Create(p.project, zone, change).Do()
if err != nil {
return err
}
return nil
}
// newRecords returns a collection of RecordSets based on the given endpoints.
func newRecords(endpoints []*endpoint.Endpoint) []*dns.ResourceRecordSet {
records := make([]*dns.ResourceRecordSet, len(endpoints))
for i, endpoint := range endpoints {
records[i] = newRecord(endpoint)
}
return records
}
// newRecord returns a RecordSet based on the given endpoint.
func newRecord(endpoint *endpoint.Endpoint) *dns.ResourceRecordSet {
return &dns.ResourceRecordSet{
Name: ensureTrailingDot(endpoint.DNSName),
Rrdatas: []string{ensureTrailingDot(endpoint.Target)},
Ttl: 300,
Type: suitableType(endpoint),
}
}