Skip to content

Commit ba190f4

Browse files
committed
feat(config): add config for dryccfile
1 parent 0a9fb6f commit ba190f4

24 files changed

Lines changed: 1128 additions & 484 deletions

rootfs/api/admissions.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,27 @@ def handle(self, request: Request) -> bool:
3030
app = models.app.App.objects.filter(id=app_id).first()
3131
ptype = request["object"]["metadata"].get("labels", {}).get("type", "")
3232
if app and ptype:
33-
status = request["object"]["status"]
34-
replicas = request["object"]["spec"].get("replicas", 0)
35-
if "active" in status:
36-
replicas += 1
37-
elif "succeeded" in status or "failed" in status:
38-
replicas -= 1
39-
replicas = 0 if replicas < 0 else replicas
40-
if app.structure.get(ptype, 0) != replicas:
41-
models.app.App.objects.filter(id=app.id).update(
42-
structure=Func(
43-
F("structure"),
44-
Value([ptype]),
45-
Value(replicas, JSONField()),
46-
function="jsonb_set",
33+
lock = app.lock()
34+
try:
35+
lock.acquire()
36+
status = request["object"]["status"]
37+
replicas = request["object"]["spec"].get("replicas", 0)
38+
if "active" in status:
39+
replicas += 1
40+
elif "succeeded" in status or "failed" in status:
41+
replicas -= 1
42+
replicas = 0 if replicas < 0 else replicas
43+
if app.structure.get(ptype, 0) != replicas:
44+
models.app.App.objects.filter(id=app.id).update(
45+
structure=Func(
46+
F("structure"),
47+
Value([ptype]),
48+
Value(replicas, JSONField()),
49+
function="jsonb_set",
50+
)
4751
)
48-
)
52+
finally:
53+
lock.release()
4954
return True
5055

5156

@@ -70,16 +75,21 @@ def handle(self, request: Request) -> bool:
7075
if key == "type":
7176
ptype = value
7277
if app and ptype:
73-
replicas = request["object"]["spec"].get("replicas", 0)
74-
if app.structure.get(ptype, 0) != replicas:
75-
models.app.App.objects.filter(id=app.id).update(
76-
structure=Func(
77-
F("structure"),
78-
Value([ptype]),
79-
Value(replicas, JSONField()),
80-
function="jsonb_set",
78+
lock = app.lock()
79+
try:
80+
lock.acquire()
81+
replicas = request["object"]["spec"].get("replicas", 0)
82+
if app.structure.get(ptype, 0) != replicas:
83+
models.app.App.objects.filter(id=app.id).update(
84+
structure=Func(
85+
F("structure"),
86+
Value([ptype]),
87+
Value(replicas, JSONField()),
88+
function="jsonb_set",
89+
)
8190
)
82-
)
91+
finally:
92+
lock.release()
8393
return True
8494

8595

rootfs/api/migrations/0017_migration_deployed_ptypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def migration_deployed_ptypes(apps, schema_editor):
8-
for release in Release.objects.all():
8+
for release in Release.objects.all().using("default"):
99
deployed_ptypes = set()
1010
for condition in release.conditions:
1111
if condition["ptypes"] is None:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.2.16 on 2024-11-12 02:14
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api', '0017_migration_deployed_ptypes'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='config',
15+
name='values_refs',
16+
field=models.JSONField(blank=True, default=dict),
17+
),
18+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 4.2.15 on 2024-09-06 03:58
2+
import json
3+
from django.db import connection, migrations
4+
from api.models.config import Config
5+
6+
7+
def migration_values(apps, schema_editor):
8+
with connection.cursor() as cursor:
9+
cursor.execute("SELECT uuid,values,typed_values FROM api_config")
10+
for uuid, values, typed_values in cursor:
11+
new_values = []
12+
config = Config.objects.using('default').get(pk=uuid)
13+
for name, value in json.loads(values).items():
14+
new_values.append({
15+
"name": name,
16+
"group": "global",
17+
"value": value
18+
})
19+
for ptype, values in json.loads(typed_values).items():
20+
for name, value in values.items():
21+
new_values.append({
22+
"name": name,
23+
"ptype": ptype,
24+
"value": value
25+
})
26+
config.values = new_values
27+
config.save(ignore_update_fields=config.allof_fields)
28+
29+
30+
class Migration(migrations.Migration):
31+
32+
dependencies = [
33+
('api', '0018_config_values_refs'),
34+
]
35+
36+
operations = [
37+
migrations.RunPython(migration_values),
38+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 4.2.16 on 2024-11-06 06:22
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('api', '0019_migration_config_values_and_typed_values'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='config',
15+
name='typed_values',
16+
),
17+
migrations.AlterField(
18+
model_name='config',
19+
name='values',
20+
field=models.JSONField(blank=True, default=list),
21+
),
22+
]

0 commit comments

Comments
 (0)