Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor admission webhook handler registration for better error handling #4170

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions pkg/webhook/handler/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package handler

import (
"os"

"github.com/fluid-cloudnative/fluid/pkg/common"
"github.com/fluid-cloudnative/fluid/pkg/webhook/handler/mutating"
"github.com/go-logr/logr"
Expand All @@ -37,7 +35,7 @@ var (

var (
// HandlerMap contains all admission webhook handlers.
HandlerMap = map[string]common.AdmissionHandler{}
handlerMap = map[string]common.AdmissionHandler{}
handlerGates = map[string]GateFunc{}
)

Expand All @@ -50,7 +48,7 @@ func init() {
func Register(mgr manager.Manager, client client.Client, log logr.Logger) {
server := mgr.GetWebhookServer()
filterActiveHandlers()
for path, handler := range HandlerMap {
for path, handler := range handlerMap {
handler.Setup(client)
server.Register(path, &webhook.Admission{Handler: handler})
log.Info("Registered webhook handler", "path", path)
Expand All @@ -67,15 +65,16 @@ func addHandlersWithGate(m map[string]common.AdmissionHandler, fn GateFunc) {
setupLog.Info("Skip handler with empty path.", "handler", handler)
continue
}
// Ensure path starts with '/', making it absolute.
if path[0] != '/' {
path = "/" + path
}
_, found := HandlerMap[path]
_, found := handlerMap[path]
if found {
setupLog.Info("error: conflicting webhook builder path in handler map", "path", path)
os.Exit(1)
panic("Conflicting webhook path detected")
}
HandlerMap[path] = handler
handlerMap[path] = handler
if fn != nil {
handlerGates[path] = fn
}
Expand All @@ -84,14 +83,14 @@ func addHandlersWithGate(m map[string]common.AdmissionHandler, fn GateFunc) {

func filterActiveHandlers() {
disablePaths := sets.NewString()
for path := range HandlerMap {
for path := range handlerMap {
if fn, ok := handlerGates[path]; ok {
if !fn() {
disablePaths.Insert(path)
}
}
}
for _, path := range disablePaths.List() {
delete(HandlerMap, path)
delete(handlerMap, path)
}
}
Loading