Skip to content

Commit f639d48

Browse files
committed
Adding support of multiple operators in one fragment
[CLOUDDST-22242]
1 parent e367bb4 commit f639d48

File tree

4 files changed

+69
-69
lines changed

4 files changed

+69
-69
lines changed

iib/workers/tasks/fbc_utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import shutil
66

7-
from typing import Tuple
7+
from typing import Tuple, List
88
from iib.exceptions import IIBError
99
from iib.workers.config import get_worker_config
1010
from iib.common.tracing import instrument_tracing
@@ -91,13 +91,13 @@ def merge_catalogs_dirs(src_config: str, dest_config: str):
9191
shutil.copytree(src_config, dest_config, dirs_exist_ok=True)
9292

9393

94-
def extract_fbc_fragment(temp_dir: str, fbc_fragment: str) -> Tuple[str, str]:
94+
def extract_fbc_fragment(temp_dir: str, fbc_fragment: str) -> Tuple[str, List[str]]:
9595
"""
96-
Extract operator package from the fbc_fragment image.
96+
Extract operator packages from the fbc_fragment image.
9797
9898
:param str temp_dir: base temp directory for IIB request.
9999
:param str fbc_fragment: pull specification of fbc_fragment in the IIB request.
100-
:return: fbc_fragment path, fbc_operator_package.
100+
:return: fbc_fragment path, fbc_operator_packages.
101101
:rtype: tuple
102102
"""
103103
from iib.workers.tasks.build import _copy_files_from_image
@@ -111,10 +111,8 @@ def extract_fbc_fragment(temp_dir: str, fbc_fragment: str) -> Tuple[str, str]:
111111

112112
log.info("fbc_fragment extracted at %s", fbc_fragment_path)
113113
operator_packages = os.listdir(fbc_fragment_path)
114-
log.info("fbc_fragment contains package %s", operator_packages)
114+
log.info("fbc_fragment contains packages %s", operator_packages)
115115
if not operator_packages:
116116
raise IIBError("No operator packages in fbc_fragment %s", fbc_fragment)
117-
if len(operator_packages) > 1:
118-
raise IIBError("More than 1 package is present in fbc_fragment %s", fbc_fragment)
119117

120-
return fbc_fragment_path, operator_packages[0]
118+
return fbc_fragment_path, operator_packages

