Skip to content

Commit

Permalink
CLOUDDST-24254 : Merge index image failing with FBC operator present …
Browse files Browse the repository at this point in the history
…in source

This commit fix this bug by ignoring deprecation operators which are not available in database.
  • Loading branch information
Ashwini Kumar committed Feb 5, 2025
1 parent 9a9845f commit b803aa2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions iib/workers/tasks/build_merge_index_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from iib.workers.tasks.utils import (
add_max_ocp_version_property,
chmod_recursively,
filter_deprecation_operator_from_db,
get_bundles_from_deprecation_list,
request_logger,
set_registry_token,
Expand Down Expand Up @@ -326,6 +327,15 @@ def handle_merge_request(
bundle['bundlePath'] for bundle in invalid_version_bundles
]

index_db_file = os.path.join(temp_dir, get_worker_config()['temp_index_db_path'])

# Operator passed in deprecation list should be available in operator database,
# filter_deprecation_operator_from_db removes operaors which are passed in
# deprecation list and does not exists in database.
deprecation_bundles = filter_deprecation_operator_from_db(
deprecation_bundles, index_db_file
)

if deprecation_bundles:
intermediate_image_name = _get_external_arch_pull_spec(
request_id, arch, include_transport=False
Expand Down
38 changes: 38 additions & 0 deletions iib/workers/tasks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,3 +1293,41 @@ def get_bundle_metadata(
for pullspec in operator_csv.get_pullspecs():
bundle_metadata['found_pullspecs'].add(pullspec)
return bundle_metadata


def filter_deprecation_operator_from_db(
deprecation_bundles: List[str], index_db_file: str
) -> List[str]:
"""
Check if operator marked for deprecation is available in operator db, if not drop them.
:param List[str] deprecation_bundles: List of bundles to be depricated
:param str index_db_file: Path of operator database
:return: A List of operator bundle to be deprecated
:raises:
"""
try:
with sqlite3.connect(index_db_file) as conn:
query = "SELECT bundlepath FROM operatorbundle WHERE bundlepath IN ({})".format(
','.join('?' for _ in deprecation_bundles)
)

cursor = conn.cursor()
cursor.execute(query, deprecation_bundles)

results = cursor.fetchall()
log.info(
f"Deprication bundles available in databse : {results}"
)

if len(results) >= 1:
deprecation_bundles = list(results[0])
else:
deprecation_bundles = []

return deprecation_bundles

except Exception as e:
log.error(f"Failed to filter deprication bundle not available in db : {e}")
raise e
30 changes: 30 additions & 0 deletions tests/test_workers/test_tasks/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import logging
import os
import sqlite3

import stat
import textwrap
from unittest import mock
Expand Down Expand Up @@ -1309,3 +1311,31 @@ def test_add_max_ocp_version_property_empty_index(mock_apti, mock_cmd, mock_gbj,

mock_gbj.assert_not_called()
mock_apti.assert_not_called()


@mock.patch('iib.workers.tasks.utils.sqlite3')
def test_filter_deprecation_operator_from_db(mock_db):

mock_db.connect.return_value.__enter__.return_value.cursor.return_value.fetchall.return_value = [
["test_operator_1"]
]
deprecation_bundles = [
"test_operator_1",
]

operator_after_deprication = utils.filter_deprecation_operator_from_db(
"index/db/file", deprecation_bundles
)

assert operator_after_deprication == ["test_operator_1"]


@mock.patch('iib.workers.tasks.utils.sqlite3')
def test_failed_filter_deprecation_operator_from_db(mock_db):
mock_db.connect.side_effect = sqlite3.IntegrityError()
deprecation_bundles = [
"test_operator_1",
]

with pytest.raises(sqlite3.IntegrityError):
utils.filter_deprecation_operator_from_db("index/db/file", deprecation_bundles)

0 comments on commit b803aa2

Please sign in to comment.