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 8, 2024
1 parent 8faf70c commit c53f359
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 34 deletions.
30 changes: 2 additions & 28 deletions testsuite/gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Classes related to Gateways"""
import abc
import enum
from abc import ABC, abstractmethod
from dataclasses import dataclass
Expand All @@ -9,7 +8,7 @@

from testsuite.certificates import Certificate
from testsuite.lifecycle import LifecycleObject
from testsuite.utils import asdict, _asdict_recurse
from testsuite.utils import asdict

if TYPE_CHECKING:
from testsuite.openshift.client import OpenShiftClient
Expand Down Expand Up @@ -76,10 +75,6 @@ class PathMatch:
type: Optional[MatchType] = None
value: Optional[str] = None

# def asdict(self):
# """Custom dict due to nested structure of matchers."""
# return {"path": _asdict_recurse(self, False)}


@dataclass
class HeadersMatch:
Expand All @@ -89,10 +84,6 @@ class HeadersMatch:
value: str
type: Optional[Literal[MatchType.EXACT, MatchType.REGULAR_EXPRESSION]] = None

# def asdict(self):
# """Custom dict due to nested structure of matchers."""
# return {"headers": [_asdict_recurse(self, False)]}


@dataclass
class QueryParamsMatch:
Expand All @@ -102,23 +93,6 @@ class QueryParamsMatch:
value: str
type: Optional[Literal[MatchType.EXACT, MatchType.REGULAR_EXPRESSION]] = None

# def asdict(self):
# """Custom dict due to nested structure of matchers."""
# return {"queryParams": [_asdict_recurse(self, False)]}


@dataclass
class MethodMatch:
"""
HTTPMethod describes how to select a HTTP route by matching the HTTP method. The value is expected in upper case.
"""

value: HTTPMethod = None

def asdict(self):
"""Custom dict due to nested structure of matchers."""
return {"method": self.value.value}


@dataclass
class RouteMatch:
Expand All @@ -132,7 +106,7 @@ class RouteMatch:
path: Optional[PathMatch] = None
headers: Optional[List[HeadersMatch]] = None
query_params: Optional[List[QueryParamsMatch]] = None
method: HTTPMethod = None
method: Optional[HTTPMethod] = None


class Gateway(LifecycleObject, Referencable):
Expand Down
18 changes: 12 additions & 6 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, Optional
from typing import Iterable, Literal, Optional, List

import openshift as oc

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

Expand All @@ -22,15 +22,21 @@ class Limit:


@dataclass
class RouteSelect:
class RouteSelector:
"""
HTRTPPathMatch, HTTPHeaderMatch, HTTPQueryParamMatch, HTTPMethodMatch
RouteSelector is an object composed of a set of HTTPRouteMatch objects (from Gateway API -
HTRTPPathMatch, HTTPHeaderMatch, HTTPQueryParamMatch, HTTPMethodMatch),
and an additional hostnames field.
https://docs.kuadrant.io/kuadrant-operator/doc/reference/route-selectors/#routeselector
"""

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

def __init__(self, *matches: RouteMatch, hostnames: Optional[List[str]] = None):
self.matches = list(matches) if matches else []
self.hostnames = hostnames


class RateLimitPolicy(OpenShiftObject):
"""RateLimitPolicy (or RLP for short) object, used for applying rate limiting rules to a Gateway/HTTPRoute"""
Expand All @@ -57,7 +63,7 @@ def add_limit(
limits: Iterable[Limit],
when: Iterable[Rule] = None,
counters: list[str] = None,
route_selectors: Iterable[RouteSelect] = None,
route_selectors: Iterable[RouteSelector] = None,
):
"""Add another limit"""
limit: dict = {
Expand Down
3 changes: 3 additions & 0 deletions testsuite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def asdict(obj) -> dict[str, JSONValues]:


def _asdict_recurse(obj):
if hasattr(obj, "asdict"):
return obj.asdict()

if not is_dataclass(obj):
return deepcopy(obj)

Expand Down

0 comments on commit c53f359

Please sign in to comment.