From 7efecc711ff796d24ba679028f437f67fb8a438d Mon Sep 17 00:00:00 2001 From: John Gardiner Myers Date: Mon, 5 Jun 2023 12:49:20 -0700 Subject: [PATCH 1/2] Simplify TargetFilterInterface --- endpoint/target_filter.go | 14 --------- endpoint/target_filter_test.go | 57 ---------------------------------- 2 files changed, 71 deletions(-) diff --git a/endpoint/target_filter.go b/endpoint/target_filter.go index 1d1b90c880..e4e69957fa 100644 --- a/endpoint/target_filter.go +++ b/endpoint/target_filter.go @@ -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 @@ -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) @@ -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 -} diff --git a/endpoint/target_filter_test.go b/endpoint/target_filter_test.go index 4d7380426e..57690f7c1d 100644 --- a/endpoint/target_filter_test.go +++ b/endpoint/target_filter_test.go @@ -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 { @@ -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()) - }) - } -} From d1fb3423c463b60d0d55d5131852d802f472f996 Mon Sep 17 00:00:00 2001 From: John Gardiner Myers Date: Mon, 5 Jun 2023 12:49:20 -0700 Subject: [PATCH 2/2] Simplify DomainFilterInterface --- endpoint/domain_filter.go | 21 +-------------------- provider/akamai/akamai.go | 2 +- provider/pihole/client.go | 4 ++-- provider/tencentcloud/privatedns.go | 2 +- provider/zone_id_filter.go | 3 +++ 5 files changed, 8 insertions(+), 24 deletions(-) diff --git a/endpoint/domain_filter.go b/endpoint/domain_filter.go index 1fe36aee05..8ddde179b6 100644 --- a/endpoint/domain_filter.go +++ b/endpoint/domain_filter.go @@ -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 diff --git a/provider/akamai/akamai.go b/provider/akamai/akamai.go index 6547edce08..f59bd384f0 100644 --- a/provider/akamai/akamai.go +++ b/provider/akamai/akamai.go @@ -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) } diff --git a/provider/pihole/client.go b/provider/pihole/client.go index df6fb4c576..4ca6552587 100644 --- a/provider/pihole/client.go +++ b/provider/pihole/client.go @@ -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 } @@ -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 } diff --git a/provider/tencentcloud/privatedns.go b/provider/tencentcloud/privatedns.go index ad29418331..7bfc9d326f 100644 --- a/provider/tencentcloud/privatedns.go +++ b/provider/tencentcloud/privatedns.go @@ -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) diff --git a/provider/zone_id_filter.go b/provider/zone_id_filter.go index 50a016b2d8..7a7b10621b 100644 --- a/provider/zone_id_filter.go +++ b/provider/zone_id_filter.go @@ -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) {