|
11 | 11 | import re |
12 | 12 | import subprocess |
13 | 13 | from collections.abc import Iterator |
| 14 | +from typing import Any, List |
14 | 15 |
|
15 | 16 | from operator_repo import Bundle |
16 | 17 | from operator_repo.checks import CheckResult, Fail, Warn |
17 | 18 | from operator_repo.utils import lookup_dict |
18 | | -from semver import Version |
19 | 19 | from operatorcert import utils |
| 20 | +from semver import Version |
20 | 21 |
|
21 | 22 | from .validations import ( |
22 | 23 | validate_capabilities, |
|
54 | 55 | } |
55 | 56 |
|
56 | 57 |
|
| 58 | +class GraphLoopException(Exception): |
| 59 | + """ |
| 60 | + Exception raised when a loop is detected in the update graph |
| 61 | + """ |
| 62 | + |
| 63 | + |
57 | 64 | def _parse_semver(version: str) -> Version: |
58 | 65 | return Version.parse(version.strip(), optional_minor_and_patch=True).replace( |
59 | 66 | prerelease=None, build=None |
@@ -288,3 +295,111 @@ def check_api_version_constraints(bundle: Bundle) -> Iterator[CheckResult]: |
288 | 295 | f"OCP version(s) {conflicting_str} conflict with " |
289 | 296 | f"minKubeVersion={k8s_version_min}" |
290 | 297 | ) |
| 298 | + |
| 299 | + |
| 300 | +def check_upgrade_graph_loop(bundle: Bundle) -> Iterator[CheckResult]: |
| 301 | + """ |
| 302 | + Detect loops in the upgrade graph |
| 303 | +
|
| 304 | + Example: |
| 305 | +
|
| 306 | + Channel beta: A -> B -> C -> B |
| 307 | +
|
| 308 | + Args: |
| 309 | + bundle (Bundle): Operator bundle |
| 310 | +
|
| 311 | + Yields: |
| 312 | + Iterator[CheckResult]: Failure if a loop is detected |
| 313 | + """ |
| 314 | + all_channels: set[str] = set(bundle.channels) |
| 315 | + if bundle.default_channel is not None: |
| 316 | + all_channels.add(bundle.default_channel) |
| 317 | + operator = bundle.operator |
| 318 | + for channel in sorted(all_channels): |
| 319 | + visited: List[Bundle] = [] |
| 320 | + try: |
| 321 | + channel_bundles = operator.channel_bundles(channel) |
| 322 | + try: |
| 323 | + graph = operator.update_graph(channel) |
| 324 | + except (NotImplementedError, ValueError) as exc: |
| 325 | + yield Fail(str(exc)) |
| 326 | + return |
| 327 | + follow_graph(graph, channel_bundles[0], visited) |
| 328 | + except GraphLoopException as exc: |
| 329 | + yield Fail(str(exc)) |
| 330 | + |
| 331 | + |
| 332 | +def follow_graph(graph: Any, bundle: Bundle, visited: List[Bundle]) -> List[Bundle]: |
| 333 | + """ |
| 334 | + Follow operator upgrade graph and raise exception if loop is detected |
| 335 | +
|
| 336 | + Args: |
| 337 | + graph (Any): Operator update graph |
| 338 | + bundle (Bundle): Current bundle that started the graph traversal |
| 339 | + visited (List[Bundle]): List of bundles visited so far |
| 340 | +
|
| 341 | + Raises: |
| 342 | + GraphLoopException: Graph loop detected |
| 343 | +
|
| 344 | + Returns: |
| 345 | + List[Bundle]: List of bundles visited so far |
| 346 | + """ |
| 347 | + if bundle in visited: |
| 348 | + visited.append(bundle) |
| 349 | + raise GraphLoopException(f"Upgrade graph loop detected for bundle: {visited}") |
| 350 | + if bundle not in graph: |
| 351 | + return visited |
| 352 | + |
| 353 | + visited.append(bundle) |
| 354 | + next_bundles = graph[bundle] |
| 355 | + for next_bundle in next_bundles: |
| 356 | + visited_copy = visited.copy() |
| 357 | + follow_graph(graph, next_bundle, visited_copy) |
| 358 | + return visited |
| 359 | + |
| 360 | + |
| 361 | +def check_replaces_availability(bundle: Bundle) -> Iterator[CheckResult]: |
| 362 | + """ |
| 363 | + Check if the current bundle and the replaced bundle support the same OCP versions |
| 364 | +
|
| 365 | + Args: |
| 366 | + bundle (Bundle): Operator bundle |
| 367 | +
|
| 368 | + Yields: |
| 369 | + Iterator[CheckResult]: Failure if the version of the replaced bundle |
| 370 | + does not match with the current bundle |
| 371 | + """ |
| 372 | + |
| 373 | + replaces = bundle.csv.get("spec", {}).get("replaces") |
| 374 | + if not replaces: |
| 375 | + return |
| 376 | + delimiter = ".v" if ".v" in replaces else "." |
| 377 | + replaces_version = replaces.split(delimiter, 1)[1] |
| 378 | + replaces_bundle = bundle.operator.bundle(replaces_version) |
| 379 | + ocp_versions_str = bundle.annotations.get("com.redhat.openshift.versions") |
| 380 | + replaces_ocp_version_str = replaces_bundle.annotations.get( |
| 381 | + "com.redhat.openshift.versions" |
| 382 | + ) |
| 383 | + if ocp_versions_str == replaces_ocp_version_str: |
| 384 | + # The annotations match, no need to check further |
| 385 | + return |
| 386 | + organization = bundle.operator.repo.config.get("organization") |
| 387 | + |
| 388 | + indexes = set(utils.get_ocp_supported_versions(organization, ocp_versions_str)) |
| 389 | + replaces_indexes = set( |
| 390 | + utils.get_ocp_supported_versions(organization, replaces_ocp_version_str) |
| 391 | + ) |
| 392 | + |
| 393 | + if indexes - replaces_indexes == set(): |
| 394 | + # The replaces bundle supports all the same versions as the current bundle |
| 395 | + return |
| 396 | + yield Fail( |
| 397 | + f"Replaces bundle {replaces_bundle} {sorted(replaces_indexes)} does not support " |
| 398 | + f"the same OCP versions as bundle {bundle} {sorted(indexes)}. In order to fix this issue, " |
| 399 | + "align the OCP version range to match the range of the replaced bundle. " |
| 400 | + "This can be done by setting the `com.redhat.openshift.versions` annotation in the " |
| 401 | + "`metadata/annotations.yaml` file.\n" |
| 402 | + f"`{bundle}` - `{ocp_versions_str}`\n" |
| 403 | + f"`{replaces_bundle}` - `{replaces_ocp_version_str}`" |
| 404 | + ) |
| 405 | + yield from [] |
0 commit comments