Skip to content

Lazy load pull secret#32

Merged
carbonin merged 1 commit intoopenshift-assisted:masterfrom
carbonin:lazy-load-pull-secret
Jul 16, 2025
Merged

Lazy load pull secret#32
carbonin merged 1 commit intoopenshift-assisted:masterfrom
carbonin:lazy-load-pull-secret

Conversation

@carbonin
Copy link
Copy Markdown
Collaborator

@carbonin carbonin commented Jul 15, 2025

Previously we were querying OCM for the pull secret on every tool call but it's only needed when we create clusters or infraenvs. This commit changes the service client so that it only queries for the pull secret when it is required

Summary by CodeRabbit

  • Refactor

    • Improved how pull secret retrieval is handled, making it load only when needed rather than during initialization.
  • Tests

    • Updated tests to explicitly trigger and verify the new lazy loading behavior of the pull secret.
    • Enhanced test reliability by consistently mocking pull secret retrieval during cluster and environment creation tests.

Previously we were querying OCM for the pull secret on every tool call
but it's only needed when we create clusters or infraenvs. This commit
changes the service client so that it only queries for the pull secret
when it is required
@openshift-ci
Copy link
Copy Markdown

openshift-ci Bot commented Jul 15, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: carbonin

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jul 15, 2025

Walkthrough

The InventoryClient class was updated to lazily retrieve and cache the pull secret only when it is first accessed, rather than during initialization. Corresponding unit tests were modified to explicitly trigger and assert the lazy loading behavior, and to consistently mock pull secret retrieval in relevant test cases.

Changes

File(s) Change Summary
service_client/assisted_service_api.py Refactored InventoryClient to lazily retrieve and cache the pull secret via a new property.
tests/test_assisted_service_api.py Updated tests to explicitly trigger lazy loading and mock pull secret retrieval for consistency.

Poem

In the warren, secrets wait,
Not fetched too soon, nor left to fate.
Now when the pull secret’s sought,
It hops in, fresh—not overwrought!
Tests nibble gently, mocking the way,
As lazy loading saves the day.
🥕✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@openshift-ci openshift-ci Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 15, 2025
Copy link
Copy Markdown

@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

🧹 Nitpick comments (1)
service_client/assisted_service_api.py (1)

43-48: Consider thread safety for the lazy loading implementation.

The lazy loading property implementation is functionally correct but not thread-safe. If multiple threads access pull_secret simultaneously, they might both see _pull_secret as None and both call _get_pull_secret(), potentially causing duplicate requests or race conditions.

For single-threaded usage, this implementation works well. If thread safety is required, consider using a lock or threading.Lock() to synchronize access.

If thread safety is needed, here's a thread-safe implementation:

+import threading
+
 class InventoryClient:
     def __init__(self, access_token: str):
         self.access_token = access_token
         self._pull_secret: Optional[str] = None
+        self._pull_secret_lock = threading.Lock()
         # ... rest of init
 
     @property
     def pull_secret(self) -> str:
-        if self._pull_secret is None:
-            self._pull_secret = self._get_pull_secret()
+        if self._pull_secret is None:
+            with self._pull_secret_lock:
+                if self._pull_secret is None:  # Double-check locking
+                    self._pull_secret = self._get_pull_secret()
         return self._pull_secret
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 55443bb and 0dfbae5.

📒 Files selected for processing (2)
  • service_client/assisted_service_api.py (1 hunks)
  • tests/test_assisted_service_api.py (7 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Red Hat Konflux / assisted-service-mcp-saas-main-on-pull-request
🔇 Additional comments (9)
service_client/assisted_service_api.py (3)

37-37: LGTM: Proper initialization for lazy loading.

The private attribute initialization correctly supports the lazy loading pattern by deferring pull secret retrieval until first access.


324-324: LGTM: Correct usage of lazy-loaded pull secret.

The pull secret is now accessed via the property, which will trigger lazy loading only when needed during cluster creation.


366-366: LGTM: Correct usage of lazy-loaded pull secret.

The pull secret is now accessed via the property, which will trigger lazy loading only when needed during infrastructure environment creation.

tests/test_assisted_service_api.py (6)

82-84: LGTM: Proper test adaptation for lazy loading.

The test correctly triggers lazy loading by accessing the pull_secret property and storing the result before assertion. This ensures the lazy loading behavior is properly tested.

Also applies to: 90-90


99-99: LGTM: Correct exception testing for lazy loading.

The test properly creates the client first and then accesses the pull_secret property to trigger the exception during lazy loading, which accurately reflects the new behavior.

Also applies to: 103-103


116-116: LGTM: Consistent lazy loading test pattern.

The test follows the same correct pattern of creating the client first, then accessing the property to trigger lazy loading with the custom URL.

Also applies to: 119-119


368-371: LGTM: Comprehensive mocking for lazy loading.

The test correctly patches both _installer_api and _get_pull_secret methods to ensure consistent mocking during cluster creation. The assertion properly expects the mocked pull secret value.

Also applies to: 387-387


401-404: LGTM: Consistent mocking pattern for single node cluster test.

The test applies the same correct mocking pattern for both API methods, ensuring consistent behavior for single node cluster creation tests.


428-431: LGTM: Proper mocking for infrastructure environment creation.

The test correctly patches both required methods to ensure consistent mocking during infrastructure environment creation, maintaining the same pattern as the cluster creation tests.

@carbonin carbonin merged commit c9c569c into openshift-assisted:master Jul 16, 2025
12 checks passed
@carbonin carbonin deleted the lazy-load-pull-secret branch July 16, 2025 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant