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

Commit

Permalink
take care of empty kube_config files
Browse files Browse the repository at this point in the history
  • Loading branch information
MridulS committed Jan 22, 2021
1 parent b002110 commit c9d7169
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ def _load_config_from_file_like_object(self, string):
else:
config = yaml.safe_load(string.read())

if config is None:
raise ConfigException(
'Invalid kube-config.')
if self.config_merged is None:
self.config_merged = copy.deepcopy(config)
# doesn't need to do any further merging
Expand All @@ -699,6 +702,11 @@ def load_config(self, path):
with open(path) as f:
config = yaml.safe_load(f)

if config is None:
raise ConfigException(
'Invalid kube-config. '
'%s file is empty' % path)

if self.config_merged is None:
config_merged = copy.deepcopy(config)
for item in ('clusters', 'contexts', 'users'):
Expand Down
9 changes: 9 additions & 0 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,15 @@ def test_load_kube_config_from_dict(self):
client_configuration=actual)
self.assertEqual(expected, actual)

def test_load_kube_config_from_empty_file(self):
config_file_like_object = io.StringIO()
self.assertRaises(ConfigException, load_kube_config, config_file_like_object)

def test_load_kube_config_from_empty_file_like_object(self):
config_file = self._create_temp_file(
yaml.safe_dump(None))
self.assertRaises(ConfigException, load_kube_config, config_file)

def test_list_kube_config_contexts(self):
config_file = self._create_temp_file(
yaml.safe_dump(self.TEST_KUBE_CONFIG))
Expand Down

0 comments on commit c9d7169

Please sign in to comment.