mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 09:36:58 +02:00
endpoint/domain_filter.go: add MatchParent DomainFilter method
This commit is contained in:
parent
249370ba85
commit
fc37ff0f2c
@ -106,6 +106,31 @@ func matchRegex(regex *regexp.Regexp, negativeRegex *regexp.Regexp, domain strin
|
||||
return regex.MatchString(strippedDomain)
|
||||
}
|
||||
|
||||
// MatchParent checks wether DomainFilter matches a given parent domain.
|
||||
func (df DomainFilter) MatchParent(domain string) bool {
|
||||
if !df.IsConfigured() {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, filter := range df.Filters {
|
||||
if strings.HasPrefix(filter, ".") {
|
||||
// We don't check parents if the filter is prefixed with "."
|
||||
continue
|
||||
}
|
||||
|
||||
if filter == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
strippedDomain := strings.ToLower(strings.TrimSuffix(domain, "."))
|
||||
if strings.HasSuffix(filter, "."+strippedDomain) && !matchFilter(df.exclude, domain, false) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsConfigured returns true if DomainFilter is configured, false otherwise
|
||||
func (df DomainFilter) IsConfigured() bool {
|
||||
if df.regex != nil && df.regex.String() != "" {
|
||||
|
@ -296,6 +296,72 @@ func TestDomainFilterMatchWithEmptyFilter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainFilterMatchParent(t *testing.T) {
|
||||
parentMatchTests := []domainFilterTest{
|
||||
{
|
||||
[]string{"a.example.com."},
|
||||
[]string{},
|
||||
[]string{"example.com"},
|
||||
true,
|
||||
},
|
||||
{
|
||||
[]string{" a.example.com "},
|
||||
[]string{},
|
||||
[]string{"example.com"},
|
||||
true,
|
||||
},
|
||||
{
|
||||
[]string{""},
|
||||
[]string{},
|
||||
[]string{"example.com"},
|
||||
true,
|
||||
},
|
||||
{
|
||||
[]string{".a.example.com."},
|
||||
[]string{},
|
||||
[]string{"example.com"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
[]string{"a.example.com.", "b.example.com"},
|
||||
[]string{},
|
||||
[]string{"example.com"},
|
||||
true,
|
||||
},
|
||||
{
|
||||
[]string{"a.example.com"},
|
||||
[]string{},
|
||||
[]string{"b.example.com"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
[]string{"example.com"},
|
||||
[]string{},
|
||||
[]string{"example.com"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
[]string{"example.com"},
|
||||
[]string{},
|
||||
[]string{"anexample.com"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
[]string{""},
|
||||
[]string{},
|
||||
[]string{""},
|
||||
true,
|
||||
},
|
||||
}
|
||||
for i, tt := range parentMatchTests {
|
||||
domainFilter := NewDomainFilterWithExclusions(tt.domainFilter, tt.exclusions)
|
||||
for _, domain := range tt.domains {
|
||||
assert.Equal(t, tt.expected, domainFilter.MatchParent(domain), "should not fail: %v in test-case #%v", domain, i)
|
||||
assert.Equal(t, tt.expected, domainFilter.MatchParent(domain+"."), "should not fail: %v in test-case #%v", domain+".", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegexDomainFilter(t *testing.T) {
|
||||
for i, tt := range regexDomainFilterTests {
|
||||
domainFilter := NewRegexDomainFilter(tt.regex, tt.regexExclusion)
|
||||
|
Loading…
Reference in New Issue
Block a user