-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlimit.py
More file actions
140 lines (121 loc) · 4 KB
/
limit.py
File metadata and controls
140 lines (121 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import logging
import jsonschema
from functools import partial
from django.db import models
from django.contrib.auth import get_user_model
from rest_framework.exceptions import ValidationError
from .base import AuditedModel
User = get_user_model()
logger = logging.getLogger(__name__)
spec_memory_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"size": {"type": "string"},
"type": {"type": "string"},
},
"required": ["size", "type"],
}
spec_cpu_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"cores": {"type": "integer"},
"clock": {"type": "string"},
"boost": {"type": "string"},
"threads": {"type": "integer"},
},
"required": ["name", "cores", "clock", "boost", "threads"],
}
spec_gpu_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"name": {"type": "string"},
"tmus": {"type": "integer"},
"rops": {"type": "integer"},
"cores": {"type": "integer"},
"memory": spec_memory_schema,
},
"required": ["name", "tmus", "rops", "cores"],
}
spec_features_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"gpu": spec_gpu_schema,
"network": {"type": "string"},
},
"required": ["gpu", "network"],
}
spec_keywords_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "array",
"minItems": 1,
"items": {"type": "string"}
}
plan_features_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"gpu": {"type": "integer"},
"network": {"type": "integer"},
},
"required": ["gpu", "memory"],
}
plan_limits_schema = {
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"cpu": {"type": "integer"},
"memory": {"type": "integer"},
},
"required": ["cpu", "memory"],
}
def validate_json(value, schema):
if value is not None:
try:
jsonschema.validate(value, schema)
except jsonschema.ValidationError as e:
raise ValidationError(e.message)
return value
class LimitSpec(AuditedModel):
id = models.CharField(max_length=63, primary_key=True)
cpu = models.JSONField(
validators=[partial(validate_json, schema=spec_cpu_schema)])
memory = models.JSONField(
validators=[partial(validate_json, schema=spec_memory_schema)])
features = models.JSONField(
validators=[partial(validate_json, schema=spec_features_schema)])
keywords = models.JSONField(
validators=[partial(validate_json, schema=spec_keywords_schema)])
disabled = models.BooleanField(default=False)
priority = models.SmallIntegerField(default=100)
class Meta:
get_latest_by = 'created'
ordering = ['-priority']
class LimitPlan(AuditedModel):
id = models.CharField(max_length=63, primary_key=True)
spec = models.ForeignKey(LimitSpec, on_delete=models.PROTECT)
cpu = models.SmallIntegerField(default=1)
memory = models.SmallIntegerField(default=1)
features = models.JSONField(
validators=[partial(validate_json, schema=plan_features_schema)])
disabled = models.BooleanField(default=False)
priority = models.SmallIntegerField(default=100)
limits = models.JSONField(
validators=[partial(validate_json, schema=plan_limits_schema)])
requests = models.JSONField(default=dict)
annotations = models.JSONField(default=dict)
node_selector = models.JSONField(default=dict)
pod_security_context = models.JSONField(default=dict)
container_security_context = models.JSONField(default=dict)
class Meta:
get_latest_by = 'created'
ordering = ['priority']
def __str__(self):
return self.name
@staticmethod
def get_default():
return LimitPlan.objects.filter(disabled=False).first()