Skip to content

Commit

Permalink
Switch to kubernetes upstream python client
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dims committed Feb 13, 2017
1 parent 26fb77b commit e634b55
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
18 changes: 11 additions & 7 deletions magnum/conductor/k8s_api.py
Expand Up @@ -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
Expand All @@ -24,7 +25,7 @@
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.
Expand All @@ -50,11 +51,14 @@ def __init__(self, context, cluster):
(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)

Expand Down
2 changes: 1 addition & 1 deletion magnum/drivers/common/k8s_monitor.py
Expand Up @@ -40,7 +40,7 @@ def metrics_spec(self):

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)
Expand Down
31 changes: 18 additions & 13 deletions magnum/tests/functional/python_client_base.py
Expand Up @@ -26,9 +26,10 @@
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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion magnum/tests/unit/conductor/test_monitors.py
Expand Up @@ -137,7 +137,7 @@ def test_k8s_monitor_pull_data_success(self, mock_k8s_api):
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()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e634b55

Please sign in to comment.