Browse Source

Merge "Adapt selfLink calculation for Service objects."

changes/68/772568/1
Zuul 1 month ago
committed by Gerrit Code Review
parent
commit
8d4ed472cf
4 changed files with 35 additions and 16 deletions
  1. +2
    -2
      kuryr_kubernetes/controller/handlers/lbaas.py
  2. +14
    -6
      kuryr_kubernetes/tests/unit/controller/handlers/test_lbaas.py
  3. +16
    -5
      kuryr_kubernetes/tests/unit/test_utils.py
  4. +3
    -3
      kuryr_kubernetes/utils.py

+ 2
- 2
kuryr_kubernetes/controller/handlers/lbaas.py View File

@ -133,7 +133,7 @@ class ServiceHandler(k8s_base.ResourceEventHandler):
if len(subnet_ids) != 1:
raise k_exc.IntegrityError(_(
"Found %(num)s subnets for service %(link)s IP %(ip)s") % {
'link': service['metadata']['selfLink'],
'link': utils.get_res_link(service),
'ip': ip,
'num': len(subnet_ids)})
@ -214,7 +214,7 @@ class ServiceHandler(k8s_base.ResourceEventHandler):
utils.has_port_changes(service, loadbalancer_crd))
def _has_ip_changes(self, service, loadbalancer_crd):
link = service['metadata']['selfLink']
link = utils.get_res_link(service)
svc_ip = self._get_service_ip(service)
if loadbalancer_crd['spec'].get('ip') is None:


+ 14
- 6
kuryr_kubernetes/tests/unit/controller/handlers/test_lbaas.py View File

