Skip to content

Commit 3a6a4c1

Browse files
committed
feat(controller): add volume size limit
1 parent 7e9f369 commit 3a6a4c1

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

rootfs/api/serializers.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,9 @@ def validate_memory(data):
324324
"where unit = M or G")
325325
range_error = "Memory setting is not in allowed range: %sM~%sM" % (
326326
min_memory, max_memory)
327-
if str(value).endswith("G"):
328-
if int(value[:-1]) * 1024 < min_memory or int(value[:-1]) * 1024 > max_memory:
329-
raise serializers.ValidationError(range_error)
330-
else:
331-
if int(value[:-1]) < min_memory or int(value[:-1]) > max_memory:
332-
raise serializers.ValidationError(range_error)
327+
memory_size = int(value[:-1]) * 1024 if value.endswith("G") else int(value[:-1])
328+
if memory_size < min_memory or memory_size > max_memory:
329+
raise serializers.ValidationError(range_error)
333330
return data
334331

335332
@staticmethod
@@ -350,12 +347,9 @@ def validate_cpu(data):
350347
"CPU limit format: <value>, where value must be a numeric")
351348
range_error = "CPU setting is not in allowed range: %sm~%sm" % (
352349
min_cpu, max_cpu)
353-
if str(value).isdigit():
354-
if int(value) * 1000 < min_cpu or int(value) * 1000 > max_cpu:
355-
raise serializers.ValidationError(range_error)
356-
else:
357-
if int(value[:-1]) < min_cpu or int(value[:-1]) > max_cpu:
358-
raise serializers.ValidationError(range_error)
350+
cpu_size = int(value) * 1000 if value.isdigit() else int(value[:-1])
351+
if cpu_size < min_cpu or cpu_size > max_cpu:
352+
raise serializers.ValidationError(range_error)
359353
return data
360354

361355
@staticmethod
@@ -697,10 +691,18 @@ class Meta:
697691

698692
@staticmethod
699693
def validate_size(data):
700-
if not re.match(VOLUME_SIZE_MATCH, str(data)):
694+
if not re.match(VOLUME_SIZE_MATCH, data):
701695
raise serializers.ValidationError(
702696
"Volume size limit format: <number><unit> or <number><unit>/<number><unit>, "
703-
"where unit = B, K, M or G")
697+
"where unit = M or G")
698+
max_volume = settings.KUBERNETES_LIMITS_MAX_VOLUME
699+
# The minimum limit memory is equal to the memory allocated by default
700+
min_volume = settings.KUBERNETES_LIMITS_MIN_VOLUME
701+
range_error = "Volume setting is not in allowed range: %sM~%sM" % (
702+
min_volume, max_volume)
703+
volume_size = int(data[:-1]) * 1024 if data.endswith("G") else int(data[:-1])
704+
if volume_size < min_volume or volume_size > max_volume:
705+
raise serializers.ValidationError(range_error)
704706
return data.upper()
705707

706708
@staticmethod

rootfs/api/settings/production.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,16 @@
345345

346346
# Minimum CPU limit, units are represented in the millicpu of CPUs
347347
KUBERNETES_LIMITS_MIN_CPU = 125
348-
# Minimum CPU limit, units are represented in the millicpu of CPUs
348+
# Max CPU limit, units are represented in the millicpu of CPUs
349349
KUBERNETES_LIMITS_MAX_CPU = 32 * 1000
350350
# Minimum Memory limit, units are represented in Megabytes(M)
351351
KUBERNETES_LIMITS_MIN_MEMORY = 128
352-
# Minimum Memory limit
352+
# Max Memory limit, units are represented in Megabytes(M)
353353
KUBERNETES_LIMITS_MAX_MEMORY = 128 * 1024
354+
# Minimum Memory limit, units are represented in Megabytes(M)
355+
KUBERNETES_LIMITS_MIN_VOLUME = 128
356+
# Max Memory limit, units are represented in Megabytes(M)
357+
KUBERNETES_LIMITS_MAX_VOLUME = 128 * 1024
354358

355359
# Default pod spec for application.
356360
KUBERNETES_POD_DEFAULT_RESOURCES = os.environ.get(

0 commit comments

Comments
 (0)