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

Commit

Permalink
Change utility functions to new set_default Configuration model, prep…
Browse files Browse the repository at this point in the history
…aring to use swagger-codegen HEAD
  • Loading branch information
mbohlool committed Oct 9, 2017
1 parent 31e13b1 commit b7a9f4a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 1,328 deletions.
657 changes: 0 additions & 657 deletions api_client.py

This file was deleted.

4 changes: 3 additions & 1 deletion config/incluster_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import os

from kubernetes.client import configuration
from kubernetes.client import Configuration

from .config_exception import ConfigException

Expand Down Expand Up @@ -77,9 +77,11 @@ def _load_config(self):
self.ssl_ca_cert = self._cert_filename

def _set_config(self):
configuration = Configuration()
configuration.host = self.host
configuration.ssl_ca_cert = self.ssl_ca_cert
configuration.api_key['authorization'] = "bearer " + self.token
Configuration.set_default(configuration)


def load_incluster_config():
Expand Down
33 changes: 18 additions & 15 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import urllib3
import yaml

from kubernetes.client import ApiClient, ConfigurationObject, configuration
from kubernetes.client import ApiClient, Configuration

from .config_exception import ConfigException
from .dateutil import UTC, format_rfc3339, parse_rfc3339
Expand Down Expand Up @@ -118,7 +118,6 @@ class KubeConfigLoader(object):

def __init__(self, config_dict, active_context=None,
get_google_credentials=None,
client_configuration=configuration,
config_base_path="",
config_persister=None):
self._config = ConfigNode('kube-config', config_dict)
Expand All @@ -139,7 +138,6 @@ def _refresh_credentials():
self._get_google_credentials = get_google_credentials
else:
self._get_google_credentials = _refresh_credentials
self._client_configuration = client_configuration

def set_active_context(self, context_name=None):
if context_name is None:
Expand Down Expand Up @@ -240,19 +238,19 @@ def _load_cluster_info(self):
if 'insecure-skip-tls-verify' in self._cluster:
self.verify_ssl = not self._cluster['insecure-skip-tls-verify']

def _set_config(self):
def _set_config(self, client_configuration):
if 'token' in self.__dict__:
self._client_configuration.api_key['authorization'] = self.token
client_configuration.api_key['authorization'] = self.token
# copy these keys directly from self to configuration object
keys = ['host', 'ssl_ca_cert', 'cert_file', 'key_file', 'verify_ssl']
for key in keys:
if key in self.__dict__:
setattr(self._client_configuration, key, getattr(self, key))
setattr(client_configuration, key, getattr(self, key))

def load_and_set(self):
def load_and_set(self, client_configuration):
self._load_authentication()
self._load_cluster_info()
self._set_config()
self._set_config(client_configuration)

def list_contexts(self):
return [context.value for context in self._config['contexts']]
Expand Down Expand Up @@ -331,15 +329,15 @@ def list_kube_config_contexts(config_file=None):


