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

Support file-ish objects in config loading #208

Merged
merged 7 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import json
import logging
import os
import io
import platform
import subprocess
import tempfile
Expand Down Expand Up @@ -667,19 +668,31 @@ def __init__(self, paths):
self.paths = []
self.config_files = {}
self.config_merged = None

for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
if path:
path = os.path.expanduser(path)
if os.path.exists(path):
self.paths.append(path)
self.load_config(path)
self.config_saved = copy.deepcopy(self.config_files)
if hasattr(paths, 'read'):
self.load_config_from_fileish(paths)
else:
for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
if path:
path = os.path.expanduser(path)
if os.path.exists(path):
self.paths.append(path)
self.load_config(path)
self.config_saved = copy.deepcopy(self.config_files)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about pushing the code path for paths to a separate internal function (say _load_config_from_file_path) as well? I think readability will increase that way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this as well, will get this in shortly as well!


@property
def config(self):
return self.config_merged

def load_config_from_fileish(self, string):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does renaming the method to _load_config_from_file_like_object sound? (_ since this is not supposed to be used externally)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a great point, will get that in shortly.

if hasattr(string, 'getvalue'):
config = yaml.safe_load(string.getvalue())
else:
config = yaml.safe_load(string.read())

if self.config_merged is None:
self.config_merged = copy.deepcopy(config)
# doesn't need to do any further merging

def load_config(self, path):
with open(path) as f:
config = yaml.safe_load(f)
Expand Down
11 changes: 11 additions & 0 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime
import json
import os
import io
import shutil
import tempfile
import unittest
Expand Down Expand Up @@ -1257,6 +1258,15 @@ def test_load_kube_config(self):
client_configuration=actual)
self.assertEqual(expected, actual)

def test_load_kube_config_from_fileish(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
config_fileish = io.StringIO()
config_fileish.write(yaml.safe_dump(self.TEST_KUBE_CONFIG))
actual = FakeConfig()
load_kube_config(config_file=config_fileish, context="simple_token", client_configuration=actual)
self.assertEqual(expected, actual)

def test_load_kube_config_from_dict(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
Expand Down Expand Up @@ -1660,6 +1670,7 @@ def test_new_client_from_config(self):
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.configuration.api_key['authorization'])


def test_save_changes(self):
kubeconfigs = self._create_multi_config()

Expand Down