Skip to content

Commit

Permalink
Change Limit defition to refelct v1beta3
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Smolar <[email protected]>
  • Loading branch information
Jakub Smolar committed Nov 11, 2024
1 parent e93b295 commit bceda3d
Show file tree
Hide file tree
Showing 20 changed files with 30 additions and 31 deletions.
3 changes: 1 addition & 2 deletions testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class Limit:
"""Limit dataclass"""

limit: int
duration: int
unit: Literal["second", "minute", "day"] = "second"
window: str


class RateLimitPolicy(Policy):
Expand Down
2 changes: 1 addition & 1 deletion testsuite/tests/kuadrantctl/cli/test_simple_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def oas(oas, blame, gateway, hostname, backend):
oas.add_top_level_route(gateway, hostname, blame("route"))
oas.add_backend_to_paths(backend)

oas["paths"]["/anything"]["get"]["x-kuadrant"] = {"rate_limit": {"rates": [asdict(Limit(3, 20))]}}
oas["paths"]["/anything"]["get"]["x-kuadrant"] = {"rate_limit": {"rates": [asdict(Limit(3, "20s"))]}}
return oas


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

LIMIT = Limit(3, 5)
LIMIT = Limit(3, "5s")


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def test_rules_exclusivity_authorization(cluster, route, oidc_provider, module_l
def test_rules_exclusivity_rate_limit(cluster, route, module_label, blame):
"""Test that server will reject object with implicit and explicit defaults simultaneously in RateLimitPolicy"""
rate_limit = RateLimitPolicy.create_instance(cluster, blame("limit"), route, labels={"testRun": module_label})
rate_limit.defaults.add_limit("inside-defaults", [Limit(2, 5)])
rate_limit.add_limit("outside-defaults", [Limit(2, 5)])
rate_limit.defaults.add_limit("inside-defaults", [Limit(2, "5s")])
rate_limit.add_limit("outside-defaults", [Limit(2, "5s")])

with pytest.raises(OpenShiftPythonException, match="Implicit and explicit defaults are mutually exclusive"):
rate_limit.commit()
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def rate_limit(cluster, blame, module_label, route):
"""

policy = RateLimitPolicy.create_instance(cluster, blame("limit"), route, labels={"testRun": module_label})
policy.add_limit("basic", [Limit(5, 10)])
policy.add_limit("basic", [Limit(5, "10s")])
return policy


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def authorization2(request, route2, blame, openshift, label):
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to 1st RateLimitPolicy allowing 1 request per 10 minutes (a.k.a. '1rp10m' RateLimitPolicy)"""
rate_limit.add_limit("1rp10m", [Limit(1, 10)])
rate_limit.add_limit("1rp10m", [Limit(1, "10s")])
return rate_limit


Expand All @@ -38,7 +38,7 @@ def rate_limit2(request, route2, blame, openshift, label):
"""2nd RateLimitPolicy allowing 2 requests per 10 minutes (a.k.a. '2rp10m' RateLimitPolicy)"""
rlp = RateLimitPolicy.create_instance(openshift, blame("2rp10m"), route2, labels={"testRun": label})
request.addfinalizer(rlp.delete)
rlp.add_limit("2rp10m", [Limit(2, 10)])
rlp.add_limit("2rp10m", [Limit(2, "10s")])
rlp.commit()
rlp.wait_for_ready()
return rlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def rate_limit2(request, gateway, blame, cluster, label):
"""2nd RateLimitPolicy object allowing 1 request per 10 minutes (a.k.a. '1rp10m')"""
rlp = RateLimitPolicy.create_instance(cluster, blame("2rp10m"), gateway, labels={"testRun": label})
request.addfinalizer(rlp.delete)
rlp.add_limit("1rp10m", [Limit(1, 600)])
rlp.add_limit("1rp10m", [Limit(1, "600s")])
rlp.commit()
rlp.wait_for_partial_enforced()
return rlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to 1st RateLimitPolicy allowing 1 request per 10 minutes (a.k.a. '1rp10m' RateLimitPolicy)"""
rate_limit.add_limit("1rp10m", [Limit(1, 10)])
rate_limit.add_limit("1rp10m", [Limit(1, "10s")])
return rate_limit


Expand All @@ -23,7 +23,7 @@ def rate_limit2(request, route2, blame, cluster, label):
"""2nd RateLimitPolicy allowing 2 requests per 10 minutes (a.k.a. '2rp10m' RateLimitPolicy)"""
rlp = RateLimitPolicy.create_instance(cluster, blame("2rp10m"), route2, labels={"testRun": label})
request.addfinalizer(rlp.delete)
rlp.add_limit("2rp10m", [Limit(2, 10)])
rlp.add_limit("2rp10m", [Limit(2, "10s")])
rlp.commit()
rlp.wait_for_ready()
return rlp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def route(route, backend):
def rate_limit(rate_limit):
"""Add limit to the policy"""
when = [Pattern("request.path", "eq", "/anything"), Pattern("request.method", "eq", "GET")]
rate_limit.add_limit("anything", [Limit(5, 10)], when=when)
rate_limit.add_limit("anything", [Limit(5, "10s")], when=when)
return rate_limit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
rate_limit.add_limit("multiple", [Limit(3, 10)])
rate_limit.add_limit("multiple", [Limit(3, "10s")])
return rate_limit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def rate_limit(rate_limit):
"""Add limit to the policy"""
when = Pattern("request.method", "eq", "GET")
rate_limit.add_limit("test", [Limit(5, 10)], when=[when])
rate_limit.add_limit("test", [Limit(5, "10s")], when=[when])
return rate_limit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
def rate_limit(rate_limit):
"""Add limit to the policy"""
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])
rate_limit.add_limit("test1", [Limit(8, "10s")], when=[when])
rate_limit.add_limit("test2", [Limit(3, "5s")], when=[when])
return rate_limit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def rate_limit(rate_limit):
"""Add limit to the policy"""
when = [Pattern("request.path", "eq", "/get")]
rate_limit.add_limit("multiple", [Limit(5, 10)], when=when)
rate_limit.add_limit("multiple", [Limit(5, "10s")], when=when)
return rate_limit


Expand Down
6 changes: 3 additions & 3 deletions testsuite/tests/singlecluster/limitador/test_basic_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
@pytest.fixture(
scope="module",
params=[
pytest.param(Limit(2, 15), id="2 requests every 15 sec"),
pytest.param(Limit(5, 10), id="5 requests every 10 sec"),
pytest.param(Limit(3, 5), id="3 request every 5 sec"),
pytest.param(Limit(2, "15s"), id="2 requests every 15 sec"),
pytest.param(Limit(5, "10s"), id="5 requests every 10 sec"),
pytest.param(Limit(3, "5s"), id="3 request every 5 sec"),
],
)
def limit(request):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
rate_limit.add_limit("multiple", [Limit(5, 10)])
rate_limit.add_limit("multiple", [Limit(5, "10s")])
return rate_limit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

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

GATEWAY_LIMIT = Limit(3, 5)
ROUTE_LIMIT = Limit(2, 5)
GATEWAY_LIMIT = Limit(3, "5s")
ROUTE_LIMIT = Limit(2, "5s")


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def authorization(authorization, oidc_provider):
@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add basic rate limiting rules in the overrides block"""
rate_limit.overrides.add_limit("override", [Limit(2, 5)])
rate_limit.overrides.add_limit("override", [Limit(2, "5s")])
return rate_limit


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def test_rules_exclusivity_explicit_authorization(cluster, route, oidc_provider,
def test_rules_exclusivity_implicit_rate_limit(cluster, route, module_label, blame):
"""Test that server will reject a RateLimitPolicy with overrides and implicit defaults defined simultaneously"""
rate_limit = RateLimitPolicy.create_instance(cluster, blame("limit"), route, labels={"testRun": module_label})
rate_limit.overrides.add_limit("overrides", [Limit(2, 5)])
rate_limit.add_limit("implicit-defaults", [Limit(2, 5)])
rate_limit.overrides.add_limit("overrides", [Limit(2, "5s")])
rate_limit.add_limit("implicit-defaults", [Limit(2, "5s")])

with pytest.raises(OpenShiftPythonException, match="Overrides and implicit defaults are mutually exclusive"):
rate_limit.commit()
Expand All @@ -56,8 +56,8 @@ def test_rules_exclusivity_implicit_rate_limit(cluster, route, module_label, bla
def test_rules_exclusivity_explicit_rate_limit(cluster, route, module_label, blame):
"""Test that server will reject a RateLimitPolicy with overrides and explicit defaults defined simultaneously"""
rate_limit = RateLimitPolicy.create_instance(cluster, blame("limit"), route, labels={"testRun": module_label})
rate_limit.overrides.add_limit("overrides", [Limit(2, 5)])
rate_limit.defaults.add_limit("explicit-defaults", [Limit(2, 5)])
rate_limit.overrides.add_limit("overrides", [Limit(2, "5s")])
rate_limit.defaults.add_limit("explicit-defaults", [Limit(2, "5s")])

with pytest.raises(OpenShiftPythonException, match="Overrides and explicit defaults are mutually exclusive"):
rate_limit.commit()
2 changes: 1 addition & 1 deletion testsuite/tests/singlecluster/test_rate_limit_anonymous.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def rate_limit(rate_limit):
"""Add limit to the policy only for anonymous users"""
rate_limit.add_limit(
"basic",
[Limit(5, 10)],
[Limit(5, "10s")],
when=[
Pattern(
selector=r"metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.anonymous",
Expand Down
2 changes: 1 addition & 1 deletion testsuite/tests/singlecluster/test_rate_limit_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def rate_limit(rate_limit):
"""Add limit to the policy"""
rate_limit.add_limit(
"basic", [Limit(5, 60)], counters=[r"metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.user"]
"basic", [Limit(5, "60s")], counters=[r"metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.user"]
)
return rate_limit

Expand Down

0 comments on commit bceda3d

Please sign in to comment.