Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 6f9322d

Browse files
author
Sergi Almacellas Abellana
committed
Use no user when the especified user is not found in the users section
1 parent 7adf7e2 commit 6f9322d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

config/kube_config.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ def set_active_context(self, context_name=None):
132132
context_name)
133133
if (self._current_context['context'].safe_get('user')
134134
and self._config.safe_get('users')):
135-
self._user = self._config['users'].get_with_name(
136-
self._current_context['context']['user'])['user']
135+
user = self._config['users'].get_with_name(
136+
self._current_context['context']['user'], safe=True)
137+
if user:
138+
self._user = user['user']
139+
else:
140+
self._user = None
137141
else:
138142
self._user = None
139143
self._cluster = self._config['clusters'].get_with_name(
@@ -257,7 +261,7 @@ def __getitem__(self, key):
257261
else:
258262
return v
259263

260-
def get_with_name(self, name):
264+
def get_with_name(self, name, safe=False):
261265
if not isinstance(self.value, list):
262266
raise ConfigException(
263267
'Invalid kube-config file. Expected %s to be a list'
@@ -270,6 +274,8 @@ def get_with_name(self, name):
270274
% self.name)
271275
if v['name'] == name:
272276
return ConfigNode('%s[name=%s]' % (self.name, name), v)
277+
if safe:
278+
return None
273279
raise ConfigException(
274280
'Invalid kube-config file. '
275281
'Expected object with name %s in %s list' % (name, self.name))

config/kube_config_test.py

+16
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,13 @@ class TestKubeConfigLoader(BaseTestCase):
339339
"user": "ssl-local-file"
340340
}
341341
},
342+
{
343+
"name": "non_existing_user",
344+
"context": {
345+
"cluster": "default",
346+
"user": "non_existing_user"
347+
}
348+
},
342349
],
343350
"clusters": [
344351
{
@@ -626,6 +633,15 @@ def test_no_users_section(self):
626633
client_configuration=actual).load_and_set()
627634
self.assertEqual(expected, actual)
628635

636+
def test_non_existing_user(self):
637+
expected = FakeConfig(host=TEST_HOST)
638+
actual = FakeConfig()
639+
KubeConfigLoader(
640+
config_dict=self.TEST_KUBE_CONFIG,
641+
active_context="non_existing_user",
642+
client_configuration=actual).load_and_set()
643+
self.assertEqual(expected, actual)
644+
629645

630646
if __name__ == '__main__':
631647
unittest.main()

0 commit comments

Comments
 (0)