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

Cannot change kube config at runtime #2240

Open
mcreutz opened this issue May 28, 2024 · 10 comments
Open

Cannot change kube config at runtime #2240

mcreutz opened this issue May 28, 2024 · 10 comments
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@mcreutz
Copy link

mcreutz commented May 28, 2024

What happened (please include outputs or screenshots):
For unit testing, I want to change the kubeconfig file at runtime and after the import of the config module. As my tests module imports the productive module, the imports are executed before fixtures and patches are executed. When I then change the value of the KUBECONFIG environment variable, the Kubernetes client ignores that change.

What you expected to happen:
Cluster can be switched at runtime by changing the KUBECONFIG env var.

How to reproduce it (as minimally and precisely as possible):
This is a short example that shows, that changes to the KUBECONFIG environment variable after the import of the configmodule are ignored. Changes to the env var before the import are followed.

export KUBECONFIG="/path/to/first.kubeconfig"
python not_working.py
# not_working.py

import os
from kubernetes import config

config.load_kube_config()
print(config.list_kube_config_contexts())
os.environ["KUBECONFIG"] = "/path/to/second.kubeconfig"
config.load_kube_config()
print(config.list_kube_config_contexts())  # still shows contexts of first.kubeconfig
export KUBECONFIG="/path/to/first.kubeconfig"
python working.py
# working.py

import os

os.environ["KUBECONFIG"] = "/path/to/second.kubeconfig"
from kubernetes import config
config.load_kube_config()
print(config.list_kube_config_contexts())  # shows contexts of second.kubeconfig

Anything else we need to know?:
I've already tried without success:

  • config.load_kube_config() with parameters to set the new kubeconfig path
  • config.load_config(), with and without parameters to set the new kubeconfig path
  • not explicitly loading the kubeconfig

Successful was to run kubectl from Python with subprocess.run(...), that way the updated KUBECONFIG env var is followed. But I need it in this package.

Environment:

  • Kubernetes version (kubectl version):
    Client Version: v1.29.3
    Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
    Server Version: v1.28.3

  • OS (e.g., MacOS 10.13.6): MacOS 14.4.1

  • Python version (python --version): 3.12.3

  • Python client version (pip list | grep kubernetes): 29.0.0

@mcreutz mcreutz added the kind/bug Categorizes issue or PR as related to a bug. label May 28, 2024
@Zerohertz
Copy link

https://github.com/kubernetes-client/python/blob/master/kubernetes/base/config/kube_config.py#L816-L817

def load_kube_config(config_file=None, context=None,
                     client_configuration=None,
                     persist_config=True,
                     temp_file_path=None):
...
    if config_file is None:
        config_file = KUBE_CONFIG_DEFAULT_LOCATION

As you can see here, this appears to be because it does not use the KUBECONFIG environment variable, but rather uses global variables within the library.

@mcreutz
Copy link
Author

mcreutz commented May 28, 2024

https://github.com/kubernetes-client/python/blob/master/kubernetes/base/config/kube_config.py#L816-L817

def load_kube_config(config_file=None, context=None,
                     client_configuration=None,
                     persist_config=True,
                     temp_file_path=None):
...
    if config_file is None:
        config_file = KUBE_CONFIG_DEFAULT_LOCATION

As you can see here, this appears to be because it does not use the KUBECONFIG environment variable, but rather uses global variables within the library.

Thanks for the answer. But from the code I would expect an

config.load_kube_config(config_file="/path/to/second.kubeconfig")

to be working. But that also does not change the config in the client.

@roycaihw
Copy link
Member

I wonder if the default config behavior is related here: https://github.com/kubernetes-client/python/blob/master/CHANGELOG.md#v1201. Could you take a look?

@yliaog
Copy link
Contributor

yliaog commented Jun 5, 2024

/assign @roycaihw

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Sep 3, 2024
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Oct 3, 2024
@glkudinov
Copy link

Hey, guys!
I use this library in my tests. It would be great to fix the problem with change kube config at runtime!

@BadmHaalg
Copy link

BadmHaalg commented Oct 24, 2024

Hello, guys! Do you have any plans to support reset/change config for this client?
Thats`s a critical problems with some scenarios with reset and startup new cluster during one test session.

@aburnasov
Copy link

Hello there! It seems this issue has affected many people. Guys, please add an option to reset/replace the config; it significantly impacts tests when we need to rebuild the cluster. Thanks!

@yliaog
Copy link
Contributor

yliaog commented Oct 25, 2024

could you send a PR? thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

No branches or pull requests

9 participants