Skip to content

Commit

Permalink
Added configurations for RLP
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolar committed May 22, 2023
1 parent e19b463 commit d2d1e49
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions testsuite/openshift/objects/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from time import sleep

import openshift as oc

from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject, modify
from testsuite.openshift.objects.gateway_api import Referencable
Expand All @@ -17,21 +18,45 @@ def create_instance(cls, openshift: OpenShiftClient, name, route: Referencable,
"apiVersion": "kuadrant.io/v1beta1",
"kind": "RateLimitPolicy",
"metadata": {"name": name, "namespace": openshift.project, "labels": labels},
"spec": {
"targetRef": route.reference,
"rateLimits": [{"limits": []}],
},
"spec": {"targetRef": route.reference},
# "rateLimits": [{"limits": []}, {"configurations": []}],
}

return cls(model, context=openshift.context)

@staticmethod
def _user_id_variable(user_id):
"""Add configuration for limit."""
return {
"configurations": [
{
"actions": [
{
"metadata": {
"descriptor_key": user_id,
"default_value": "no-user",
"metadata_key": {
"key": "envoy.filters.http.ext_authz",
"path": [{"segment": {"key": "ext_auth_data"}}, {"segment": {"key": user_id}}],
},
}
}
]
}
]
}

@modify
def add_limit(self, max_value, seconds, conditions: list[str] = None):
def add_limit(self, max_value, seconds, conditions: list[str] = None, variable=None):
"""Add another limit"""
limits = self.model.spec.rateLimits[0].setdefault("limits", [])
limit = {"maxValue": max_value, "seconds": seconds}
if conditions:
limit["conditions"] = conditions
if variable:
limit["variables"] = variable
configurations = self.model.spec.rateLimits[0].setdefault("configurations", [])
configurations.append(self._user_id_variable(variable))
limits.append(limit)

def commit(self):
Expand Down

0 comments on commit d2d1e49

Please sign in to comment.