forked from github/multus-cni
Multus is refactored as a thick plugin, featuring 2 main components: - a server listening to a unix domain socket, running in a pod - a shim, a binary on the host that will send JSON requests built from its environment / stdin values to the aforementioned server. The pod where the multus daemon is running must share the host's PID namespace. Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> react to maintainers review Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> thick, deployment: update the daemonset spec Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> thick, config: validate the cni config passed by the runtime Without this patch, we're blindly trusting anything sent by the server. This way, we assure the requests arriving at the multus controller are valid before hand. Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> thick: model client / server config Also add a new command line parameter on the multus controller, pointing it to the server configuration. Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> SQUASH candidate, thick, config: cleanup the configuration Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> multus: use args.args instead of an env variable CNI is already filling the args structure; we should consume that rather than rely on the environment variables. Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> unit tests: remove weird tests that check an impossible scenario Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> docs, thick: document the thick plugin variant Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com> thick, server, multus: re-use common types Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
75 lines
2.6 KiB
Go
75 lines
2.6 KiB
Go
package mux
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
|
|
// Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
|
|
// to it, and then calls the handler passed as parameter to the MiddlewareFunc.
|
|
type MiddlewareFunc func(http.Handler) http.Handler
|
|
|
|
// middleware interface is anything which implements a MiddlewareFunc named Middleware.
|
|
type middleware interface {
|
|
Middleware(handler http.Handler) http.Handler
|
|
}
|
|
|
|
// Middleware allows MiddlewareFunc to implement the middleware interface.
|
|
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
|
|
return mw(handler)
|
|
}
|
|
|
|
// Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
|
|
func (r *Router) Use(mwf ...MiddlewareFunc) {
|
|
for _, fn := range mwf {
|
|
r.middlewares = append(r.middlewares, fn)
|
|
}
|
|
}
|
|
|
|
// useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
|
|
func (r *Router) useInterface(mw middleware) {
|
|
r.middlewares = append(r.middlewares, mw)
|
|
}
|
|
|
|
// CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header
|
|
// on requests for routes that have an OPTIONS method matcher to all the method matchers on
|
|
// the route. Routes that do not explicitly handle OPTIONS requests will not be processed
|
|
// by the middleware. See examples for usage.
|
|
func CORSMethodMiddleware(r *Router) MiddlewareFunc {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
allMethods, err := getAllMethodsForRoute(r, req)
|
|
if err == nil {
|
|
for _, v := range allMethods {
|
|
if v == http.MethodOptions {
|
|
w.Header().Set("Access-Control-Allow-Methods", strings.Join(allMethods, ","))
|
|
}
|
|
}
|
|
}
|
|
|
|
next.ServeHTTP(w, req)
|
|
})
|
|
}
|
|
}
|
|
|
|
// getAllMethodsForRoute returns all the methods from method matchers matching a given
|
|
// request.
|
|
func getAllMethodsForRoute(r *Router, req *http.Request) ([]string, error) {
|
|
var allMethods []string
|
|
|
|
for _, route := range r.routes {
|
|
var match RouteMatch
|
|
if route.Match(req, &match) || match.MatchErr == ErrMethodMismatch {
|
|
methods, err := route.GetMethods()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
allMethods = append(allMethods, methods...)
|
|
}
|
|
}
|
|
|
|
return allMethods, nil
|
|
}
|