Skip to content

Commit

Permalink
options: Add RegisterFilters callback (#3203)
Browse files Browse the repository at this point in the history
Add a callback to register additional filters that
can also re-use already registered filters.

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Aug 23, 2024
1 parent 2167128 commit 85ae2fe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
11 changes: 10 additions & 1 deletion skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ type Options struct {
// List of custom filter specifications.
CustomFilters []filters.Spec

// RegisterFilters callback can be used to register additional filters.
// Built-in and custom filters are registered before the callback is called.
RegisterFilters func(registry filters.Registry)

// Urls of nodes in an etcd cluster, storing route definitions.
EtcdUrls []string

Expand Down Expand Up @@ -1141,7 +1145,8 @@ func initLog(o Options) error {
}

// filterRegistry creates a filter registry with the builtin and
// custom filter specs registered excluding disabled filters
// custom filter specs registered excluding disabled filters.
// If [Options.RegisterFilters] callback is set, it will be called.
func (o *Options) filterRegistry() filters.Registry {
registry := make(filters.Registry)

Expand All @@ -1162,6 +1167,10 @@ func (o *Options) filterRegistry() filters.Registry {
}
}

if o.RegisterFilters != nil {
o.RegisterFilters(registry)
}

return registry
}

Expand Down
48 changes: 33 additions & 15 deletions skipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,42 @@ func findAddress() (string, error) {
}

func TestOptionsFilterRegistry(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
}
fr := o.filterRegistry()
t.Run("custom filters", func(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
}
fr := o.filterRegistry()

assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.Contains(t, fr, filters.LuaName)
assert.Contains(t, fr, filters.BearerInjectorName)
assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.Contains(t, fr, filters.LuaName)
assert.Contains(t, fr, filters.BearerInjectorName)
})

o = &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
DisabledFilters: []string{filters.LuaName, filters.BearerInjectorName},
}
fr = o.filterRegistry()
t.Run("disabled filters", func(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
DisabledFilters: []string{filters.LuaName, filters.BearerInjectorName},
}
fr := o.filterRegistry()

assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.NotContains(t, fr, filters.LuaName)
assert.NotContains(t, fr, filters.BearerInjectorName)
assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.NotContains(t, fr, filters.LuaName)
assert.NotContains(t, fr, filters.BearerInjectorName)
})

t.Run("register filters", func(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
RegisterFilters: func(registry filters.Registry) {
registry.Register(auth.NewWebhook(0))
},
}
fr := o.filterRegistry()

assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.Contains(t, fr, filters.BearerInjectorName)
assert.Contains(t, fr, filters.WebhookName)
})
}

func TestOptionsOpenTracingTracerInstanceOverridesOpenTracing(t *testing.T) {
Expand Down

0 comments on commit 85ae2fe

Please sign in to comment.