Skip to content

Commit

Permalink
Add Domain filter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tjamet committed Aug 14, 2024
1 parent 82c6983 commit b2ff161
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type Controller struct {
// The interval between individual synchronizations
Interval time.Duration
// The DomainFilter defines which DNS records to keep or exclude
DomainFilter endpoint.DomainFilter
DomainFilter endpoint.DomainFilterInterface
// The nextRunAt used for throttling and batching reconciliation
nextRunAt time.Time
// The runAtMutex is for atomic updating of nextRunAt and lastRunAt
Expand Down Expand Up @@ -245,7 +245,7 @@ func (c *Controller) RunOnce(ctx context.Context) error {
Policies: []plan.Policy{c.Policy},
Current: records,
Desired: endpoints,
DomainFilter: endpoint.MatchAllDomainFilters{&c.DomainFilter, &registryFilter},
DomainFilter: endpoint.MatchAllDomainFilters{c.DomainFilter, registryFilter},
ManagedRecords: c.ManagedRecordTypes,
ExcludeRecords: c.ExcludeRecordTypes,
OwnerID: c.Registry.OwnerID(),
Expand Down
2 changes: 1 addition & 1 deletion controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type errorMockProvider struct {
mockProvider
}

func (p *filteredMockProvider) GetDomainFilter() endpoint.DomainFilter {
func (p *filteredMockProvider) GetDomainFilter() endpoint.DomainFilterInterface {
return p.domainFilter
}

Expand Down
8 changes: 7 additions & 1 deletion endpoint/domain_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"strings"
)

type MatchAllDomainFilters []*DomainFilter
type MatchAllDomainFilters []DomainFilterInterface

func (f MatchAllDomainFilters) Match(domain string) bool {
for _, filter := range f {
Expand All @@ -39,6 +39,10 @@ func (f MatchAllDomainFilters) Match(domain string) bool {
return true
}

type DomainFilterInterface interface {
Match(domain string) bool
}

// DomainFilter holds a lists of valid domain names
type DomainFilter struct {
// Filters define what domains to match
Expand All @@ -51,6 +55,8 @@ type DomainFilter struct {
regexExclusion *regexp.Regexp
}

var _ DomainFilterInterface = &DomainFilter{}

// domainFilterSerde is a helper type for serializing and deserializing DomainFilter.
type domainFilterSerde struct {
Include []string `json:"include,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Config struct {
AlwaysPublishNotReadyAddresses bool
ConnectorSourceServer string
Provider string
ProviderCacheTime int
ProviderCacheTime time.Duration
GoogleProject string
GoogleBatchChangeSize int
GoogleBatchChangeInterval time.Duration
Expand Down
2 changes: 1 addition & 1 deletion provider/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (p *AWSProvider) createUpdateChanges(newEndpoints, oldEndpoints []*endpoint
}

// GetDomainFilter generates a filter to exclude any domain that is not controlled by the provider
func (p *AWSProvider) GetDomainFilter() endpoint.DomainFilter {
func (p *AWSProvider) GetDomainFilter() endpoint.DomainFilterInterface {
zones, err := p.Zones(context.Background())
if err != nil {
log.Errorf("failed to list zones: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions provider/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,10 @@ func TestAWSZones(t *testing.T) {
func TestAWSRecordsFilter(t *testing.T) {
provider, _ := newAWSProvider(t, endpoint.DomainFilter{}, provider.ZoneIDFilter{}, provider.ZoneTypeFilter{}, false, false, nil)
domainFilter := provider.GetDomainFilter()
assert.NotNil(t, domainFilter)
require.NotNil(t, domainFilter)
require.IsType(t, endpoint.DomainFilter{}, domainFilter)
count := 0
filters := domainFilter.Filters
filters := domainFilter.(endpoint.DomainFilter).Filters
for _, tld := range []string{
"zone-4.ext-dns-test-3.teapot.zalan.do",
".zone-4.ext-dns-test-3.teapot.zalan.do",
Expand Down
16 changes: 8 additions & 8 deletions provider/cached_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,26 @@ var (
type CachedProvider struct {
Provider
RefreshDelay time.Duration
err error
lastRead time.Time
cache []*endpoint.Endpoint
}

func (c *CachedProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
if c.needRefresh() {
log.Info("Records cache provider: refreshing records list cache")
c.cache, c.err = c.Provider.Records(ctx)
if c.err != nil {
log.Errorf("Records cache provider: list records failed: %v", c.err)
records, err := c.Provider.Records(ctx)
if err != nil {
c.cache = nil
return nil, err
}
c.cache = records
c.lastRead = time.Now()
cachedRecordsCallsTotal.WithLabelValues("false").Inc()
} else {
log.Info("Records cache provider: using records list from cache")
log.Debug("Records cache provider: using records list from cache")
cachedRecordsCallsTotal.WithLabelValues("true").Inc()
}
return c.cache, c.err
return c.cache, nil
}
func (c *CachedProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
if !changes.HasChanges() {
Expand All @@ -81,13 +82,12 @@ func (c *CachedProvider) ApplyChanges(ctx context.Context, changes *plan.Changes
}

func (c *CachedProvider) Reset() {
c.err = nil
c.cache = nil
c.lastRead = time.Time{}
}

func (c *CachedProvider) needRefresh() bool {
if c.cache == nil || c.err != nil {
if c.cache == nil {
log.Debug("Records cache provider is not initialized")
return true
}
Expand Down
2 changes: 1 addition & 1 deletion provider/inmemory/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
// initialized as dns provider with no records
type InMemoryProvider struct {
provider.BaseProvider
domain endpoint.DomainFilter
domain endpoint.DomainFilterInterface
client *inMemoryClient
filter *filter
OnApplyChanges func(ctx context.Context, changes *plan.Changes)
Expand Down
4 changes: 2 additions & 2 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Provider interface {
// unnecessary (potentially failing) changes. It may also modify other fields, add, or remove
// Endpoints. It is permitted to modify the supplied endpoints.
AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error)
GetDomainFilter() endpoint.DomainFilter
GetDomainFilter() endpoint.DomainFilterInterface
}

type BaseProvider struct{}
Expand All @@ -57,7 +57,7 @@ func (b BaseProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoi
return endpoints, nil
}

func (b BaseProvider) GetDomainFilter() endpoint.DomainFilter {
func (b BaseProvider) GetDomainFilter() endpoint.DomainFilterInterface {
return endpoint.DomainFilter{}
}

Expand Down
2 changes: 1 addition & 1 deletion registry/aws_sd_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewAWSSDRegistry(provider provider.Provider, ownerID string) (*AWSSDRegistr
}, nil
}

func (sdr *AWSSDRegistry) GetDomainFilter() endpoint.DomainFilter {
func (sdr *AWSSDRegistry) GetDomainFilter() endpoint.DomainFilterInterface {
return sdr.provider.GetDomainFilter()
}

Expand Down
2 changes: 1 addition & 1 deletion registry/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewDynamoDBRegistry(provider provider.Provider, ownerID string, dynamodbAPI
}, nil
}

func (im *DynamoDBRegistry) GetDomainFilter() endpoint.DomainFilter {
func (im *DynamoDBRegistry) GetDomainFilter() endpoint.DomainFilterInterface {
return im.provider.GetDomainFilter()
}

Expand Down
2 changes: 1 addition & 1 deletion registry/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewNoopRegistry(provider provider.Provider) (*NoopRegistry, error) {
}, nil
}

func (im *NoopRegistry) GetDomainFilter() endpoint.DomainFilter {
func (im *NoopRegistry) GetDomainFilter() endpoint.DomainFilterInterface {
return im.provider.GetDomainFilter()
}

Expand Down
2 changes: 1 addition & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ type Registry interface {
Records(ctx context.Context) ([]*endpoint.Endpoint, error)
ApplyChanges(ctx context.Context, changes *plan.Changes) error
AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error)
GetDomainFilter() endpoint.DomainFilter
GetDomainFilter() endpoint.DomainFilterInterface
OwnerID() string
}
2 changes: 1 addition & 1 deletion registry/txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func getSupportedTypes() []string {
return []string{endpoint.RecordTypeA, endpoint.RecordTypeAAAA, endpoint.RecordTypeCNAME, endpoint.RecordTypeNS}
}

func (im *TXTRegistry) GetDomainFilter() endpoint.DomainFilter {
func (im *TXTRegistry) GetDomainFilter() endpoint.DomainFilterInterface {
return im.provider.GetDomainFilter()
}

Expand Down

0 comments on commit b2ff161

Please sign in to comment.