Skip to content

Commit

Permalink
ADD: strategy to policy
Browse files Browse the repository at this point in the history
Allow adding strategy field to policies.

Signed-off-by: Jim Fitzpatrick <[email protected]>
  • Loading branch information
Boomatang authored and martinhesko committed Nov 29, 2024
1 parent 85510e8 commit dcf8668
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions testsuite/kuadrant/policy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"""Contains Base class for policies"""

from dataclasses import dataclass
from enum import Enum

from testsuite.kubernetes import KubernetesObject
from testsuite.utils import check_condition


class Strategy(Enum):
"""Class for merge strategies of defaults and overrides."""

ATOMIC = "atomic"
MERGE = "merge"


@dataclass
class CelPredicate:
"""Dataclass that references CEL predicate e.g. auth.identity.anonymous == 'true'"""
Expand Down
20 changes: 18 additions & 2 deletions testsuite/kuadrant/policy/authorization/auth_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from testsuite.kubernetes import modify
from testsuite.kubernetes.client import KubernetesClient
from testsuite.utils import asdict

from .. import CelPredicate, Policy, Strategy
from . import Pattern
from .auth_config import AuthConfig
from .sections import ResponseSection
from .. import Policy, CelPredicate
from . import Pattern


class AuthPolicy(Policy, AuthConfig):
Expand Down Expand Up @@ -49,6 +50,21 @@ def add_rule(self, when: list[CelPredicate]):
self.model.spec.setdefault("when", [])
self.model.spec["when"].extend([asdict(x) for x in when])

# TODO: need to check if the typing is set up correctlly.
@modify
def strategy(self, strategy: Strategy) -> None:
"""Add strategy type to default or overrides spec"""
if self.spec_section is None:
if "defaults" in self.model.spec:
self.spec_section = self.model.spec["default"]
elif "overrides" in self.model.spec:
self.spec_section = self.model.spec["overrides"]
else:
raise TypeError("no default or override section found in spec")

self.spec_section["strategy"] = strategy.value
self.spec_section = None

@property
def auth_section(self):
if self.spec_section is None:
Expand Down
17 changes: 16 additions & 1 deletion testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from typing import Iterable

from testsuite.gateway import Referencable
from testsuite.kuadrant.policy import CelExpression, CelPredicate, Policy, Strategy
from testsuite.kubernetes import modify
from testsuite.kubernetes.client import KubernetesClient
from testsuite.kuadrant.policy import Policy, CelPredicate, CelExpression
from testsuite.utils import asdict


Expand Down Expand Up @@ -72,6 +72,21 @@ def add_limit(
self.spec_section.setdefault("limits", {})[name] = limit
self.spec_section = None

# TODO: need to check if the typing is set up correctlly.
@modify
def strategy(self, strategy: Strategy) -> None:
"""Add strategy type to default or overrides spec"""
if self.spec_section is None:
if "defaults" in self.model.spec:
self.spec_section = self.model.spec["defaults"]
elif "overrides" in self.model.spec:
self.spec_section = self.model.spec["overrides"]
else:
raise TypeError("no default or override section found in spec")

self.spec_section["strategy"] = strategy.value
self.spec_section = None

@property
def defaults(self):
"""Add new rule into the `defaults` RateLimitPolicy section"""
Expand Down

0 comments on commit dcf8668

Please sign in to comment.