initializer.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2016 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 admission
  14. import (
  15. "k8s.io/apimachinery/pkg/api/meta"
  16. "k8s.io/apiserver/pkg/admission"
  17. "k8s.io/apiserver/pkg/authorization/authorizer"
  18. "k8s.io/apiserver/pkg/util/webhook"
  19. quota "k8s.io/kubernetes/pkg/quota/v1"
  20. )
  21. // TODO add a `WantsToRun` which takes a stopCh. Might make it generic.
  22. // WantsCloudConfig defines a function which sets CloudConfig for admission plugins that need it.
  23. type WantsCloudConfig interface {
  24. SetCloudConfig([]byte)
  25. }
  26. // WantsRESTMapper defines a function which sets RESTMapper for admission plugins that need it.
  27. type WantsRESTMapper interface {
  28. SetRESTMapper(meta.RESTMapper)
  29. }
  30. // WantsQuotaConfiguration defines a function which sets quota configuration for admission plugins that need it.
  31. type WantsQuotaConfiguration interface {
  32. SetQuotaConfiguration(quota.Configuration)
  33. admission.InitializationValidator
  34. }
  35. // PluginInitializer is used for initialization of the Kubernetes specific admission plugins.
  36. type PluginInitializer struct {
  37. authorizer authorizer.Authorizer
  38. cloudConfig []byte
  39. restMapper meta.RESTMapper
  40. quotaConfiguration quota.Configuration
  41. serviceResolver webhook.ServiceResolver
  42. authenticationInfoResolverWrapper webhook.AuthenticationInfoResolverWrapper
  43. }
  44. var _ admission.PluginInitializer = &PluginInitializer{}
  45. // NewPluginInitializer constructs new instance of PluginInitializer
  46. // TODO: switch these parameters to use the builder pattern or just make them
  47. // all public, this construction method is pointless boilerplate.
  48. func NewPluginInitializer(
  49. cloudConfig []byte,
  50. restMapper meta.RESTMapper,
  51. quotaConfiguration quota.Configuration,
  52. ) *PluginInitializer {
  53. return &PluginInitializer{
  54. cloudConfig: cloudConfig,
  55. restMapper: restMapper,
  56. quotaConfiguration: quotaConfiguration,
  57. }
  58. }
  59. // Initialize checks the initialization interfaces implemented by each plugin
  60. // and provide the appropriate initialization data
  61. func (i *PluginInitializer) Initialize(plugin admission.Interface) {
  62. if wants, ok := plugin.(WantsCloudConfig); ok {
  63. wants.SetCloudConfig(i.cloudConfig)
  64. }
  65. if wants, ok := plugin.(WantsRESTMapper); ok {
  66. wants.SetRESTMapper(i.restMapper)
  67. }
  68. if wants, ok := plugin.(WantsQuotaConfiguration); ok {
  69. wants.SetQuotaConfiguration(i.quotaConfiguration)
  70. }
  71. }