admission.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 options
  14. import (
  15. "fmt"
  16. "strings"
  17. "github.com/spf13/pflag"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/apiserver/pkg/admission"
  20. "k8s.io/apiserver/pkg/server"
  21. genericoptions "k8s.io/apiserver/pkg/server/options"
  22. "k8s.io/client-go/informers"
  23. "k8s.io/client-go/rest"
  24. "k8s.io/component-base/featuregate"
  25. )
  26. // AdmissionOptions holds the admission options.
  27. // It is a wrap of generic AdmissionOptions.
  28. type AdmissionOptions struct {
  29. // GenericAdmission holds the generic admission options.
  30. GenericAdmission *genericoptions.AdmissionOptions
  31. // DEPRECATED flag, should use EnabledAdmissionPlugins and DisabledAdmissionPlugins.
  32. // They are mutually exclusive, specify both will lead to an error.
  33. PluginNames []string
  34. }
  35. // NewAdmissionOptions creates a new instance of AdmissionOptions
  36. // Note:
  37. // In addition it calls RegisterAllAdmissionPlugins to register
  38. // all kube-apiserver admission plugins.
  39. //
  40. // Provides the list of RecommendedPluginOrder that holds sane values
  41. // that can be used by servers that don't care about admission chain.
  42. // Servers that do care can overwrite/append that field after creation.
  43. func NewAdmissionOptions() *AdmissionOptions {
  44. options := genericoptions.NewAdmissionOptions()
  45. // register all admission plugins
  46. RegisterAllAdmissionPlugins(options.Plugins)
  47. // set RecommendedPluginOrder
  48. options.RecommendedPluginOrder = AllOrderedPlugins
  49. // set DefaultOffPlugins
  50. options.DefaultOffPlugins = DefaultOffAdmissionPlugins()
  51. return &AdmissionOptions{
  52. GenericAdmission: options,
  53. }
  54. }
  55. // AddFlags adds flags related to admission for kube-apiserver to the specified FlagSet
  56. func (a *AdmissionOptions) AddFlags(fs *pflag.FlagSet) {
  57. fs.StringSliceVar(&a.PluginNames, "admission-control", a.PluginNames, ""+
  58. "Admission is divided into two phases. "+
  59. "In the first phase, only mutating admission plugins run. "+
  60. "In the second phase, only validating admission plugins run. "+
  61. "The names in the below list may represent a validating plugin, a mutating plugin, or both. "+
  62. "The order of plugins in which they are passed to this flag does not matter. "+
  63. "Comma-delimited list of: "+strings.Join(a.GenericAdmission.Plugins.Registered(), ", ")+".")
  64. fs.MarkDeprecated("admission-control", "Use --enable-admission-plugins or --disable-admission-plugins instead. Will be removed in a future version.")
  65. fs.Lookup("admission-control").Hidden = false
  66. a.GenericAdmission.AddFlags(fs)
  67. }
  68. // Validate verifies flags passed to kube-apiserver AdmissionOptions.
  69. // Kube-apiserver verifies PluginNames and then call generic AdmissionOptions.Validate.
  70. func (a *AdmissionOptions) Validate() []error {
  71. if a == nil {
  72. return nil
  73. }
  74. errs := []error{}
  75. if a.PluginNames != nil &&
  76. (a.GenericAdmission.EnablePlugins != nil || a.GenericAdmission.DisablePlugins != nil) {
  77. errs = append(errs, fmt.Errorf("admission-control and enable-admission-plugins/disable-admission-plugins flags are mutually exclusive"))
  78. }
  79. registeredPlugins := sets.NewString(a.GenericAdmission.Plugins.Registered()...)
  80. for _, name := range a.PluginNames {
  81. if !registeredPlugins.Has(name) {
  82. errs = append(errs, fmt.Errorf("admission-control plugin %q is unknown", name))
  83. }
  84. }
  85. errs = append(errs, a.GenericAdmission.Validate()...)
  86. return errs
  87. }
  88. // ApplyTo adds the admission chain to the server configuration.
  89. // Kube-apiserver just call generic AdmissionOptions.ApplyTo.
  90. func (a *AdmissionOptions) ApplyTo(
  91. c *server.Config,
  92. informers informers.SharedInformerFactory,
  93. kubeAPIServerClientConfig *rest.Config,
  94. features featuregate.FeatureGate,
  95. pluginInitializers ...admission.PluginInitializer,
  96. ) error {
  97. if a == nil {
  98. return nil
  99. }
  100. if a.PluginNames != nil {
  101. // pass PluginNames to generic AdmissionOptions
  102. a.GenericAdmission.EnablePlugins, a.GenericAdmission.DisablePlugins = computePluginNames(a.PluginNames, a.GenericAdmission.RecommendedPluginOrder)
  103. }
  104. return a.GenericAdmission.ApplyTo(c, informers, kubeAPIServerClientConfig, features, pluginInitializers...)
  105. }
  106. // explicitly disable all plugins that are not in the enabled list
  107. func computePluginNames(explicitlyEnabled []string, all []string) (enabled []string, disabled []string) {
  108. return explicitlyEnabled, sets.NewString(all...).Difference(sets.NewString(explicitlyEnabled...)).List()
  109. }