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

Add proxy support to Kubernetes client configuration in get_client #2291

Conversation

sbahar619
Copy link
Contributor

@sbahar619 sbahar619 commented Jan 26, 2025

Short description:

Add proxy support to Kubernetes client configuration in get_client

More details:

This update modifies the get_client function to support setting a proxy for the Kubernetes client configuration. The client_configuration argument is introduced to allow passing a client.Configuration object directly, and if not provided, it defaults to creating a new configuration instance. Additionally, if the HTTPS_PROXY or HTTP_PROXY environment variables are set, the proxy is applied to the Kubernetes client configuration.

Introduced dynamic proxy enablement by reading the OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY environment variable.
Updated README.md with instructions on using the new proxy enablement feature.

What this PR does / why we need it:

This solution is preferred over requiring the caller to set the client.Configuration for each client instance creation. Doing so would necessitate updating every instance where the client is created, which would be less efficient and error-prone.

Which issue(s) this PR fixes:

https://issues.redhat.com/browse/CNV-46351

Special notes for reviewer:

A similar solution was previously proposed in PR #2066 , but it was not accepted at the time. This PR aims to provide additional reasoning and demonstrate why this approach is the most effective.
This change is especially useful in environments where proxies are required, and it simplifies the client setup process while minimizing the chances of errors.

Note:
We can also promote this upstream PR in kubernetes-client, which is a good solution:
kubernetes-client/python#2182

Bug:

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced Kubernetes client configuration with optional proxy support.
    • Added ability to dynamically enable proxy settings through environment variables.
  • Bug Fixes

    • Implemented error handling for conflicting proxy settings in client configuration.
  • Documentation

    • Introduced a new section in the README for "Proxy Enablement" with instructions for configuring proxy settings.
  • Tests

    • Added tests to verify error handling for conflicting proxy settings.

Copy link

coderabbitai bot commented Jan 26, 2025

Walkthrough

The changes involve modifying the get_client function in the ocp_resources/resource.py file to enhance Kubernetes client configuration capabilities. A new parameter, client_configuration, has been added to manage proxy settings based on the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY. The function now raises a ValueError if required proxy variables are not set when proxy configuration is enabled. Additionally, a new section titled "Proxy Enablement" has been added to the README.md file, providing instructions for enabling proxy configuration. Two new test methods have also been introduced to validate the behavior of the get_client function regarding proxy conflicts.

Changes

File Change Summary
ocp_resources/resource.py - Added client_configuration parameter to get_client function signature
- Enhanced logic for proxy settings based on environment variables
- Updated client instantiation to utilize client_configuration
README.md - Added new section ## Proxy Enablement with instructions for enabling proxy configuration dynamically
tests/test_resources.py - Added methods test_proxy_enabled_but_no_proxy_set and test_proxy_conflict_raises_value_error to test proxy scenarios
- Included necessary import statements for kubernetes
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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. (Beta)
  • @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-bot1
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
  • to assign reviewer to PR use /assign-reviewer @<reviewer>
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

@sbahar619
Copy link
Contributor Author

/verified

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

🧹 Nitpick comments (1)
ocp_resources/resource.py (1)

124-129: LGTM! Proxy configuration implementation is robust.

The implementation correctly handles proxy configuration through environment variables, following standard practices. Consider adding a debug log when no proxy is found to improve observability.

 client_configuration = client_configuration or client.Configuration()
 proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY")
+if not proxy:
+    LOGGER.debug("No proxy configuration found in environment variables")
 
 if proxy:
     LOGGER.info(f"Trying to get client using proxy {proxy}")
     client_configuration.proxy = proxy
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f0771b and 043a0f7.

📒 Files selected for processing (1)
  • ocp_resources/resource.py (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: tox
  • GitHub Check: python-module-install
🔇 Additional comments (2)
ocp_resources/resource.py (2)

83-87: LGTM! Function signature changes are well-structured.

The new client_configuration parameter is properly typed and documented, allowing for flexible client configuration injection.

Also applies to: 103-103


132-134: LGTM! Client creation properly utilizes the configuration.

The updated client creation correctly passes the configuration object to new_client_from_config.

ocp_resources/resource.py Outdated Show resolved Hide resolved
ocp_resources/resource.py Outdated Show resolved Hide resolved
ocp_resources/resource.py Outdated Show resolved Hide resolved
@sbahar619
Copy link
Contributor Author

/verified

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: 1

🧹 Nitpick comments (1)
ocp_resources/resource.py (1)

102-102: Enhance the docstring for better clarity.

The documentation for client_configuration should be more descriptive to help users understand its purpose and usage.

-        client_configuration (kubernetes.client.Configuration): The kubernetes.client.Configuration to set configs to.
+        client_configuration (kubernetes.client.Configuration): Optional custom Kubernetes client configuration.
+            Use this to provide pre-configured settings such as proxy configuration, SSL certificates,
+            API keys, etc. If not provided, a new configuration will be created.
+            Example:
+                config = kubernetes.client.Configuration()
+                config.proxy = "http://proxy.example.com:8080"
+                client = get_client(client_configuration=config)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 043a0f7 and e6d27e6.

📒 Files selected for processing (1)
  • ocp_resources/resource.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: python-module-install
  • GitHub Check: tox
🔇 Additional comments (2)
ocp_resources/resource.py (2)

82-86: LGTM! Function signature changes are well-structured.

The new client_configuration parameter is properly typed and maintains backward compatibility.


131-133: LGTM! Client configuration is properly utilized.

The client_configuration is correctly passed to new_client_from_config, ensuring consistent configuration throughout the client creation process.

ocp_resources/resource.py Outdated Show resolved Hide resolved
ocp_resources/resource.py Outdated Show resolved Hide resolved
@sbahar619 sbahar619 force-pushed the CNV-46351/add-proxy-support-to-k8s-client-configuration branch from e6d27e6 to 791e1b5 Compare January 26, 2025 10:07
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

🧹 Nitpick comments (1)
tests/test_resources.py (1)

108-120: Add documentation and improve readability.

The test implementation is solid, using monkeypatch for safe environment variable handling and flexible error message assertions. However, a few improvements would make it even better:

  1. Add a docstring explaining the test's purpose
  2. Add comments to group the logical sections

Apply this diff to improve documentation and readability:

     def test_proxy_conflict_raises_value_error(self, monkeypatch):
+        """Test that get_client raises ValueError when proxy settings conflict.
+        
+        When both environment variables and client configuration define different proxy
+        settings, get_client should raise a ValueError indicating the conflict.
+        """
+        # Setup proxy via environment variables
         monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1")
         monkeypatch.setenv(name="HTTPS_PROXY", value="http://env-proxy.com")

+        # Create conflicting client configuration
         client_configuration = kubernetes.client.Configuration()
         client_configuration.proxy = "http://not-env-proxy.com"

+        # Verify that ValueError is raised with appropriate message
         with pytest.raises(ValueError) as exc_info:
             get_client(client_configuration=client_configuration)

         assert "Conflicting proxy settings" in str(exc_info.value)
         assert "http://not-env-proxy.com" in str(exc_info.value)
         assert "http://env-proxy.com" in str(exc_info.value)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de9c6a8 and 85965ca.

📒 Files selected for processing (1)
  • tests/test_resources.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: tox
  • GitHub Check: python-module-install
🔇 Additional comments (1)
tests/test_resources.py (1)

3-3: LGTM!

The kubernetes import is correctly placed and necessary for creating the Configuration object in the new test.

@sbahar619
Copy link
Contributor Author

/verified

tests/test_resources.py Outdated Show resolved Hide resolved
ocp_resources/resource.py Show resolved Hide resolved
ocp_resources/resource.py Show resolved Hide resolved
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

🧹 Nitpick comments (1)
tests/test_resources.py (1)

108-115: Add docstring and make error message assertion more maintainable.

Good use of monkeypatch for environment variable management. Consider these improvements:

 def test_proxy_enabled_but_no_proxy_set(self, monkeypatch):
+    """Test that get_client raises ValueError when proxy is enabled but not configured.
+    
+    When OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY is set but neither HTTPS_PROXY
+    nor HTTP_PROXY are defined, get_client should raise a ValueError.
+    """
     monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1")

     with pytest.raises(
         ValueError,
-        match="Proxy configuration is enabled but neither HTTPS_PROXY nor HTTP_PROXY environment variables are set.",
+        match=r".*neither HTTPS_PROXY nor HTTP_PROXY.*",
     ):
         get_client()
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 85965ca and 798ef9e.

📒 Files selected for processing (1)
  • tests/test_resources.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: python-module-install
  • GitHub Check: tox
🔇 Additional comments (2)
tests/test_resources.py (2)

3-3: LGTM!

The kubernetes import is correctly placed and necessary for the new test functionality.


117-129: Improve test structure and maintainability.

Good use of monkeypatch for environment variable management. Consider these improvements:

 def test_proxy_conflict_raises_value_error(self, monkeypatch):
+    """Test that get_client raises ValueError when proxy settings conflict.
+    
+    When both environment variables and client configuration define different proxy
+    settings, get_client should raise a ValueError indicating the conflict.
+    """
+    # Setup environment proxy
     monkeypatch.setenv(name="OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY", value="1")
     monkeypatch.setenv(name="HTTPS_PROXY", value="http://env-proxy.com")

+    # Setup conflicting client proxy
     client_configuration = kubernetes.client.Configuration()
     client_configuration.proxy = "http://not-env-proxy.com"

+    # Verify error is raised with appropriate message
     with pytest.raises(
         ValueError,
-        match="Conflicting proxy settings: client_configuration.proxy=http://not-env-proxy.com, "
-        "but the environment variable 'OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY' defines proxy as http://env-proxy.com.",
+        match=r".*Conflicting proxy settings.*http://not-env-proxy\.com.*http://env-proxy\.com.*",
     ):
         get_client(client_configuration=client_configuration)

@sbahar619
Copy link
Contributor Author

/verified

ocp_resources/resource.py Show resolved Hide resolved
README.md Show resolved Hide resolved
ocp_resources/resource.py Show resolved Hide resolved
@myakove myakove merged commit dfa9792 into RedHatQE:main Jan 30, 2025
6 checks passed
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.

9 participants