Skip to content

Commit

Permalink
Keep olm.substitutesFor from original image
Browse files Browse the repository at this point in the history
When original image has `olm.substitutesFor` annotation, in most cases
it was added by Freshmaker with the `operator_csv_modifications_url` in
build task parameters, it's not in any file from dist-git repo, when
Freshmaker rebuild this image and don't add any new `olm.substitutesFor`
annotation, the `olm.substitutesFor` annotation in original image will
be lost, this is wrong.

JIRA: CWFHEALTH-2003
  • Loading branch information
qixiang committed Jun 29, 2023
1 parent 14fe6da commit e60f57b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
41 changes: 33 additions & 8 deletions freshmaker/handlers/botas/botas_shipped_advisory.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,23 @@ def _get_bundles_to_rebuild(self):
if not (csv_name and version):
log.error("CSV data is missing for bundle %s, skip it.", bundle_nvr)
continue

substitutes_for = None
if olm_substitutes:
substitutes_for = csv_name
else:
# olm_substitutes is not enabled, but if original image has `olm.substitutesFor`
# annotation, it should be kept in rebuild.
# When the original image was built by Freshmaker, the `olm.substitutesFor`
# could be added by Freshmaker via `operator_csv_modifications_url` in build
# task parameters, it's not in image dist-git repo, if Freshmaker doesn't add
# the `olm.substitutesFor` annotation in rebuild explicitly, the annotation
# will be lost in rebuild.
csv_data = self._get_bundle_csv(bundle_nvr)
substitutes_for = csv_data["metadata"]["annotations"].get("olm.substitutesFor")

bundle_data.update(
self._get_csv_updates(csv_name, version, olm_substitutes=olm_substitutes)
self._get_csv_updates(csv_name, version, substitutes_for)
)
bundles_to_rebuild.append(bundle_data)
if not bundles_to_rebuild:
Expand Down Expand Up @@ -368,8 +383,7 @@ def _get_bundle_csv_name_and_version(self, bundle_nvr):
log.debug(
"Can't find bundle data of %s in Pyxis, trying to find that in brew.", bundle_nvr
)
koji_api = KojiService(conf.koji_profile)
csv_data = koji_api.get_bundle_csv(bundle_nvr)
csv_data = self._get_bundle_csv(bundle_nvr)
# this should not happen
if not csv_data:
log.error("Bundle data of %s is not available in brew.", bundle_nvr)
Expand All @@ -378,14 +392,26 @@ def _get_bundle_csv_name_and_version(self, bundle_nvr):
version = csv_data["spec"]["version"]
return (csv_name, version)

@staticmethod
def _get_bundle_csv(bundle_nvr):
"""
Get bundle image's CSV data from Koji/Brew
:param str bundle_nvr: NVR of bundle image
:return: bundle CSV data
:rtype: dict
"""
koji_api = KojiService(conf.koji_profile)
return koji_api.get_bundle_csv(bundle_nvr)

@classmethod
def _get_csv_updates(cls, csv_name, version, olm_substitutes=True):
def _get_csv_updates(cls, csv_name, version, substitutes_for=None):
"""
Determine the CSV updates required for the bundle image.
:param str csv_name: the name field in the bundle's ClusterServiceVersion file
:param str version: the version of the bundle image being rebuilt
:param bool olm_substitutes: add `olm.substitutesFor` annotation if True
:param str substitutes_for: value to add as `olm.substitutesFor` annotation
:return: a dictionary of the CSV updates needed
:rtype: dict
"""
Expand All @@ -403,10 +429,9 @@ def _get_csv_updates(cls, csv_name, version, olm_substitutes=True):
"version": new_version,
},
}
if olm_substitutes:
# Declare that this rebuild is a substitute of the bundle being rebuilt
if substitutes_for is not None:
csv_modifications["update"]["metadata"]["annotations"] = {
"olm.substitutesFor": csv_name
"olm.substitutesFor": substitutes_for
}

return csv_modifications
Expand Down
1 change: 1 addition & 0 deletions freshmaker/kojiservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ def get_ocp_versions_range(self, build_nvr):
return ocp_versions_range

@freshmaker.utils.retry(wait_on=(requests.Timeout, requests.ConnectionError), logger=log)
@region.cache_on_arguments()
def get_bundle_csv(self, build_nvr):
"""
Return CSV(cluster service version) data of operator bundle build
Expand Down
4 changes: 2 additions & 2 deletions tests/handlers/botas/test_botas_shipped_advisory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ def test_get_csv_name():
def test_get_csv_updates(mock_grbv, mock_gcn):
mock_grbv.return_value = ("1.2.3+0.1608854400.p", "0.1608854400.p")
mock_gcn.return_value = "amq-streams.1.2.3-0.1608854400.p"
rv = HandleBotasAdvisory._get_csv_updates("amq-streams.1.2.3", "1.2.3")
rv = HandleBotasAdvisory._get_csv_updates("amq-streams.1.2.3", "1.2.3", "amq-streams.1.2.3")
assert rv == {
"update": {
"metadata": {
Expand All @@ -1074,7 +1074,7 @@ def test_get_csv_updates(mock_grbv, mock_gcn):
def test_get_csv_updates_without_olm_substitutes(mock_grbv, mock_gcn):
mock_grbv.return_value = ("1.2.3+0.1608854400.p", "0.1608854400.p")
mock_gcn.return_value = "amq-streams.1.2.3-0.1608854400.p"
rv = HandleBotasAdvisory._get_csv_updates("amq-streams.1.2.3", "1.2.3", olm_substitutes=False)
rv = HandleBotasAdvisory._get_csv_updates("amq-streams.1.2.3", "1.2.3")
assert rv == {
"update": {
"metadata": {
Expand Down

0 comments on commit e60f57b

Please sign in to comment.