Use custom mux instead of DefaultServeMux

Signed-off-by: Poor12 <shentiecheng@huawei.com>
This commit is contained in:
Poor12 2022-07-04 09:45:41 +08:00
parent 18e3fba143
commit 1613839201
3 changed files with 12 additions and 9 deletions

View File

@ -142,12 +142,13 @@ func run(opts *options.Options, stopChan <-chan struct{}) error {
} }
func serveHealthzAndMetrics(address string) { func serveHealthzAndMetrics(address string) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok")) _, _ = w.Write([]byte("ok"))
}) })
http.Handle("/metrics", promhttp.Handler()) mux.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(address, nil)) klog.Fatal(http.ListenAndServe(address, mux))
} }

View File

@ -87,12 +87,13 @@ func run(ctx context.Context, opts *options.Options) error {
} }
func serveHealthzAndMetrics(address string) { func serveHealthzAndMetrics(address string) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok")) _, _ = w.Write([]byte("ok"))
}) })
http.Handle("/metrics", promhttp.Handler()) mux.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(address, nil)) klog.Fatal(http.ListenAndServe(address, mux))
} }

View File

@ -169,12 +169,13 @@ func run(opts *options.Options, stopChan <-chan struct{}, registryOptions ...Opt
} }
func serveHealthzAndMetrics(address string) { func serveHealthzAndMetrics(address string) {
http.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok")) _, _ = w.Write([]byte("ok"))
}) })
http.Handle("/metrics", promhttp.Handler()) mux.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(address, nil)) klog.Fatal(http.ListenAndServe(address, mux))
} }