Skip to content

Commit

Permalink
renamed config, which also returns an array now. interceptor is
Browse files Browse the repository at this point in the history
http.handler
  • Loading branch information
facchettos committed Apr 5, 2024
1 parent 1de06bd commit 22ade11
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 22ade11

Please sign in to comment.