Skip to content

Commit

Permalink
Add Route Selector for Rate Limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolar committed Jan 4, 2024
1 parent 0fa965f commit a9bd463
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
28 changes: 24 additions & 4 deletions testsuite/policy/rate_limit_policy.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""RateLimitPolicy related objects"""
from dataclasses import dataclass
from time import sleep
from typing import Iterable, Literal
from typing import Iterable, Literal, Optional

import openshift as oc

from testsuite.policy.authorization import Pattern
from testsuite.policy.authorization import Rule
from testsuite.utils import asdict
from testsuite.gateway import Referencable
from testsuite.gateway import Referencable, HTTPMatcher
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift import OpenShiftObject, modify

Expand All @@ -21,6 +21,17 @@ class Limit:
unit: Literal["second", "minute", "day"] = "second"


@dataclass
class RouteSelect:
"""
HTRTPPathMatch, HTTPHeaderMatch, HTTPQueryParamMatch, HTTPMethodMatch
https://docs.kuadrant.io/kuadrant-operator/doc/reference/route-selectors/#routeselector
"""

matches: Optional[list[HTTPMatcher]] = None
hostnames: Optional[list[str]] = None


class RateLimitPolicy(OpenShiftObject):
"""RateLimitPolicy (or RLP for short) object, used for applying rate limiting rules to a Gateway/HTTPRoute"""

Expand All @@ -40,7 +51,14 @@ def create_instance(cls, openshift: OpenShiftClient, name, target: Referencable,
return cls(model, context=openshift.context)

@modify
def add_limit(self, name, limits: Iterable[Limit], when: Iterable[Pattern] = None, counters: list[str] = None):
def add_limit(
self,
name,
limits: Iterable[Limit],
when: Iterable[Rule] = None,
counters: list[str] = None,
route_selectors: Iterable[RouteSelect] = None,
):
"""Add another limit"""
limit: dict = {
"rates": [asdict(limit) for limit in limits],
Expand All @@ -49,6 +67,8 @@ def add_limit(self, name, limits: Iterable[Limit], when: Iterable[Pattern] = Non
limit["when"] = [asdict(rule) for rule in when]
if counters:
limit["counters"] = counters
if route_selectors:
limit["routeSelectors"] = [asdict(rule) for rule in route_selectors]
self.model.spec.limits[name] = limit

def wait_for_ready(self):
Expand Down
6 changes: 4 additions & 2 deletions testsuite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def asdict(obj) -> dict[str, JSONValues]:
return _asdict_recurse(obj)


def _asdict_recurse(obj):
if hasattr(obj, "asdict"):
def _asdict_recurse(obj, include_self=True):
if hasattr(obj, "asdict") and include_self:
return obj.asdict()

if not is_dataclass(obj):
Expand All @@ -156,6 +156,8 @@ def _asdict_recurse(obj):
result[field.name] = type(value)(_asdict_recurse(i) for i in value)
elif isinstance(value, dict):
result[field.name] = type(value)((_asdict_recurse(k), _asdict_recurse(v)) for k, v in value.items())
elif isinstance(value, enum.Enum):
result[field.name] = value.value
else:
result[field.name] = deepcopy(value)
return result

0 comments on commit a9bd463

Please sign in to comment.