Skip to content

Commit

Permalink
Removed RouteSelectors due to version bump
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Hesko <[email protected]>
  • Loading branch information
martinhesko committed Oct 10, 2024
1 parent 6f54c6c commit 020a04e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 96 deletions.
26 changes: 3 additions & 23 deletions testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import time
from dataclasses import dataclass
from typing import Iterable, Literal, Optional, List
from typing import Iterable, Literal

from testsuite.gateway import Referencable, RouteMatch
from testsuite.gateway import Referencable
from testsuite.kubernetes import modify
from testsuite.kubernetes.client import KubernetesClient
from testsuite.kuadrant.policy import Policy
Expand All @@ -21,23 +21,6 @@ class Limit:
unit: Literal["second", "minute", "day"] = "second"


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

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(Policy):
"""RateLimitPolicy (or RLP for short) object, used for applying rate limiting rules to a Gateway/HTTPRoute"""

Expand All @@ -49,7 +32,7 @@ def __init__(self, *args, **kwargs):
def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, labels: dict[str, str] = None):
"""Creates new instance of RateLimitPolicy"""
model = {
"apiVersion": "kuadrant.io/v1beta2",
"apiVersion": "kuadrant.io/v1beta3",
"kind": "RateLimitPolicy",
"metadata": {"name": name, "labels": labels},
"spec": {
Expand All @@ -66,7 +49,6 @@ def add_limit(
limits: Iterable[Limit],
when: Iterable[Rule] = None,
counters: list[str] = None,
route_selectors: Iterable[RouteSelector] = None,
):
"""Add another limit"""
limit: dict = {
Expand All @@ -76,8 +58,6 @@ def add_limit(
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]

if self.spec_section is None:
self.spec_section = self.model.spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType, HTTPMethod
from testsuite.kuadrant.policy.rate_limit import Limit, RouteSelector
from testsuite.kuadrant.policy.authorization import Pattern
from testsuite.kuadrant.policy.rate_limit import Limit


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
Expand All @@ -27,13 +28,12 @@ def route(route, backend):
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX), method=HTTPMethod.GET)
)
rate_limit.add_limit("anything", [Limit(5, 10)], route_selectors=[selector])
when = [Pattern("request.path", "eq", "/get"), Pattern("request.method", "eq", "GET")]
rate_limit.add_limit("anything", [Limit(5, 10)], when=when)
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/kuadrant-operator/issues/821")
def test_route_subset_method(client):
"""Tests that RLP for a HTTPRouteRule doesn't apply to separate HTTPRouteRule with different method"""
responses = client.get_many("/anything", 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType
from testsuite.kuadrant.policy.rate_limit import RouteSelector, Limit
from testsuite.kuadrant.policy.authorization import Pattern
from testsuite.kuadrant.policy.rate_limit import Limit


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
Expand All @@ -12,14 +12,12 @@
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)),
)
rate_limit.add_limit("test", [Limit(5, 10)], route_selectors=[selector])
when = Pattern("request.method", "eq", "GET")
rate_limit.add_limit("test", [Limit(5, 10)], when=[when])
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/kuadrant-operator/issues/821")
def test_limit_targeting_two_rules(client):
"""Tests that one RLP limit targeting two rules limits them together"""
responses = client.get_many("/get", 3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType
from testsuite.kuadrant.policy.rate_limit import RouteSelector, Limit
from testsuite.kuadrant.policy.rate_limit import Limit
from testsuite.kuadrant.policy.authorization import Pattern


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
Expand All @@ -12,14 +12,14 @@
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
)
rate_limit.add_limit("test1", [Limit(8, 10)], route_selectors=[selector])
rate_limit.add_limit("test2", [Limit(3, 5)], route_selectors=[selector])
when = Pattern("request.path", "eq", "/get")
rate_limit.add_limit("test1", [Limit(8, 10)], when=[when])
rate_limit.add_limit("test2", [Limit(3, 5)], when=[when])
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/kuadrant-operator/issues/821")
@pytest.mark.issue("https://github.com/Kuadrant/wasm-shim/issues/104")
def test_two_rules_targeting_one_limit(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
Expand Down
12 changes: 5 additions & 7 deletions testsuite/tests/singlecluster/limitador/route/test_route_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType
from testsuite.kuadrant.policy.rate_limit import Limit, RouteSelector
from testsuite.kuadrant.policy.rate_limit import Limit
from testsuite.kuadrant.policy.authorization import Pattern

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
selector = RouteSelector(
RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)),
RouteMatch(path=PathMatch(value="/anything/test", type=MatchType.PATH_PREFIX)),
)
rate_limit.add_limit("multiple", [Limit(5, 10)], route_selectors=[selector])
when = [Pattern("request.path", "eq", "/get")]
rate_limit.add_limit("multiple", [Limit(5, 10)], when=when)
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/kuadrant-operator/issues/821")
def test_rule_match(client):
"""Tests that RLP correctly applies to the given HTTPRoute rule"""
responses = client.get_many("/get", 5)
Expand Down
47 changes: 0 additions & 47 deletions testsuite/tests/singlecluster/limitador/test_route_rule_invalid.py

This file was deleted.

0 comments on commit 020a04e

Please sign in to comment.