admission.go 3.6 KB

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