mnubo / kubernetes-py

A python module for Kubernetes.
Apache License 2.0
123 stars 46 forks source link

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

Closed gitkodak closed 6 years ago

gitkodak commented 6 years ago

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'
gitkodak commented 6 years ago

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
''
>>>