Skip to content

Commit

Permalink
add-deprecation worker implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tulsi Chandwani committed Oct 10, 2024
1 parent 36ac719 commit b615ec7
Show file tree
Hide file tree
Showing 10 changed files with 1,034 additions and 8 deletions.
67 changes: 65 additions & 2 deletions iib/web/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
RequestTypeMapping,
RequestCreateEmptyIndex,
User,
RequestAddDeprecationsDeprecationSchema,
DeprecationSchema,
)
from iib.web.s3_utils import get_object_from_s3_bucket
from botocore.response import StreamingBody
Expand All @@ -45,6 +47,7 @@
handle_add_request,
handle_rm_request,
)
from iib.workers.tasks.build_add_deprecations import handle_add_deprecations_request
from iib.workers.tasks.build_fbc_operations import handle_fbc_operation_request
from iib.workers.tasks.build_recursive_related_bundles import (
handle_recursive_related_bundles_request,
Expand Down Expand Up @@ -1345,5 +1348,65 @@ def add_deprecations() -> Tuple[flask.Response, int]:
db.session.add(request)
db.session.commit()

flask.current_app.logger.debug('Successfully validated request %d', request.id)
return flask.jsonify({'msg': 'This API endpoint hasn not been implemented yet'}), 501
messaging.send_message_for_state_change(request, new_batch_msg=True)

overwrite_from_index = payload.get('overwrite_from_index', False)
from_index_pull_spec = request.from_index.pull_specification
celery_queue = _get_user_queue(
serial=overwrite_from_index, from_index_pull_spec=from_index_pull_spec
)

args = [
request.id,
payload['operator_package'],
payload['deprecation_schema'],
payload['from_index'],
payload.get('binary_image'),
payload.get('build_tags'),
payload.get('overwrite_from_index'),
payload.get('overwrite_from_index_token'),
flask.current_app.config['IIB_BINARY_IMAGE_CONFIG'],
]
safe_args = _get_safe_args(args, payload)
error_callback = failed_request_callback.s(request.id)
try:
handle_add_deprecations_request.apply_async(
args=args, link_error=error_callback, argsrepr=repr(safe_args), queue=celery_queue
)
except kombu.exceptions.OperationalError:
handle_broker_error(request)

flask.current_app.logger.debug('Successfully scheduled request %d', request.id)
return flask.jsonify(request.to_json()), 201


@api_v1.route('/builds/<int:request_id>/deprecation-schema')
@instrument_tracing(span_name="web.api_v1.get_deprecation_schema")
def get_deprecation_schema(request_id: int) -> flask.Response:
"""
Retrieve the deprecation-schema for add-deprecations request.
:param int request_id: the request ID that was passed in through the URL.
:rtype: flask.Response
:raise NotFound: if the request is not found or there are no deprecation_schema for the request
:raise ValidationError: if the request is of invalid type or is not completed yet
"""
request = Request.query.get_or_404(request_id)
if request.type != RequestTypeMapping.add_deprecations.value:
raise ValidationError(
f'The request {request_id} is of type {request.type_name}. '
'This endpoint is only valid for requests of type add-deprecations.'
)

try:
deprecation_schema_for_request = (
DeprecationSchema.query.join(RequestAddDeprecationsDeprecationSchema)
.filter(
RequestAddDeprecationsDeprecationSchema.request_add_deprecations_id == request_id
)
.first()
)
except Exception as e:
raise e

return flask.Response(deprecation_schema_for_request.schema, mimetype='application/json')
8 changes: 8 additions & 0 deletions iib/web/iib_static_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class AddDeprecationRequestPayload(TypedDict):
"""Data structure of the request to /builds/add-deprecations API endpoint."""

binary_image: NotRequired[str]
build_tags: NotRequired[List[str]]
deprecation_schema: str
from_index: str
operator_package: str
Expand Down Expand Up @@ -447,4 +448,11 @@ class FbcOperationRequestResponse(BaseClassRequestResponse):
fbc_fragment_resolved: Optional[str]


class AddDeprecationsRequestResponse(BaseClassRequestResponse):
"""Datastructure of the response to request from /builds/add-deprecations API point."""

operator_package: str
deprecation_schema_url: str


# End of the RequestResponses Part
33 changes: 32 additions & 1 deletion iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
RmRequestPayload,
FbcOperationRequestPayload,
FbcOperationRequestResponse,
AddDeprecationsRequestResponse,
)


