kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.8k stars 3.27k forks source link

KUBECONFIG loading does not match kubectl behavior #2180

Closed sebhoss closed 9 months ago

sebhoss commented 10 months ago

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 https://github.com/kubernetes-client/python/issues/636 (?)

Environment:

tomplus commented 9 months ago

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

roycaihw commented 9 months ago

Thank you @tomplus! /assign @tomplus