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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

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.
If use_proxy is enabled, the client will fetch the proxy URL from HTTPS_PROXY or HTTP_PROXY environment variables.
A ValueError is raised if proxy variables are not set when use_proxy is enabled.
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.
  • Documentation

    • Introduced a new section in the README for "Proxy Enablement" with instructions for configuring 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, use_proxy, of type bool, has been added to control proxy settings via environment variables. The function's logic has been updated to raise a ValueError if required proxy variables are not set when use_proxy is True. Additionally, a new section titled "Proxy Enablement" has been added to the README.md file, providing instructions for enabling proxy configuration.

Changes

File Change Summary
ocp_resources/resource.py - Added use_proxy parameter to get_client function signature
- Enhanced logic for proxy settings based on environment variables
- Updated client instantiation to utilize proxy configuration
README.md - Added new section ## Proxy Enablement with instructions for enabling proxy configuration dynamically

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 84b9023 and 59367f4.

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

82-86: LGTM! Clean function signature update.

The new use_proxy parameter is well-defined with appropriate type hints and default value.


102-103: LGTM! Clear and comprehensive documentation.

The documentation effectively explains both the use_proxy parameter and the OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY environment variable override.


124-135: LGTM! Robust proxy configuration implementation.

The implementation:

  • Correctly handles both use_proxy parameter and environment variable override
  • Properly validates proxy environment variables
  • Creates new configuration only when needed
  • Follows the agreed approach from previous discussions

137-142: LGTM! Clean client creation integration.

The proxy configuration is correctly integrated into the client creation process while maintaining backward compatibility.

✨ 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 (2)
README.md (2)

100-101: Improve formatting consistency of environment variables.

For better readability and consistency with markdown formatting:

-Set the environment variable OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=<any value>
-This enables the proxy without requiring the use_proxy argument when creating the client.
+Set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=<any value>`
+This enables proxy support without requiring the `use_proxy` argument when creating the client.

103-104: Enhance clarity of proxy URL configuration.

For better clarity and consistency:

-Define the HTTPS_PROXY or HTTP_PROXY environment variable for the proxy URL.
-If neither is set, a ValueError will be raised.
+Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL:
+```bash
+export HTTPS_PROXY="http://proxy.example.com:8080"
+# or
+export HTTP_PROXY="http://proxy.example.com:8080"
+```
+If neither variable is set when proxy is enabled, a `ValueError` will be raised.
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 90d5bbb and 595cea7.

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

96-105: LGTM! The proxy configuration section is well-structured.

The section clearly explains how to enable and configure proxy support for the client.

@sbahar619
Copy link
Contributor Author

/verified

@rh-bot-1 rh-bot-1 removed the verified label Jan 27, 2025
@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 (3)
README.md (3)

96-110: LGTM! Consider adding more details for better user experience.

The proxy configuration documentation is clear and well-structured. Consider these enhancements:

  1. Add a Python code example showing the use_proxy=True argument usage
  2. Specify the order of precedence between HTTPS_PROXY and HTTP_PROXY
  3. Include a troubleshooting section for common proxy issues

Example additions:

### Usage Example
```python
from ocp_resources.resource import get_client

# Enable proxy via argument
client = get_client(use_proxy=True)

Proxy Precedence

When both proxy variables are set, HTTPS_PROXY takes precedence over HTTP_PROXY.

Troubleshooting

  • Ensure proxy URL is correctly formatted (e.g., includes protocol)
  • Verify proxy server is accessible
  • Check proxy authentication if required

---

`100-101`: **Improve environment variable documentation format.**

The environment variable documentation could be clearer with consistent formatting and explicit value examples.

```diff
-1. Set the `use_proxy` argument to `True` when creating the client or set the environment variable `OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY=<any value>`
-This enables the proxy without requiring the `use_proxy` argument when creating the client.
+1. Set the `use_proxy` argument to `True` when creating the client or set the environment variable:
+```bash
+export OPENSHIFT_PYTHON_WRAPPER_CLIENT_USE_PROXY="true"  # Any non-empty value works
+```
+This enables the proxy without requiring the `use_proxy` argument when creating the client.

103-109: Add proxy authentication and HTTPS details.

The proxy URL examples could benefit from additional details about authentication and HTTPS support.

 2. Define either `HTTPS_PROXY` or `HTTP_PROXY` environment variable with your proxy URL:
 ```bash
 export HTTPS_PROXY="http://proxy.example.com:8080"
 # or
 export HTTP_PROXY="http://proxy.example.com:8080"
+# With authentication:
+export HTTPS_PROXY="http://username:[email protected]:8080"
+# For HTTPS proxy:
+export HTTPS_PROXY="https://proxy.example.com:8443"

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

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

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 595cea710827aeb43f983fd17ffdf708e34edf0b and 84b9023cc47ce527bc9b29a785983924e22320eb.

</details>

<details>
<summary>📒 Files selected for processing (1)</summary>

* `README.md` (1 hunks)

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (2)</summary>

* GitHub Check: python-module-install
* GitHub Check: tox

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@sbahar619
Copy link
Contributor Author

/verified

@@ -115,8 +121,25 @@ def get_client(
# If `KUBECONFIG` environment variable is set via code, the `KUBE_CONFIG_DEFAULT_LOCATION` will be None since
# is populated during import which comes before setting the variable in code.
config_file = config_file or os.environ.get("KUBECONFIG", "~/.kube/config")
client_configuration = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

client_configuration can be passed via kwargs , this will overwrite what the user sends.
set only if not passed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, done.

@sbahar619
Copy link
Contributor Author

/verified

@sbahar619
Copy link
Contributor Author

/verified

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.

7 participants