From 965d6c3640ec7e735f0c17985c583952962fbbba Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Wed, 3 Nov 2021 15:45:40 +0800 Subject: [PATCH] Add adapter to get k8s cluster info Add adapter to get cpu info Issue-ID: INF-196 Signed-off-by: Bin Yang Change-Id: I4176ca4bc9df8556e5252c7b4f3247d6f70da9e1 --- .gitignore | 3 +- README.md | 2 + o2ims/adapter/clients/ocloud_sa_client.py | 70 ++++++++++++++++++++-- o2ims/domain/stx_object.py | 9 +++ .../integration-ocloud/test_clientdriver_stx_sa.py | 25 ++++++++ 5 files changed, 102 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index addeb00..c46d404 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ __pycache__ *.egg-info *.pyc -.tox \ No newline at end of file +.tox +*.log diff --git a/README.md b/README.md index edd927e..fdc1d39 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Prerequisite: in case of testing against real ocloud, download openrc file from admin_openrc.sh docker-compose run --rm --no-deps --entrypoint=pytest api /tests/unit /tests/integration-ocloud +docker-compose run --rm --no-deps --entrypoint=pytest api /tests/integration-ocloud --log-level=DEBUG --log-file=/test +s/debug.log ``` ## Tear down containers diff --git a/o2ims/adapter/clients/ocloud_sa_client.py b/o2ims/adapter/clients/ocloud_sa_client.py index 5d1a5f9..5e9e64c 100644 --- a/o2ims/adapter/clients/ocloud_sa_client.py +++ b/o2ims/adapter/clients/ocloud_sa_client.py @@ -14,6 +14,7 @@ # client talking to Stx standalone +import uuid from o2ims.service.client.base_client import BaseClient from typing import List # Optional, Set @@ -61,8 +62,8 @@ class StxSaDmsClient(BaseClient): super().__init__() self.driver = StxSaClientImp() - def _get(self, id) -> ocloudModel.StxGenericModel: - return self.driver.getK8sDetail(id) + def _get(self, name) -> ocloudModel.StxGenericModel: + return self.driver.getK8sDetail(name) def _list(self): return self.driver.getK8sList() @@ -79,6 +80,19 @@ class StxPserverClient(BaseClient): def _list(self) -> List[ocloudModel.StxGenericModel]: return self.driver.getPserverList() + +class StxCpuClient(BaseClient): + def __init__(self, pserver_id): + super().__init__() + self._pserver_id = pserver_id + self.driver = StxSaClientImp() + + def _get(self, id) -> ocloudModel.StxGenericModel: + return self.driver.getCpu(id) + + def _list(self) -> List[ocloudModel.StxGenericModel]: + return self.driver.getCpuList(self._pserver_id) + # internal driver which implement client call to Stx Standalone instance @@ -109,12 +123,56 @@ class StxSaClientImp(object): return ocloudModel.StxGenericModel(self._hostconverter(host)) def getK8sList(self) -> List[ocloudModel.StxGenericModel]: - raise NotImplementedError - - def getK8sDetail(self, id) -> ocloudModel.StxGenericModel: - raise NotImplementedError + k8sclusters = self.stxclient.kube_cluster.list() + logger.debug("k8sresources:" + str(k8sclusters[0].to_dict())) + + return [ocloudModel.StxGenericModel(self._k8sconverter(k8sres)) + for k8sres in k8sclusters if k8sres] + + def getK8sDetail(self, name) -> ocloudModel.StxGenericModel: + k8scluster = self.stxclient.kube_cluster.get(name) + logger.debug("k8sresource:" + str(k8scluster.to_dict())) + return ocloudModel.StxGenericModel(self._k8sconverter(k8scluster)) + + def getCpuList(self, hostid) -> List[ocloudModel.StxGenericModel]: + cpulist = self.stxclient.icpu.list(hostid) + return [ocloudModel.StxGenericModel(self._cpuconverter(cpures)) + for cpures in cpulist if cpures] + + def getCpu(self, id) -> ocloudModel.StxGenericModel: + cpuinfo = self.stxclient.icpu.get(id) + return ocloudModel.StxGenericModel(self._cpuconverter(cpuinfo)) + + def _getIsystems(self): + return self.stxclient.isystem.list() + + def _getIsystem(self, id=None): + if id: + return self.stxclient.isystem.get(id) + else: + isystems = self.stxclient.isystem.list() + if len(isystems) != 1 and not id: + raise Exception('No system uuid was provided and ' + 'more than one system exists in the account.') + return isystems[0] @staticmethod def _hostconverter(host): setattr(host, "name", host.hostname) return host + + @staticmethod + def _cpuconverter(cpu): + setattr(cpu, "name", "core-"+str(cpu.core)) + return cpu + + @staticmethod + def _k8sconverter(host): + setattr(host, "name", host.cluster_name) + setattr(host, "uuid", + uuid.uuid3(uuid.NAMESPACE_URL, host.cluster_name)) + setattr(host, 'updated_at', None) + setattr(host, 'created_at', None) + logger.debug("k8s cluster name/uuid:" + + host.name + "/" + str(host.uuid)) + return host diff --git a/o2ims/domain/stx_object.py b/o2ims/domain/stx_object.py index 7345694..09d1a16 100644 --- a/o2ims/domain/stx_object.py +++ b/o2ims/domain/stx_object.py @@ -41,3 +41,12 @@ class StxGenericModel: self.content = newmodel.content self.createtime = newmodel.createtime self.updatetime = newmodel.updatetime + + +class StxK8sClusterModel(StxGenericModel): + def __init__(self, api_response: dict = None) -> None: + super().__init__(api_response=api_response) + + def is_outdated(self, newmodel) -> bool: + # never outdated since lack of such evidence + return False diff --git a/tests/integration-ocloud/test_clientdriver_stx_sa.py b/tests/integration-ocloud/test_clientdriver_stx_sa.py index 551a4c5..4e45070 100644 --- a/tests/integration-ocloud/test_clientdriver_stx_sa.py +++ b/tests/integration-ocloud/test_clientdriver_stx_sa.py @@ -62,3 +62,28 @@ def test_get_pserver(real_stx_aio_client): host2 = stxSaClientImp.getPserver(host1.id) assert host1 != host2 assert host1.id == host2.id + +def test_get_k8s_list(real_stx_aio_client): + stxSaClientImp = StxSaClientImp(real_stx_aio_client) + assert stxSaClientImp is not None + k8slist = stxSaClientImp.getK8sList() + assert k8slist is not None + assert len(k8slist) > 0 + k8s1 = k8slist[0] + k8s2 = stxSaClientImp.getK8sDetail(k8s1.name) + assert k8s1 != k8s2 + assert k8s1.name == k8s2.name + assert k8s1.id == k8s2.id + +def test_get_cpu_list(real_stx_aio_client): + stxSaClientImp = StxSaClientImp(real_stx_aio_client) + assert stxSaClientImp is not None + hostlist = stxSaClientImp.getPserverList() + assert len(hostlist) > 0 + + cpulist = stxSaClientImp.getCpuList(hostlist[0].id) + assert len(cpulist) > 0 + cpu1 = cpulist[0] + cpu2 = stxSaClientImp.getCpu(cpu1.id) + assert cpu1 != cpu2 + assert cpu1.id == cpu2.id -- 2.16.6