Skip to content

Commit

Permalink
Use opm render - get_operator_package_list, get_list_bundles
Browse files Browse the repository at this point in the history
[CLOUDDST-25207]
  • Loading branch information
lipoja committed Feb 5, 2025
1 parent 0411ab9 commit 3d176c1
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 354 deletions.
24 changes: 7 additions & 17 deletions iib/workers/tasks/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from iib.workers.greenwave import gate_bundles
from iib.workers.tasks.fbc_utils import is_image_fbc, get_catalog_dir, merge_catalogs_dirs
from iib.workers.tasks.opm_operations import (
opm_serve_from_index,
opm_registry_add_fbc,
opm_migrate,
opm_registry_rm_fbc,
Expand All @@ -40,12 +39,12 @@
verify_operators_exists,
create_dockerfile,
opm_validate,
get_list_bundles,
)
from iib.workers.tasks.utils import (
add_max_ocp_version_property,
chmod_recursively,
get_bundles_from_deprecation_list,
get_bundle_json,
get_resolved_bundles,
get_resolved_image,
podman_pull,
Expand All @@ -58,7 +57,6 @@
get_image_label,
verify_labels,
prepare_request_for_build,
terminate_process,
get_bundle_metadata,
)
from iib.workers.tasks.iib_static_types import (
Expand Down Expand Up @@ -392,22 +390,14 @@ def _get_present_bundles(from_index: str, base_dir: str) -> Tuple[List[BundleIma
:rtype: list, list
:raises IIBError: if any of the commands fail.
"""
port, rpc_proc = opm_serve_from_index(base_dir, from_index=from_index)

bundles = run_cmd(
['grpcurl', '-plaintext', f'localhost:{port}', 'api.Registry/ListBundles'],
exc_msg='Failed to get bundle data from index image',
)
terminate_process(rpc_proc)

# If no data is returned there are not bundles present
if not bundles:
return [], []

# Transform returned data to parsable json
# Get list of bundles
unique_present_bundles: List[BundleImage] = []
unique_present_bundles_pull_spec: List[str] = []
present_bundles: List[BundleImage] = get_bundle_json(bundles)
present_bundles: List[BundleImage] = get_list_bundles(from_index, base_dir)

# If no data is returned there are no bundles present
if not present_bundles:
return [], []

for bundle in present_bundles:
bundle_path = bundle['bundlePath']
Expand Down
39 changes: 8 additions & 31 deletions iib/workers/tasks/build_create_empty_index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import logging
import tempfile
import json
import re
from typing import Dict, List, Optional
from typing import Dict, Optional

from iib.common.tracing import instrument_tracing
from iib.exceptions import IIBError
Expand All @@ -20,12 +18,16 @@
)
from iib.workers.tasks.celery import app
from iib.workers.tasks.fbc_utils import is_image_fbc
from iib.workers.tasks.opm_operations import opm_create_empty_fbc, opm_index_rm, Opm
from iib.workers.tasks.opm_operations import (
opm_create_empty_fbc,
opm_index_rm,
Opm,
get_operator_package_list,
)
from iib.workers.tasks.utils import (
request_logger,
prepare_request_for_build,
RequestConfigCreateIndexImage,
grpcurl_get_db_data,
)
from iib.workers.tasks.iib_static_types import PrebuildInfo

Expand All @@ -34,31 +36,6 @@
log = logging.getLogger(__name__)


def _get_present_operators(from_index: str, base_dir: str) -> List[str]:
"""Get a list of operators already present in the index image.
:param str from_index: index image to inspect.
:param str base_dir: base directory to create temporary files in.
:return: list of unique present operators as provided by the grpc query
:rtype: list
:raises IIBError: if any of the commands fail.
"""
operators = grpcurl_get_db_data(from_index, base_dir, "api.Registry/ListPackages")

# If no data is returned there are not operators present
if not operators:
return []

# Transform returned data to parsable json
present_operators = []
new_operators = [json.loads(operator) for operator in re.split(r'(?<=})\n(?={)', operators)]

for operator in new_operators:
present_operators.append(operator['name'])

return present_operators


@app.task
@request_logger
@instrument_tracing(
Expand Down Expand Up @@ -108,7 +85,7 @@ def handle_create_empty_index_request(
with tempfile.TemporaryDirectory(prefix=f'iib-{request_id}-') as temp_dir:
set_request_state(request_id, 'in_progress', 'Checking operators present in index image')

operators = _get_present_operators(from_index_resolved, temp_dir)
operators = get_operator_package_list(from_index_resolved, temp_dir)

# if output_fbc parameter is true, create an empty FBC index image
# else create empty SQLite index image
Expand Down
36 changes: 4 additions & 32 deletions iib/workers/tasks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from iib.workers.config import get_worker_config
from iib.workers.s3_utils import upload_file_to_s3_bucket
from iib.workers.api_utils import set_request_state
from iib.workers.tasks.opm_operations import opm_registry_serve, opm_serve_from_index
from iib.workers.tasks.opm_operations import get_list_bundles
from iib.workers.tasks.iib_static_types import (
IndexImageInfo,
AllIndexImagesInfo,
Expand Down Expand Up @@ -96,26 +96,20 @@ def add_max_ocp_version_property(resolved_bundles: List[str], temp_dir: str) ->
# Get the CSV name and version (not just the bundle path)
temp_index_db_path = get_worker_config()['temp_index_db_path']
db_path = os.path.join(temp_dir, temp_index_db_path)
port, rpc_proc = opm_registry_serve(db_path=db_path)

raw_bundles = run_cmd(
['grpcurl', '-plaintext', f'localhost:{port}', 'api.Registry/ListBundles'],
exc_msg='Failed to get bundle data from index image',
)
terminate_process(rpc_proc)
bundles = get_list_bundles(input_data=db_path, base_dir=temp_dir)

# This branch is hit when `bundles` attribute is empty and the index image is empty.
# Ideally the code should not reach here if the bundles attribute is empty but adding
# this here as a failsafe if it's called from some other place. Also, if the bundles
# attribute is not empty, the index image cannot be empty here because we add the
# bundle to the index before adding the maxOpenShiftVersion property
if not raw_bundles:
if not bundles:
log.info('No bundles found in the index image')
return

# Filter index image bundles to get pull spec for bundles in the request
updated_bundles: List[BundleImage] = list(
filter(lambda b: b['bundlePath'] in resolved_bundles, get_bundle_json(raw_bundles))
filter(lambda b: b['bundlePath'] in resolved_bundles, bundles)
)

for bundle in updated_bundles:
Expand Down Expand Up @@ -1236,28 +1230,6 @@ def prepare_request_for_build(
}


def grpcurl_get_db_data(from_index: str, base_dir: str, endpoint: str) -> str:
"""Get a str with operators already present in the index image.
:param str from_index: index image to inspect.
:param str base_dir: base directory to create temporary files in.
:return: str result of the grpc query
:rtype: str
:raises IIBError: if any of the commands fail.
"""
port, rpc_proc = opm_serve_from_index(base_dir, from_index=from_index)

if endpoint not in ["api.Registry/ListPackages", "api.Registry/ListBundles"]:
raise IIBError(f"The endpoint '{endpoint}' is not allowed to be used")

result = run_cmd(
['grpcurl', '-plaintext', f'localhost:{port}', endpoint],
exc_msg=f'Failed to get {endpoint} data from index image',
)
terminate_process(rpc_proc)
return result


def get_bundle_metadata(
operator_manifest: OperatorManifest,
pinned_by_iib: bool,
Expand Down
Loading

0 comments on commit 3d176c1

Please sign in to comment.