From 7adf7e280d5b52c736c20832b3d1556bda5285b1 Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Mon, 5 Jun 2017 12:47:05 +0200 Subject: [PATCH] Use no user when the users section is missing in kubeconfig Fixes kubernetes-incubator/client-python#236 --- config/kube_config.py | 3 ++- config/kube_config_test.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/config/kube_config.py b/config/kube_config.py index b0ddeaa6a..eaa2e460c 100644 --- a/config/kube_config.py +++ b/config/kube_config.py @@ -130,7 +130,8 @@ class KubeConfigLoader(object): context_name = self._config['current-context'] self._current_context = self._config['contexts'].get_with_name( context_name) - if self._current_context['context'].safe_get('user'): + if (self._current_context['context'].safe_get('user') + and self._config.safe_get('users')): self._user = self._config['users'].get_with_name( self._current_context['context']['user'])['user'] else: diff --git a/config/kube_config_test.py b/config/kube_config_test.py index 6784b75b6..fd83466f2 100644 --- a/config/kube_config_test.py +++ b/config/kube_config_test.py @@ -615,6 +615,17 @@ class TestKubeConfigLoader(BaseTestCase): self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64, client.config.api_key['authorization']) + def test_no_users_section(self): + expected = FakeConfig(host=TEST_HOST) + actual = FakeConfig() + test_kube_config = self.TEST_KUBE_CONFIG.copy() + del test_kube_config['users'] + KubeConfigLoader( + config_dict=test_kube_config, + active_context="gcp", + client_configuration=actual).load_and_set() + self.assertEqual(expected, actual) + if __name__ == '__main__': unittest.main()