Skip to content

Commit

Permalink
Add interface for defaults/overrides
Browse files Browse the repository at this point in the history
Signed-off-by: averevki <[email protected]>
  • Loading branch information
averevki committed Jul 30, 2024
1 parent d6499c9 commit b387844
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
28 changes: 24 additions & 4 deletions testsuite/kuadrant/policy/authorization/auth_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
class AuthPolicy(Policy, AuthConfig):
"""AuthPolicy object, it serves as Kuadrants AuthConfig"""

@property
def auth_section(self):
return self.model.spec.setdefault("rules", {})
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.spec_section = None

@classmethod
def create_instance(
Expand All @@ -35,7 +35,6 @@ def create_instance(
"metadata": {"name": name, "namespace": cluster.project, "labels": labels},
"spec": {
"targetRef": target.reference,
"rules": {},
},
}

Expand All @@ -46,3 +45,24 @@ def add_rule(self, when: list["Rule"]):
"""Add rule for the skip of entire AuthPolicy"""
self.model.spec.setdefault("when", [])
self.model.spec["when"].extend([asdict(x) for x in when])

@property
def auth_section(self):
if self.spec_section is None:
self.spec_section = self.model.spec

spec_section = self.spec_section
self.spec_section = None
return spec_section.setdefault("rules", {})

@property
def defaults(self):
"""Add new rule into the `defaults` AuthPolicy section"""
self.spec_section = self.model.spec.setdefault("defaults", {})
return self

@property
def overrides(self):
"""Add new rule into the `overrides` AuthPolicy section"""
self.spec_section = self.model.spec.setdefault("overrides", {})
return self
24 changes: 22 additions & 2 deletions testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def __init__(self, *matches: RouteMatch, hostnames: Optional[List[str]] = None):
class RateLimitPolicy(Policy):
"""RateLimitPolicy (or RLP for short) object, used for applying rate limiting rules to a Gateway/HTTPRoute"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.spec_section = None

@classmethod
def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, labels: dict[str, str] = None):
"""Creates new instance of RateLimitPolicy"""
Expand All @@ -50,7 +54,6 @@ def create_instance(cls, cluster: KubernetesClient, name, target: Referencable,
"metadata": {"name": name, "labels": labels},
"spec": {
"targetRef": target.reference,
"limits": {},
},
}

Expand All @@ -75,7 +78,24 @@ def add_limit(
limit["counters"] = counters
if route_selectors:
limit["routeSelectors"] = [asdict(rule) for rule in route_selectors]
self.model.spec.limits[name] = limit

if self.spec_section is None:
self.spec_section = self.model.spec

self.spec_section.setdefault("limits", {})[name] = limit
self.spec_section = None

@property
def defaults(self):
"""Add new rule into the `defaults` RateLimitPolicy section"""
self.spec_section = self.model.spec.setdefault("defaults", {})
return self

@property
def overrides(self):
"""Add new rule into the `overrides` RateLimitPolicy section"""
self.spec_section = self.model.spec.setdefault("overrides", {})
return self

def wait_for_ready(self):
"""Wait for RLP to be enforced"""
Expand Down
6 changes: 4 additions & 2 deletions testsuite/tests/singlecluster/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ def authorization_name(blame):


@pytest.fixture(scope="module")
def authorization(kuadrant, route, authorization_name, cluster, label):
def authorization(request, kuadrant, route, gateway, blame, cluster, label): # pylint: disable=unused-argument
"""Authorization object (In case of Kuadrant AuthPolicy)"""
target_ref = request.getfixturevalue(getattr(request, "param", "route"))

if kuadrant:
return AuthPolicy.create_instance(cluster, authorization_name, route, labels={"testRun": label})
return AuthPolicy.create_instance(cluster, blame("authz"), target_ref, labels={"testRun": label})
return None


Expand Down

0 comments on commit b387844

Please sign in to comment.