-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(BA-674): Implement Image
Soft/Hard Delete APIs
#3628
Open
jopemachine
wants to merge
12
commits into
topic/02-10-feat_add_status_to_image_imagenode_gql_field
Choose a base branch
from
topic/02-10-feat_implment_image_soft_hard_delete_apis
base: topic/02-10-feat_add_status_to_image_imagenode_gql_field
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−50
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9046078
feat: Implment Image Soft/Hard Delete APIs
jopemachine 29a6d00
docs: Add news fragment
jopemachine ff6a3e9
feat: Make ClearImages to perform soft-delete
jopemachine 2ae7861
docs: Rename news fragment
jopemachine 3b0a084
fix: Remove ok, msg from PurgeImage mutation
jopemachine 19c632d
feat: Add `purge` image cli
jopemachine bd6e556
misc: Rename news fragment file
jopemachine d9722b3
misc: Update milestone
jopemachine 3fe13ce
refactor: use `ImageRow.is_customized_by`
jopemachine ab12f67
refactor: use `extract_object_uuid`
jopemachine ad292e6
refactor: use `ImageRow.mark_as_deleted`
jopemachine c9c1f0a
refactor: simplify `ImageRow.is_customized_by`
jopemachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 @@ | ||
Update `ForgetImage`, `ForgetImageById`, `ClearImages` to perform soft delete and added `PurgeImage`, `PurgeImageById` API for hard delete. |
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 |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
from ai.backend.manager.models.rbac.context import ClientContext | ||
from ai.backend.manager.models.rbac.permission_defs import ImagePermission | ||
|
||
from ...api.exceptions import ImageNotFound, ObjectNotFound | ||
from ...api.exceptions import GenericForbidden, ImageNotFound, ObjectNotFound | ||
from ...defs import DEFAULT_IMAGE_ARCH | ||
from ..base import ( | ||
FilterExprArg, | ||
|
@@ -57,6 +57,7 @@ | |
ImageIdentifier, | ||
ImageLoadFilter, | ||
ImageRow, | ||
ImageStatus, | ||
ImageType, | ||
get_permission_ctx, | ||
rescan_images, | ||
|
@@ -69,6 +70,7 @@ | |
KVPairInput, | ||
ResourceLimit, | ||
ResourceLimitInput, | ||
extract_object_uuid, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
|
@@ -85,6 +87,8 @@ | |
"RescanImages", | ||
"ForgetImage", | ||
"ForgetImageById", | ||
"PurgeImage", | ||
"PurgeImageById", | ||
"UntagImageFromRegistry", | ||
"ModifyImage", | ||
"AliasImage", | ||
|
@@ -668,16 +672,9 @@ async def mutate( | |
info: graphene.ResolveInfo, | ||
image_id: str, | ||
) -> ForgetImageById: | ||
_, raw_image_id = AsyncNode.resolve_global_id(info, image_id) | ||
if not raw_image_id: | ||
raw_image_id = image_id | ||
|
||
try: | ||
_image_id = UUID(raw_image_id) | ||
except ValueError: | ||
raise ObjectNotFound("image") | ||
|
||
log.info("forget image {0} by API request", image_id) | ||
_image_id = extract_object_uuid(info, image_id, "image") | ||
|
||
ctx: GraphQueryContext = info.context | ||
client_role = ctx.user["role"] | ||
|
||
|
@@ -686,15 +683,9 @@ async def mutate( | |
if not image_row: | ||
raise ObjectNotFound("image") | ||
if client_role != UserRole.SUPERADMIN: | ||
customized_image_owner = (image_row.labels or {}).get( | ||
"ai.backend.customized-image.owner" | ||
) | ||
if ( | ||
not customized_image_owner | ||
or customized_image_owner != f"user:{ctx.user['uuid']}" | ||
): | ||
if not image_row.is_customized_by(ctx.user["uuid"]): | ||
return ForgetImageById(ok=False, msg="Forbidden") | ||
await session.delete(image_row) | ||
await image_row.mark_as_deleted(session) | ||
return ForgetImageById(ok=True, msg="", image=ImageNode.from_row(image_row)) | ||
|
||
|
||
|
@@ -733,18 +724,90 @@ async def mutate( | |
], | ||
) | ||
if client_role != UserRole.SUPERADMIN: | ||
customized_image_owner = (image_row.labels or {}).get( | ||
"ai.backend.customized-image.owner" | ||
) | ||
if ( | ||
not customized_image_owner | ||
or customized_image_owner != f"user:{ctx.user['uuid']}" | ||
): | ||
if not image_row.is_customized_by(ctx.user["uuid"]): | ||
return ForgetImage(ok=False, msg="Forbidden") | ||
await session.delete(image_row) | ||
await image_row.mark_as_deleted(session) | ||
return ForgetImage(ok=True, msg="", image=ImageNode.from_row(image_row)) | ||
|
||
|
||
class PurgeImage(graphene.Mutation): | ||
"""Added in 25.3.1.""" | ||
|
||
allowed_roles = ( | ||
UserRole.SUPERADMIN, | ||
UserRole.ADMIN, | ||
UserRole.USER, | ||
) | ||
|
||
class Arguments: | ||
reference = graphene.String(required=True) | ||
architecture = graphene.String(default_value=DEFAULT_IMAGE_ARCH) | ||
|
||
image = graphene.Field(ImageNode) | ||
|
||
@staticmethod | ||
async def mutate( | ||
root: Any, | ||
info: graphene.ResolveInfo, | ||
reference: str, | ||
architecture: str, | ||
) -> PurgeImage: | ||
log.info("purge image {0} by API request", reference) | ||
ctx: GraphQueryContext = info.context | ||
client_role = ctx.user["role"] | ||
|
||
async with ctx.db.begin_session() as session: | ||
image_row = await ImageRow.resolve( | ||
session, | ||
[ | ||
ImageIdentifier(reference, architecture), | ||
ImageAlias(reference), | ||
], | ||
) | ||
if client_role != UserRole.SUPERADMIN: | ||
if not image_row.is_customized_by(ctx.user["uuid"]): | ||
return PurgeImage(ok=False, msg="Forbidden") | ||
await session.delete(image_row) | ||
return PurgeImage(image=ImageNode.from_row(image_row)) | ||
|
||
|
||
class PurgeImageById(graphene.Mutation): | ||
"""Added in 25.3.1.""" | ||
|
||
allowed_roles = ( | ||
UserRole.SUPERADMIN, | ||
UserRole.ADMIN, | ||
UserRole.USER, | ||
) | ||
|
||
class Arguments: | ||
image_id = graphene.String(required=True) | ||
|
||
image = graphene.Field(ImageNode) | ||
|
||
@staticmethod | ||
async def mutate( | ||
root: Any, | ||
info: graphene.ResolveInfo, | ||
image_id: str, | ||
) -> PurgeImageById: | ||
log.info("purge image {0} by API request", image_id) | ||
_image_id = extract_object_uuid(info, image_id, "image") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would actually be less confusing if you changed the variable name to a different one. |
||
|
||
ctx: GraphQueryContext = info.context | ||
client_role = ctx.user["role"] | ||
|
||
async with ctx.db.begin_session() as session: | ||
image_row = await ImageRow.get(session, _image_id, load_aliases=True) | ||
if not image_row: | ||
raise ObjectNotFound("image") | ||
if client_role != UserRole.SUPERADMIN: | ||
if not image_row.is_customized_by(ctx.user["uuid"]): | ||
raise GenericForbidden("Image is not owned by your account.") | ||
await session.delete(image_row) | ||
return PurgeImageById(image=ImageNode.from_row(image_row)) | ||
|
||
|
||
class UntagImageFromRegistry(graphene.Mutation): | ||
"""Added in 24.03.1""" | ||
|
||
|
@@ -769,14 +832,7 @@ async def mutate( | |
) -> UntagImageFromRegistry: | ||
from ai.backend.manager.container_registry.harbor import HarborRegistry_v2 | ||
|
||
_, raw_image_id = AsyncNode.resolve_global_id(info, image_id) | ||
if not raw_image_id: | ||
raw_image_id = image_id | ||
|
||
try: | ||
_image_id = UUID(raw_image_id) | ||
except ValueError: | ||
raise ObjectNotFound("image") | ||
_image_id = extract_object_uuid(info, image_id, "image") | ||
|
||
log.info("remove image from registry {0} by API request", str(_image_id)) | ||
ctx: GraphQueryContext = info.context | ||
|
@@ -787,13 +843,7 @@ async def mutate( | |
if not image_row: | ||
raise ImageNotFound | ||
if client_role != UserRole.SUPERADMIN: | ||
customized_image_owner = (image_row.labels or {}).get( | ||
"ai.backend.customized-image.owner" | ||
) | ||
if ( | ||
not customized_image_owner | ||
or customized_image_owner != f"user:{ctx.user['uuid']}" | ||
): | ||
if not image_row.is_customized_by(ctx.user["uuid"]): | ||
return UntagImageFromRegistry(ok=False, msg="Forbidden") | ||
|
||
query = sa.select(ContainerRegistryRow).where( | ||
|
@@ -968,15 +1018,12 @@ async def mutate( | |
ctx: GraphQueryContext = info.context | ||
try: | ||
async with ctx.db.begin_session() as session: | ||
result = await session.execute( | ||
sa.select(ImageRow).where(ImageRow.registry == registry) | ||
) | ||
image_ids = [x.id for x in result.scalars().all()] | ||
|
||
await session.execute( | ||
sa.delete(ImageAliasRow).where(ImageAliasRow.image_id.in_(image_ids)) | ||
sa.update(ImageRow) | ||
.where(ImageRow.registry == registry) | ||
.where(ImageRow.status != ImageStatus.DELETED) | ||
.values(status=ImageStatus.DELETED) | ||
) | ||
await session.execute(sa.delete(ImageRow).where(ImageRow.registry == registry)) | ||
except ValueError as e: | ||
return ClearImages(ok=False, msg=str(e)) | ||
return ClearImages(ok=True, msg="") | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference between purge_image_by_id?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
purge_image
is an API that deletes an image using its name as a string,whereas
purge_image_by_id
directly deletes an image using itsimage_id
(DB ID).It follows the same structure as
forget_image
andforget_image_by_id
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it's nice to gradually deprecate the name-based APIs.