@ -45,6 +45,8 @@ class TestServiceHandler(test_base.TestCase):
@mock.patch('kuryr_kubernetes.clients.get_kubernetes_client')
def test_on_present(self, get_k8s_client):
svc_event = {
'apiVersion': 'v1',
'kind': 'Service',
"metadata": {
"creationTimestamp": "2020-07-25T18:15:12Z",
"finalizers": [
@ -56,7 +58,6 @@ class TestServiceHandler(test_base.TestCase):
"name": "test",
"namespace": "test",
"resourceVersion": "413753",
"selfLink": "",
"uid": "a026ae48-6141-4029-b743-bac48dae7f06"
},
"spec": {
@ -120,6 +121,8 @@ class TestServiceHandler(test_base.TestCase):
@mock.patch('kuryr_kubernetes.clients.get_kubernetes_client')
def test_on_present_no_changes(self, get_k8s_client):
svc_event = {
'apiVersion': 'v1',
'kind': 'Service',
"metadata": {
"creationTimestamp": "2020-07-25T18:15:12Z",
"finalizers": [
@ -131,7 +134,6 @@ class TestServiceHandler(test_base.TestCase):
"name": "test",
"namespace": "test",
"resourceVersion": "413753",
"selfLink": "",
"uid": "a026ae48-6141-4029-b743-bac48dae7f06"
},
"spec": {
@ -232,7 +234,10 @@ class TestServiceHandler(test_base.TestCase):
def test_has_ip_changes(self):
m_handler = mock.Mock(spec=h_lbaas.ServiceHandler)
m_service = mock.MagicMock()
m_service = {'apiVersion': 'v1',
'kind': 'Service',
"metadata": {"name": "test",
"namespace": "test"}}
m_handler._get_service_ip.return_value = '1.1.1.1'
m_lbaas_spec = mock.MagicMock()
m_lbaas_spec.ip.__str__.return_value = '2.2.2.2'
@ -243,6 +248,8 @@ class TestServiceHandler(test_base.TestCase):
def test_has_ip_changes__no_changes(self):
service = {
'apiVersion': 'v1',
'kind': 'Service',
"metadata": {
"creationTimestamp": "2020-07-25T18:15:12Z",
"finalizers": [
@ -254,7 +261,6 @@ class TestServiceHandler(test_base.TestCase):
"name": "test",
"namespace": "test",
"resourceVersion": "413753",
"selfLink": "",
"uid": "a026ae48-6141-4029-b743-bac48dae7f06"
},
"spec": {
@ -283,6 +289,8 @@ class TestServiceHandler(test_base.TestCase):
m_handler = mock.Mock(spec=h_lbaas.ServiceHandler)
m_handler._get_service_ip.return_value = '1.1.1.1'
service = {
'apiVersion': 'v1',
'kind': 'Service',
"metadata": {
"creationTimestamp": "2020-07-25T18:15:12Z",
"finalizers": [
@ -294,7 +302,6 @@ class TestServiceHandler(test_base.TestCase):
"name": "test",
"namespace": "test",
"resourceVersion": "413753",
"selfLink": "",
"uid": "a026ae48-6141-4029-b743-bac48dae7f06"
},
"spec": {
@ -314,6 +321,8 @@ class TestServiceHandler(test_base.TestCase):
def test_has_ip_changes__no_nothing(self):
m_handler = mock.Mock(spec=h_lbaas.ServiceHandler)
service = {
'apiVersion': 'v1',
'kind': 'Service',
"metadata": {
"creationTimestamp": "2020-07-25T18:15:12Z",
"finalizers": [
@ -325,7 +334,6 @@ class TestServiceHandler(test_base.TestCase):
"name": "test",
"namespace": "test",
"resourceVersion": "413753",
"selfLink": "",
"uid": "a026ae48-6141-4029-b743-bac48dae7f06"
},
"spec": {


+ 16
- 5
kuryr_kubernetes/tests/unit/test_utils.py View File

@ -164,8 +164,10 @@ class TestUtils(test_base.TestCase):
kubernetes.get.assert_called_once()
def test_get_endpoints_link(self):
service = {'metadata': {
'selfLink': "/api/v1/namespaces/default/services/test"}}
service = {'apiVersion': 'v1',
'kind': 'Service',
'metadata': {'namespace': 'default',
'name': 'test'}}
ret = utils.get_endpoints_link(service)
expected_link = "/api/v1/namespaces/default/endpoints/test"
self.assertEqual(expected_link, ret)
@ -187,8 +189,11 @@ class TestUtils(test_base.TestCase):
@mock.patch('kuryr_kubernetes.utils.get_service_ports')
def test_has_port_changes(self, m_get_service_ports):
service = {
'apiVersion': 'v1',
'kind': 'Service',
'metadata': {
'selfLink': ""
'name': 'serv-1',
'namespace': 'ns1'
},
'spec': {
'ports': [
@ -219,8 +224,11 @@ class TestUtils(test_base.TestCase):
@mock.patch('kuryr_kubernetes.utils.get_service_ports')
def test_has_port_changes_more_ports(self, m_get_service_ports):
service = {
'apiVersion': 'v1',
'kind': 'Service',
'metadata': {
'selfLink': ""
'name': 'serv-1',
'namespace': 'ns1'
},
'spec': {
'ports': [
@ -259,8 +267,11 @@ class TestUtils(test_base.TestCase):
def test_has_port_changes_no_changes(self, m_get_service_ports):
service = {
'apiVersion': 'v1',
'kind': 'Service',
'metadata': {
'selfLink': ""
'name': 'serv-1',
'namespace': 'ns1'
},
'spec': {
'ports': [


+ 3
- 3
kuryr_kubernetes/utils.py View File

@ -376,7 +376,7 @@ def set_lbaas_spec(service, lbaas_spec):
LOG.debug("Setting LBaaSServiceSpec annotation: %r", lbaas_spec)
annotation = jsonutils.dumps(lbaas_spec.obj_to_primitive(),
sort_keys=True)
svc_link = service['metadata']['selfLink']
svc_link = get_res_link(service)
ep_link = get_endpoints_link(service)
k8s = clients.get_kubernetes_client()
@ -430,7 +430,7 @@ def set_lbaas_state(endpoints, lbaas_state):
def get_endpoints_link(service):
svc_link = service['metadata']['selfLink']
svc_link = get_res_link(service)
link_parts = svc_link.split('/')
if link_parts[-2] != 'services':
@ -456,7 +456,7 @@ def get_service_link(endpoints):
def has_port_changes(service, loadbalancer_crd):
if not loadbalancer_crd:
return False
link = service['metadata']['selfLink']
link = get_res_link(service)
svc_port_set = service['spec'].get('ports')
for port in svc_port_set:


Loading…
Cancel
Save