Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix IndexError if no virt-launcher pod #2225

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

vsibirsk
Copy link
Contributor

@vsibirsk vsibirsk commented Dec 10, 2024

Short description:

raise ResourceNotFoundError with meaningful message instead of IndexError

More details:

Encountered issue when calling for wait_until_running() method

03:33:36  2024-12-04T01:33:36.613602 ocp_resources VirtualMachineInstance INFO Wait for VirtualMachineInstance windows-efi-secureboot-1733276010-0263808 status to be Running
03:33:36  2024-12-04T01:33:36.613914 timeout_sampler INFO Waiting for 240 seconds [0:04:00], retry every 1 seconds. (Function: kubernetes.dynamic.client.get Kwargs: {'field_selector': 'metadata.name==windows-efi-secureboot-1733276010-0263808', 'namespace': 'virt-general-efi-secureboot-test-vm-with-efi-secureboot'})
03:33:36  2024-12-04T01:33:36.618097 ocp_resources VirtualMachineInstance INFO Status of VirtualMachineInstance windows-efi-secureboot-1733276010-0263808 is Pending
03:37:43  2024-12-04T01:37:36.784198 ocp_resources VirtualMachineInstance ERROR Status of VirtualMachineInstance windows-efi-secureboot-1733276010-0263808 is Pending

...

