From b521fee8cbdc2852f22fe84a6055af85461712f5 Mon Sep 17 00:00:00 2001 From: Roman Dobosz Date: Wed, 23 Dec 2020 11:32:59 +0100 Subject: [PATCH] Adapt selfLink calculation for Pod objects. Implements: blueprint selflink Change-Id: Ie98aae21ef910f11eaff5a6f6814d16ee1542ff2 --- kuryr_kubernetes/cmd/status.py | 3 ++- kuryr_kubernetes/controller/drivers/sriov.py | 3 ++- .../controller/handlers/pod_label.py | 3 ++- kuryr_kubernetes/controller/handlers/vif.py | 2 +- kuryr_kubernetes/os_vif_util.py | 3 ++- kuryr_kubernetes/tests/fake.py | 20 +++++++++++++++ .../tests/unit/cmd/test_status.py | 8 +++--- .../unit/controller/drivers/test_sriov.py | 25 ++++++++----------- .../unit/controller/handlers/test_vif.py | 13 +++------- .../tests/unit/test_os_vif_util.py | 5 ++-- 10 files changed, 52 insertions(+), 33 deletions(-) diff --git a/kuryr_kubernetes/cmd/status.py b/kuryr_kubernetes/cmd/status.py index 3cef16688..1c43107c4 100644 --- a/kuryr_kubernetes/cmd/status.py +++ b/kuryr_kubernetes/cmd/status.py @@ -34,6 +34,7 @@ from kuryr_kubernetes import constants from kuryr_kubernetes import exceptions from kuryr_kubernetes import objects from kuryr_kubernetes.objects import vif +from kuryr_kubernetes import utils from kuryr_kubernetes import version CONF = config.CONF @@ -173,7 +174,7 @@ class UpgradeCommands(object): jsonutils.dumps(serialized) } self.k8s.annotate( - pod['metadata']['selfLink'], ann, + utils.get_res_link(pod), ann, pod['metadata']['resourceVersion']) except exceptions.K8sClientException: print('Error when updating annotation for pod %s/%s' % diff --git a/kuryr_kubernetes/controller/drivers/sriov.py b/kuryr_kubernetes/controller/drivers/sriov.py index b16eeef0b..da334ca79 100644 --- a/kuryr_kubernetes/controller/drivers/sriov.py +++ b/kuryr_kubernetes/controller/drivers/sriov.py @@ -22,6 +22,7 @@ from kuryr_kubernetes import constants from kuryr_kubernetes.controller.drivers import neutron_vif from kuryr_kubernetes.controller.drivers import utils as c_utils from kuryr_kubernetes import os_vif_util as ovu +from kuryr_kubernetes import utils LOG = logging.getLogger(__name__) CONF = cfg.CONF @@ -64,7 +65,7 @@ class SriovVIFDriver(neutron_vif.NeutronPodVIFDriver): vif = ovu.neutron_to_osvif_vif(vif_plugin, port, subnets) vif.physnet = physnet vif.pod_name = pod_name - vif.pod_link = pod['metadata']['selfLink'] + vif.pod_link = utils.get_res_link(pod) LOG.debug("{} vifs are available for the pod {}".format( amount, pod_name)) diff --git a/kuryr_kubernetes/controller/handlers/pod_label.py b/kuryr_kubernetes/controller/handlers/pod_label.py index 456772ca8..13cbc55cb 100644 --- a/kuryr_kubernetes/controller/handlers/pod_label.py +++ b/kuryr_kubernetes/controller/handlers/pod_label.py @@ -22,6 +22,7 @@ from kuryr_kubernetes.controller.drivers import base as drivers from kuryr_kubernetes.controller.drivers import utils as driver_utils from kuryr_kubernetes import exceptions as k_exc from kuryr_kubernetes.handlers import k8s_base +from kuryr_kubernetes import utils LOG = logging.getLogger(__name__) @@ -97,7 +98,7 @@ class PodLabelHandler(k8s_base.ResourceEventHandler): LOG.debug("Setting info annotations: %r", annotation) k8s = clients.get_kubernetes_client() - k8s.annotate(pod['metadata']['selfLink'], + k8s.annotate(utils.get_res_link(pod), { constants.K8S_ANNOTATION_LABEL: annotation[0], constants.K8S_ANNOTATION_IP: annotation[1] diff --git a/kuryr_kubernetes/controller/handlers/vif.py b/kuryr_kubernetes/controller/handlers/vif.py index 63bdb834e..abfe59e40 100644 --- a/kuryr_kubernetes/controller/handlers/vif.py +++ b/kuryr_kubernetes/controller/handlers/vif.py @@ -240,7 +240,7 @@ class VIFHandler(k8s_base.ResourceEventHandler): raise k_exc.ResourceNotReady(pod) k8s = clients.get_kubernetes_client() - k8s.remove_annotations(pod['metadata']['selfLink'], + k8s.remove_annotations(utils.get_res_link(pod), constants.K8S_ANNOTATION_VIF) return True diff --git a/kuryr_kubernetes/os_vif_util.py b/kuryr_kubernetes/os_vif_util.py index 9f16492e7..fec855af8 100644 --- a/kuryr_kubernetes/os_vif_util.py +++ b/kuryr_kubernetes/os_vif_util.py @@ -33,6 +33,7 @@ from kuryr_kubernetes import config from kuryr_kubernetes import constants as const from kuryr_kubernetes import exceptions as k_exc from kuryr_kubernetes.objects import vif as k_vif +from kuryr_kubernetes import utils LOG = logging.getLogger(__name__) @@ -400,7 +401,7 @@ def neutron_to_osvif_vif_dpdk(os_port, subnets, pod): details = os_port.get('binding:vif_details', {}) profile = osv_vif.VIFPortProfileK8sDPDK( l3_setup=False, - selflink=pod['metadata']['selfLink']) + selflink=utils.get_res_link(pod)) return k_vif.VIFDPDKNested( id=os_port['id'], diff --git a/kuryr_kubernetes/tests/fake.py b/kuryr_kubernetes/tests/fake.py index 08a86a98d..3fa8c6650 100644 --- a/kuryr_kubernetes/tests/fake.py +++ b/kuryr_kubernetes/tests/fake.py @@ -161,3 +161,23 @@ def get_sgr_obj(sgr_id='7621d1e0-a2d2-4496-94eb-ffd375d20877', 'tenant_id': '5ea46368c7fe436bb8732738c149fbce'} return os_sgr.SecurityGroupRule(**sgr_data) + + +def get_k8s_pod(): + + return {'apiVersion': 'v1', + 'kind': 'Pod', + 'metadata': {'creationTimestamp': '2020-12-22T09:04:29Z', + 'finalizers': ['kuryr.openstack.org/pod-finalizer'], + 'generateName': 'pod-5bb648d658-', + 'labels': {'app': 'pod', + 'pod-template-hash': '5bb648d658'}, + 'operation': 'Update', + 'name': 'pod-5bb648d658-55n76', + 'namespace': 'default', + 'resourceVersion': '19416', + 'selfLink': '/api/v1/namespaces/default/pods/' + 'pod-5bb648d658-55n76', + 'uid': '683da866-6bb1-4da2-bf6a-a5f4137c38e7'}, + 'spec': {}, + 'status': {}} diff --git a/kuryr_kubernetes/tests/unit/cmd/test_status.py b/kuryr_kubernetes/tests/unit/cmd/test_status.py index a3909968f..bb1ff9765 100644 --- a/kuryr_kubernetes/tests/unit/cmd/test_status.py +++ b/kuryr_kubernetes/tests/unit/cmd/test_status.py @@ -161,11 +161,13 @@ class TestStatusCmd(test_base.TestCase): pods = { 'items': [ { + 'apiVersion': 'v1', + 'kind': 'Pod', 'metadata': { 'annotations': { constants.K8S_ANNOTATION_VIF: ann }, - 'selfLink': name, + 'name': name, 'resourceVersion': 1, } } for name, ann in ann_objs @@ -178,8 +180,8 @@ class TestStatusCmd(test_base.TestCase): def test_update_annotations(self): self._test__convert_annotations(self.cmd.update_annotations, - [('bar', mock.ANY, 1)]) + [('/api/v1/pods/bar', mock.ANY, 1)]) def test_downgrade_annotations(self): self._test__convert_annotations(self.cmd.downgrade_annotations, - [('foo', mock.ANY, 1)]) + [('/api/v1/pods/foo', mock.ANY, 1)]) diff --git a/kuryr_kubernetes/tests/unit/controller/drivers/test_sriov.py b/kuryr_kubernetes/tests/unit/controller/drivers/test_sriov.py index 8a26b0e0d..fc8db9627 100644 --- a/kuryr_kubernetes/tests/unit/controller/drivers/test_sriov.py +++ b/kuryr_kubernetes/tests/unit/controller/drivers/test_sriov.py @@ -17,6 +17,7 @@ import uuid from kuryr_kubernetes.controller.drivers import sriov as drvs from kuryr_kubernetes.tests import base as test_base +from kuryr_kubernetes.tests import fake from kuryr_kubernetes.tests.unit import kuryr_fixtures as k_fix from kuryr_kubernetes import constants as k_const @@ -49,23 +50,19 @@ class TestSriovVIFDriver(test_base.TestCase): drvs.sriov_make_resource(k_const.K8S_SRIOV_PREFIX, SRIOV_RESOURCE_NAME_B): ( str(AMOUNT_FOR_SUBNET_B))} - - self._pod = { - 'metadata': { - 'resourceVersion': mock.sentinel.pod_version, - 'selfLink': mock.sentinel.pod_link, - 'name': 'podname'}, - 'status': {'phase': k_const.K8S_POD_STATUS_PENDING}, - 'spec': { - 'hostNetwork': False, - 'nodeName': 'hostname', - 'containers': [{ + self._pod = fake.get_k8s_pod() + self._pod['status'] = {'phase': k_const.K8S_POD_STATUS_PENDING} + self._pod['spec'] = { + 'hostNetwork': False, + 'nodeName': 'hostname', + 'containers': [ + { 'resources': { 'requests': sriov_request - } - }] + } } - } + ] + } def test_activate_vif(self): cls = drvs.SriovVIFDriver diff --git a/kuryr_kubernetes/tests/unit/controller/handlers/test_vif.py b/kuryr_kubernetes/tests/unit/controller/handlers/test_vif.py index 79c7148a1..c1b7b5c0a 100644 --- a/kuryr_kubernetes/tests/unit/controller/handlers/test_vif.py +++ b/kuryr_kubernetes/tests/unit/controller/handlers/test_vif.py @@ -24,6 +24,7 @@ from kuryr_kubernetes.controller.handlers import vif as h_vif from kuryr_kubernetes import exceptions as k_exc from kuryr_kubernetes.objects import vif from kuryr_kubernetes.tests import base as test_base +from kuryr_kubernetes.tests import fake class TestVIFHandler(test_base.TestCase): @@ -46,15 +47,9 @@ class TestVIFHandler(test_base.TestCase): self._pod_namespace = mock.sentinel.namespace self._pod_uid = mock.sentinel.pod_uid self._pod_name = 'pod1' - self._pod = { - 'metadata': {'resourceVersion': self._pod_version, - 'selfLink': self._pod_link, - 'name': self._pod_name, - 'namespace': self._pod_namespace}, - 'status': {'phase': k_const.K8S_POD_STATUS_PENDING}, - 'spec': {'hostNetwork': False, - 'nodeName': 'hostname'} - } + self._pod = fake.get_k8s_pod() + self._pod['status'] = {'phase': k_const.K8S_POD_STATUS_PENDING} + self._pod['spec'] = {'hostNetwork': False, 'nodeName': 'hostname'} self._kp_version = mock.sentinel.kp_version self._kp_link = mock.sentinel.kp_link diff --git a/kuryr_kubernetes/tests/unit/test_os_vif_util.py b/kuryr_kubernetes/tests/unit/test_os_vif_util.py index 590bf098f..78e1e114a 100644 --- a/kuryr_kubernetes/tests/unit/test_os_vif_util.py +++ b/kuryr_kubernetes/tests/unit/test_os_vif_util.py @@ -29,6 +29,7 @@ from kuryr_kubernetes import exceptions as k_exc from kuryr_kubernetes import os_vif_util as ovu from kuryr_kubernetes.tests import base as test_base from kuryr_kubernetes.tests import fake +from kuryr_kubernetes import utils # REVISIT(ivc): move to kuryr-lib along with 'os_vif_util' @@ -455,7 +456,7 @@ class TestOSVIFUtils(test_base.TestCase): m_mk_vif.return_value = vif m_mk_port_profile.return_value = port_profile - pod = mock.MagicMock() + pod = fake.get_k8s_pod() port = {'id': port_id, 'mac_address': mac_address, @@ -471,7 +472,7 @@ class TestOSVIFUtils(test_base.TestCase): m_get_vif_name.assert_called_once_with(port) m_mk_port_profile.assert_called_once_with( l3_setup=False, - selflink=pod['metadata']['selfLink']) + selflink=utils.get_res_link(pod)) m_mk_vif.assert_called_once_with( id=port_id,