Skip to content

Commit

Permalink
Update models.py
Browse files Browse the repository at this point in the history
Divide CPU / Memory by two and ensure sound defaults if values are too low
  • Loading branch information
joshua-seals authored Jan 22, 2025
1 parent 12a7704 commit f842850
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions appstore/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class ResourceRequest:

def __post_init__(self):

# Dividing resources by 2 for reservations
# This helps in cloud scenarios to reduce costs.
if self.cpus >= 0.5:
self.cpus_reservation = self.cpus / 2 # Allow fractional CPU reservation
else:
self.cpus_reservation = self.cpus
self.gpus_reservation = self.gpus
# self.gpus_reservation = self.gpus // 2 # Use floor division for integer GPUs
self.memory_reservation = self._divide_memory(self.memory)

self.resources = {
"deploy": {
"resources": {
Expand All @@ -91,15 +101,42 @@ def __post_init__(self):
"ephemeralStorage": self.ephemeralStorage,
},
"reservations": {
"cpus": "1",
"memory": "1Gi",
"gpus": self.gpus,
"cpus": self.cpus_reservation,
"memory": self.memory_reservation,
"gpus": self.gpus_reservation,
"ephemeralStorage": self.ephemeralStorage,
},
}
}
}


def _divide_memory(self, memory: str) -> str:
"""Helper method to divide memory by 2 (converting Gi to Mi and ensuring result is never less than 100Mi)."""

# Case for Gi (Gibibytes)
if memory.endswith("Gi"):
value = float(memory[:-2]) # Extract numeric value (removes 'Gi' suffix)
# Convert Gi to Mi by multiplying by 1024
value_in_mi = value * 1024
# Divide by 2
divided_value = value_in_mi / 2
# Ensure the result is not less than 100Mi
if divided_value < 100:
divided_value = 100
return f"{int(divided_value)}Mi" # Return as Mi with no decimal

# Case for Mi (Mebibytes)
elif memory.endswith("Mi"):
value = float(memory[:-2]) # Extract numeric value (removes 'Mi' suffix)
divided_value = value / 2
# Ensure the result is not less than 100Mi
if divided_value < 100:
divided_value = 100
return f"{int(divided_value)}Mi" # Return as Mi with no decimal

# Default case for unrecognized memory format
else:
return memory

@dataclass
class InstanceSpec:
Expand Down Expand Up @@ -148,4 +185,4 @@ class LoginProvider:
class User:
REMOTE_USER: str
ACCESS_TOKEN: str
SESSION_TIMEOUT: int
SESSION_TIMEOUT: int

0 comments on commit f842850

Please sign in to comment.