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

KUBECONFIG loading does not match kubectl behavior #2180

Closed
sebhoss opened this issue Jan 17, 2024 · 2 comments · Fixed by #2187
Closed

KUBECONFIG loading does not match kubectl behavior #2180

sebhoss opened this issue Jan 17, 2024 · 2 comments · Fixed by #2187
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug.

Comments

@sebhoss
Copy link

sebhoss commented Jan 17, 2024

What happened (please include outputs or screenshots):

Calling config.load_kube_config() with a KUBECONFIG that uses colon delimited files throws an error:

.venv/lib64/python3.11/site-packages/kubernetes/config/kube_config.py:815: in load_kube_config
    loader = _get_kube_config_loader(
.venv/lib64/python3.11/site-packages/kubernetes/config/kube_config.py:775: in _get_kube_config_loader
    return KubeConfigLoader(
.venv/lib64/python3.11/site-packages/kubernetes/config/kube_config.py:206: in __init__
    self.set_active_context(active_context)
.venv/lib64/python3.11/site-packages/kubernetes/config/kube_config.py:258: in set_active_context
    context_name = self._config['current-context']
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <kubernetes.config.kube_config.ConfigNode object at 0x7fb7e4108f90>, key = 'current-context'

    def __getitem__(self, key):
        v = self.safe_get(key)
        if v is None:
>           raise ConfigException(
                'Invalid kube-config file. Expected key %s in %s'
                % (key, self.name))
E           kubernetes.config.config_exception.ConfigException: Invalid kube-config file. Expected key current-context in /var/home/seb/.config/kube/clusters/kube.example.com.yaml

.venv/lib64/python3.11/site-packages/kubernetes/config/kube_config.py:625: ConfigException

The code that causes this is just:

    @pytest.fixture
    def k8s_client():
        config.load_kube_config()

KUBECONFIG is defined as follows:

echo $KUBECONFIG
/var/home/seb/.config/kube/clusters/kube.example.com.yaml:/var/home/seb/.config/kube/contexts/kube.example.com.yaml:/var/home/seb/.config/kube/users/kube.example.com.yaml

These files define clusters, contexts, and users each on their own.

What you expected to happen:

The load_kube_config call should behave just like kubectl which merges all files first and then attempts to load them and thus does not care about the order of these files.

How to reproduce it (as minimally and precisely as possible):

Create 3 files for cluster, context, and user:

# cluster
apiVersion: v1
kind: Config
clusters:
  - name: kube.example.com
    cluster:
      certificate-authority-data: doesnotmatter
      server: https://kube.example.com:6443
# context
apiVersion: v1
kind: Config
contexts:
  - name: kube.example.com
    context:
      cluster: kube.example.com
      user: kube.example.com
      namespace: kube-system
current-context: kube.example.com
# user
apiVersion: v1
kind: Config
users:
  - name: kube.example.com

Then define KUBECONFIG to something like cluster.yaml:context.yaml:user.yaml.

Anything else we need to know?:

Changing the order of these files so that the context file is at the first position fixes this!

Support for colon separated files was introduced with #636 (?)

Environment:

  • Kubernetes version (kubectl version): v1.29.0
  • OS (e.g., MacOS 10.13.6): Fedora 39
  • Python version (python --version): 3.11.7
  • Python client version (pip list | grep kubernetes): 29.0.0
@sebhoss sebhoss added the kind/bug Categorizes issue or PR as related to a bug. label Jan 17, 2024
@tomplus
Copy link
Member

tomplus commented Jan 31, 2024

@sebhoss Thanks for the detailed issue, I've prepared fix for it.

@roycaihw
Copy link
Member

Thank you @tomplus!
/assign @tomplus

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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants