-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathovh.go
448 lines (377 loc) · 12.5 KB
/
ovh.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
/*
Copyright 2020 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 ovh
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/miekg/dns"
"github.com/ovh/go-ovh/ovh"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/pkg/apis/externaldns"
"sigs.k8s.io/external-dns/plan"
"sigs.k8s.io/external-dns/provider"
"go.uber.org/ratelimit"
)
const (
ovhDefaultTTL = 0
ovhCreate = iota
ovhDelete
)
var (
// ErrRecordToMutateNotFound when ApplyChange has to update/delete and didn't found the record in the existing zone (Change with no record ID)
ErrRecordToMutateNotFound = errors.New("record to mutate not found in current zone")
// ErrNoDryRun No dry run support for the moment
ErrNoDryRun = errors.New("dry run not supported")
)
// OVHProvider is an implementation of Provider for OVH DNS.
type OVHProvider struct {
provider.BaseProvider
client ovhClient
apiRateLimiter ratelimit.Limiter
domainFilter endpoint.DomainFilter
DryRun bool
// UseCache controls if the OVHProvider will cache records in memory, and serve them
// without recontacting the OVHcloud API if the SOA of the domain zone hasn't changed.
// Note that, when disabling cache, OVHcloud API has rate-limiting that will hit if
// your refresh rate/number of records is too big, which might cause issue with the
// provider.
// Default value: true
UseCache bool
cacheInstance *cache.Cache
dnsClient dnsClient
}
type ovhClient interface {
Post(string, interface{}, interface{}) error
Get(string, interface{}) error
Delete(string, interface{}) error
}
type dnsClient interface {
ExchangeContext(ctx context.Context, m *dns.Msg, a string) (*dns.Msg, time.Duration, error)
}
type ovhRecordFields struct {
FieldType string `json:"fieldType"`
SubDomain string `json:"subDomain"`
TTL int64 `json:"ttl"`
Target string `json:"target"`
}
type ovhRecord struct {
ovhRecordFields
ID uint64 `json:"id"`
Zone string `json:"zone"`
}
type ovhChange struct {
ovhRecord
Action int
}
// NewOVHProvider initializes a new OVH DNS based Provider.
func NewOVHProvider(ctx context.Context, domainFilter endpoint.DomainFilter, endpoint string, apiRateLimit int, dryRun bool) (*OVHProvider, error) {
client, err := ovh.NewEndpointClient(endpoint)
if err != nil {
return nil, err
}
client.UserAgent = externaldns.Version
// TODO: Add Dry Run support
if dryRun {
return nil, ErrNoDryRun
}
return &OVHProvider{
client: client,
domainFilter: domainFilter,
apiRateLimiter: ratelimit.New(apiRateLimit),
DryRun: dryRun,
cacheInstance: cache.New(cache.NoExpiration, cache.NoExpiration),
dnsClient: new(dns.Client),
UseCache: true,
}, nil
}
// Records returns the list of records in all relevant zones.
func (p *OVHProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
_, records, err := p.zonesRecords(ctx)
if err != nil {
return nil, err
}
endpoints := ovhGroupByNameAndType(records)
log.Infof("OVH: %d endpoints have been found", len(endpoints))
return endpoints, nil
}
// ApplyChanges applies a given set of changes in a given zone.
func (p *OVHProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) (err error) {
zones, records, err := p.zonesRecords(ctx)
if err != nil {
return provider.NewSoftError(err)
}
zonesChangeUniques := map[string]bool{}
// Always refresh zones even in case of errors.
defer func() {
log.Debugf("OVH: %d zones will be refreshed", len(zonesChangeUniques))
eg, _ := errgroup.WithContext(ctx)
for zone := range zonesChangeUniques {
// This is necessary because the loop variable zone is reused in each iteration of the loop,
// and without this line, the goroutines launched by eg.Go would all reference the same zone variable.
zone := zone
eg.Go(func() error { return p.refresh(zone) })
}
if e := eg.Wait(); e != nil && err == nil { // return the error only if there is no error during the changes
err = provider.NewSoftError(e)
}
}()
allChanges := make([]ovhChange, 0, countTargets(changes.Create, changes.UpdateNew, changes.UpdateOld, changes.Delete))
allChanges = append(allChanges, newOvhChange(ovhCreate, changes.Create, zones, records)...)
allChanges = append(allChanges, newOvhChange(ovhCreate, changes.UpdateNew, zones, records)...)
allChanges = append(allChanges, newOvhChange(ovhDelete, changes.UpdateOld, zones, records)...)
allChanges = append(allChanges, newOvhChange(ovhDelete, changes.Delete, zones, records)...)
log.Infof("OVH: %d changes will be done", len(allChanges))
eg, _ := errgroup.WithContext(ctx)
for _, change := range allChanges {
change := change
zonesChangeUniques[change.Zone] = true
eg.Go(func() error { return p.change(change) })
}
if err := eg.Wait(); err != nil {
return provider.NewSoftError(err)
}
return nil
}
func (p *OVHProvider) refresh(zone string) error {
log.Debugf("OVH: Refresh %s zone", zone)
// Zone has been altered so we invalidate the cache
// so that the next run will reload it.
p.invalidateCache(zone)
p.apiRateLimiter.Take()
if err := p.client.Post(fmt.Sprintf("/domain/zone/%s/refresh", zone), nil, nil); err != nil {
return provider.NewSoftError(err)
}
return nil
}
func (p *OVHProvider) change(change ovhChange) error {
p.apiRateLimiter.Take()
switch change.Action {
case ovhCreate:
log.Debugf("OVH: Add an entry to %s", change.String())
return p.client.Post(fmt.Sprintf("/domain/zone/%s/record", change.Zone), change.ovhRecordFields, nil)
case ovhDelete:
if change.ID == 0 {
return ErrRecordToMutateNotFound
}
log.Debugf("OVH: Delete an entry to %s", change.String())
return p.client.Delete(fmt.Sprintf("/domain/zone/%s/record/%d", change.Zone, change.ID), nil)
}
return nil
}
func (p *OVHProvider) invalidateCache(zone string) {
p.cacheInstance.Delete(zone + "#soa")
}
func (p *OVHProvider) zonesRecords(ctx context.Context) ([]string, []ovhRecord, error) {
var allRecords []ovhRecord
zones, err := p.zones()
if err != nil {
return nil, nil, provider.NewSoftError(err)
}
chRecords := make(chan []ovhRecord, len(zones))
eg, ctx := errgroup.WithContext(ctx)
for _, zone := range zones {
zone := zone
eg.Go(func() error { return p.records(&ctx, &zone, chRecords) })
}
if err := eg.Wait(); err != nil {
return nil, nil, provider.NewSoftError(err)
}
close(chRecords)
for records := range chRecords {
allRecords = append(allRecords, records...)
}
return zones, allRecords, nil
}
func (p *OVHProvider) zones() ([]string, error) {
zones := []string{}
filteredZones := []string{}
p.apiRateLimiter.Take()
if err := p.client.Get("/domain/zone", &zones); err != nil {
return nil, err
}
for _, zoneName := range zones {
if p.domainFilter.Match(zoneName) {
filteredZones = append(filteredZones, zoneName)
}
}
log.Infof("OVH: %d zones found", len(filteredZones))
return filteredZones, nil
}
type ovhSoa struct {
Server string `json:"server"`
Serial uint32 `json:"serial"`
records []ovhRecord
}
func (p *OVHProvider) records(ctx *context.Context, zone *string, records chan<- []ovhRecord) error {
var recordsIds []uint64
ovhRecords := make([]ovhRecord, len(recordsIds))
eg, _ := errgroup.WithContext(*ctx)
if p.UseCache {
if cachedSoaItf, ok := p.cacheInstance.Get(*zone + "#soa"); ok {
cachedSoa := cachedSoaItf.(ovhSoa)
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(*zone), dns.TypeSOA)
in, _, err := p.dnsClient.ExchangeContext(*ctx, m, strings.TrimSuffix(cachedSoa.Server, ".")+":53")
if err == nil {
if s, ok := in.Answer[0].(*dns.SOA); ok {
// do something with t.Txt
if s.Serial == cachedSoa.Serial {
records <- cachedSoa.records
return nil
}
}
}
p.invalidateCache(*zone)
}
}
log.Debugf("OVH: Getting records for %s", *zone)
p.apiRateLimiter.Take()
var soa ovhSoa
if p.UseCache {
if err := p.client.Get("/domain/zone/"+*zone+"/soa", &soa); err != nil {
return err
}
}
if err := p.client.Get(fmt.Sprintf("/domain/zone/%s/record", *zone), &recordsIds); err != nil {
return err
}
chRecords := make(chan ovhRecord, len(recordsIds))
for _, id := range recordsIds {
id := id
eg.Go(func() error { return p.record(zone, id, chRecords) })
}
if err := eg.Wait(); err != nil {
return err
}
close(chRecords)
for record := range chRecords {
ovhRecords = append(ovhRecords, record)
}
if p.UseCache {
soa.records = ovhRecords
_ = p.cacheInstance.Add(*zone+"#soa", soa, time.Hour)
}
records <- ovhRecords
return nil
}
func (p *OVHProvider) record(zone *string, id uint64, records chan<- ovhRecord) error {
record := ovhRecord{}
log.Debugf("OVH: Getting record %d for %s", id, *zone)
p.apiRateLimiter.Take()
if err := p.client.Get(fmt.Sprintf("/domain/zone/%s/record/%d", *zone, id), &record); err != nil {
return err
}
if provider.SupportedRecordType(record.FieldType) {
log.Debugf("OVH: Record %d for %s is %+v", id, *zone, record)
records <- record
}
return nil
}
func ovhGroupByNameAndType(records []ovhRecord) []*endpoint.Endpoint {
endpoints := []*endpoint.Endpoint{}
// group supported records by name and type
groups := map[string][]ovhRecord{}
for _, r := range records {
groupBy := r.Zone + r.SubDomain + r.FieldType
if _, ok := groups[groupBy]; !ok {
groups[groupBy] = []ovhRecord{}
}
groups[groupBy] = append(groups[groupBy], r)
}
// create single endpoint with all the targets for each name/type
for _, records := range groups {
targets := []string{}
for _, record := range records {
targets = append(targets, record.Target)
}
endpoint := endpoint.NewEndpointWithTTL(
strings.TrimPrefix(records[0].SubDomain+"."+records[0].Zone, "."),
records[0].FieldType,
endpoint.TTL(records[0].TTL),
targets...,
)
endpoints = append(endpoints, endpoint)
}
return endpoints
}
func newOvhChange(action int, endpoints []*endpoint.Endpoint, zones []string, records []ovhRecord) []ovhChange {
// Copy the records because we need to mutate the list.
newRecords := make([]ovhRecord, len(records))
copy(newRecords, records)
zoneNameIDMapper := provider.ZoneIDName{}
ovhChanges := make([]ovhChange, 0, countTargets(endpoints))
for _, zone := range zones {
zoneNameIDMapper.Add(zone, zone)
}
for _, e := range endpoints {
zone, _ := zoneNameIDMapper.FindZone(e.DNSName)
if zone == "" {
log.Debugf("Skipping record %s because no hosted zone matching record DNS Name was detected", e.DNSName)
continue
}
for _, target := range e.Targets {
if e.RecordType == endpoint.RecordTypeCNAME {
target = target + "."
}
change := ovhChange{
Action: action,
ovhRecord: ovhRecord{
Zone: zone,
ovhRecordFields: ovhRecordFields{
FieldType: e.RecordType,
SubDomain: strings.TrimSuffix(e.DNSName, "."+zone),
TTL: ovhDefaultTTL,
Target: target,
},
},
}
if e.RecordTTL.IsConfigured() {
change.TTL = int64(e.RecordTTL)
}
// The Zone might have multiple records with the same target. In order to avoid applying the action to the
// same OVH record, we remove a record from the list when a match is found.
for i := 0; i < len(newRecords); i++ {
rec := newRecords[i]
if rec.Zone == change.Zone && rec.SubDomain == change.SubDomain && rec.FieldType == change.FieldType && rec.Target == change.Target {
change.ID = rec.ID
// Deleting this record from the list to avoid retargetting it later if a change with a similar target exists.
newRecords = append(newRecords[:i], newRecords[i+1:]...)
break
}
}
ovhChanges = append(ovhChanges, change)
}
}
return ovhChanges
}
func countTargets(allEndpoints ...[]*endpoint.Endpoint) int {
count := 0
for _, endpoints := range allEndpoints {
for _, endpoint := range endpoints {
count += len(endpoint.Targets)
}
}
return count
}
func (c *ovhChange) String() string {
if c.ID != 0 {
return fmt.Sprintf("%s zone (ID : %d) : %s %d IN %s %s", c.Zone, c.ID, c.SubDomain, c.TTL, c.FieldType, c.Target)
}
return fmt.Sprintf("%s zone : %s %d IN %s %s", c.Zone, c.SubDomain, c.TTL, c.FieldType, c.Target)
}