Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed RouteSelectors due to version bump #552

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "/anything"), 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/testsuite/issues/561")
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")
azgabur marked this conversation as resolved.
Show resolved Hide resolved
rate_limit.add_limit("test", [Limit(5, 10)], when=[when])
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561")
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,13 @@
@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/testsuite/issues/561")
def test_two_rules_targeting_one_limit(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
Expand Down
18 changes: 5 additions & 13 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")]
azgabur marked this conversation as resolved.
Show resolved Hide resolved
rate_limit.add_limit("multiple", [Limit(5, 10)], when=when)
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561")
def test_rule_match(client):
"""Tests that RLP correctly applies to the given HTTPRoute rule"""
responses = client.get_many("/get", 5)
Expand All @@ -28,9 +26,3 @@ def test_rule_match(client):

response = client.get("/anything")
assert response.status_code == 200


def test_rule_missmatch(client):
"""Tests that RLP is not applied for not existing route rule"""
responses = client.get_many("/anything/test", 7)
responses.assert_all(status_code=200)
47 changes: 0 additions & 47 deletions testsuite/tests/singlecluster/limitador/test_route_rule_invalid.py

This file was deleted.

Loading