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 23, 2023
1 parent e19b463 commit 5d2720e
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 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 @@ -19,20 +20,43 @@ def create_instance(cls, openshift: OpenShiftClient, name, route: Referencable,
"metadata": {"name": name, "namespace": openshift.project, "labels": labels},
"spec": {
"targetRef": route.reference,
"rateLimits": [{"limits": []}],
"rateLimits": [],
},
}

return cls(model, context=openshift.context)

@staticmethod
def _user_id_variable(user_id):
"""Add configuration for rate limit."""
return {
"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}
configuration = []
if conditions:
limit["conditions"] = conditions
limits.append(limit)
if variable:
limit["variables"] = [variable]
configuration = [self._user_id_variable(variable)]
rate_limit = {"limits": [limit], "configurations": configuration}

self.model.spec.setdefault("rateLimits", []).append(rate_limit)

def commit(self):
result = super().commit()
Expand Down

0 comments on commit 5d2720e

Please sign in to comment.