Skip to content

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

25 changes: 20 additions & 5 deletions ocp_resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

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

User will give as configured client_configuration and we just use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Please consider keeping the proxy to be set additionally as environment variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:
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
Expand Down