Expand Down Expand Up @@ -2344,8 +2345,13 @@ def from_json( # type: ignore[override] # noqa: F821
deprecation_schema=_deprecation_schema
)

build_tags = request_kwargs.pop('build_tags', [])
request = cls(**request_kwargs)
request.add_state('failed', 'The API endpoint has not been implemented yet')

for bt in build_tags:
request.add_build_tag(bt)

request.add_state('in_progress', 'The request was initiated')
return request

def get_mutable_keys(self) -> Set[str]:
Expand All @@ -2358,3 +2364,28 @@ def get_mutable_keys(self) -> Set[str]:
rv = super().get_mutable_keys()
rv.update(self.get_index_image_mutable_keys())
return rv

def to_json(self, verbose: Optional[bool] = True) -> AddDeprecationsRequestResponse:
"""
Provide the JSON representation of a "add-deprecations" build request.
:param bool verbose: determines if the JSON output should be verbose
:return: a dictionary representing the JSON of the build request
:rtype: dict
"""
# cast to result type, super-type returns Union
rv = cast(AddDeprecationsRequestResponse, super().to_json(verbose=verbose))
rv.update(self.get_common_index_image_json()) # type: ignore
rv['operator_package'] = self.operator_package.name
rv['deprecation_schema_url'] = url_for(
'.get_deprecation_schema', request_id=self.id, _external=True
)

rv.pop('bundles')
rv.pop('bundle_mapping')
rv.pop('deprecation_list')
rv.pop('distribution_scope')
rv.pop('organization')
rv.pop('removed_operators')

