From e634b55637093c1d791f31ed0e0945bffe55e4b8 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Fri, 10 Feb 2017 13:51:04 -0500 Subject: [PATCH] Switch to kubernetes upstream python client For a really long time, we generated and maintained our very own python client generated from kubernetes swagger json files. Now in Kubernetes Community there is a concerted effort to organize an official python client (also generated from swagger) for everyone to use. So let us please switch over from our python-k8sclient and use the community driven python client. I have ported all of our end-to-end tests and got them working in kubernetes client-python project upstream so we should be protected from regressions. Implements: blueprint replace-k8sclient-with-upstream-kubernetes-client Depends-On: I72359f2b811392008eb5267812bf343797b1553a Change-Id: Ib81a69cfdc25198e259e3b3d4081c92c01fd1bc5 --- magnum/conductor/k8s_api.py | 18 ++++++----- magnum/drivers/common/k8s_monitor.py | 2 +- magnum/tests/functional/python_client_base.py | 31 +++++++++++-------- magnum/tests/unit/conductor/test_monitors.py | 2 +- requirements.txt | 2 +- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/magnum/conductor/k8s_api.py b/magnum/conductor/k8s_api.py index aa036ff419..78e5ffe219 100644 --- a/magnum/conductor/k8s_api.py +++ b/magnum/conductor/k8s_api.py @@ -14,8 +14,9 @@ import tempfile -from k8sclient.client import api_client -from k8sclient.client.apis import apiv_api +from kubernetes import client as k8s_config +from kubernetes.client import api_client +from kubernetes.client.apis import core_v1_api from oslo_log import log as logging from magnum.conductor.handlers.common.cert_manager import create_client_files @@ -24,7 +25,7 @@ from magnum.i18n import _LE LOG = logging.getLogger(__name__) -class K8sAPI(apiv_api.ApivApi): +class K8sAPI(core_v1_api.CoreV1Api): def _create_temp_file_with_content(self, content): """Creates temp file and write content to the file. @@ -50,11 +51,14 @@ class K8sAPI(apiv_api.ApivApi): (self.ca_file, self.key_file, self.cert_file) = create_client_files(cluster, context) + config = k8s_config.ConfigurationObject() + config.host = cluster.api_address + config.ssl_ca_cert = self.ca_file.name + config.cert_file = self.cert_file.name + config.key_file = self.key_file.name + # build a connection with Kubernetes master - client = api_client.ApiClient(cluster.api_address, - key_file=self.key_file.name, - cert_file=self.cert_file.name, - ca_certs=self.ca_file.name) + client = api_client.ApiClient(config=config) super(K8sAPI, self).__init__(client) diff --git a/magnum/drivers/common/k8s_monitor.py b/magnum/drivers/common/k8s_monitor.py index c15cd9efdf..c6ac1472d3 100644 --- a/magnum/drivers/common/k8s_monitor.py +++ b/magnum/drivers/common/k8s_monitor.py @@ -40,7 +40,7 @@ class K8sMonitor(monitors.MonitorBase): def pull_data(self): k8s_api = k8s.create_k8s_api(self.context, self.cluster) - nodes = k8s_api.list_namespaced_node() + nodes = k8s_api.list_node() self.data['nodes'] = self._parse_node_info(nodes) pods = k8s_api.list_namespaced_pod('default') self.data['pods'] = self._parse_pod_info(pods) diff --git a/magnum/tests/functional/python_client_base.py b/magnum/tests/functional/python_client_base.py index 37de75d588..855d3b60ef 100644 --- a/magnum/tests/functional/python_client_base.py +++ b/magnum/tests/functional/python_client_base.py @@ -26,9 +26,10 @@ import fixtures from six.moves import configparser from heatclient import client as heatclient -from k8sclient.client import api_client -from k8sclient.client.apis import apiv_api from keystoneclient.v3 import client as ksclient +from kubernetes import client as k8s_config +from kubernetes.client import api_client +from kubernetes.client.apis import core_v1_api from magnum.common.utils import rmtree_without_raise import magnum.conf @@ -386,26 +387,30 @@ class BaseK8sTest(ClusterTest): def setUpClass(cls): super(BaseK8sTest, cls).setUpClass() cls.kube_api_url = cls.cs.clusters.get(cls.cluster.uuid).api_address - k8s_client = api_client.ApiClient(cls.kube_api_url, - key_file=cls.key_file, - cert_file=cls.cert_file, - ca_certs=cls.ca_file) - cls.k8s_api = apiv_api.ApivApi(k8s_client) + config = k8s_config.ConfigurationObject() + config.host = cls.kube_api_url + config.ssl_ca_cert = cls.ca_file + config.cert_file = cls.cert_file + config.key_file = cls.key_file + k8s_client = api_client.ApiClient(config=config) + cls.k8s_api = core_v1_api.CoreV1Api(k8s_client) def setUp(self): super(BaseK8sTest, self).setUp() self.kube_api_url = self.cs.clusters.get(self.cluster.uuid).api_address - k8s_client = api_client.ApiClient(self.kube_api_url, - key_file=self.key_file, - cert_file=self.cert_file, - ca_certs=self.ca_file) - self.k8s_api = apiv_api.ApivApi(k8s_client) + config = k8s_config.ConfigurationObject() + config.host = self.kube_api_url + config.ssl_ca_cert = self.ca_file + config.cert_file = self.cert_file + config.key_file = self.key_file + k8s_client = api_client.ApiClient(config=config) + self.k8s_api = core_v1_api.CoreV1Api(k8s_client) # TODO(coreypobrien) https://bugs.launchpad.net/magnum/+bug/1551824 utils.wait_for_condition(self._is_api_ready, 5, 600) def _is_api_ready(self): try: - self.k8s_api.list_namespaced_node() + self.k8s_api.list_node() self.LOG.info(_LI("API is ready.")) return True except Exception: diff --git a/magnum/tests/unit/conductor/test_monitors.py b/magnum/tests/unit/conductor/test_monitors.py index ef63a6131e..16e69f1a62 100644 --- a/magnum/tests/unit/conductor/test_monitors.py +++ b/magnum/tests/unit/conductor/test_monitors.py @@ -137,7 +137,7 @@ class MonitorsTestCase(base.TestCase): mock_node.status = mock.MagicMock() mock_node.status.capacity = {'memory': '2000Ki', 'cpu': '1'} mock_nodes.items = [mock_node] - mock_k8s_api.return_value.list_namespaced_node.return_value = ( + mock_k8s_api.return_value.list_node.return_value = ( mock_nodes) mock_pods = mock.MagicMock() mock_pod = mock.MagicMock() diff --git a/requirements.txt b/requirements.txt index 9a29e38c31..ccd293490c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,6 +20,7 @@ iso8601>=0.1.11 # MIT jsonpatch>=1.1 # BSD keystoneauth1>=2.18.0 # Apache-2.0 keystonemiddleware>=4.12.0 # Apache-2.0 +kubernetes>=1.0.0b1 # Apache-2.0 marathon>=0.8.6 # MIT netaddr!=0.7.16,>=0.7.13 # BSD oslo.concurrency>=3.8.0 # Apache-2.0 @@ -43,7 +44,6 @@ python-barbicanclient>=4.0.0 # Apache-2.0 python-glanceclient>=2.5.0 # Apache-2.0 python-heatclient>=1.6.1 # Apache-2.0 python-neutronclient>=5.1.0 # Apache-2.0 -python-k8sclient>=0.2.0 # Apache-2.0 python-novaclient>=7.1.0 # Apache-2.0 python-keystoneclient>=3.8.0 # Apache-2.0 requests!=2.12.2,!=2.13.0,>=2.10.0 # Apache-2.0