Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify filter interfaces #3660

Merged
merged 2 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions endpoint/domain_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,22 @@ import (
// DomainFilterInterface defines the interface to select matching domains for a specific provider or runtime
type DomainFilterInterface interface {
Match(domain string) bool
IsConfigured() bool
}

type MatchAllDomainFilters []DomainFilterInterface

func (f MatchAllDomainFilters) Match(domain string) bool {
if !f.IsConfigured() {
return true
}
for _, filter := range f {
if filter == nil {
continue
}
if filter.IsConfigured() && !filter.Match(domain) {
if !filter.Match(domain) {
return false
}
}
return true
}

func (f MatchAllDomainFilters) IsConfigured() bool {
if f == nil {
return false
}
for _, filter := range f {
if filter == nil {
continue
}
if filter.IsConfigured() {
return true
}
}
return len(f) > 0
}

// DomainFilter holds a lists of valid domain names
type DomainFilter struct {
// Filters define what domains to match
Expand Down
14 changes: 0 additions & 14 deletions endpoint/target_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
// TargetFilterInterface defines the interface to select matching targets for a specific provider or runtime
type TargetFilterInterface interface {
Match(target string) bool
IsConfigured() bool
}

// TargetNetFilter holds a lists of valid target names
Expand Down Expand Up @@ -61,11 +60,6 @@ func NewTargetNetFilterWithExclusions(targetFilterNets []string, excludeNets []s
return TargetNetFilter{FilterNets: prepareTargetFilters(targetFilterNets), excludeNets: prepareTargetFilters(excludeNets)}
}

// NewTargetNetFilter returns a new TargetNetFilter given a comma separated list of targets
func NewTargetNetFilter(targetFilterNets []string) TargetNetFilter {
return TargetNetFilter{FilterNets: prepareTargetFilters(targetFilterNets)}
}

// Match checks whether a target can be found in the TargetNetFilter.
func (tf TargetNetFilter) Match(target string) bool {
return matchTargetNetFilter(tf.FilterNets, target, true) && !matchTargetNetFilter(tf.excludeNets, target, false)
Expand All @@ -89,11 +83,3 @@ func matchTargetNetFilter(filters []*net.IPNet, target string, emptyval bool) bo

return false
}

// IsConfigured returns true if TargetFilter is configured, false otherwise
func (tf TargetNetFilter) IsConfigured() bool {
if len(tf.FilterNets) == 1 {
return tf.FilterNets[0].Network() != ""
}
return len(tf.FilterNets) > 0
}
57 changes: 0 additions & 57 deletions endpoint/target_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,6 @@ var targetFilterTests = []targetFilterTest{
},
}

func TestTargetFilterMatch(t *testing.T) {
for i, tt := range targetFilterTests {
if len(tt.exclusions) > 0 {
t.Logf("NewTargetFilter() doesn't support exclusions - skipping test %+v", tt)
continue
}
targetFilter := NewTargetNetFilter(tt.targetFilter)
for _, target := range tt.targets {
assert.Equal(t, tt.expected, targetFilter.Match(target), "should not fail: %v in test-case #%v", target, i)
}
}
}

func TestTargetFilterWithExclusions(t *testing.T) {
for i, tt := range targetFilterTests {
if len(tt.exclusions) == 0 {
Expand All @@ -107,47 +94,3 @@ func TestMatchTargetFilterReturnsProperEmptyVal(t *testing.T) {
assert.Equal(t, true, matchFilter(emptyFilters, "sometarget.com", true))
assert.Equal(t, false, matchFilter(emptyFilters, "sometarget.com", false))
}

func TestTargetFilterIsConfigured(t *testing.T) {
for _, tt := range []struct {
filters []string
exclude []string
expected bool
}{
{
[]string{""},
[]string{""},
false,
},
{
[]string{" "},
[]string{" "},
false,
},
{
[]string{"", ""},
[]string{""},
false,
},
{
[]string{"10/8"},
[]string{" "},
false,
},
{
[]string{"10.0.0.0/8"},
[]string{" "},
true,
},
{
[]string{" 10.0.0.0/8 "},
[]string{" ignored "},
true,
},
} {
t.Run("test IsConfigured", func(t *testing.T) {
tf := NewTargetNetFilterWithExclusions(tt.filters, tt.exclude)
assert.Equal(t, tt.expected, tf.IsConfigured())
})
}
}
2 changes: 1 addition & 1 deletion provider/akamai/akamai.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (p AkamaiProvider) fetchZones() (akamaiZones, error) {
}

for _, zone := range resp.Zones {
if p.domainFilter.Match(zone.Zone) || !p.domainFilter.IsConfigured() {
if p.domainFilter.Match(zone.Zone) {
filteredZones.Zones = append(filteredZones.Zones, akamaiZone{ContractID: zone.ContractId, Zone: zone.Zone})
log.Debugf("Fetched zone: '%s' (ZoneID: %s)", zone.Zone, zone.ContractId)
}
Expand Down
4 changes: 2 additions & 2 deletions provider/pihole/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (p *piholeClient) listRecords(ctx context.Context, rtype string) ([]*endpoi
for _, rec := range data {
name := rec[0]
target := rec[1]
if p.cfg.DomainFilter.IsConfigured() && !p.cfg.DomainFilter.Match(name) {
if !p.cfg.DomainFilter.Match(name) {
log.Debugf("Skipping %s that does not match domain filter", name)
continue
}
Expand Down Expand Up @@ -195,7 +195,7 @@ type actionResponse struct {
}

func (p *piholeClient) apply(ctx context.Context, action string, ep *endpoint.Endpoint) error {
if p.cfg.DomainFilter.IsConfigured() && !p.cfg.DomainFilter.Match(ep.DNSName) {
if !p.cfg.DomainFilter.Match(ep.DNSName) {
log.Debugf("Skipping %s %s that does not match domain filter", action, ep.DNSName)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion provider/tencentcloud/privatedns.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (p *TencentCloudProvider) getPrivateZones() ([]*privatedns.PrivateZone, err

privateZonesFilter := make([]*privatedns.PrivateZone, 0)
for _, privateZone := range privateZones {
if p.domainFilter.IsConfigured() && !p.domainFilter.Match(*privateZone.Domain) {
if !p.domainFilter.Match(*privateZone.Domain) {
continue
}
privateZonesFilter = append(privateZonesFilter, privateZone)
Expand Down
3 changes: 3 additions & 0 deletions provider/zone_id_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func (f ZoneIDFilter) Match(zoneID string) bool {
if len(f.ZoneIDs) == 0 {
return true
}
if len(f.ZoneIDs) == 1 && f.ZoneIDs[0] == "" {
return true
}

for _, id := range f.ZoneIDs {
if strings.HasSuffix(zoneID, id) {
Expand Down