Skip to content

Commit

Permalink
Make flake8 happy
Browse files Browse the repository at this point in the history
  • Loading branch information
vfreex committed Dec 3, 2024
1 parent d254d59 commit 4f6a513
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions doozer/doozerlib/backend/konflux_image_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,18 @@ def __init__(self, config: KonfluxImageBuilderConfig, logger: Optional[logging.L
@limit_concurrency(limit=constants.MAX_KONFLUX_BUILD_QUEUE_SIZE)
async def build(self, metadata: ImageMetadata):
""" Build a container image with Konflux. """
logger = self._logger.getChild(f"[{metadata.distgit_key}]")
metadata.build_status = False
try:
dest_dir = self._config.base_dir.joinpath(metadata.qualified_key)
if dest_dir.exists():
# Load exiting build source repository
build_repo = await BuildRepo.from_local_dir(dest_dir, self._logger)
build_repo = await BuildRepo.from_local_dir(dest_dir, logger)
else:
# Clone the build source repository
source = None
if metadata.has_source():
self._logger.info(f"Resolving source for {metadata.qualified_key}")
logger.info(f"Resolving source for {metadata.qualified_key}")
source = cast(SourceResolution, await exectools.to_thread(metadata.runtime.source_resolver.resolve_source, metadata))
else:
raise IOError(f"Image {metadata.qualified_key} doesn't have upstream source. This is no longer supported.")
Expand All @@ -85,7 +86,7 @@ async def build(self, metadata: ImageMetadata):
"assembly_name": metadata.runtime.assembly,
"distgit_key": metadata.distgit_key
})
build_repo = BuildRepo(url=source.url, branch=dest_branch, local_dir=dest_dir, logger=self._logger)
build_repo = BuildRepo(url=source.url, branch=dest_branch, local_dir=dest_dir, logger=logger)
await build_repo.ensure_source()

# Wait for parent members to be built
Expand All @@ -101,33 +102,32 @@ async def build(self, metadata: ImageMetadata):
if unsupported_arches:
# TODO: Update once we are on the new cluster
# raise ValueError(f"[{metadata.distgit_key}] Unsupported arches: {', '.join(unsupported_arches)}")
self._logger.warning(f"[{metadata.distgit_key}] Skipping arches: {', '.join(unsupported_arches)}")
logger.warning(f"Skipping arches: {', '.join(unsupported_arches)}")

# Start the build
self._logger.info("Starting Konflux image build for %s...", metadata.distgit_key)
logger.info("Starting Konflux image build for %s...", metadata.distgit_key)
retries = 3
error = None
for attempt in range(retries):
self._logger.info("[%s] Build attempt %s/%s", metadata.distgit_key, attempt + 1, retries)
logger.info("Build attempt %s/%s", attempt + 1, retries)
pipelinerun = await self._start_build(metadata, build_repo, building_arches)
await self.update_konflux_db(metadata, build_repo, pipelinerun, KonfluxBuildOutcome.PENDING, building_arches)

pipelinerun_name = pipelinerun['metadata']['name']
self._logger.info("[%s] Waiting for PipelineRun %s to complete...", metadata.distgit_key, pipelinerun_name)
logger.info("Waiting for PipelineRun %s to complete...", pipelinerun_name)
pipelinerun = await self._konflux_client.wait_for_pipelinerun(pipelinerun_name, namespace=self._config.namespace)
self._logger.info("[%s] PipelineRun %s completed", metadata.distgit_key, pipelinerun_name)
logger.info("PipelineRun %s completed", pipelinerun_name)

status = pipelinerun.status.conditions[0].status
outcome = KonfluxBuildOutcome.SUCCESS if status == "True" else KonfluxBuildOutcome.FAILURE
if self._config.dry_run:
self._logger.info("[%s] Dry run: Would have inserted build record in Konflux DB",
metadata.distgit_key)
logger.info("Dry run: Would have inserted build record in Konflux DB")
else:
await self.update_konflux_db(metadata, build_repo, pipelinerun, outcome, building_arches)

if status != "True":
error = KonfluxImageBuildError(f"Konflux image build for {metadata.distgit_key} failed",
pipelinerun_name, pipelinerun)
pipelinerun_name, pipelinerun)
else:
metadata.build_status = True
break
Expand All @@ -139,12 +139,12 @@ async def build(self, metadata: ImageMetadata):

async def _wait_for_parent_members(self, metadata: ImageMetadata):
# If this image is FROM another group member, we need to wait on that group member to be built
logger = self._logger.getChild(f"[{metadata.distgit_key}]")
parent_members = list(metadata.get_parent_members().values())
for parent_member in parent_members:
if parent_member is None:
continue # Parent member is not included in the group; no need to wait
self._logger.info("[%s] Parent image %s is building; waiting...", metadata.distgit_key,
parent_member.distgit_key)
logger.info("Parent image %s is building; waiting...", parent_member.distgit_key)
# wait for parent member to be built
while not parent_member.build_event.is_set():
# asyncio.sleep instead of Event.wait since it's less CPU intensive
Expand All @@ -168,7 +168,7 @@ async def _start_build(self, metadata: ImageMetadata, build_repo: BuildRepo, bui
# Ensure the Application resource exists
app_name = self._config.group_name.replace(".", "-")
logger.info(f"Using application: {app_name}")
app = await self._konflux_client.ensure_application(name=app_name, display_name=app_name)
await self._konflux_client.ensure_application(name=app_name, display_name=app_name)

# Ensure the component resource exists
# Openshift doesn't allow dots or underscores in any of its fields, so we replace them with dashes
Expand All @@ -189,7 +189,7 @@ async def _start_build(self, metadata: ImageMetadata, build_repo: BuildRepo, bui
]
default_revision = f"art-{self._config.group_name}-assembly-test-dgk-{metadata.distgit_key}"
logger.info(f"Using component: {component_name}")
component = await self._konflux_client.ensure_component(
await self._konflux_client.ensure_component(
name=component_name,
application=app_name,
component_name=component_name,
Expand All @@ -213,7 +213,7 @@ async def _start_build(self, metadata: ImageMetadata, build_repo: BuildRepo, bui
skip_checks=self._config.skip_checks,
)

logger.info(f"[%s] Created PipelineRun: {self.build_pipeline_url(pipelinerun)}", metadata.distgit_key)
logger.info(f"Created PipelineRun: {self.build_pipeline_url(pipelinerun)}")
return pipelinerun

def build_pipeline_url(self, pipelinerun):
Expand Down Expand Up @@ -256,9 +256,9 @@ async def _get_for_arch(arch):
return sorted(installed_packages)

async def update_konflux_db(self, metadata, build_repo, pipelinerun, outcome, building_arches):
logger = self._logger.getChild(f"[{metadata.distgit_key}]")
if not metadata.runtime.konflux_db:
self._logger.warning('Konflux DB connection is not initialized, not writing build record to the Konflux '
'DB.')
logger.warning('Konflux DB connection is not initialized, not writing build record to the Konflux DB.')
return

try:
Expand Down Expand Up @@ -341,7 +341,7 @@ async def update_konflux_db(self, metadata, build_repo, pipelinerun, outcome, bu

build_record = KonfluxBuildRecord(**build_record_params)
metadata.runtime.konflux_db.add_build(build_record)
self._logger.info(f'[{metadata.distgit_key}] Konflux build info stored successfully with status {outcome}')
logger.info(f'Konflux build info stored successfully with status {outcome}')

except Exception as err:
self._logger.error('Failed writing record to the konflux DB: %s', err)
logger.error('Failed writing record to the konflux DB: %s', err)

0 comments on commit 4f6a513

Please sign in to comment.