-
Notifications
You must be signed in to change notification settings - Fork 55
Add support to set client with cluster proxy #2066
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
Changes from all commits
a055619
aeaa340
b8934d9
b279107
7c61687
dc55809
114b02a
dedada6
0ef82fb
3109d00
500ee4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from typing import Optional, Any, Dict, List | ||
|
||
import kubernetes | ||
from kubernetes import client | ||
from kubernetes.dynamic import DynamicClient, ResourceInstance | ||
import yaml | ||
from benedict import benedict | ||
|
@@ -78,7 +79,11 @@ def _get_api_version(dyn_client: DynamicClient, api_group: str, kind: str) -> st | |
|
||
|
||
def get_client( | ||
config_file: str = "", config_dict: Dict[str, Any] | None = None, context: str = "", **kwargs: Any | ||
config_file: str = "", | ||
config_dict: Dict[str, Any] | None = None, | ||
client_configuration: client.Configuration | None = None, | ||
context: str = "", | ||
**kwargs: Any, | ||
) -> DynamicClient: | ||
""" | ||
Get a kubernetes client. | ||
|
@@ -94,6 +99,7 @@ def get_client( | |
Args: | ||
config_file (str): path to a kubeconfig file. | ||
config_dict (dict): dict with kubeconfig configuration. | ||
client_configuration: The kubernetes.client.Configuration to set configs to. | ||
context (str): name of the context to use. | ||
|
||
Returns: | ||
|
@@ -107,15 +113,24 @@ def get_client( | |
) | ||
) | ||
try: | ||
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py | ||
LOGGER.info("Trying to get client via new_client_from_config") | ||
|
||
# kubernetes.config.kube_config.load_kube_config sets KUBE_CONFIG_DEFAULT_LOCATION during module import. | ||
# 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 = client_configuration or client.Configuration() | ||
proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") | ||
sbahar619 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User will give as configured There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will make the implementation very complicated on the caller side if we omit this important option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add logic to override the proxy part if it's already being configured in the client_configuration regardless of being set by environment variable. |
||
|
||
if proxy: | ||
sbahar619 marked this conversation as resolved.
Show resolved
Hide resolved
sbahar619 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LOGGER.info(f"Trying to get client using proxy {proxy}") | ||
client_configuration.proxy = proxy | ||
|
||
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py | ||
LOGGER.info("Trying to get client via new_client_from_config") | ||
|
||
return kubernetes.dynamic.DynamicClient( | ||
client=kubernetes.config.new_client_from_config(config_file=config_file, context=context or None, **kwargs) | ||
client=kubernetes.config.new_client_from_config( | ||
config_file=config_file, client_configuration=client_configuration, context=context or None, **kwargs | ||
) | ||
) | ||
except MaxRetryError: | ||
# Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py | ||
|
Uh oh!
There was an error while loading. Please reload this page.