Skip to content

Commit

Permalink
CH-108 improve quotas conversion consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
filippomc committed Dec 19, 2023
1 parent 197aef1 commit 8a71778
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ def change_pod_manifest(self: KubeSpawner):

# set user quota cpu/mem usage if value has a "value" else don't change the value
logging.info("Setting user quota cpu/mem usage")
set_key_value(self, key="cpu_guarantee", value=float(user_quotas.get(
"quota-ws-guaranteecpu")))
set_key_value(self, key="cpu_guarantee", value=user_quotas.get(
"quota-ws-guaranteecpu"))
set_key_value(self, key="cpu_limit",
value=float(user_quotas.get("quota-ws-maxcpu")))
value=user_quotas.get("quota-ws-maxcpu"))
set_key_value(self, key="mem_guarantee", value=user_quotas.get(
"quota-ws-guaranteemem"), unit="G")
set_key_value(self, key="mem_limit", value=user_quotas.get(
Expand Down
15 changes: 11 additions & 4 deletions libraries/cloudharness-common/cloudharness/auth/quota.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from keycloak import KeycloakError
from .keycloak import AuthClient
from cloudharness.applications import get_current_configuration
Expand Down Expand Up @@ -93,17 +94,23 @@ def _compute_quotas_from_tree(node: QuotaNode):
child_attrs = _compute_quotas_from_tree(child)
for key in child_attrs:
try:
child_val = float(child_attrs[key])
# we expect all quota values to be numbers: the unit is implicit and
# defined at usage time
child_val = attribute_to_quota(child_attrs[key])
except:
# value not a float, skip (use 0)
child_val = 0
# value not a float, skip
continue
if not key in new_attrs or new_attrs[key] < child_val:
new_attrs.update({key: child_val})
for key in new_attrs:
node.attrs.update({key: new_attrs[key]})
return node.attrs


def attribute_to_quota(attr_value: str):
return float(re.sub("[^0-9.]", "", attr_value))


def get_user_quotas(application_config: ApplicationConfig = None, user_id: str = None) -> dict:
"""Get the user quota from Keycloak and application
Expand Down Expand Up @@ -142,5 +149,5 @@ def get_user_quotas(application_config: ApplicationConfig = None, user_id: str =
user_quotas.update({key: group_quotas[key]})
for key in base_quotas:
if key not in user_quotas:
user_quotas.update({key: base_quotas[key]})
user_quotas.update({key: attribute_to_quota(base_quotas[key])})
return user_quotas

0 comments on commit 8a71778

Please sign in to comment.