|
| 1 | +# Move createDomainFilter to Endpoint Package |
| 2 | + |
| 3 | +## Overview |
| 4 | +Refactor `createDomainFilter` from `controller/execute.go` to the `endpoint` package, addressing the TODO comments in both the implementation and tests. |
| 5 | + |
| 6 | +## Current State |
| 7 | +- **Function**: `controller/execute.go:466-475` with TODO: "move to endpoint package" |
| 8 | +- **Test**: `controller/execute_test.go:335-470` with TODO: "this test should live in endpoint package" |
| 9 | +- **Usage**: Single call site at `controller/execute.go:117` |
| 10 | + |
| 11 | +## Implementation Strategy |
| 12 | + |
| 13 | +### Architectural Constraint |
| 14 | +The `endpoint` package cannot import `externaldns.Config` due to dependency direction (`pkg/apis/externaldns` → `endpoint`). Solution: use individual parameters instead of config struct. |
| 15 | + |
| 16 | +### New Function Design |
| 17 | +```go |
| 18 | +// In endpoint/domain_filter.go |
| 19 | +func NewDomainFilterFromConfig( |
| 20 | + domainFilter []string, |
| 21 | + excludeDomains []string, |
| 22 | + regexDomainFilter *regexp.Regexp, |
| 23 | + regexDomainExclusion *regexp.Regexp, |
| 24 | +) *DomainFilter |
| 25 | +``` |
| 26 | + |
| 27 | +This function will: |
| 28 | +- Be exported (public) - tests and providers can use it |
| 29 | +- Follow existing endpoint package patterns (simple parameters) |
| 30 | +- Preserve the logic: regex filters take precedence over plain filters |
| 31 | +- Match naming convention: similar to `NewDomainFilterWithExclusions` |
| 32 | + |
| 33 | +## Implementation Steps |
| 34 | + |
| 35 | +### 1. Add Factory Function to Endpoint Package |
| 36 | +**File**: `endpoint/domain_filter.go` |
| 37 | +**Location**: After line 96 (after `NewRegexDomainFilter`) |
| 38 | + |
| 39 | +Add `NewDomainFilterFromConfig` with: |
| 40 | +- Full GoDoc explaining regex precedence behavior |
| 41 | +- Same logic as current `createDomainFilter` |
| 42 | +- ~15 lines of code |
| 43 | + |
| 44 | +### 2. Migrate Tests to Endpoint Package |
| 45 | +**File**: `endpoint/domain_filter_test.go` |
| 46 | +**Location**: End of file (after line 1041) |
| 47 | + |
| 48 | +Create `TestNewDomainFilterFromConfig` by: |
| 49 | +- Copying all 15 test cases from `controller/execute_test.go:335-470` |
| 50 | +- Adapting setup to use explicit parameters instead of `externaldns.Config` |
| 51 | +- Removing dependency on `externaldns` package |
| 52 | +- ~150 lines of test code |
| 53 | + |
| 54 | +Example transformation: |
| 55 | +```go |
| 56 | +// Before (controller) |
| 57 | +cfg := &externaldns.Config{RegexDomainFilter: regexp.MustCompile(`example\.com`)} |
| 58 | +filter := createDomainFilter(cfg) |
| 59 | + |
| 60 | +// After (endpoint) |
| 61 | +filter := NewDomainFilterFromConfig(nil, nil, regexp.MustCompile(`example\.com`), nil) |
| 62 | +``` |
| 63 | + |
| 64 | +### 3. Update Controller Call Site |
| 65 | +**File**: `controller/execute.go` |
| 66 | + |
| 67 | +Replace line 117: |
| 68 | +```go |
| 69 | +domainFilter := endpoint.NewDomainFilterFromConfig( |
| 70 | + cfg.DomainFilter, |
| 71 | + cfg.ExcludeDomains, |
| 72 | + cfg.RegexDomainFilter, |
| 73 | + cfg.RegexDomainExclusion, |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +Delete lines 466-475 (old `createDomainFilter` function and TODOs) |
| 78 | + |
| 79 | +### 4. Remove Old Test from Controller |
| 80 | +**File**: `controller/execute_test.go` |
| 81 | + |
| 82 | +Delete lines 335-470 (`TestCreateDomainFilter` function) |
| 83 | + |
| 84 | +### 5. Verify Implementation |
| 85 | +Run tests: |
| 86 | +```bash |
| 87 | +go test ./endpoint/... # New tests pass |
| 88 | +go test ./controller/... # Controller tests still pass |
| 89 | +go test ./... # Full suite passes |
| 90 | +``` |
| 91 | + |
| 92 | +## Files Modified |
| 93 | +1. ✏️ `endpoint/domain_filter.go` - Add new factory function |
| 94 | +2. ✏️ `endpoint/domain_filter_test.go` - Add 15 test cases |
| 95 | +3. ✏️ `controller/execute.go` - Update call site, remove old function |
| 96 | +4. ✏️ `controller/execute_test.go` - Remove old tests |
| 97 | + |
| 98 | +## Success Criteria |
| 99 | +- [ ] All 15 test cases pass in new location |
| 100 | +- [ ] No circular import dependencies |
| 101 | +- [ ] Controller tests pass |
| 102 | +- [ ] No references to `createDomainFilter` remain |
| 103 | +- [ ] Domain filtering behavior unchanged |
0 commit comments