Skip to content

Commit

Permalink
Intergrated quotas and limits into python code
Browse files Browse the repository at this point in the history
  • Loading branch information
QuanMPhm committed Jan 18, 2024
1 parent 76bfc04 commit 58216c8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 64 deletions.
18 changes: 6 additions & 12 deletions src/coldfront_plugin_cloud/acct_mgt/moc_openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,14 @@ def validate_role(role):
f"Invalid role, {role} is not one of {', '.join(OPENSHIFT_ROLES)}"
)

def __init__(self, client, logger, config):
def __init__(self, client, logger, config, quotas, limits):
self.client = client
self.logger = logger
self.id_provider = config["IDENTITY_PROVIDER"]
self.quotafile = config["QUOTA_DEF_FILE"]
self.limitfile = config["LIMIT_DEF_FILE"]
self.quotas = quotas
self.limits = limits
self.apis = {}

if not self.limitfile:
self.logger.error("No default limit file provided.")
sys.exit(1)

def get_resource_api(self, api_version: str, kind: str):
"""Either return the cached resource api from self.apis, or fetch a
new one, store it in self.apis, and return it."""
Expand Down Expand Up @@ -159,17 +155,15 @@ def update_moc_quota(self, project_name, new_quota, patch=False):
return {"msg": "MOC quotas updated"}

def get_quota_definitions(self):
self.logger.info("reading quotas from %s", self.quotafile)
with open(self.quotafile, "r") as file:
quota = json.load(file)

quota = self.quotas
for k in quota:
quota[k]["value"] = None

return quota

def get_limit_definitions(self):
with open(self.limitfile, "r") as file:
return json.load(file)
return self.limits

def get_project(self, project_name):
api = self.get_resource_api(API_PROJECT, "Project")
Expand Down
56 changes: 55 additions & 1 deletion src/coldfront_plugin_cloud/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,59 @@

from coldfront_plugin_cloud import attributes, base, utils

QUOTA_OPENSHIFT = {
":requests.cpu": { "base": 2, "coefficient": 0 },
":requests.memory": { "base": 2, "coefficient": 0 },
":limits.cpu": { "base": 2, "coefficient": 0 },
":limits.memory": { "base": 2, "coefficient": 0 },
":requests.storage": { "base": 2, "coefficient": 0, "units": "Gi" },
":limits.storage": { "base": 2, "coefficient": 0, "units": "Gi" },
":requests.ephemeral-storage": { "base": 2, "coefficient": 8, "units": "Gi" },
":requests.nvidia.com/gpu": { "base": 0, "coefficient": 0 },
":limits.ephemeral-storage": { "base": 2, "coefficient": 8, "units": "Gi" },
":persistentvolumeclaims": { "base": 2, "coefficient": 0 },
":replicationcontrollers": { "base": 2, "coefficient": 0 },
":resourcequotas": { "base": 5, "coefficient": 0 },
":services": { "base": 4, "coefficient": 0 },
":services.loadbalancers": { "base": 2, "coefficient": 0 },
":services.nodeports": { "base": 2, "coefficient": 0 },
":secrets": { "base": 4, "coefficient": 0 },
":configmaps": { "base": 4, "coefficient": 0 },
":openshift.io/imagestreams": { "base": 2, "coefficient": 0 },
"BestEffort:pods": { "base": 2, "coefficient": 2 },
"NotBestEffort:pods": { "base": 2, "coefficient": 2 },
"NotBestEffort:requests.memory": { "base": 2, "coefficient": 4, "units": "Gi" },
"NotBestEffort:limits.memory": { "base": 2, "coefficient": 4, "units": "Gi" },
"NotBestEffort:requests.cpu": { "base": 2, "coefficient": 2 },
"NotBestEffort:limits.cpu": { "base": 2, "coefficient": 2 },
"Terminating:pods": { "base": 2, "coefficient": 2 },
"Terminating:requests.memory": { "base": 2, "coefficient": 4, "units": "Gi" },
"Terminating:limits.memory": { "base": 2, "coefficient": 4, "units": "Gi" },
"Terminating:requests.cpu": { "base": 2, "coefficient": 2 },
"Terminating:limits.cpu": { "base": 2, "coefficient": 2 },
"NotTerminating:pods": { "base": 2, "coefficient": 2 },
"NotTerminating:requests.memory": { "base": 2, "coefficient": 4, "units": "Gi" },
"NotTerminating:limits.memory": { "base": 2, "coefficient": 4, "units": "Gi" },
"NotTerminating:requests.cpu": { "base": 2, "coefficient": 2 },
"NotTerminating:limits.cpu": { "base": 2, "coefficient": 2 }
}

LIMITS_OPENSHIFT = [
{
"type": "Container",
"default": {
"cpu": "2",
"memory": "1024Mi",
"nvidia.com/gpu": "0"
},
"defaultRequest": {
"cpu": "1",
"memory": "512Mi",
"nvidia.com/gpu": "0"
}
}
]

QUOTA_KEY_MAPPING = {
attributes.QUOTA_LIMITS_CPU: lambda x: {":limits.cpu": f"{x * 1000}m"},
attributes.QUOTA_LIMITS_MEMORY: lambda x: {":limits.memory": f"{x}Mi"},
Expand Down Expand Up @@ -76,10 +129,11 @@ def __init__(
logger = logging.getLogger()
else:
logger = logging.getLogger("django")

config = env_config()

self.client = moc_openshift.MocOpenShift4x(
DynamicClient(k8s_client), logger, config
DynamicClient(k8s_client), logger, config, QUOTA_OPENSHIFT, LIMITS_OPENSHIFT
)

@functools.cached_property
Expand Down
15 changes: 0 additions & 15 deletions src/config/limits.json

This file was deleted.

36 changes: 0 additions & 36 deletions src/config/quotas.json

This file was deleted.

0 comments on commit 58216c8

Please sign in to comment.