123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package eventratelimit
- import (
- "testing"
- "time"
- "k8s.io/apimachinery/pkg/api/errors"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/types"
- "k8s.io/apimachinery/pkg/util/clock"
- "k8s.io/apiserver/pkg/admission"
- "k8s.io/apiserver/pkg/authentication/user"
- api "k8s.io/kubernetes/pkg/apis/core"
- eventratelimitapi "k8s.io/kubernetes/plugin/pkg/admission/eventratelimit/apis/eventratelimit"
- )
- const (
- qps = 1
- eventKind = "Event"
- nonEventKind = "NonEvent"
- )
- // attributesForRequest generates the admission.Attributes that for the specified request
- func attributesForRequest(rq request) admission.Attributes {
- return admission.NewAttributesRecord(
- rq.event,
- nil,
- api.Kind(rq.kind).WithVersion("version"),
- rq.namespace,
- "name",
- api.Resource("resource").WithVersion("version"),
- "",
- admission.Create,
- &metav1.CreateOptions{},
- rq.dryRun,
- &user.DefaultInfo{Name: rq.username})
- }
- type request struct {
- kind string
- namespace string
- username string
- event *api.Event
- delay time.Duration
- accepted bool
- dryRun bool
- }
- func newRequest(kind string) request {
- return request{
- kind: kind,
- accepted: true,
- }
- }
- func newEventRequest() request {
- return newRequest(eventKind)
- }
- func newNonEventRequest() request {
- return newRequest(nonEventKind)
- }
- func (r request) withNamespace(namespace string) request {
- r.namespace = namespace
- return r
- }
- func (r request) withEvent(event *api.Event) request {
- r.event = event
- return r
- }
- func (r request) withEventComponent(component string) request {
- return r.withEvent(&api.Event{
- Source: api.EventSource{
- Component: component,
- },
- })
- }
- func (r request) withDryRun(dryRun bool) request {
- r.dryRun = dryRun
- return r
- }
- func (r request) withUser(name string) request {
- r.username = name
- return r
- }
- func (r request) blocked() request {
- r.accepted = false
- return r
- }
- // withDelay will adjust the clock to simulate the specified delay, in seconds
- func (r request) withDelay(delayInSeconds int) request {
- r.delay = time.Duration(delayInSeconds) * time.Second
- return r
- }
- // createSourceAndObjectKeyInclusionRequests creates a series of requests that can be used
- // to test that a particular part of the event is included in the source+object key
- func createSourceAndObjectKeyInclusionRequests(eventFactory func(label string) *api.Event) []request {
- return []request{
- newEventRequest().withEvent(eventFactory("A")),
- newEventRequest().withEvent(eventFactory("A")).blocked(),
- newEventRequest().withEvent(eventFactory("B")),
- }
- }
- func TestEventRateLimiting(t *testing.T) {
- cases := []struct {
- name string
- serverBurst int32
- namespaceBurst int32
- namespaceCacheSize int32
- sourceAndObjectBurst int32
- sourceAndObjectCacheSize int32
- userBurst int32
- userCacheSize int32
- requests []request
- }{
- {
- name: "event not blocked when tokens available",
- serverBurst: 3,
- requests: []request{
- newEventRequest(),
- },
- },
- {
- name: "non-event not blocked",
- serverBurst: 3,
- requests: []request{
- newNonEventRequest(),
- },
- },
- {
- name: "event blocked after tokens exhausted",
- serverBurst: 3,
- requests: []request{
- newEventRequest(),
- newEventRequest(),
- newEventRequest(),
- newEventRequest().blocked(),
- },
- },
- {
- name: "event not blocked by dry-run requests",
- serverBurst: 3,
- requests: []request{
- newEventRequest(),
- newEventRequest(),
- newEventRequest().withDryRun(true),
- newEventRequest().withDryRun(true),
- newEventRequest().withDryRun(true),
- newEventRequest().withDryRun(true),
- newEventRequest(),
- newEventRequest().blocked(),
- newEventRequest().withDryRun(true),
- },
- },
- {
- name: "non-event not blocked after tokens exhausted",
- serverBurst: 3,
- requests: []request{
- newEventRequest(),
- newEventRequest(),
- newEventRequest(),
- newNonEventRequest(),
- },
- },
- {
- name: "non-events should not count against limit",
- serverBurst: 3,
- requests: []request{
- newEventRequest(),
- newEventRequest(),
- newNonEventRequest(),
- newEventRequest(),
- },
- },
- {
- name: "event accepted after token refill",
- serverBurst: 3,
- requests: []request{
- newEventRequest(),
- newEventRequest(),
- newEventRequest(),
- newEventRequest().blocked(),
- newEventRequest().withDelay(1),
- },
- },
- {
- name: "event blocked by namespace limits",
- serverBurst: 100,
- namespaceBurst: 3,
- namespaceCacheSize: 10,
- requests: []request{
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A").blocked(),
- },
- },
- {
- name: "event from other namespace not blocked",
- serverBurst: 100,
- namespaceBurst: 3,
- namespaceCacheSize: 10,
- requests: []request{
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("B"),
- },
- },
- {
- name: "events from other namespaces should not count against limit",
- serverBurst: 100,
- namespaceBurst: 3,
- namespaceCacheSize: 10,
- requests: []request{
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("B"),
- newEventRequest().withNamespace("A"),
- },
- },
- {
- name: "event accepted after namespace token refill",
- serverBurst: 100,
- namespaceBurst: 3,
- namespaceCacheSize: 10,
- requests: []request{
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A").blocked(),
- newEventRequest().withNamespace("A").withDelay(1),
- },
- },
- {
- name: "event from other namespaces should not clear namespace limits",
- serverBurst: 100,
- namespaceBurst: 3,
- namespaceCacheSize: 10,
- requests: []request{
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("B"),
- newEventRequest().withNamespace("A").blocked(),
- },
- },
- {
- name: "namespace limits from lru namespace should clear when cache size exceeded",
- serverBurst: 100,
- namespaceBurst: 3,
- namespaceCacheSize: 2,
- requests: []request{
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("B"),
- newEventRequest().withNamespace("B"),
- newEventRequest().withNamespace("B"),
- newEventRequest().withNamespace("A"),
- newEventRequest().withNamespace("B").blocked(),
- newEventRequest().withNamespace("A").blocked(),
- // This should clear out namespace B from the lru cache
- newEventRequest().withNamespace("C"),
- newEventRequest().withNamespace("A").blocked(),
- newEventRequest().withNamespace("B"),
- },
- },
- {
- name: "event blocked by source+object limits",
- serverBurst: 100,
- sourceAndObjectBurst: 3,
- sourceAndObjectCacheSize: 10,
- requests: []request{
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A").blocked(),
- },
- },
- {
- name: "event from other source+object not blocked",
- serverBurst: 100,
- sourceAndObjectBurst: 3,
- sourceAndObjectCacheSize: 10,
- requests: []request{
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("B"),
- },
- },
- {
- name: "events from other source+object should not count against limit",
- serverBurst: 100,
- sourceAndObjectBurst: 3,
- sourceAndObjectCacheSize: 10,
- requests: []request{
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("B"),
- newEventRequest().withEventComponent("A"),
- },
- },
- {
- name: "event accepted after source+object token refill",
- serverBurst: 100,
- sourceAndObjectBurst: 3,
- sourceAndObjectCacheSize: 10,
- requests: []request{
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A").blocked(),
- newEventRequest().withEventComponent("A").withDelay(1),
- },
- },
- {
- name: "event from other source+object should not clear source+object limits",
- serverBurst: 100,
- sourceAndObjectBurst: 3,
- sourceAndObjectCacheSize: 10,
- requests: []request{
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("B"),
- newEventRequest().withEventComponent("A").blocked(),
- },
- },
- {
- name: "source+object limits from lru source+object should clear when cache size exceeded",
- serverBurst: 100,
- sourceAndObjectBurst: 3,
- sourceAndObjectCacheSize: 2,
- requests: []request{
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("B"),
- newEventRequest().withEventComponent("B"),
- newEventRequest().withEventComponent("B"),
- newEventRequest().withEventComponent("A"),
- newEventRequest().withEventComponent("B").blocked(),
- newEventRequest().withEventComponent("A").blocked(),
- // This should clear out component B from the lru cache
- newEventRequest().withEventComponent("C"),
- newEventRequest().withEventComponent("A").blocked(),
- newEventRequest().withEventComponent("B"),
- },
- },
- {
- name: "source host should be included in source+object key",
- serverBurst: 100,
- sourceAndObjectBurst: 1,
- sourceAndObjectCacheSize: 10,
- requests: createSourceAndObjectKeyInclusionRequests(func(label string) *api.Event {
- return &api.Event{Source: api.EventSource{Host: label}}
- }),
- },
- {
- name: "involved object kind should be included in source+object key",
- serverBurst: 100,
- sourceAndObjectBurst: 1,
- sourceAndObjectCacheSize: 10,
- requests: createSourceAndObjectKeyInclusionRequests(func(label string) *api.Event {
- return &api.Event{InvolvedObject: api.ObjectReference{Kind: label}}
- }),
- },
- {
- name: "involved object namespace should be included in source+object key",
- serverBurst: 100,
- sourceAndObjectBurst: 1,
- sourceAndObjectCacheSize: 10,
- requests: createSourceAndObjectKeyInclusionRequests(func(label string) *api.Event {
- return &api.Event{InvolvedObject: api.ObjectReference{Namespace: label}}
- }),
- },
- {
- name: "involved object name should be included in source+object key",
- serverBurst: 100,
- sourceAndObjectBurst: 1,
- sourceAndObjectCacheSize: 10,
- requests: createSourceAndObjectKeyInclusionRequests(func(label string) *api.Event {
- return &api.Event{InvolvedObject: api.ObjectReference{Name: label}}
- }),
- },
- {
- name: "involved object UID should be included in source+object key",
- serverBurst: 100,
- sourceAndObjectBurst: 1,
- sourceAndObjectCacheSize: 10,
- requests: createSourceAndObjectKeyInclusionRequests(func(label string) *api.Event {
- return &api.Event{InvolvedObject: api.ObjectReference{UID: types.UID(label)}}
- }),
- },
- {
- name: "involved object APIVersion should be included in source+object key",
- serverBurst: 100,
- sourceAndObjectBurst: 1,
- sourceAndObjectCacheSize: 10,
- requests: createSourceAndObjectKeyInclusionRequests(func(label string) *api.Event {
- return &api.Event{InvolvedObject: api.ObjectReference{APIVersion: label}}
- }),
- },
- {
- name: "event blocked by user limits",
- userBurst: 3,
- userCacheSize: 10,
- requests: []request{
- newEventRequest().withUser("A"),
- newEventRequest().withUser("A"),
- newEventRequest().withUser("A"),
- newEventRequest().withUser("A").blocked(),
- },
- },
- {
- name: "event from other user not blocked",
- requests: []request{
- newEventRequest().withUser("A"),
- newEventRequest().withUser("A"),
- newEventRequest().withUser("A"),
- newEventRequest().withUser("B"),
- },
- },
- {
- name: "events from other user should not count against limit",
- requests: []request{
- newEventRequest().withUser("A"),
- newEventRequest().withUser("A"),
- newEventRequest().withUser("B"),
- newEventRequest().withUser("A"),
- },
- },
- }
- for _, tc := range cases {
- t.Run(tc.name, func(t *testing.T) {
- clock := clock.NewFakeClock(time.Now())
- config := &eventratelimitapi.Configuration{}
- if tc.serverBurst > 0 {
- serverLimit := eventratelimitapi.Limit{
- Type: eventratelimitapi.ServerLimitType,
- QPS: qps,
- Burst: tc.serverBurst,
- }
- config.Limits = append(config.Limits, serverLimit)
- }
- if tc.namespaceBurst > 0 {
- namespaceLimit := eventratelimitapi.Limit{
- Type: eventratelimitapi.NamespaceLimitType,
- Burst: tc.namespaceBurst,
- QPS: qps,
- CacheSize: tc.namespaceCacheSize,
- }
- config.Limits = append(config.Limits, namespaceLimit)
- }
- if tc.userBurst > 0 {
- userLimit := eventratelimitapi.Limit{
- Type: eventratelimitapi.UserLimitType,
- Burst: tc.userBurst,
- QPS: qps,
- CacheSize: tc.userCacheSize,
- }
- config.Limits = append(config.Limits, userLimit)
- }
- if tc.sourceAndObjectBurst > 0 {
- sourceAndObjectLimit := eventratelimitapi.Limit{
- Type: eventratelimitapi.SourceAndObjectLimitType,
- Burst: tc.sourceAndObjectBurst,
- QPS: qps,
- CacheSize: tc.sourceAndObjectCacheSize,
- }
- config.Limits = append(config.Limits, sourceAndObjectLimit)
- }
- eventratelimit, err := newEventRateLimit(config, clock)
- if err != nil {
- t.Fatalf("%v: Could not create EventRateLimit: %v", tc.name, err)
- }
- for rqIndex, rq := range tc.requests {
- if rq.delay > 0 {
- clock.Step(rq.delay)
- }
- attributes := attributesForRequest(rq)
- err = eventratelimit.Validate(attributes, nil)
- if rq.accepted != (err == nil) {
- expectedAction := "admitted"
- if !rq.accepted {
- expectedAction = "blocked"
- }
- t.Fatalf("%v: Request %v should have been %v: %v", tc.name, rqIndex, expectedAction, err)
- }
- if err != nil {
- statusErr, ok := err.(*errors.StatusError)
- if ok && statusErr.ErrStatus.Code != errors.StatusTooManyRequests {
- t.Fatalf("%v: Request %v should yield a 429 response: %v", tc.name, rqIndex, err)
- }
- }
- }
- })
- }
- }
|