return rv
180 changes: 180 additions & 0 deletions iib/web/static/api_v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,79 @@ paths:
error:
type: string
example: You must be authenticated to perform this action
/builds/add-deprecations:
post:
description: >
Submit a request to add deprecation information to an index image
requestBody:
description: The request to add deprecation schema
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/AddDeprecationsRequest'
security:
- Kerberos Authentication: []
responses:
'201':
description: The build request was initiated
content:
application/json:
schema:
$ref: '#/components/schemas/AddDeprecationsResponseVerbose'
'400':
description: The input is invalid
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: '"operator_package" should be a non-empty string'
'401':
description: >
The user is not allowed to create a request with this input
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: You must be authenticated to perform this action
'/builds/{id}/deprecation-schema':
get:
description: Return deprecation schema of the build request
parameters:
- name: id
in: path
required: true
description: The ID of the build request to retrieve the related bundles for
schema:
type: integer
responses:
'200':
description: The deprecation schema for the build request
content:
application/json:
schema:
type: array
items:
type: string
example:
- '{"schema":"olm.deprecations","package":"test-operator"}'
'404':
description: >
The deprecation schema is not available for this request.
content:
application/json:
schema:
type: object
properties:
error:
type: string
example: The requested resource was not found.
/builds/merge-index-image:
post:
description: Submit a build request to merge index images
Expand Down Expand Up @@ -1601,6 +1674,109 @@ components:
fbc_fragment_resolved:
type: string
example: "quay.io/iib/fbc-fragment@sha256:7d8e5dddad0275bc903b6ef17840f98441b15b8b999609af2c9579960e52080e"
AddDeprecationsRequest:
type: object
properties:
binary_image:
type: string
description: >
The pull specification of the container image where the opm binary gets copied from.
example: 'quay.io/operator-framework/upstream-registry-builder:v1.26.3'
operator_package:
type: string
description: >
operator_package is required.
example: 'test-operator'
deprecation_schema:
type: string
description: >
Jsonified string of deprecation scheme to be added
example: '{"schema":"olm.deprecations","package":"test-operator"}'
from_index:
type: string
description: >
from_index is required.
example: 'quay.io/iib-stage/iib:4'
overwrite_from_index:
type: boolean
description: >
Overwrites the input from_index image with the built index image. This can only be
performed when overwrite_from_index_token is provided.
default: false
overwrite_from_index_token:
type: string
description: >
The token used for reading and overwriting the input from_index image. This is required
to use overwrite_from_index. The format of the token is in the format "user:password".
example: token
build_tags:
description: >
Extra tags applied to intermediate index image
type: array
items:
type: string
example: ["v4.5-10-08-2021"]
required:
- operator_package
- deprecation_schema
- from_index
AddDeprecationsResponse:
allOf:
- $ref: '#/components/schemas/BaseResponse'
- type: object
properties:
binary_image:
type: string
example: 'quay.io/operator-framework/upstream-registry-builder:v1.5.9'
binary_image_resolved:
type: string
example: >-
quay.io/operator-framework/upstream-registry-builder@sha256:7d8e5dddad0275bc903b6ef17840f98441b15b8b999609af2c9579960e52080e
from_index:
type: string
example: 'quay.io/iib-stage/iib:4'
from_index_resolved:
type: string
example: >-
quay.io/iib-stage/iib@sha256:7d8e5dddad0275bc903b6ef17840f98441b15b8b999609af2c9579960e52080e
index_image:
type: string
example: 'quay.io/iib-stage/iib:5'
index_image_resolved:
type: string
example: 'quay.io/iib-stage/iib@sha256:abcdef012356789'
internal_index_image_copy:
description: >
The pullspec of the internal copy of the index image built by IIB.
It will have the same value as index_image when overwrite_from_index
is not provided.
type: string
example: 'quay.io/iib-stage/iib:5'
internal_index_image_copy_resolved:
description: >
The resolved pullspec of the internal copy of the index image built by IIB.
It will have the same value as index_image_resolved when overwrite_from_index
is not provided.
type: string
example: 'quay.io/iib-stage/iib@sha256:abcdef012356789'
build_tags:
description: >
Extra tags applied to intermediate index image
type: array
items:
type: string
example: ["v4.5-10-08-2021"]
request_type:
type: string
example: add-deprecations
operator_package:
type: string
description: operator_package of deprecation information.
example: 'test-operator'
deprecation_schema_url:
type: string
description: url to access deprecation schema
example: https://iib.domain.local/api/v1/get_deprecation_schema
RequestUpdate:
type: object
properties:
Expand Down Expand Up @@ -1676,6 +1852,10 @@ components:
allOf:
- $ref: '#/components/schemas/BaseResponseVerbose'
- $ref: '#/components/schemas/FbcResponse'
AddDeprecationsResponseVerbose:
allOf:
- $ref: '#/components/schemas/BaseResponseVerbose'
- $ref: '#/components/schemas/AddDeprecationsResponse'
StateHistory:
type: object
properties:
Expand Down
4 changes: 2 additions & 2 deletions iib/workers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ class Config(object):
'iib.workers.tasks.build_regenerate_bundle',
'iib.workers.tasks.build_create_empty_index',
'iib.workers.tasks.build_fbc_operations',
'iib.workers.tasks.build_add_deprecations',
'iib.workers.tasks.general',
]
# Path to hidden location of SQLite database
hidden_index_db_path: str = '/var/lib/iib/_hidden/do.not.edit.db'
# path where catalog resides in fbc_fragment
# might need to be changed, currently based on test fbc-fragment
fbc_fragment_catalog_path: str = '/configs'
# path where operator deprecations will be stored
# it's a sub-directory of fbc_fragment_catalog_path
# sub-directory under fbc_fragment_catalog_path where operator deprecations will be stored
operator_deprecations_dir: str = '_operator-deprecations-content'
# The task messages will be acknowledged after the task has been executed,
# instead of just before
Expand Down
Loading

0 comments on commit b615ec7

Please sign in to comment.