From 22ade11e322e90f8c75d8607177909a9e1f8cd2c Mon Sep 17 00:00:00 2001 From: Jeremy Facchetti Date: Fri, 5 Apr 2024 10:07:21 +0200 Subject: [PATCH] renamed config, which also returns an array now. interceptor is http.handler --- plugin/manager.go | 14 ++++++++------ plugin/server.go | 2 +- plugin/types.go | 6 +++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/plugin/manager.go b/plugin/manager.go index f1631439..18843bc1 100644 --- a/plugin/manager.go +++ b/plugin/manager.go @@ -195,10 +195,12 @@ func (m *manager) Register(syncer syncertypes.Base) error { defer m.m.Unlock() if int, ok := syncer.(Interceptor); ok { - if _, ok := m.interceptors[int.Config().HandlerName]; ok { - return fmt.Errorf("could not add the interceptor %s because the handler name %s is already in use", int.Name(), int.Config().HandlerName) + for _, rule := range int.InterceptedRequests() { + if _, ok := m.interceptors[rule.HandlerName]; ok { + return fmt.Errorf("could not add the interceptor %s because the handler name %s is already in use", int.Name(), rule.HandlerName) + } + m.interceptors[rule.HandlerName] = int } - m.interceptors[int.Config().HandlerName] = int.Handler() } else { m.syncers = append(m.syncers, syncer) } @@ -217,14 +219,14 @@ func (m *manager) Start() error { func (m *manager) startInterceptors() error { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - handlerName := r.Header.Get("Vcluster-Plugin-Handler-Name") + handlerName := r.Header.Get("VCluster-Plugin-Handler-Name") if handlerName == "" { - responsewriters.InternalError(w, r, errors.New("header Vcluster-Plugin-Handler-Name wasn't set")) + responsewriters.InternalError(w, r, errors.New("header VCluster-Plugin-Handler-Name wasn't set")) return } interceptorHandler, ok := m.interceptors[handlerName] if !ok { - responsewriters.InternalError(w, r, errors.New("header Vcluster-Plugin-Handler-Name had no match")) + responsewriters.InternalError(w, r, errors.New("header VCluster-Plugin-Handler-Name had no match")) return } interceptorHandler.ServeHTTP(w, r) diff --git a/plugin/server.go b/plugin/server.go index 17e0d022..5a35c63f 100644 --- a/plugin/server.go +++ b/plugin/server.go @@ -277,7 +277,7 @@ func (p *pluginServer) getInterceptorConfig() *v2.InterceptorConfig { Port: p.interceptorsPort, } for _, interceptor := range p.interceptors { - interceptorConfig.Interceptors = append(interceptorConfig.Interceptors, interceptor.Config()) + interceptorConfig.Interceptors = append(interceptorConfig.Interceptors, interceptor.InterceptedRequests()...) } return interceptorConfig diff --git a/plugin/types.go b/plugin/types.go index 3ec4fa82..f0d55fff 100644 --- a/plugin/types.go +++ b/plugin/types.go @@ -63,10 +63,10 @@ type Interceptor interface { syncertypes.Base // Handler is the handler that will handle the requests delegated by the syncer - Handler() http.Handler + http.Handler - // Config returns an rbac style struct which defines what to intercept - Config() v2.Interceptor + // InterceptedRequests returns an rbac style struct which defines what to intercept + InterceptedRequests() []v2.Interceptor } type MutateCreateVirtual interface {