Skip to content

Commit

Permalink
Adding config variable to allow adding bundles without ocp version label
Browse files Browse the repository at this point in the history
- added iib_no_ocp_label_allow_list to worker config file
- allow bundles without "com.redhat.openshift.versions" label set to be added for index images set in config
- added unit tests for is_bundle_version_valid

[CLOUDDST-20868]
  • Loading branch information
lipoja committed Nov 14, 2023
1 parent 654e528 commit c2c9139
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ The custom configuration options for the Celery workers are listed below:
* `iib_log_level` - the Python log level for `iib.workers` logger. This defaults to `INFO`.
* `iib_max_recursive_related_bundles` - the maximum number of recursive related bundles IIB will
recurse through. This is to avoid DOS attacks.
* `iib_no_ocp_label_allow_list` - list of index images to which we can add bundles
without "com.redhat.openshift.versions" label
* `iib_organization_customizations` - this is used to customize aspects of the bundle being
regenerated. The format is a dictionary where each key is an organization that requires
customizations. Each value is a list of dictionaries with the ``type`` key set to one of the
Expand Down
2 changes: 2 additions & 0 deletions iib/workers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Config(object):
iib_index_image_output_registry: Optional[str] = None
iib_log_level: str = 'INFO'
iib_max_recursive_related_bundles = 15
# list of index images to which we can add bundles without "com.redhat.openshift.versions" label
iib_no_ocp_label_allow_list: List[str] = []
iib_organization_customizations: iib_organization_customizations_type = {}
iib_sac_queues: List[str] = []
iib_request_logs_dir: Optional[str] = None
Expand Down
19 changes: 17 additions & 2 deletions iib/workers/tasks/build_merge_index_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ def _add_bundles_missing_in_source(
missing_bundles.append(bundle)
missing_bundle_paths.append(bundle['bundlePath'])

allow_no_ocp_version = any(
target_index.startswith(index)
for index in get_worker_config()['iib_no_ocp_label_allow_list']
)

for bundle in itertools.chain(missing_bundles, source_index_bundles):
if not is_bundle_version_valid(bundle['bundlePath'], ocp_version):
if not is_bundle_version_valid(bundle['bundlePath'], ocp_version, allow_no_ocp_version):
invalid_bundles.append(bundle)

if invalid_bundles:
Expand Down Expand Up @@ -390,12 +395,16 @@ def handle_merge_request(
)


def is_bundle_version_valid(bundle_path: str, valid_ocp_version: str) -> bool:
def is_bundle_version_valid(
bundle_path: str, valid_ocp_version: str, allow_no_ocp_version: bool
) -> bool:
"""
Check if the version label of the bundle satisfies the index ocp_version.
:param str bundle_path: pull specification of the bundle to be validated.
:param str valid_ocp_version: the index ocp version against which the bundles will be validated.
:param bool allow_no_ocp_version: when set to tue True
we allow validating bundles without "com.redhat.openshift.versions" label
:return: a boolean indicating if the bundle_path satisfies the index ocp_version
:rtype: bool
Expand All @@ -415,6 +424,12 @@ def is_bundle_version_valid(bundle_path: str, valid_ocp_version: str) -> bool:
raise IIBError(f'Invalid OCP version, "{valid_ocp_version}", specified in Index Image')
try:
bundle_version_label = get_image_label(bundle_path, 'com.redhat.openshift.versions')
if allow_no_ocp_version and not bundle_version_label:
log.info(
'Marking bundle %s without label `com.redhat.openshift.versions` as valid.',
bundle_path,
)
return True
# MYPY error: Item "None" of "Optional[str]" has no attribute "replace"
bundle_version = bundle_version_label.replace('v', '') # type: ignore
log.debug(f'Bundle version {bundle_version}, Index image version {valid_ocp_version}')
Expand Down
30 changes: 18 additions & 12 deletions tests/test_workers/test_tasks/test_build_merge_index_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,22 +648,28 @@ def test_add_bundles_missing_in_source_none_missing(


@pytest.mark.parametrize(
'version_label, ocp_version, result',
'version_label, ocp_version, allow_no_ocp_version, result',
(
('=v4.5', 'v4.6', False),
('v4.5-v4.7', 'v4.6', True),
('v4.5-v4.7', 'v4.8', False),
('v4.6', 'v4.6', True),
('v=4.6', 'v4.6', False),
('v4.5,v4.6', 'v4.6', True),
('v4.6,v4.5', 'v4.10', True),
('tom_brady', 'v4.6', False),
('=v4.5', 'v4.6', False, False),
('v4.5-v4.7', 'v4.6', False, True),
('v4.5-v4.7', 'v4.8', False, False),
('v4.6', 'v4.6', False, True),
('v=4.6', 'v4.6', False, False),
('v4.5,v4.6', 'v4.6', False, True),
('v4.6,v4.5', 'v4.10', False, True),
('tom_brady', 'v4.6', False, False),
('', 'v4.6', False, False),
('', 'v4.6', True, True),
),
)
@mock.patch('iib.workers.tasks.build_merge_index_image.get_image_label')
def test_is_bundle_version_valid(mock_gil, version_label, ocp_version, result):
def test_is_bundle_version_valid(
mock_gil, version_label, ocp_version, allow_no_ocp_version, result
):
mock_gil.return_value = version_label
is_valid = build_merge_index_image.is_bundle_version_valid('some_bundle', ocp_version)
is_valid = build_merge_index_image.is_bundle_version_valid(
'some_bundle', ocp_version, allow_no_ocp_version
)
assert is_valid == result


Expand All @@ -673,7 +679,7 @@ def test_is_bundle_version_valid(mock_gil, version_label, ocp_version, result):
def test_is_bundle_version_valid_invalid_index_ocp_version(version_label):
match_str = f'Invalid OCP version, "{version_label}", specified in Index Image'
with pytest.raises(IIBError, match=match_str):
build_merge_index_image.is_bundle_version_valid('some_bundle', version_label)
build_merge_index_image.is_bundle_version_valid('some_bundle', version_label, False)


@mock.patch('iib.workers.tasks.build_merge_index_image._update_index_image_build_state')
Expand Down

0 comments on commit c2c9139

Please sign in to comment.