mnubo / kubernetes-py

A python module for Kubernetes.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

K8sConfig() KeyError when optional "preferences" section is omitted from k8s config

gitkodak opened this issue · comments

I'm not sure if this is a K8sConfig problem, a rancher problem or a k8s documentation problem.

Rancher (2.x) generates a config file without a "preferences:" section. When trying to use K8sConfig() we get an error due to that key not existing. This is probably easily fixable by using the .get method on the yaml dict with an empty default instead of referencing it directly.

prefs = yaml.get('preferences', '')

It probably wouldn't be a bad idea to do this for everything that's optional -- if we can figure out what that is.

I can't find anything about what's actually required in a k8s config file (neither of the words "required" nor "optional" appear on the documentation page for kubeconfig.) Considering that k8s itself operates just fine without a preferences section then I suspect it safely falls in the "optional" category. If that's the case then K8sConfig() needs to be OK with it being omitted from the config file.

Python 2.7.15 (default, May 15 2018, 15:37:31) 
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from kubernetes import K8sConfig
>>> K8sConfig()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/kubernetes/K8sConfig.py", line 65, in __init__
    self._init_with_defaults()
  File "/usr/lib/python2.7/site-packages/kubernetes/K8sConfig.py", line 140, in _init_with_defaults
    self._read_config(filename=kubeconfig)
  File "/usr/lib/python2.7/site-packages/kubernetes/K8sConfig.py", line 186, in _read_config
    self.preferences = dotconf['preferences']
KeyError: 'preferences'

Yup:

<         self.preferences = dotconf['preferences']
---
>         self.preferences = dotconf.get('preferences', '')
Python 2.7.15 (default, May 15 2018, 15:37:31) 
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from kubernetes import K8sConfig
>>> K8sConfig()
<kubernetes.K8sConfig.K8sConfig object at 0x7f545a57ac50>
>>> K8sConfig().preferences
''
>>>