-
Notifications
You must be signed in to change notification settings - Fork 66
/
selection.go
251 lines (227 loc) · 6.54 KB
/
selection.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package selection
import (
"fmt"
"strings"
"github.com/gardener/controller-manager-library/pkg/utils"
"github.com/gardener/external-dns-management/pkg/apis/dns/v1alpha1"
"github.com/gardener/external-dns-management/pkg/dns"
dnsutils "github.com/gardener/external-dns-management/pkg/dns/utils"
)
// SelectionResult contains the result of the CalcZoneAndDomainSelection function
type SelectionResult struct {
Zones []LightDNSHostedZone
SpecZoneSel SubSelection
SpecDomainSel SubSelection
ZoneSel SubSelection
DomainSel SubSelection
Error string
Warnings []string
}
// SubSelection contains an included and an excluded string set
type SubSelection struct {
Include utils.StringSet
Exclude utils.StringSet
}
// NewSubSelection creates an empty SubSelection
func NewSubSelection() SubSelection {
return SubSelection{
Include: utils.NewStringSet(),
Exclude: utils.NewStringSet(),
}
}
// LightDNSHostedZone contains the info of a DNSHostedZone needed for selection
type LightDNSHostedZone interface {
Id() dns.ZoneID
Domain() string
ForwardedDomains() []string
}
// CalcZoneAndDomainSelection calculates the effective included/excluded domains and zones for the given spec and
// zones supported by a provider.
func CalcZoneAndDomainSelection(spec v1alpha1.DNSProviderSpec, allzones []LightDNSHostedZone) SelectionResult {
this := SelectionResult{
SpecDomainSel: PrepareSelection(spec.Domains),
SpecZoneSel: PrepareSelection(spec.Zones),
ZoneSel: NewSubSelection(),
DomainSel: NewSubSelection(),
}
if err := validateDomains(this.SpecDomainSel.Include, "domains include"); err != nil {
this.Error = err.Error()
return this
}
if err := validateDomains(this.SpecDomainSel.Exclude, "domains exclude"); err != nil {
this.Error = err.Error()
return this
}
var zones []LightDNSHostedZone
for _, z := range allzones {
if z.Id().ProviderType == spec.Type {
zones = append(zones, z)
}
}
if len(this.SpecZoneSel.Include) > 0 {
for _, z := range zones {
if this.SpecZoneSel.Include.Contains(z.Id().ID) {
this.ZoneSel.Include.Add(z.Id().ID)
} else {
this.ZoneSel.Exclude.Add(z.Id().ID)
}
}
} else {
for _, z := range zones {
this.ZoneSel.Include.Add(z.Id().ID)
}
}
if len(this.SpecZoneSel.Exclude) > 0 {
for id := range this.ZoneSel.Include {
if this.SpecZoneSel.Exclude.Contains(id) {
this.ZoneSel.Include.Remove(id)
this.ZoneSel.Exclude.Add(id)
}
}
}
for _, z := range zones {
if this.ZoneSel.Include.Contains(z.Id().ID) {
this.Zones = append(this.Zones, z)
}
}
if len(zones) > 0 && len(this.Zones) == 0 {
this.Error = "no zone available in account matches zone filter"
return this
}
var err error
this.DomainSel.Include, err = filterByZones(normalizeDomains(this.SpecDomainSel.Include), this.Zones)
if err != nil {
this.Warnings = append(this.Warnings, err.Error())
}
this.DomainSel.Exclude, err = filterByZones(normalizeDomains(this.SpecDomainSel.Exclude), this.Zones)
if err != nil {
this.Warnings = append(this.Warnings, err.Error())
}
if len(this.SpecDomainSel.Include) == 0 {
if len(this.Zones) == 0 {
this.Error = "no hosted zones found"
return this
}
for _, z := range this.Zones {
this.DomainSel.Include.Add(z.Domain())
}
} else {
if len(this.DomainSel.Include) == 0 {
this.ZoneSel.Exclude.AddSet(this.ZoneSel.Include)
this.ZoneSel.Include = utils.NewStringSet()
zoneDomains := []string{}
for _, z := range this.Zones {
zoneDomains = append(zoneDomains, z.Domain())
}
this.Zones = nil
this.Error = fmt.Sprintf("no domain matching hosting zones. Need to be a (sub)domain of [%s]",
strings.Join(zoneDomains, ", "))
for _, z := range allzones {
this.DomainSel.Exclude.Add(z.Domain())
}
return this
}
}
excludedSubdomains := excludeForwardedSubdomains(this.DomainSel.Include, this.Zones)
this.DomainSel.Exclude.AddSet(excludedSubdomains)
outer:
for _, zone := range this.Zones {
for domain := range this.DomainSel.Include {
if dnsutils.Match(domain, zone.Domain()) {
ok := true
for _, fd := range zone.ForwardedDomains() {
if dnsutils.Match(domain, fd) {
ok = false
break
}
}
if ok {
continue outer
}
}
}
this.ZoneSel.Include.Remove(zone.Id().ID)
this.ZoneSel.Exclude.Add(zone.Id().ID)
}
for _, z := range allzones {
if !this.ZoneSel.Include.Contains(z.Id().ID) && !this.DomainSel.Include.Contains(z.Domain()) {
this.DomainSel.Exclude.Add(z.Domain())
}
}
if len(this.ZoneSel.Include) != len(this.Zones) {
this.Zones = nil
for _, z := range zones {
if this.ZoneSel.Include.Contains(z.Id().ID) {
this.Zones = append(this.Zones, z)
}
}
}
return this
}
func validateDomains(domains utils.StringSet, name string) error {
for domain := range domains {
if strings.HasPrefix(domain, "*.") {
return fmt.Errorf("wildcards are not allowed in %s '%s' (hint: remove the wildcard)", name, domain)
}
}
return nil
}
func PrepareSelection(sel *v1alpha1.DNSSelection) SubSelection {
subSel := NewSubSelection()
if sel != nil {
subSel.Include = utils.NewStringSetByArray(sel.Include)
subSel.Exclude = utils.NewStringSetByArray(sel.Exclude)
}
return subSel
}
func filterByZones(domains utils.StringSet, zones []LightDNSHostedZone) (result utils.StringSet, err error) {
result = utils.StringSet{}
for d := range domains {
_zones:
for _, z := range zones {
if dnsutils.Match(d, z.Domain()) {
for _, sub := range z.ForwardedDomains() {
if dnsutils.Match(d, sub) {
continue _zones
}
}
result.Add(d)
break
}
}
if !result.Contains(d) {
err = fmt.Errorf("domain %q not in hosted domains", d)
}
}
return result, err
}
// excludeForwardedSubdomains excludes all forwarded subdomains
func excludeForwardedSubdomains(includedDomains utils.StringSet, zones []LightDNSHostedZone) utils.StringSet {
exclude := utils.StringSet{}
for d := range includedDomains {
for _, z := range zones {
if dnsutils.Match(d, z.Domain()) {
for _, sub := range z.ForwardedDomains() {
if dnsutils.Match(sub, d) && !includedDomains.Contains(sub) {
exclude.Add(sub)
}
}
}
}
}
return exclude
}
func normalizeDomains(domains utils.StringSet) utils.StringSet {
if len(domains) == 0 {
return domains
}
normalized := utils.NewStringSet()
for k := range domains {
k = strings.TrimSuffix(strings.ToLower(k), ".")
normalized.Add(k)
}
return normalized
}