admission.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2017 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 eventratelimit
  14. import (
  15. "io"
  16. apierrors "k8s.io/apimachinery/pkg/api/errors"
  17. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  18. "k8s.io/apiserver/pkg/admission"
  19. "k8s.io/client-go/util/flowcontrol"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. eventratelimitapi "k8s.io/kubernetes/plugin/pkg/admission/eventratelimit/apis/eventratelimit"
  22. "k8s.io/kubernetes/plugin/pkg/admission/eventratelimit/apis/eventratelimit/validation"
  23. )
  24. // PluginName indicates name of admission plugin.
  25. const PluginName = "EventRateLimit"
  26. // Register registers a plugin
  27. func Register(plugins *admission.Plugins) {
  28. plugins.Register(PluginName,
  29. func(config io.Reader) (admission.Interface, error) {
  30. // load the configuration provided (if any)
  31. configuration, err := LoadConfiguration(config)
  32. if err != nil {
  33. return nil, err
  34. }
  35. // validate the configuration (if any)
  36. if configuration != nil {
  37. if errs := validation.ValidateConfiguration(configuration); len(errs) != 0 {
  38. return nil, errs.ToAggregate()
  39. }
  40. }
  41. return newEventRateLimit(configuration, realClock{})
  42. })
  43. }
  44. // Plugin implements an admission controller that can enforce event rate limits
  45. type Plugin struct {
  46. *admission.Handler
  47. // limitEnforcers is the collection of limit enforcers. There is one limit enforcer for each
  48. // active limit type. As there are 4 limit types, the length of the array will be at most 4.
  49. // The array is read-only after construction.
  50. limitEnforcers []*limitEnforcer
  51. }
  52. var _ admission.ValidationInterface = &Plugin{}
  53. // newEventRateLimit configures an admission controller that can enforce event rate limits
  54. func newEventRateLimit(config *eventratelimitapi.Configuration, clock flowcontrol.Clock) (*Plugin, error) {
  55. limitEnforcers := make([]*limitEnforcer, 0, len(config.Limits))
  56. for _, limitConfig := range config.Limits {
  57. enforcer, err := newLimitEnforcer(limitConfig, clock)
  58. if err != nil {
  59. return nil, err
  60. }
  61. limitEnforcers = append(limitEnforcers, enforcer)
  62. }
  63. eventRateLimitAdmission := &Plugin{
  64. Handler: admission.NewHandler(admission.Create, admission.Update),
  65. limitEnforcers: limitEnforcers,
  66. }
  67. return eventRateLimitAdmission, nil
  68. }
  69. // Validate makes admission decisions while enforcing event rate limits
  70. func (a *Plugin) Validate(attr admission.Attributes, o admission.ObjectInterfaces) (err error) {
  71. // ignore all operations that do not correspond to an Event kind
  72. if attr.GetKind().GroupKind() != api.Kind("Event") {
  73. return nil
  74. }
  75. // ignore all requests that specify dry-run
  76. // because they don't correspond to any calls to etcd,
  77. // they should not be affected by the ratelimit
  78. if attr.IsDryRun() {
  79. return nil
  80. }
  81. var errors []error
  82. // give each limit enforcer a chance to reject the event
  83. for _, enforcer := range a.limitEnforcers {
  84. if err := enforcer.accept(attr); err != nil {
  85. errors = append(errors, err)
  86. }
  87. }
  88. if aggregatedErr := utilerrors.NewAggregate(errors); aggregatedErr != nil {
  89. return apierrors.NewTooManyRequestsError(aggregatedErr.Error())
  90. }
  91. return nil
  92. }