-
Notifications
You must be signed in to change notification settings - Fork 63
Add proxy support to Kubernetes client configuration in get_client #2291
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
WalkthroughThe changes involve modifying the Changes
✨ Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
Report bugs in Issues The following are automatically added:
Available user actions:
Supported /retest check runs
Supported labels
|
/verified |
There was a problem hiding this 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
📒 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
.
043a0f7
to
e6d27e6
Compare
/verified |
There was a problem hiding this 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
📒 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 tonew_client_from_config
, ensuring consistent configuration throughout the client creation process.
e6d27e6
to
791e1b5
Compare
/verified |
There was a problem hiding this 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
📒 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)
/verified |
…2291) * Add proxy support to Kubernetes client configuration in get_client 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. This enhancement enables easier integration of Kubernetes client setup when working behind a proxy. Signed-off-by: Shahaf Bahar <sbahar@redhat.com> * Simplify docstring by removing redundant default value explanation * Update proxy configuration logic and README documentation * Simplify use_proxy docstring in get_client method * Add support for enabling proxy with use_proxy flag * Remove client_configuration from get_client and initialize only for proxy usage Signed-off-by: Shahaf Bahar <sbahar@redhat.com> * Refactor proxy handling in get_client function * Refactor docstring to clarify proxy behavior * Simplify proxy configuration logic in get_client * Improve error message for missing proxy configuration in get_client * Remove redundant dot in README * Remove unnecessary check for client_configuration in get_client * Update README to clarify proxy enablement instructions * Clarify proxy enablement in README * Clarify use_proxy argument type in README * Retrieve client_configuration from kwargs if provided * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Ensure environment variable overrides use_proxy * Remove redundant proxy configuration log * Preserve client_configuration from kwargs when setting proxy * Update docstring to clarify proxy conf behavior with env var * Refactor proxy configuration logic for clarity * Update README with proxy enablement details * Refactor client_configuration retrieval in get_client * Refactor proxy configuration in get_client for improved clarity * Refactor use_proxy argument in get_client to support None as default value * Remove use_proxy argument from get_client and update README * Ensure proxy settings are always applied when enabled * Check for proxy conflicts in client_configuration and environment variable * Add test to verify proxy conflict raises ValueError for mismatched proxy settings * Refactor test for proxy conflict in get_client method Signed-off-by: Shahaf Bahar <sbahar@redhat.com> * Refactor test for proxy conflict to use pytest.raises match exp * Add test for proxy enabled without proxy env variables set --------- Signed-off-by: Shahaf Bahar <sbahar@redhat.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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
Bug Fixes
Documentation
Tests