-
Couldn't load subscription status.
- Fork 67
fix: incluster config handling #2540
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -35,7 +35,6 @@ | |
| TimeoutSampler, | ||
| TimeoutWatch, | ||
| ) | ||
| from urllib3.exceptions import MaxRetryError | ||
|
|
||
| from fake_kubernetes_client.dynamic_client import FakeDynamicClient | ||
| from ocp_resources.event import Event | ||
|
|
@@ -204,16 +203,20 @@ def get_client( | |
| token: str | None = None, | ||
| fake: bool = False, | ||
| ) -> DynamicClient | FakeDynamicClient: | ||
| """ | ||
| Get a kubernetes client. | ||
| """Get a kubernetes client. | ||
|
|
||
| Valid combinations for arguments include: | ||
| - config file (kubeconfig path) | ||
| - config_dict | ||
| - username, password and host (uses basic auth) | ||
| - host and token (bearer token) | ||
| - client_configuration | ||
|
|
||
| This function is a replica of `ocp_utilities.infra.get_client` which cannot be imported as ocp_utilities imports | ||
| from ocp_resources. | ||
| If none of the above arguments are provided: | ||
| - if the `KUBECONFIG` env var is set: it will be used to configure the client | ||
| - if not set and `~/.kube/config` exists, it will be used to configure the client | ||
|
|
||
| Pass either config_file or config_dict. | ||
| If none of them are passed, client will be created from default OS kubeconfig | ||
| (environment variable or .kube folder). | ||
| If no configuration files are available, incluster config loading will be attempted | ||
|
|
||
| Args: | ||
| config_file (str): path to a kubeconfig file. | ||
|
|
@@ -264,34 +267,38 @@ def get_client( | |
| persist_config=persist_config, | ||
| temp_file_path=temp_file_path, | ||
| ) | ||
| else: | ||
| # 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") | ||
|
|
||
| elif config_file or ("KUBECONFIG" in os.environ) or os.path.exists(os.path.expanduser("~/.kube/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") | ||
| # If `KUBECONFIG` environment variable is set via code, the `KUBE_CONFIG_DEFAULT_LOCATION` will not be updated | ||
| # to reflect the new value since it's set to at import-time. | ||
|
|
||
| # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/__init__.py | ||
| if not config_file: | ||
| config_file = os.environ.get("KUBECONFIG", "~/.kube/config") | ||
| LOGGER.info("Trying to get client via new_client_from_config config_file=%s", config_file) | ||
|
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. Please use f-string and not 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. This is the correct way to pass argument to logging statements: formatting of message arguments is deferred until it cannot be avoided. https://docs.python.org/3/howto/logging.html#optimization |
||
|
|
||
| _client = kubernetes.config.new_client_from_config( | ||
| config_file=config_file, | ||
| context=context, | ||
| client_configuration=client_configuration, | ||
| persist_config=persist_config, | ||
| ) | ||
| elif all( | ||
|
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. Any reason why not query 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. This is supposed to be a minimal check to assess whether this is running in a k8s environment, where If these are set, there's a fairly good chance an incluster configuration is available, so we call |
||
| var in os.environ | ||
| for var in ( | ||
| kubernetes.config.incluster_config.SERVICE_HOST_ENV_NAME, | ||
| kubernetes.config.incluster_config.SERVICE_PORT_ENV_NAME, | ||
| ) | ||
| ): | ||
| LOGGER.info("Trying to get client via incluster_config") | ||
|
|
||
| # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py | ||
| kubernetes.config.load_incluster_config(client_configuration) | ||
| _client = kubernetes.client.ApiClient(client_configuration) | ||
|
|
||
| kubernetes.client.Configuration.set_default(default=client_configuration) | ||
|
|
||
| try: | ||
| return kubernetes.dynamic.DynamicClient(client=_client) | ||
| except MaxRetryError: | ||
| # Ref: https://github.com/kubernetes-client/python/blob/v26.1.0/kubernetes/base/config/incluster_config.py | ||
| LOGGER.info("Trying to get client via incluster_config") | ||
| return kubernetes.dynamic.DynamicClient( | ||
| client=kubernetes.config.incluster_config.load_incluster_config( | ||
| client_configuration=client_configuration, try_refresh_token=try_refresh_token | ||
| ), | ||
| ) | ||
| return kubernetes.dynamic.DynamicClient(client=_client) | ||
|
|
||
|
|
||
| def sub_resource_level(current_class: Any, owner_class: Any, parent_class: Any) -> str | None: | ||
|
|
||
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.
("KUBECONFIG" in os.environ)this is not a good check, this will be true forTEST_KUBECONFIGinos.environ.Use
os.environ.get("KUBECONFIG")and save it since you need to use it later.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.
TEST_KUBECONFIGas an environment variable? In that case"KUBECONFIG" in os.environwill not be true,os.environis a dict and will not have the"KUBECONFIG"key.The idea here is to go through this path only if:
config_fileis passed by the user explicitlyKUBECONFIGenvironment variable (so the key will be inos.environ)