def __init__(self, config): """Establish session using configurations. :param Config config: An object of the .kube/config configuration """ if isinstance(config, dict): config = Config(config) # assume type is string as the previous handled input # as a dict, so the only remaining option when not # an instance of Config is a file location. if not isinstance(config, Config): config = Config.from_file(config) self.kubeconfig = config # Check the API url self.base_url = self.kubeconfig.cluster.get('server', 'http://localhost:8080') validator.check_url(self.base_url) # Initialize the connection using all the .kube/config credentials self.session = self._connection() # Test the connection before proceeding self._test_connection(self.base_url + '/api/') self.api_resources = {} # Load API Resources self._load_resources('api/v1/', 'v1') self._load_group_resources('apis/')
def test_format_session_no_context(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'empty_kubeconfig')) self.assertIsNotNone(cfg) self.assertIsNone(cfg.current_context) self.assertEqual( cfg.format_session(), { 'cert': None, 'headers': {}, 'verify': True } )
def test_format_session_certs_no_user(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'certs_no_user_kubeconfig')) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'minishift') session = cfg.format_session() self.assertIsNone(session['cert']) self.assertEqual(session['headers'], {}) # indicates it was a base64 encoded cert that was written # to a tempfile self.assertTrue(session['verify'].startswith('/tmp/')) # clean up the test file self.addCleanup(remove_file, session['verify'])
def test_format_session_token(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'simple_kubeconfig')) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'default/192-168-99-103:8443/admin') self.assertEqual( cfg.format_session(), { 'cert': None, 'headers': { 'Authorization': 'Bearer qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM' }, 'verify': False } )
def test_format_session_certs(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'certs_kubeconfig')) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'minikube') self.assertEqual( cfg.format_session(), { 'cert': ( '/tmp/.minikube/apiserver.crt', '/tmp/.minikube/apiserver.key' ), 'headers': {}, 'verify': '/tmp/.minikube/ca.crt' } )
def test_format_session_certs_data(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'certs_data_kubeconfig')) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'default/192-168-99-103:8443/admin') session = cfg.format_session() self.assertIsNone(session['cert']) self.assertEqual(session['headers'], { 'Authorization': 'Bearer 10me0gx3el9uHDfF7JdksZO_rEuBt_JuAZLrT0htoe4' }) # indicates it was a base64 encoded cert that was written # to a tempfile self.assertTrue(session['verify'].startswith('/tmp/')) # clean up the test file self.addCleanup(remove_file, session['verify'])
def test_from_file_empty_file(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'empty_kubeconfig')) self.assertIsNotNone(cfg) self.assertIsNone(cfg.current_context) self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 0) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 0) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 0) self.assertIsInstance(cfg.context, dict) self.assertEqual(len(cfg.context), 0) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(len(cfg.cluster), 0) self.assertIsInstance(cfg.user, dict) self.assertEqual(len(cfg.user), 0)
def test_from_file_empty_string(self): cfg = Config.from_file('') self.assertIsNotNone(cfg) self.assertIsNone(cfg.current_context) self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 0) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 0) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 0) self.assertIsInstance(cfg.context, dict) self.assertEqual(len(cfg.context), 0) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(len(cfg.cluster), 0) self.assertIsInstance(cfg.user, dict) self.assertEqual(len(cfg.user), 0)
def test_from_file_simple(self): cfg = Config.from_file(os.path.join(FIXTURE_DIR, 'simple_kubeconfig')) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'default/192-168-99-103:8443/admin') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual( cfg.context, { 'cluster': '192-168-99-103:8443', 'namespace': 'default', 'user': '******' } ) self.assertIsInstance(cfg.cluster, dict) self.assertEqual( cfg.cluster, { 'insecure-skip-tls-verify': True, 'server': 'https://192.168.99.103:8443' } ) self.assertIsInstance(cfg.user, dict) self.assertEqual( cfg.user, { 'token': 'qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM' } )