Skip to content

Commit 11a60fe

Browse files
committed
refactor: remove domain arg from label provider
1 parent 996316b commit 11a60fe

5 files changed

Lines changed: 11 additions & 35 deletions

File tree

‎internal/service/access_controls_service.go‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ import (
1212
"go.uber.org/dig"
1313
)
1414

15-
// LabelProvider looks up the apps it knows about for the given domain. A
16-
// provider that knows which hosts its apps are served on MUST only yield the
17-
// ones that are actually served on domain, so that an unrelated app cannot
18-
// claim it by name.
1915
type LabelProvider interface {
20-
Lookup(domain string, locator func(name string, app *model.App) bool) error
16+
Lookup(locator func(name string, app *model.App) bool) error
2117
}
2218

2319
type AccessControlsService struct {
@@ -149,9 +145,7 @@ func (service *AccessControlsService) GetAccessControls(domain string) (*model.A
149145

150146
// If we have a label provider configured, try to get ACLs from it
151147
if service.labelProvider != nil {
152-
return service.getACLs(domain, func(locator func(name string, app *model.App) bool) error {
153-
return service.labelProvider.Lookup(domain, locator)
154-
})
148+
return service.getACLs(domain, service.labelProvider.Lookup)
155149
}
156150

157151
// No labels

‎internal/service/access_controls_service_test.go‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newMockProvider(acls map[string]model.App, shouldError bool) *mockProvider
2020
return &mockProvider{acls: acls, shouldError: shouldError}
2121
}
2222

23-
func (m *mockProvider) Lookup(_ string, locator func(name string, app *model.App) bool) error {
23+
func (m *mockProvider) Lookup(locator func(name string, app *model.App) bool) error {
2424
if m.shouldError {
2525
return errors.New("mock error")
2626
}
@@ -153,9 +153,7 @@ func TestAccessControlsService(t *testing.T) {
153153
Config: &model.Config{},
154154
LabelProvider: mock,
155155
})
156-
app, err := acls.getACLs(test.domain, func(locator func(name string, app *model.App) bool) error {
157-
return mock.Lookup(test.domain, locator)
158-
})
156+
app, err := acls.getACLs(test.domain, mock.Lookup)
159157
if test.errorFunc != nil {
160158
test.errorFunc(t, err)
161159
return
@@ -193,9 +191,7 @@ func TestAccessControlsService(t *testing.T) {
193191
Config: &model.Config{},
194192
LabelProvider: mock,
195193
})
196-
_, err := acls.getACLs("example.com", func(locator func(name string, app *model.App) bool) error {
197-
return mock.Lookup("example.com", locator)
198-
})
194+
_, err := acls.getACLs("example.com", mock.Lookup)
199195
assert.Error(t, err)
200196

201197
// get acls should return an error when multiple apps with the same domain exist

‎internal/service/docker_service.go‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ func (docker *DockerService) inspectContainer(containerId string) (container.Ins
119119
return docker.client.ContainerInspect(docker.context, containerId)
120120
}
121121

122-
// Lookup yields every app labelled on a running container. Container labels
123-
// carry no routing information, so the domain cannot be used to narrow the
124-
// results down and the caller is left to match them.
125-
func (docker *DockerService) Lookup(_ string, locator func(name string, app *model.App) bool) error {
122+
func (docker *DockerService) Lookup(locator func(name string, app *model.App) bool) error {
126123
if !docker.isConnected {
127124
docker.log.App.Debug().Msg("Docker service not connected, returning empty labels")
128125
return nil

‎internal/service/kubernetes_service.go‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,12 @@ func (k *KubernetesService) removeResource(key resourceKey) {
211211
delete(k.apps, key)
212212
}
213213

214-
func (k *KubernetesService) getEntry(domain string, locator func(name string, app *model.App) bool) {
215-
if !ensureAscii(domain) {
216-
k.log.App.Debug().Str("domain", domain).Msg("Domain is invalid, skipping lookup")
217-
return
218-
}
219-
214+
func (k *KubernetesService) getEntry(locator func(name string, app *model.App) bool) {
220215
k.mu.RLock()
221216
defer k.mu.RUnlock()
222217

223218
// O(n^2) is not great but the number of resource entries is expected to be small
224219
for _, app := range k.apps {
225-
if !slices.ContainsFunc(app.hosts, func(host string) bool {
226-
return hostMatchesHostname(host, domain)
227-
}) {
228-
continue
229-
}
230220
for _, entry := range app.entries {
231221
if ok := locator(entry.name, &entry.app); ok {
232222
return
@@ -412,13 +402,13 @@ func (k *KubernetesService) watchGVR(res watchedResource, ctx context.Context) {
412402
}
413403
}
414404

415-
func (k *KubernetesService) Lookup(domain string, locator func(name string, app *model.App) bool) error {
405+
func (k *KubernetesService) Lookup(locator func(name string, app *model.App) bool) error {
416406
if !k.connected {
417407
k.log.App.Debug().Msg("Kubernetes label provider not started, skipping")
418408
return nil
419409
}
420410

421-
k.getEntry(domain, locator)
411+
k.getEntry(locator)
422412

423413
return nil
424414
}

‎internal/service/kubernetes_service_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func testIngress(name string, annotations map[string]string, hosts ...string) *t
4949

5050
func lookupApp(service *KubernetesService, domain string) *model.App {
5151
var app *model.App
52-
service.getEntry(domain, func(name string, candidate *model.App) bool {
52+
service.getEntry(func(name string, candidate *model.App) bool {
5353
if candidate.Config.Domain == domain || strings.HasPrefix(domain, name+".") {
5454
app = candidate
5555
return true
@@ -175,7 +175,6 @@ func TestKubernetesServiceLookup(t *testing.T) {
175175
}{
176176
{"Returns a matching app when connected", true, "app.example.com", true},
177177
{"Skips the cache before the service is connected", false, "app.example.com", false},
178-
{"Skips an invalid domain", true, "app.example.com\xC3\xA9", false},
179178
}
180179

181180
for _, test := range tests {
@@ -188,7 +187,7 @@ func TestKubernetesServiceLookup(t *testing.T) {
188187
}})
189188

190189
var app *model.App
191-
err := service.Lookup(test.domain, func(_ string, candidate *model.App) bool {
190+
err := service.Lookup(func(_ string, candidate *model.App) bool {
192191
app = candidate
193192
return true
194193
})

0 commit comments

Comments
 (0)