forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvinyldns.go
270 lines (227 loc) · 6.94 KB
/
vinyldns.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
/*
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 (
"context"
"fmt"
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/vinyldns/go-vinyldns/vinyldns"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
)
const (
vinyldnsCreate = "CREATE"
vinyldnsDelete = "DELETE"
vinyldnsUpdate = "UPDATE"
vinyldnsRecordTTL = 300
)
type vinyldnsZoneInterface interface {
Zones() ([]vinyldns.Zone, error)
RecordSets(id string) ([]vinyldns.RecordSet, error)
RecordSet(zoneID, recordSetID string) (vinyldns.RecordSet, error)
RecordSetCreate(rs *vinyldns.RecordSet) (*vinyldns.RecordSetUpdateResponse, error)
RecordSetUpdate(rs *vinyldns.RecordSet) (*vinyldns.RecordSetUpdateResponse, error)
RecordSetDelete(zoneID, recordSetID string) (*vinyldns.RecordSetUpdateResponse, error)
}
type vinyldnsProvider struct {
client vinyldnsZoneInterface
zoneFilter ZoneIDFilter
domainFilter endpoint.DomainFilter
dryRun bool
}
type vinyldnsChange struct {
Action string
ResourceRecordSet vinyldns.RecordSet
}
// NewVinylDNSProvider provides support for VinylDNS records
func NewVinylDNSProvider(domainFilter endpoint.DomainFilter, zoneFilter ZoneIDFilter, dryRun bool) (Provider, error) {
_, ok := os.LookupEnv("VINYLDNS_ACCESS_KEY")
if !ok {
return nil, fmt.Errorf("no vinyldns access key found")
}
client := vinyldns.NewClientFromEnv()
return &vinyldnsProvider{
client: client,
dryRun: dryRun,
zoneFilter: zoneFilter,
domainFilter: domainFilter,
}, nil
}
func (p *vinyldnsProvider) Records(ctx context.Context) (endpoints []*endpoint.Endpoint, _ error) {
zones, err := p.client.Zones()
if err != nil {
return nil, err
}
for _, zone := range zones {
if !p.zoneFilter.Match(zone.ID) {
continue
}
if !p.domainFilter.Match(zone.Name) {
continue
}
log.Infof(fmt.Sprintf("Zone: [%s:%s]", zone.ID, zone.Name))
records, err := p.client.RecordSets(zone.ID)
if err != nil {
return nil, err
}
for _, r := range records {
if supportedRecordType(r.Type) {
recordsCount := len(r.Records)
log.Debugf(fmt.Sprintf("%s.%s.%d.%s", r.Name, r.Type, recordsCount, zone.Name))
//TODO: AAAA Records
if len(r.Records) > 0 {
targets := make([]string, len(r.Records))
for idx, rr := range r.Records {
switch r.Type {
case "A":
targets[idx] = rr.Address
case "CNAME":
targets[idx] = rr.CName
case "TXT":
targets[idx] = rr.Text
}
}
endpoints = append(endpoints, endpoint.NewEndpointWithTTL(r.Name+"."+zone.Name, r.Type, endpoint.TTL(r.TTL), targets...))
}
}
}
}
return endpoints, nil
}
func vinyldnsSuitableZone(hostname string, zones []vinyldns.Zone) *vinyldns.Zone {
var zone *vinyldns.Zone
for _, z := range zones {
log.Debugf("hostname: %s and zoneName: %s", hostname, z.Name)
// Adding a . as vinyl appends it to each zone record
if strings.HasSuffix(hostname+".", z.Name) {
zone = &z
break
}
}
return zone
}
func (p *vinyldnsProvider) submitChanges(changes []*vinyldnsChange) error {
if len(changes) == 0 {
log.Infof("All records are already up to date")
return nil
}
zones, err := p.client.Zones()
if err != nil {
return err
}
for _, change := range changes {
zone := vinyldnsSuitableZone(change.ResourceRecordSet.Name, zones)
if zone == nil {
log.Debugf("Skipping record %s because no hosted zone matching record DNS Name was detected", change.ResourceRecordSet.Name)
continue
}
change.ResourceRecordSet.Name = strings.TrimSuffix(change.ResourceRecordSet.Name+".", "."+zone.Name)
change.ResourceRecordSet.ZoneID = zone.ID
log.Infof("Changing records: %s %v in zone: %s", change.Action, change.ResourceRecordSet, zone.Name)
if !p.dryRun {
switch change.Action {
case vinyldnsCreate:
_, err := p.client.RecordSetCreate(&change.ResourceRecordSet)
if err != nil {
return err
}
case vinyldnsUpdate:
recordID, err := p.findRecordSetID(zone.ID, change.ResourceRecordSet.Name)
if err != nil {
return err
}
change.ResourceRecordSet.ID = recordID
_, err = p.client.RecordSetUpdate(&change.ResourceRecordSet)
if err != nil {
return err
}
case vinyldnsDelete:
recordID, err := p.findRecordSetID(zone.ID, change.ResourceRecordSet.Name)
if err != nil {
return err
}
_, err = p.client.RecordSetDelete(zone.ID, recordID)
if err != nil {
return err
}
}
}
}
return nil
}
func (p *vinyldnsProvider) findRecordSetID(zoneID string, recordSetName string) (recordID string, err error) {
records, err := p.client.RecordSets(zoneID)
if err != nil {
return "", err
}
for _, r := range records {
if r.Name == recordSetName {
return r.ID, nil
}
}
return "", fmt.Errorf("Record not found")
}
func (p *vinyldnsProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
combinedChanges := make([]*vinyldnsChange, 0, len(changes.Create)+len(changes.UpdateNew)+len(changes.Delete))
combinedChanges = append(combinedChanges, newVinylDNSChanges(vinyldnsCreate, changes.Create)...)
combinedChanges = append(combinedChanges, newVinylDNSChanges(vinyldnsUpdate, changes.UpdateNew)...)
combinedChanges = append(combinedChanges, newVinylDNSChanges(vinyldnsDelete, changes.Delete)...)
return p.submitChanges(combinedChanges)
}
// newVinylDNSChanges returns a collection of Changes based on the given records and action.
func newVinylDNSChanges(action string, endpoints []*endpoint.Endpoint) []*vinyldnsChange {
changes := make([]*vinyldnsChange, 0, len(endpoints))
for _, e := range endpoints {
changes = append(changes, newVinylDNSChange(action, e))
}
return changes
}
func newVinylDNSChange(action string, endpoint *endpoint.Endpoint) *vinyldnsChange {
var ttl = vinyldnsRecordTTL
if endpoint.RecordTTL.IsConfigured() {
ttl = int(endpoint.RecordTTL)
}
records := []vinyldns.Record{}
// TODO: AAAA
if endpoint.RecordType == "CNAME" {
records = []vinyldns.Record{
{
CName: endpoint.Targets[0],
},
}
} else if endpoint.RecordType == "TXT" {
records = []vinyldns.Record{
{
Text: endpoint.Targets[0],
},
}
} else if endpoint.RecordType == "A" {
records = []vinyldns.Record{
{
Address: endpoint.Targets[0],
},
}
}
change := &vinyldnsChange{
Action: action,
ResourceRecordSet: vinyldns.RecordSet{
Name: endpoint.DNSName,
Type: endpoint.RecordType,
TTL: ttl,
Records: records,
},
}
return change
}