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 all 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
25 changes: 20 additions & 5 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,19 +667,34 @@ def __init__(self, paths):
self.paths = []
self.config_files = {}
self.config_merged = None
if hasattr(paths, 'read'):
self._load_config_from_file_like_object(paths)
else:
self._load_config_from_file_path(paths)

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

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

for path in paths.split(ENV_KUBECONFIG_PATH_SEPARATOR):
if self.config_merged is None:
self.config_merged = copy.deepcopy(config)
# doesn't need to do any further merging

def _load_config_from_file_path(self, string):
for path in string.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)

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

def load_config(self, path):
with open(path) as f:
config = yaml.safe_load(f)
Expand Down
27 changes: 25 additions & 2 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import base64
import datetime
import io
import json
import os
import shutil
Expand Down Expand Up @@ -1247,7 +1248,7 @@ def test_ssl_with_relative_ssl_files(self):
finally:
shutil.rmtree(temp_dir)

def test_load_kube_config(self):
def test_load_kube_config_from_file_path(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
config_file = self._create_temp_file(
Expand All @@ -1257,10 +1258,32 @@ def test_load_kube_config(self):
client_configuration=actual)
self.assertEqual(expected, actual)

def test_load_kube_config_from_dict(self):
def test_load_kube_config_from_file_like_object(self):
expected = FakeConfig(host=TEST_HOST,
token=BEARER_TOKEN_FORMAT % TEST_DATA_BASE64)
config_file_like_object = io.StringIO()
# py3 (won't have unicode) vs py2 (requires it)
try:
unicode('')
config_file_like_object.write(
unicode(
yaml.safe_dump(
self.TEST_KUBE_CONFIG),
errors='replace'))
except NameError:
config_file_like_object.write(
yaml.safe_dump(
self.TEST_KUBE_CONFIG))
actual = FakeConfig()
load_kube_config(
config_file=config_file_like_object,
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)
actual = FakeConfig()
load_kube_config_from_dict(config_dict=self.TEST_KUBE_CONFIG,
context="simple_token",
Expand Down