-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the ability to override resources on a per-user basis
Closes #1951
- Loading branch information
Showing
21 changed files
with
439 additions
and
47 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
backend/capellacollab/alembic/versions/4d42177579a2_add_resource_override.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Add resource override | ||
Revision ID: 4d42177579a2 | ||
Revises: 2f8449c217fa | ||
Create Date: 2024-11-12 17:43:23.486104 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "4d42177579a2" | ||
down_revision = "2f8449c217fa" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
t_tools = sa.Table( | ||
"tools", | ||
sa.MetaData(), | ||
sa.Column("id", sa.Integer()), | ||
sa.Column("integrations", postgresql.JSONB(astext_type=sa.Text())), | ||
sa.Column("config", postgresql.JSONB(astext_type=sa.Text())), | ||
) | ||
|
||
|
||
def upgrade(): | ||
connection = op.get_bind() | ||
results = connection.execute(sa.select(t_tools)).mappings().all() | ||
|
||
for row in results: | ||
config = row["config"] | ||
config["resources"] = { | ||
"default_profile": { | ||
"cpu": config["resources"]["cpu"], | ||
"memory": config["resources"]["memory"], | ||
}, | ||
"additional": {}, | ||
} | ||
|
||
connection.execute( | ||
sa.update(t_tools) | ||
.where(t_tools.c.id == row["id"]) | ||
.values(config=config) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pydantic | ||
import pytest | ||
|
||
from capellacollab.tools import models as tools_models | ||
|
||
|
||
def test_validate_tools(): | ||
with pytest.raises(pydantic.ValidationError): | ||
tools_models.Resources( | ||
default_profile=tools_models.DefaultResourceProfile(), | ||
additional={ | ||
"test1": tools_models.AdditionalResourceProfile( | ||
usernames=["test", "test"] | ||
), | ||
}, | ||
) | ||
|
||
with pytest.raises(pydantic.ValidationError): | ||
tools_models.Resources( | ||
default_profile=tools_models.DefaultResourceProfile(), | ||
additional={ | ||
"test1": tools_models.AdditionalResourceProfile( | ||
usernames=["test"] | ||
), | ||
"test2": tools_models.AdditionalResourceProfile( | ||
usernames=["test"] | ||
), | ||
}, | ||
) | ||
|
||
|
||
def test_get_profile(): | ||
default_profile = tools_models.DefaultResourceProfile( | ||
memory=tools_models.MemoryResources(requests="1Gi", limits="2Gi"), | ||
cpu=tools_models.CPUResources(requests=0.4, limits=2), | ||
) | ||
different_profile = tools_models.AdditionalResourceProfile( | ||
usernames=["testuser"], | ||
memory=tools_models.MemoryResources(requests="1Gi", limits="2Gi"), | ||
cpu=tools_models.CPUResources(requests=0.4, limits=2), | ||
) | ||
|
||
resources = tools_models.Resources( | ||
default_profile=default_profile, | ||
additional={ | ||
"test": different_profile, | ||
}, | ||
) | ||
|
||
resource_profile = resources.get_profile(None) | ||
assert resource_profile == default_profile | ||
|
||
resource_profile = resources.get_profile("fakeuser") | ||
assert resource_profile == default_profile | ||
|
||
resource_profile = resources.get_profile("testuser") | ||
assert resource_profile == different_profile |
Oops, something went wrong.