Skip to content

Commit

Permalink
Update rm endpoint to remove fbc opted in operators
Browse files Browse the repository at this point in the history
Earlier the code will not even reach to the point of removing
packages from /configs because opm rm was failing on opted in
operators, change rm endpoint such that both opted-in and non-opted-in
operators can be removed
  • Loading branch information
Tulsi Chandwani committed Jul 4, 2024
1 parent c7c59a6 commit f83fc23
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
57 changes: 39 additions & 18 deletions iib/workers/tasks/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
opm_index_rm,
deprecate_bundles,
Opm,
verify_operators_exists,
opm_generate_dockerfile,
)
from iib.workers.tasks.utils import (
add_max_ocp_version_property,
Expand Down Expand Up @@ -1049,35 +1051,46 @@ def handle_rm_request(

if image_is_fbc:
log.info("Processing File-Based Catalog image")
fbc_dir, _ = opm_registry_rm_fbc(
base_dir=temp_dir,
from_index=from_index_resolved,
operators=operators,
binary_image=prebuild_info['binary_image'],
overwrite_from_index_token=overwrite_from_index_token,
generate_cache=False,
)

# rename `catalog` directory because we need to use this name for
# final destination of catalog (defined in Dockerfile)
catalog_from_db = os.path.join(temp_dir, 'from_db')
os.rename(fbc_dir, catalog_from_db)

# remove operator from /configs
os.makedirs(os.path.join(temp_dir, 'from_index'), exist_ok=True)
# get catalog with opted-in operators
# get catalog with opted-in and non-opted-in operators
# store it in /<temp_dir>/from_index/configs
with set_registry_token(overwrite_from_index_token, from_index_resolved, append=True):
catalog_from_index = get_catalog_dir(
from_index=from_index_resolved, base_dir=os.path.join(temp_dir, 'from_index')
)
# remove operators from from_index file-based catalog
# remove operators from /<temp_dir>/from_index/configs if exists
for operator in operators:
operator_path = os.path.join(catalog_from_index, operator)
if os.path.exists(operator_path):
log.debug('Removing operator from from_index FBC %s', operator_path)
shutil.rmtree(operator_path)
# overwrite data in `catalog_from_index` by data from `catalog_from_db`
# this adds changes on not opted in operators to final
merge_catalogs_dirs(catalog_from_db, catalog_from_index)

# if operator is not opted in, remove from db
operators_in_db, index_db_path = verify_operators_exists(
from_index=from_index,
base_dir=temp_dir,
operator_packages=operators,
overwrite_from_index_token=overwrite_from_index_token,
)

if operators_in_db:
fbc_dir, _ = opm_registry_rm_fbc(
base_dir=temp_dir,
from_index=from_index_resolved,
operators=operators,
index_db_path=index_db_path,
)

# rename `catalog` directory because we need to use this name for
# final destination of catalog (defined in Dockerfile)
catalog_from_db = os.path.join(temp_dir, 'from_db')
os.rename(fbc_dir, catalog_from_db)

# overwrite data in `catalog_from_index` by data from `catalog_from_db`
# this adds changes on not opted in operators to final
merge_catalogs_dirs(catalog_from_db, catalog_from_index)

fbc_dir_path = os.path.join(temp_dir, 'catalog')
# We need to regenerate file-based catalog because we merged changes
Expand All @@ -1086,6 +1099,14 @@ def handle_rm_request(
# move migrated catalog to correct location expected in Dockerfile
shutil.move(catalog_from_index, fbc_dir_path)

opm_generate_dockerfile(
fbc_dir=fbc_dir_path,
base_dir=temp_dir,
index_db=index_db_path,
binary_image=prebuild_info['binary_image'],
dockerfile_name='index.Dockerfile',
)

# Remove outdated cache before generating new one
local_cache_path = os.path.join(temp_dir, 'cache')
if os.path.exists(local_cache_path):
Expand Down
23 changes: 4 additions & 19 deletions iib/workers/tasks/opm_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,9 +967,7 @@ def opm_registry_rm_fbc(
base_dir: str,
from_index: str,
operators: List[str],
binary_image: str,
overwrite_from_index_token: Optional[str] = None,
generate_cache: bool = True,
index_db_path: str,
) -> Tuple[str, Optional[str]]:
"""
Remove operator/s from a File Based Catalog index image.
Expand All @@ -992,25 +990,12 @@ def opm_registry_rm_fbc(
:return: Returns paths to directories for containing file-based catalog and it's cache
:rtype: str, str|None
"""
from iib.workers.tasks.utils import set_registry_token

log.info('Removing %s from a FBC Image %s', operators, from_index)
log.info('Using the existing database from %s', from_index)
log.info('Removing %s from %s index.db ', operators, from_index)
_opm_registry_rm(index_db_path=index_db_path, operators=operators, base_dir=base_dir)

with set_registry_token(overwrite_from_index_token, from_index, append=True):
index_db_path = get_hidden_index_database(from_index=from_index, base_dir=base_dir)

_opm_registry_rm(index_db_path, operators, base_dir)
fbc_dir, cache_dir = opm_migrate(
index_db=index_db_path, base_dir=base_dir, generate_cache=generate_cache
)

opm_generate_dockerfile(
fbc_dir=fbc_dir,
base_dir=base_dir,
index_db=index_db_path,
binary_image=binary_image,
dockerfile_name='index.Dockerfile',
index_db=index_db_path, base_dir=base_dir, generate_cache=False
)

return fbc_dir, cache_dir
Expand Down

0 comments on commit f83fc23

Please sign in to comment.