From 5d2720e13779e805c5105a00f734ac6974872ece Mon Sep 17 00:00:00 2001 From: jsmolar Date: Mon, 22 May 2023 16:25:23 +0200 Subject: [PATCH] Added configurations for RLP --- testsuite/openshift/objects/rate_limit.py | 32 ++++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/testsuite/openshift/objects/rate_limit.py b/testsuite/openshift/objects/rate_limit.py index d731745c6..e254e206b 100644 --- a/testsuite/openshift/objects/rate_limit.py +++ b/testsuite/openshift/objects/rate_limit.py @@ -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 @@ -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()