Adapt selfLink calculation for Pod objects.

Implements: blueprint selflink
Change-Id: Ie98aae21ef910f11eaff5a6f6814d16ee1542ff2
This commit is contained in:
Roman Dobosz 2020-12-23 11:32:59 +01:00
parent 9bc33d9b78
commit b521fee8cb
10 changed files with 52 additions and 33 deletions

View File

@ -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' %

View File

@ -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))

View File

@ -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]

View File

@ -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

View File

@ -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'],

View File

@ -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': {}}

View File

@ -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)])

View File

@ -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

View File

@ -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

View File

@ -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,