iib/workers/tasks/opm_operations.py

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -819,57 +819,56 @@ def opm_registry_add_fbc_fragment(
819819
"""
820820
set_request_state(request_id, 'in_progress', 'Extracting operator package from fbc_fragment')
821821
# fragment path will look like /tmp/iib-**/fbc-fragment
822-
fragment_path, fragment_operator = extract_fbc_fragment(
822+
fragment_path, fragment_operators = extract_fbc_fragment(
823823
temp_dir=temp_dir, fbc_fragment=fbc_fragment
824824
)
825825

826-
is_operator_in_db, index_db_path = verify_operator_exists(
826+
# the dir where all the configs from from_index are stored
827+
# this will look like /tmp/iib-**/configs
828+
from_index_configs_dir = get_catalog_dir(from_index=from_index, base_dir=temp_dir)
829+
log.info("The content of from_index configs located at %s", from_index_configs_dir)
830+
831+
operators_in_db, index_db_path = verify_operators_exists(
827832
from_index=from_index,
828833
base_dir=temp_dir,
829-
operator_package=fragment_operator,
834+
operator_packages=fragment_operators,
830835
overwrite_from_index_token=overwrite_from_index_token,
831836
)
832837

833-
# the dir where all the configs from from_index is stored
834-
# this will look like /tmp/iib-**/configs
835-
from_index_configs_dir = get_catalog_dir(from_index=from_index, base_dir=temp_dir)
836-
837-
log.info("The content of from_index configs located at %s", from_index_configs_dir)
838+
if operators_in_db:
839+
log.info('Removing %s from %s index.db ', operators_in_db, from_index)
840+
_opm_registry_rm(index_db_path=index_db_path, operators=operators_in_db, base_dir=temp_dir)
838841

839-
if is_operator_in_db:
840-
log.info('Removing %s from %s index.db ', fragment_operator, from_index)
841-
_opm_registry_rm(
842-
index_db_path=index_db_path, operators=[fragment_operator], base_dir=temp_dir
843-
)
844-
# migated_catalog_dir path will look like /tmp/iib-**/catalog
845-
migated_catalog_dir, _ = opm_migrate(
842+
# migrated_catalog_dir path will look like /tmp/iib-**/catalog
843+
migrated_catalog_dir, _ = opm_migrate(
846844
index_db=index_db_path,
847845
base_dir=temp_dir,
848846
generate_cache=False,
849847
)
850-
log.info("Migated catalog after removing from db at %s", migated_catalog_dir)
848+
log.info("Migrated catalog after removing from db at %s", migrated_catalog_dir)
851849

852850
# copy the content of migrated_catalog to from_index's config
853-
log.info("Copying content of %s to %s", migated_catalog_dir, from_index_configs_dir)
854-
for operator_package in os.listdir(migated_catalog_dir):
851+
log.info("Copying content of %s to %s", migrated_catalog_dir, from_index_configs_dir)
852+
for operator_package in os.listdir(migrated_catalog_dir):
855853
shutil.copytree(
856-
os.path.join(migated_catalog_dir, operator_package),
854+
os.path.join(migrated_catalog_dir, operator_package),
857855
os.path.join(from_index_configs_dir, operator_package),
858856
dirs_exist_ok=True,
859857
)
860858

861-
# copy fragment_operator to from_index configs
862-
set_request_state(request_id, 'in_progress', 'Adding fbc_fragment to from_index')
863-
fragment_opr_src_path = os.path.join(fragment_path, fragment_operator)
864-
fragment_opr_dest_path = os.path.join(from_index_configs_dir, fragment_operator)
865-
if os.path.exists(fragment_opr_dest_path):
866-
shutil.rmtree(fragment_opr_dest_path)
867-
log.info(
868-
"Copying content of %s to %s",
869-
fragment_opr_src_path,
870-
fragment_opr_dest_path,
871-
)
872-
shutil.copytree(fragment_opr_src_path, fragment_opr_dest_path)
859+
for fragment_operator in fragment_operators:
860+
# copy fragment_operator to from_index configs
861+
set_request_state(request_id, 'in_progress', 'Adding fbc_fragment to from_index')
862+
fragment_opr_src_path = os.path.join(fragment_path, fragment_operator)
863+
fragment_opr_dest_path = os.path.join(from_index_configs_dir, fragment_operator)
864+
if os.path.exists(fragment_opr_dest_path):
865+
shutil.rmtree(fragment_opr_dest_path)
866+
log.info(
867+
"Copying content of %s to %s",
868+
fragment_opr_src_path,
869+
fragment_opr_dest_path,
870+
)
871+
shutil.copytree(fragment_opr_src_path, fragment_opr_dest_path)
873872

874873
local_cache_path = os.path.join(temp_dir, 'cache')
875874
generate_cache_locally(
@@ -886,32 +885,32 @@ def opm_registry_add_fbc_fragment(
886885
)
887886

888887

889-
def verify_operator_exists(
888+
def verify_operators_exists(
890889
from_index: str,
891890
base_dir: str,
892-
operator_package: str,
891+
operator_packages: List[str],
893892
overwrite_from_index_token: Optional[str],
894893
):
895894
"""
896-
Check if operator exists in index image.
895+
Check if operators exists in index image.
897896
898897
:param str from_index: index in which operator existence is checked
899898
:param str base_dir: base temp directory for IIB request
900-
:param str operator_package: operator_package to check
899+
:param list(str) operator_packages: operator_package to check
901900
:param str overwrite_from_index_token: token used to access the image
902-
:return: is_package_in_index, index_db_path
903-
:rtype: (str, str)
901+
:return: packages_in_index, index_db_path
902+
:rtype: (list, str)
904903
"""
905904
from iib.workers.tasks.build import terminate_process, get_bundle_json
906905
from iib.workers.tasks.iib_static_types import BundleImage
907906
from iib.workers.tasks.utils import run_cmd
908907
from iib.workers.tasks.utils import set_registry_token
909908

910-
is_package_in_index = False
909+
packages_in_index: List[str] = []
911910

912-
log.info("Verifying if operator package %s exists in index %s", operator_package, from_index)
911+
log.info("Verifying if operator packages %s exists in index %s", operator_packages, from_index)
913912

914-
# check if operater package exists in hidden index.db
913+
# check if operator packages exists in hidden index.db
915914
with set_registry_token(overwrite_from_index_token, from_index, append=True):
916915
index_db_path = get_hidden_index_database(from_index=from_index, base_dir=base_dir)
917916

@@ -924,10 +923,13 @@ def verify_operator_exists(
924923
present_bundles: List[BundleImage] = get_bundle_json(bundles)
925924

926925
for bundle in present_bundles:
927-
if bundle['packageName'] == operator_package:
928-
is_package_in_index = True
929-
log.info("operator package %s found in index_db %s", operator_package, index_db_path)
930-
return is_package_in_index, index_db_path
926+
if bundle['packageName'] in operator_packages:
927+
packages_in_index.append(bundle['packageName'])
928+
929+
if packages_in_index:
930+
log.info("operator packages found in index_db %s: %s", index_db_path, packages_in_index)
931+
932+
return packages_in_index, index_db_path
931933

932934

933935
@retry(

tests/test_workers/test_tasks/test_fbc_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,14 @@ def test_merge_catalogs_dirs_raise(mock_isdir, mock_cpt, tmpdir):
144144
@pytest.mark.parametrize('ldr_output', [['testoperator'], ['test1', 'test2'], []])
145145
@mock.patch('os.listdir')
146146
@mock.patch('iib.workers.tasks.build._copy_files_from_image')
147-
def test_extract_fbc_fragment_one_operator(mock_cffi, mock_osldr, ldr_output, tmpdir):
147+
def test_extract_fbc_fragment(mock_cffi, mock_osldr, ldr_output, tmpdir):
148148
test_fbc_fragment = "example.com/test/fbc_fragment:latest"
149149
mock_osldr.return_value = ldr_output
150150
fbc_fragment_path = os.path.join(tmpdir, get_worker_config()['temp_fbc_fragment_path'])
151151

152-
if len(ldr_output) != 1:
152+
if not ldr_output:
153153
with pytest.raises(IIBError):
154154
extract_fbc_fragment(tmpdir, test_fbc_fragment)
155-
156155
else:
157156
extract_fbc_fragment(tmpdir, test_fbc_fragment)
158157
mock_cffi.assert_has_calls([mock.call(test_fbc_fragment, '/configs', fbc_fragment_path)])

tests/test_workers/test_tasks/test_opm_operations.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,8 @@ def test_verify_cache_insertion_edit_dockerfile_failed():
648648

649649

650650
@pytest.mark.parametrize(
651-
'is_operator_exists, index_db_path', [(True, "index_path"), (False, "index_path")]
651+
'operators_exists, index_db_path',
652+
[(['test-operator'], "index_path"), ([], "index_path")],
652653
)
653654
@mock.patch('iib.workers.tasks.opm_operations.opm_generate_dockerfile')
654655
@mock.patch('iib.workers.tasks.opm_operations.generate_cache_locally')
@@ -658,7 +659,7 @@ def test_verify_cache_insertion_edit_dockerfile_failed():
658659
@mock.patch('iib.workers.tasks.opm_operations.opm_migrate')
659660
@mock.patch('iib.workers.tasks.opm_operations._opm_registry_rm')
660661
@mock.patch('iib.workers.tasks.opm_operations.get_catalog_dir')
661-
@mock.patch('iib.workers.tasks.opm_operations.verify_operator_exists')
662+
@mock.patch('iib.workers.tasks.opm_operations.verify_operators_exists')
662663
@mock.patch('iib.workers.tasks.opm_operations.extract_fbc_fragment')
663664
@mock.patch('iib.workers.tasks.opm_operations.set_request_state')
664665
def test_opm_registry_add_fbc_fragment(
@@ -673,16 +674,16 @@ def test_opm_registry_add_fbc_fragment(
673674
mock_rmt,
674675
mock_gcc,
675676
mock_ogd,
676-
is_operator_exists,
677+
operators_exists,
677678
index_db_path,
678679
tmpdir,
679680
):
680681
from_index = "example.com/test/index"
681682
binary_image = "example.com/ose/binary"
682683
fbc_fragment = "example.com/test/fragment"
683-
fbc_fragment_operator = "test-operator"
684-
mock_eff.return_value = (os.path.join(tmpdir, "fbc_fragment"), fbc_fragment_operator)
685-
mock_voe.return_value = is_operator_exists, index_db_path
684+
fbc_fragment_operators = ["test-operator"]
685+
mock_eff.return_value = (os.path.join(tmpdir, "fbc_fragment"), fbc_fragment_operators)
686+
mock_voe.return_value = operators_exists, index_db_path
686687
mock_gcr.return_value = os.path.join(tmpdir, "configs")
687688
mock_om.return_value = os.path.join(tmpdir, "catalog"), None
688689
mock_ldr.return_value = [
@@ -695,13 +696,13 @@ def test_opm_registry_add_fbc_fragment(
695696
mock_voe.assert_called_with(
696697
from_index=from_index,
697698
base_dir=tmpdir,
698-
operator_package=fbc_fragment_operator,
699+
operator_packages=fbc_fragment_operators,
699700
overwrite_from_index_token=None,
700701
)
701702
mock_gcr.assert_called_with(from_index=from_index, base_dir=tmpdir)
702-
if is_operator_exists:
703+
if operators_exists:
703704
mock_orr.assert_called_with(
704-
index_db_path=index_db_path, operators=[fbc_fragment_operator], base_dir=tmpdir
705+
index_db_path=index_db_path, operators=fbc_fragment_operators, base_dir=tmpdir
705706
)
706707
mock_om.assert_called_with(index_db=index_db_path, base_dir=tmpdir, generate_cache=False)
707708
mock_cpt.assert_has_calls(
@@ -721,8 +722,8 @@ def test_opm_registry_add_fbc_fragment(
721722
mock_cpt.assert_has_calls(
722723
[
723724
mock.call(
724-
os.path.join(tmpdir, "fbc_fragment", fbc_fragment_operator),
725-
os.path.join(tmpdir, "configs", fbc_fragment_operator),
725+
os.path.join(tmpdir, "fbc_fragment", fbc_fragment_operators[0]),
726+
os.path.join(tmpdir, "configs", fbc_fragment_operators[0]),
726727
)
727728
]
728729
)
@@ -736,12 +737,12 @@ def test_opm_registry_add_fbc_fragment(
736737
(
737738
'{"packageName": "test-operator", "version": "v1.0", "bundlePath":"bundle1"\n}'
738739
'\n{\n"packageName": "package2", "version": "v2.0", "bundlePath":"bundle2"}',
739-
True,
740+
["test-operator"],
740741
),
741742
(
742743
'{"packageName": "package1", "version": "v1.0", "bundlePath":"bundle1"\n}'
743744
'\n{\n"packageName": "package2", "version": "v2.0", "bundlePath":"bundle2"}',
744-
False,
745+
[],
745746
),
746747
],
747748
)
@@ -757,7 +758,7 @@ def test_verify_operator_exists(
757758
mock_ors.return_value = 500, mock.MagicMock()
758759
mock_rc.return_value = bundles_in_db
759760
index_db_path = os.path.join(tmpdir, get_worker_config()['temp_index_db_path'])
760-
package_exists, index_db_path = opm_operations.verify_operator_exists(
761+
package_exists, index_db_path = opm_operations.verify_operators_exists(
761762
from_index, tmpdir, 'test-operator', None
762763
)
763764
mock_ors.assert_has_calls([mock.call(db_path=index_db_path)])

0 commit comments

Comments
 (0)