2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
goruntime "runtime"
|
|
|
|
|
|
|
|
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
|
|
|
|
apirequest "k8s.io/apiserver/pkg/endpoints/request"
|
|
|
|
apiserver "k8s.io/apiserver/pkg/server"
|
|
|
|
genericfilters "k8s.io/apiserver/pkg/server/filters"
|
|
|
|
"k8s.io/apiserver/pkg/server/healthz"
|
|
|
|
"k8s.io/apiserver/pkg/server/mux"
|
|
|
|
"k8s.io/apiserver/pkg/server/routes"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/client-go/kubernetes/scheme"
|
2019-04-07 17:07:55 +00:00
|
|
|
componentbaseconfig "k8s.io/component-base/config"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/component-base/configz"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/component-base/metrics/legacyregistry"
|
2020-03-26 21:07:15 +00:00
|
|
|
_ "k8s.io/component-base/metrics/prometheus/workqueue" // for workqueue metric registration
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BuildHandlerChain builds a handler chain with a base handler and CompletedConfig.
|
|
|
|
func BuildHandlerChain(apiHandler http.Handler, authorizationInfo *apiserver.AuthorizationInfo, authenticationInfo *apiserver.AuthenticationInfo) http.Handler {
|
|
|
|
requestInfoResolver := &apirequest.RequestInfoFactory{}
|
2020-08-10 17:43:49 +00:00
|
|
|
failedHandler := genericapifilters.Unauthorized(scheme.Codecs)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
handler := apiHandler
|
|
|
|
if authorizationInfo != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
handler = genericapifilters.WithAuthorization(apiHandler, authorizationInfo.Authorizer, scheme.Codecs)
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
if authenticationInfo != nil {
|
|
|
|
handler = genericapifilters.WithAuthentication(handler, authenticationInfo.Authenticator, failedHandler, nil)
|
|
|
|
}
|
|
|
|
handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
|
2019-09-27 21:51:53 +00:00
|
|
|
handler = genericapifilters.WithCacheControl(handler)
|
2020-12-01 01:06:26 +00:00
|
|
|
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBaseHandler takes in CompletedConfig and returns a handler.
|
2019-09-27 21:51:53 +00:00
|
|
|
func NewBaseHandler(c *componentbaseconfig.DebuggingConfiguration, checks ...healthz.HealthChecker) *mux.PathRecorderMux {
|
2019-01-12 04:58:27 +00:00
|
|
|
mux := mux.NewPathRecorderMux("controller-manager")
|
|
|
|
healthz.InstallHandler(mux, checks...)
|
|
|
|
if c.EnableProfiling {
|
|
|
|
routes.Profiling{}.Install(mux)
|
|
|
|
if c.EnableContentionProfiling {
|
|
|
|
goruntime.SetBlockProfileRate(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configz.InstallHandler(mux)
|
2019-12-12 01:27:03 +00:00
|
|
|
//lint:ignore SA1019 See the Metrics Stability Migration KEP
|
2019-09-27 21:51:53 +00:00
|
|
|
mux.Handle("/metrics", legacyregistry.Handler())
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|