def load_kube_config(config_file=None, context=None,
client_configuration=configuration,
client_configuration=None,
persist_config=True):
"""Loads authentication and cluster information from kube-config file
and stores them in kubernetes.client.configuration.
:param config_file: Name of the kube-config file.
:param context: set the active context. If is set to None, current_context
from config file will be used.
:param client_configuration: The kubernetes.client.ConfigurationObject to
:param client_configuration: The kubernetes.client.Configuration to
set configs to.
:param persist_config: If True, config file will be updated when changed
(e.g GCP token refresh).
Expand All @@ -355,10 +353,15 @@ def _save_kube_config(config_map):
yaml.safe_dump(config_map, f, default_flow_style=False)
config_persister = _save_kube_config

_get_kube_config_loader_for_yaml_file(
loader = _get_kube_config_loader_for_yaml_file(
config_file, active_context=context,
client_configuration=client_configuration,
config_persister=config_persister).load_and_set()
config_persister=config_persister)
if client_configuration is None:
config = type.__call__(Configuration)
loader.load_and_set(config)
Configuration.set_default(config)
else:
loader.load_and_set(client_configuration)


def new_client_from_config(
Expand All @@ -368,8 +371,8 @@ def new_client_from_config(
"""Loads configuration the same as load_kube_config but returns an ApiClient
to be used with any API object. This will allow the caller to concurrently
talk with multiple clusters."""
client_config = ConfigurationObject()
client_config = type.__call__(Configuration)
load_kube_config(config_file=config_file, context=context,
client_configuration=client_config,
persist_config=persist_config)
return ApiClient(config=client_config)
return ApiClient(configuration=client_config)
44 changes: 18 additions & 26 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def _create_temp_file(self, content=""):
os.close(handler)
return name

def expect_exception(self, func, message_part):
def expect_exception(self, func, message_part, *args, **kwargs):
with self.assertRaises(ConfigException) as context:
func()
func(*args, **kwargs)
self.assertIn(message_part, str(context.exception))


Expand Down Expand Up @@ -473,8 +473,7 @@ def test_no_user_context(self):
actual = FakeConfig()
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="no_user",
client_configuration=actual).load_and_set()
active_context="no_user").load_and_set(actual)
self.assertEqual(expected, actual)

def test_simple_token(self):
Expand All @@ -483,8 +482,7 @@ def test_simple_token(self):
actual = FakeConfig()
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="simple_token",
client_configuration=actual).load_and_set()
active_context="simple_token").load_and_set(actual)
self.assertEqual(expected, actual)

def test_load_user_token(self):
Expand All @@ -502,9 +500,8 @@ def test_gcp_no_refresh(self):
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="gcp",
client_configuration=actual,
get_google_credentials=lambda: _raise_exception(
"SHOULD NOT BE CALLED")).load_and_set()
"SHOULD NOT BE CALLED")).load_and_set(actual)
self.assertEqual(expected, actual)

def test_load_gcp_token_no_refresh(self):
Expand Down Expand Up @@ -536,8 +533,7 @@ def test_user_pass(self):
actual = FakeConfig()
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="user_pass",
client_configuration=actual).load_and_set()
active_context="user_pass").load_and_set(actual)
self.assertEqual(expected, actual)

def test_load_user_pass_token(self):
Expand All @@ -548,12 +544,13 @@ def test_load_user_pass_token(self):
self.assertEqual(TEST_BASIC_TOKEN, loader.token)

def test_ssl_no_cert_files(self):
actual = FakeConfig()
loader = KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="ssl-no_file",
client_configuration=actual)
self.expect_exception(loader.load_and_set, "does not exists")
active_context="ssl-no_file")
self.expect_exception(
loader.load_and_set,
"does not exists",
FakeConfig())

def test_ssl(self):
expected = FakeConfig(
Expand All @@ -566,8 +563,7 @@ def test_ssl(self):
actual = FakeConfig()
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="ssl",
client_configuration=actual).load_and_set()
active_context="ssl").load_and_set(actual)
self.assertEqual(expected, actual)

def test_ssl_no_verification(self):
Expand All @@ -582,8 +578,7 @@ def test_ssl_no_verification(self):
actual = FakeConfig()
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="no_ssl_verification",
client_configuration=actual).load_and_set()
active_context="no_ssl_verification").load_and_set(actual)
self.assertEqual(expected, actual)

def test_list_contexts(self):
Expand Down Expand Up @@ -631,8 +626,7 @@ def test_ssl_with_relative_ssl_files(self):
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="ssl-local-file",
config_base_path=temp_dir,
client_configuration=actual).load_and_set()
config_base_path=temp_dir).load_and_set(actual)
self.assertEqual(expected, actual)
finally:
shutil.rmtree(temp_dir)
Expand Down Expand Up @@ -663,9 +657,9 @@ def test_new_client_from_config(self):
config_file = self._create_temp_file(yaml.dump(self.TEST_KUBE_CONFIG))
client = new_client_from_config(
config_file=config_file, context="simple_token")
self.assertEqual(TEST_HOST, client.config.host)
self.assertEqual(TEST_HOST, client.configuration.host)
self.assertEqual(BEARER_TOKEN_FORMAT % TEST_DATA_BASE64,
client.config.api_key['authorization'])
client.configuration.api_key['authorization'])

def test_no_users_section(self):
expected = FakeConfig(host=TEST_HOST)
Expand All @@ -674,17 +668,15 @@ def test_no_users_section(self):
del test_kube_config['users']
KubeConfigLoader(
config_dict=test_kube_config,
active_context="gcp",
client_configuration=actual).load_and_set()
active_context="gcp").load_and_set(actual)
self.assertEqual(expected, actual)

def test_non_existing_user(self):
expected = FakeConfig(host=TEST_HOST)
actual = FakeConfig()
KubeConfigLoader(
config_dict=self.TEST_KUBE_CONFIG,
active_context="non_existing_user",
client_configuration=actual).load_and_set()
active_context="non_existing_user").load_and_set(actual)
self.assertEqual(expected, actual)


Expand Down
Loading

0 comments on commit b7a9f4a

Please sign in to comment.