-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrecords.go
More file actions
121 lines (95 loc) · 3.06 KB
/
records.go
File metadata and controls
121 lines (95 loc) · 3.06 KB
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
package stackitprovider
import (
"context"
stackitdnsclient "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/provider"
)
// Records returns resource records.
func (d *StackitDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
zones, err := d.zoneFetcherClient.zones(ctx)
if err != nil {
return nil, err
}
var endpoints []*endpoint.Endpoint
endpointsErrorChannel := make(chan endpointError, len(zones))
zoneIdsChannel := make(chan string, len(zones))
for i := 0; i < d.workers; i++ {
go d.fetchRecordsWorker(ctx, zoneIdsChannel, endpointsErrorChannel)
}
for i := range zones {
zone := &zones[i]
zoneIdsChannel <- zone.Id
}
for i := 0; i < len(zones); i++ {
endpointsErrorList := <-endpointsErrorChannel
if endpointsErrorList.err != nil {
close(zoneIdsChannel)
return nil, endpointsErrorList.err
}
endpoints = append(endpoints, endpointsErrorList.endpoints...)
}
close(zoneIdsChannel)
return endpoints, nil
}
// fetchRecordsWorker fetches all records from a given zone.
func (d *StackitDNSProvider) fetchRecordsWorker(
ctx context.Context,
zoneIdChannel chan string,
endpointsErrorChannel chan<- endpointError,
) {
for zoneId := range zoneIdChannel {
d.processZoneRRSets(ctx, zoneId, endpointsErrorChannel)
}
d.logger.Debug("fetch record set worker finished")
}
// processZoneRRSets fetches and processes DNS records for a given zone.
func (d *StackitDNSProvider) processZoneRRSets(
ctx context.Context,
zoneId string,
endpointsErrorChannel chan<- endpointError,
) {
var endpoints []*endpoint.Endpoint
rrSets, err := d.rrSetFetcherClient.fetchRecords(ctx, zoneId, nil)
if err != nil {
endpointsErrorChannel <- endpointError{
endpoints: nil,
err: err,
}
return
}
endpoints = d.collectEndPoints(rrSets)
endpointsErrorChannel <- endpointError{
endpoints: endpoints,
err: nil,
}
}
// collectEndPoints creates a list of Endpoints from the provided rrSets.
func (d *StackitDNSProvider) collectEndPoints(
rrSets []stackitdnsclient.RecordSet,
) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint
for i := range rrSets {
r := &rrSets[i]
name, recordType, ttl, records, ok := recordSetCoreFields(r)
if !ok || !provider.SupportedRecordType(recordType) {
continue
}
endpoints = append(endpoints, endpointsFromRecords(name, recordType, ttl, records)...)
}
return endpoints
}
func recordSetCoreFields(r *stackitdnsclient.RecordSet) (name string, recordType string, ttl endpoint.TTL, records []stackitdnsclient.Record, ok bool) {
if r == nil || len(r.Records) == 0 {
return "", "", 0, nil, false
}
return r.Name, r.Type, endpoint.TTL(r.Ttl), r.Records, true
}
func endpointsFromRecords(name, recordType string, ttl endpoint.TTL, records []stackitdnsclient.Record) []*endpoint.Endpoint {
endpoints := make([]*endpoint.Endpoint, 0, len(records))
for i := range records {
rec := &records[i]
endpoints = append(endpoints, endpoint.NewEndpointWithTTL(name, recordType, ttl, rec.Content))
}
return endpoints
}