admission.go 4.7 KB

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