serve.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package app
  14. import (
  15. "net/http"
  16. goruntime "runtime"
  17. genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
  18. apirequest "k8s.io/apiserver/pkg/endpoints/request"
  19. apiserver "k8s.io/apiserver/pkg/server"
  20. genericfilters "k8s.io/apiserver/pkg/server/filters"
  21. "k8s.io/apiserver/pkg/server/healthz"
  22. "k8s.io/apiserver/pkg/server/mux"
  23. "k8s.io/apiserver/pkg/server/routes"
  24. componentbaseconfig "k8s.io/component-base/config"
  25. "k8s.io/component-base/metrics/legacyregistry"
  26. _ "k8s.io/component-base/metrics/prometheus/workqueue" // for workqueue metric registration
  27. "k8s.io/kubernetes/pkg/api/legacyscheme"
  28. "k8s.io/kubernetes/pkg/util/configz"
  29. )
  30. // BuildHandlerChain builds a handler chain with a base handler and CompletedConfig.
  31. func BuildHandlerChain(apiHandler http.Handler, authorizationInfo *apiserver.AuthorizationInfo, authenticationInfo *apiserver.AuthenticationInfo) http.Handler {
  32. requestInfoResolver := &apirequest.RequestInfoFactory{}
  33. failedHandler := genericapifilters.Unauthorized(legacyscheme.Codecs, false)
  34. handler := apiHandler
  35. if authorizationInfo != nil {
  36. handler = genericapifilters.WithAuthorization(apiHandler, authorizationInfo.Authorizer, legacyscheme.Codecs)
  37. }
  38. if authenticationInfo != nil {
  39. handler = genericapifilters.WithAuthentication(handler, authenticationInfo.Authenticator, failedHandler, nil)
  40. }
  41. handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
  42. handler = genericapifilters.WithCacheControl(handler)
  43. handler = genericfilters.WithPanicRecovery(handler)
  44. return handler
  45. }
  46. // NewBaseHandler takes in CompletedConfig and returns a handler.
  47. func NewBaseHandler(c *componentbaseconfig.DebuggingConfiguration, checks ...healthz.HealthChecker) *mux.PathRecorderMux {
  48. mux := mux.NewPathRecorderMux("controller-manager")
  49. healthz.InstallHandler(mux, checks...)
  50. if c.EnableProfiling {
  51. routes.Profiling{}.Install(mux)
  52. if c.EnableContentionProfiling {
  53. goruntime.SetBlockProfileRate(1)
  54. }
  55. }
  56. configz.InstallHandler(mux)
  57. //lint:ignore SA1019 See the Metrics Stability Migration KEP
  58. mux.Handle("/metrics", legacyregistry.Handler())
  59. return mux
  60. }