03:37:43    File "/cnv-tests/utilities/virt.py", line 1646, in running_vm
03:37:43      wait_for_running_vm(
03:37:43    File "/cnv-tests/utilities/virt.py", line 1579, in wait_for_running_vm
03:37:43      vm.vmi.wait_until_running(timeout=wait_until_running_timeout)
03:37:43    File "/cnv-tests/.venv/lib/python3.9/site-packages/ocp_resources/virtual_machine_instance.py", line 122, in wait_until_running
03:37:43      virt_pod = self.virt_launcher_pod
03:37:43    File "/cnv-tests/.venv/lib/python3.9/site-packages/ocp_resources/virtual_machine_instance.py", line 86, in virt_launcher_pod
03:37:43      return pods[0]
03:37:43  IndexError: list index out of range
What this PR does / why we need it:
Which issue(s) this PR fixes:
Special notes for reviewer:
Bug:

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for pod retrieval, providing clearer exceptions when no pods are found.

Copy link

coderabbitai bot commented Dec 10, 2024

Walkthrough

The changes in this pull request focus on enhancing error handling within the VirtualMachineInstance class found in the ocp_resources/virtual_machine_instance.py file. Modifications were made to the virt_launcher_pod and virt_handler_pod properties to introduce more precise conditions under which a ResourceNotFoundError is raised. The new logic ensures that exceptions are only triggered when no pods are found, rather than unconditionally at the end of the method.

Changes

File Change Summary
ocp_resources/virtual_machine_instance.py Updated virt_launcher_pod to raise ResourceNotFoundError with a message if no pods are found; simplified virt_handler_pod to raise ResourceNotFoundError without a message when no matching pods are found.

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 884b20e and 97bff1f.

📒 Files selected for processing (1)
  • ocp_resources/virtual_machine_instance.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • ocp_resources/virtual_machine_instance.py

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@redhat-qe-bot2
Copy link

Report bugs in Issues

The following are automatically added:

  • Add reviewers from OWNER file (in the root of the repository) under reviewers section.
  • Set PR size label.
  • New issue is created for the PR. (Closed when PR is merged/closed)
  • Run pre-commit if .pre-commit-config.yaml exists in the repo.

Available user actions:

  • To mark PR as WIP comment /wip to the PR, To remove it from the PR comment /wip cancel to the PR.
  • To block merging of PR comment /hold, To un-block merging of PR comment /hold cancel.
  • To mark PR as verified comment /verified to the PR, to un-verify comment /verified cancel to the PR.
    verified label removed on each new commit push.
  • To cherry pick a merged PR comment /cherry-pick <target branch to cherry-pick to> in the PR.
    • Multiple target branches can be cherry-picked, separated by spaces. (/cherry-pick branch1 branch2)
    • Cherry-pick will be started when PR is merged
  • To build and push container image command /build-and-push-container in the PR (tag will be the PR number).
    • You can add extra args to the Podman build command
      • Example: /build-and-push-container --build-arg OPENSHIFT_PYTHON_WRAPPER_COMMIT=<commit_hash>
  • To add a label by comment use /<label name>, to remove, use /<label name> cancel
  • To assign reviewers based on OWNERS file use /assign-reviewers
  • To check if PR can be merged use /check-can-merge
Supported /retest check runs
  • /retest tox: Retest tox
  • /retest python-module-install: Retest python-module-install
  • /retest all: Retest all
Supported labels
  • hold
  • verified
  • wip
  • lgtm

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
ocp_resources/virtual_machine_instance.py (2)

78-80: LGTM! Consider enhancing the error message.

The error handling is correctly placed before any list indexing operations, preventing the IndexError. However, the error message could be more specific about the cause.

Consider this enhanced error message:

-            raise ResourceNotFoundError(f"VIRT launcher POD not found for {self.kind}:{self.name}")
+            raise ResourceNotFoundError(
+                f"No virt-launcher pod found for {self.kind} '{self.name}'. "
+                f"The pod might not have been created yet or was terminated."
+            )

Line range hint 133-147: Consider enhancing error logging in wait_until_running.

The error handling flow is good, but the debug information could be more comprehensive.

Consider adding VMI status logging:

     try:
         self.wait_for_status(status=self.Status.RUNNING, timeout=timeout, stop_status=stop_status)
     except TimeoutExpiredError as sampler_ex:
         if not logs:
             raise
         try:
+            self.logger.error(f"VMI status: {self.instance.status}")
             virt_pod = self.virt_launcher_pod
             self.logger.error(f"Status of virt-launcher pod {virt_pod.name}: {virt_pod.status}")
             self.logger.debug(f"{virt_pod.name} *****LOGS*****")
             self.logger.debug(virt_pod.log(container="compute"))
         except ResourceNotFoundError as virt_pod_ex:
-            self.logger.error(virt_pod_ex)
+            self.logger.error(
+                f"Failed to get virt-launcher pod logs: {virt_pod_ex}. "
+                f"This might indicate that the pod creation failed or was delayed."
+            )
             raise sampler_ex
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5abe185 and 884b20e.

📒 Files selected for processing (1)
  • ocp_resources/virtual_machine_instance.py (1 hunks)
🔇 Additional comments (1)
ocp_resources/virtual_machine_instance.py (1)

Line range hint 3-3: Verify usage of virt_launcher_pod property.

Let's verify other methods that might need similar error handling when accessing the virt_launcher_pod property.

✅ Verification successful

Let me analyze the usage of virt_launcher_pod to verify if similar error handling is needed in other methods.


The error handling for virt_launcher_pod property is already properly implemented

The property virt_launcher_pod already has appropriate error handling:

  • Raises ResourceNotFoundError when no pods are found
  • Handles migration scenarios by correctly selecting the target pod
  • All methods using this property (execute_virsh_command, get_uid, etc.) will automatically inherit this error handling
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all usages of virt_launcher_pod property to verify error handling

# Search for methods accessing virt_launcher_pod
ast-grep --pattern 'self.virt_launcher_pod'

# Search for direct property access
rg '\bvirt_launcher_pod\b' -A 2 -B 2

Length of output: 3130


Script:

#!/bin/bash
# Let's examine the implementation of virt_launcher_pod property and its error handling
ast-grep --pattern $'@property\n    def virt_launcher_pod(self):\n        $$$'

# Also check the error handling in the methods that use it
ast-grep --pattern $'def execute_virsh_command(self, $_):\n        $$$'

Length of output: 2396

@vsibirsk
Copy link
Contributor Author

/verified

@myakove myakove merged commit 0693432 into RedHatQE:main Dec 10, 2024
6 checks passed
@vsibirsk vsibirsk deleted the fix-vmi-no-virt-launcher branch December 10, 2024 13:24
@vsibirsk
Copy link
Contributor Author

/cherry-pick v4.16 v4.17

@redhat-qe-bot1
Copy link

Cherry-picked PR fix IndexError if no virt-launcher pod into v4.17

@redhat-qe-bot1
Copy link

Cherry-picked PR fix IndexError if no virt-launcher pod into v4.16

redhat-qe-bot2 pushed a commit that referenced this pull request Dec 10, 2024
Co-authored-by: vsibirsk <57763370+vsibirsk@users.noreply.github.com>
rh-bot-1 pushed a commit that referenced this pull request Dec 10, 2024
Co-authored-by: vsibirsk <57763370+vsibirsk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants