-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhelper.go
More file actions
128 lines (108 loc) · 3.51 KB
/
helper.go
File metadata and controls
128 lines (108 loc) · 3.51 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
122
123
124
125
126
127
128
package stackitprovider
import (
"math"
"strings"
stackitdnsclient "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
"go.uber.org/zap"
"sigs.k8s.io/external-dns/endpoint"
)
// findBestMatchingZone finds the best matching zone for a given record set name. The criteria are
// that the zone name is contained in the record set name and that the zone name is the longest
// possible match. Eg foo.bar.com. would have precedence over bar.com. if rr set name is foo.bar.com.
func findBestMatchingZone(
rrSetName string,
zones []stackitdnsclient.Zone,
) (*stackitdnsclient.Zone, bool) {
count := 0
var domainZone *stackitdnsclient.Zone
for i := range zones {
zone := &zones[i]
if l := len(zone.DnsName); l > count && strings.Contains(rrSetName, zone.DnsName) {
count = l
domainZone = zone
}
}
if domainZone == nil {
return nil, false
}
return domainZone, true
}
// findRRSet finds a record set by name and type in a list of record sets.
func findRRSet(
rrSetName, recordType string,
rrSets []stackitdnsclient.RecordSet,
) (*stackitdnsclient.RecordSet, bool) {
for i := range rrSets {
rrSet := &rrSets[i]
if rrSet.Name == rrSetName && rrSet.Type == recordType {
return rrSet, true
}
}
return nil, false
}
// appendDotIfNotExists appends a dot to the end of a string if it doesn't already end with a dot.
func appendDotIfNotExists(s string) string {
if !strings.HasSuffix(s, ".") {
return s + "."
}
return s
}
// modifyChange modifies a change to ensure it is valid for this stackitprovider.
func modifyChange(change *endpoint.Endpoint) {
change.DNSName = appendDotIfNotExists(change.DNSName)
if change.RecordTTL == 0 {
change.RecordTTL = 300
}
}
// getStackitRecordSetPayload returns a stackitdnsclient.RecordSetPayload from a change for the api client.
func getStackitRecordSetPayload(change *endpoint.Endpoint) stackitdnsclient.CreateRecordSetPayload {
records := make([]stackitdnsclient.RecordPayload, len(change.Targets))
for i := range change.Targets {
records[i] = stackitdnsclient.RecordPayload{
Content: change.Targets[i],
}
}
return stackitdnsclient.CreateRecordSetPayload{
Name: change.DNSName,
Records: records,
Ttl: safeTTLToInt32(change.RecordTTL),
Type: change.RecordType,
}
}
// getStackitPartialUpdateRecordSetPayload returns a stackitdnsclient.PartialUpdateRecordSetPayload from a change for the api client.
func getStackitPartialUpdateRecordSetPayload(change *endpoint.Endpoint) stackitdnsclient.PartialUpdateRecordSetPayload {
records := make([]stackitdnsclient.RecordPayload, len(change.Targets))
for i := range change.Targets {
records[i] = stackitdnsclient.RecordPayload{
Content: change.Targets[i],
}
}
return stackitdnsclient.PartialUpdateRecordSetPayload{
Name: &change.DNSName,
Records: records,
Ttl: safeTTLToInt32(change.RecordTTL),
}
}
// getLogFields returns a log.Fields object for a change.
func getLogFields(change *endpoint.Endpoint, action string, id string) []zap.Field {
return []zap.Field{
zap.String("record", change.DNSName),
zap.String("content", strings.Join(change.Targets, ",")),
zap.String("type", change.RecordType),
zap.String("action", action),
zap.String("id", id),
}
}
// safeTTLToInt32 safely converts an endpoint.TTL (int64) to *int32, clamping to valid bounds.
func safeTTLToInt32(ttl endpoint.TTL) *int32 {
var v int32
switch {
case int64(ttl) > math.MaxInt32:
v = math.MaxInt32
case int64(ttl) < 0:
v = 0
default:
v = int32(ttl) // #nosec G115 -- bounds checked above
}
return &v
}