Exemple #1
0
    def on_present(self, route):
        if not self._l7_router or not self._l7_router_listeners:
            ing_ctl = ingress_ctl.IngressCtrlr.get_instance()
            self._l7_router, self._l7_router_listeners = (
                ing_ctl.get_router_and_listener())
            if not self._l7_router or not self._l7_router_listeners:
                LOG.info("No L7 router found - do nothing")
                return

        route_spec = self._get_route_spec(route)
        if not route_spec:
            route_spec = obj_route.RouteSpec()

        if self._should_ignore(route, route_spec):
            return
        route_state = self._get_route_state(route)
        if not route_state:
            route_state = obj_route.RouteState()

        self._sync_router_pool(route, route_spec, route_state)
        self._sync_l7_policy(route, route_spec, route_state)
        self._sync_host_l7_rule(route, route_spec, route_state)
        self._sync_path_l7_rule(route, route_spec, route_state)

        self._set_route_state(route, route_state)
        self._set_route_spec(route, route_spec)
        self._send_route_notification_to_ep(route, route_spec.to_service)
    def test_sync_host_l7_rule_already_exist(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_lbaas = mock.Mock(spec=drv_base.LBaaSDriver)
        h_l7_rule = obj_lbaas.LBaaSL7Rule(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C44',
            compare_type='EQUAL_TO',
            l7policy_id='00EE9E11-91C2-41CF-8FD4-7970579E5C45',
            type='HOST',
            value='www.example.com')

        route_state = obj_route.RouteState(h_l7_rule=h_l7_rule)
        route = {
            'metadata': {
                'namespace': 'namespace',
                'name': 'name'
            },
            'spec': {
                'host': 'www.test.com',
                'path': 'mypath',
                'to': {
                    'name': 'target_service'
                }
            }
        }

        route_spec = obj_route.RouteSpec(host='www.test.com',
                                         path='mypath',
                                         to_service='target_service')

        h_route.OcpRouteHandler._sync_host_l7_rule(m_handler, route,
                                                   route_spec, route_state)
        self.assertEqual(route_state.h_l7_rule, h_l7_rule)
    def test_sync_router_pool_empty_pool(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_lbaas = mock.Mock(spec=drv_base.LBaaSDriver)
        m_handler._drv_lbaas.get_pool_by_name.return_value = None
        m_handler._drv_lbaas.ensure_pool_attached_to_lb.return_value = None

        route = {
            'metadata': {
                'namespace': 'namespace'
            },
            'spec': {
                'host': 'www.test.com',
                'path': 'mypath',
                'to': {
                    'name': 'target_service'
                }
            }
        }
        route_spec = obj_route.RouteSpec(host='www.test.com1',
                                         path='mypath',
                                         to_service='target_service')

        route_state = obj_route.RouteState()

        h_route.OcpRouteHandler._sync_router_pool(m_handler, route, route_spec,
                                                  route_state)
        self.assertIsNone(route_state.router_pool)
Exemple #4
0
 def _get_route_spec(self, route):
     try:
         annotations = route['metadata']['annotations']
         annotation = annotations[k_const.K8S_ANNOTATION_ROUTE_SPEC]
     except KeyError:
         return obj_route.RouteSpec()
     obj_dict = jsonutils.loads(annotation)
     obj = obj_route.RouteSpec.obj_from_primitive(obj_dict)
     LOG.debug("Got RouteSpec from annotation: %r", obj)
     return obj
    def test_sync_path_l7_rule(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_lbaas = mock.Mock(spec=drv_base.LBaaSDriver)

        l7_policy = obj_lbaas.LBaaSL7Policy(
            id='00EE9E11-91C2-41CF-8FD4-7970579E6666',
            name='myname',
            listener_id='00EE9E11-91C2-41CF-8FD4-7970579E5C45',
            redirect_pool_id='00EE9E11-91C2-41CF-8FD4-7970579E5C46',
            project_id='00EE9E11-91C2-41CF-8FD4-7970579E5C46')

        route_state = obj_route.RouteState(l7_policy=l7_policy)

        route = {
            'metadata': {
                'namespace': 'namespace',
                'name': 'name'
            },
            'spec': {
                'host': 'new.www.test.com',
                'path': '/nice_path',
                'to': {
                    'name': 'target_service'
                }
            }
        }

        route_spec = obj_route.RouteSpec(host='www.test.com',
                                         path=None,
                                         to_service='target_service')

        ret_p_l7_rule = obj_lbaas.LBaaSL7Rule(
            id='55559E11-91C2-41CF-8FD4-7970579E5C44',
            compare_type=OCP_ROUTE_PATH_COMP_TYPE,
            l7policy_id='55559E11-91C2-41CF-8FD4-7970579E5C45',
            type='PATH',
            value='/nice_path')

        m_handler._drv_lbaas.ensure_l7_rule.return_value = ret_p_l7_rule
        h_route.OcpRouteHandler._sync_path_l7_rule(m_handler, route,
                                                   route_spec, route_state)
        self.assertEqual(route_state.p_l7_rule, ret_p_l7_rule)
        m_handler._drv_lbaas.ensure_l7_rule.assert_called_once_with(
            m_handler._l7_router, route_state.l7_policy,
            OCP_ROUTE_PATH_COMP_TYPE, 'PATH', route['spec']['path'])
    def test_sync_path_l7_rule_edit_usecase(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_lbaas = mock.Mock(spec=drv_base.LBaaSDriver)

        old_p_l7_rule = obj_lbaas.LBaaSL7Rule(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C44',
            compare_type=OCP_ROUTE_PATH_COMP_TYPE,
            l7policy_id='00EE9E11-91C2-41CF-8FD4-7970579E5C45',
            type='PATH',
            value='/cur_path')

        route_state = obj_route.RouteState(p_l7_rule=old_p_l7_rule)

        route = {
            'metadata': {
                'namespace': 'namespace',
                'name': 'name'
            },
            'spec': {
                'host': 'new.www.test.com',
                'path': '/new_path',
                'to': {
                    'name': 'target_service'
                }
            }
        }
        route_spec = obj_route.RouteSpec(host='www.test.com',
                                         path=old_p_l7_rule.value,
                                         to_service='target_service')

        ret_p_l7_rule = obj_lbaas.LBaaSL7Rule(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C44',
            compare_type=OCP_ROUTE_PATH_COMP_TYPE,
            l7policy_id='00EE9E11-91C2-41CF-8FD4-7970579E5C45',
            type='PATH',
            value=route['spec']['path'])

        m_handler._drv_lbaas.update_l7_rule.return_value = True
        h_route.OcpRouteHandler._sync_path_l7_rule(m_handler, route,
                                                   route_spec, route_state)
        self.assertEqual(route_state.p_l7_rule.value, ret_p_l7_rule.value)
        m_handler._drv_lbaas.update_l7_rule.assert_called_once_with(
            old_p_l7_rule, route['spec']['path'])
Exemple #7
0
    def test_sync_path_l7_rule(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_l7_router = mock.Mock(
            spec=d_l7_rtr.LBaaSv2L7RouterDriver)

        old_p_l7_rule = obj_route.RouteL7Rule(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C44',
            compare_type='EQUAL_TO',
            l7policy_id='00EE9E11-91C2-41CF-8FD4-7970579E5C45',
            type='PATH',
            value='/nice_path/')

        route_state = obj_route.RouteState(p_l7_rule=old_p_l7_rule)

        route = {
            'metadata': {
                'namespace': 'namespace',
                'name': 'name'
            },
            'spec': {
                'host': 'new.www.test.com',
                'path': 'mypath',
                'to': {
                    'name': 'target_service'
                }
            }
        }

        route_spec = obj_route.RouteSpec(host='www.test.com',
                                         path='mypath',
                                         to_service='target_service')

        ret_p_l7_rule = obj_route.RouteL7Rule(
            id='55559E11-91C2-41CF-8FD4-7970579E5C44',
            compare_type='EQUAL_TO',
            l7policy_id='55559E11-91C2-41CF-8FD4-7970579E5C45',
            type='PATH',
            value='/nice_path/')

        m_handler._drv_l7_router.ensure_l7_rule.return_value = ret_p_l7_rule
        h_route.OcpRouteHandler._sync_path_l7_rule(m_handler, route,
                                                   route_spec, route_state)
        self.assertEqual(route_state.p_l7_rule, old_p_l7_rule)
Exemple #8
0
    def test_sync_l7_policy(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_l7_router = mock.Mock(
            spec=d_l7_rtr.LBaaSv2L7RouterDriver)
        listener = obj_lbaas.LBaaSListener(
            id='123443545',
            name='TEST_NAME',
            project_id='TEST_PROJECT',
            protocol='TCP',
            port=80,
            loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C')
        m_handler._l7_router_listeners = {'80': listener}
        l7_policy = obj_route.RouteL7Policy(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C44',
            name='myname',
            listener_id='00EE9E11-91C2-41CF-8FD4-7970579E5C45',
            redirect_pool_id='00EE9E11-91C2-41CF-8FD4-7970579E5C46',
            project_id='00EE9E11-91C2-41CF-8FD4-7970579E5C46')

        route_state = obj_route.RouteState()
        m_handler._drv_l7_router.ensure_l7_policy.return_value = l7_policy

        route = {
            'metadata': {
                'namespace': 'namespace',
                'name': 'name'
            },
            'spec': {
                'host': 'www.test.com',
                'path': 'mypath',
                'to': {
                    'name': 'target_service'
                }
            }
        }
        route_spec = obj_route.RouteSpec(host='www.test.com1',
                                         path='mypath',
                                         to_service='target_service')

        h_route.OcpRouteHandler._sync_l7_policy(m_handler, route, route_spec,
                                                route_state)
        self.assertEqual(route_state.l7_policy, l7_policy)
 def test_should_ignore_l7_router_exist_with_changes(self):
     m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
     m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
         id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
         project_id='TEST_PROJECT')
     route = {
         'spec': {
             'host': 'www.test.com',
             'path': 'mypath',
             'to': {
                 'name': 'target_service'
             }
         }
     }
     route_spec = obj_route.RouteSpec(host='www.test.com1',
                                      path='mypath',
                                      to_service='target_service')
     expected_result = False
     ret_value = h_route.OcpRouteHandler._should_ignore(
         m_handler, route, route_spec)
     self.assertEqual(ret_value, expected_result)
    def test_should_ignore_l7_router_not_exist(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = None
        route = {
            'spec': {
                'host': 'www.test.com',
                'path': 'mypath',
                'to': {
                    'name': 'target_service'
                }
            }
        }

        route_spec = obj_route.RouteSpec(host='www.test.com',
                                         path='mypath',
                                         to_service='target_service')
        expected_result = True

        ret_value = h_route.OcpRouteHandler._should_ignore(
            m_handler, route, route_spec)

        self.assertEqual(ret_value, expected_result)
    def test_sync_router_pool_valid_pool(self):
        m_handler = mock.Mock(spec=h_route.OcpRouteHandler)
        m_handler._l7_router = obj_lbaas.LBaaSLoadBalancer(
            id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C',
            project_id='TEST_PROJECT')
        m_handler._drv_lbaas = mock.Mock(spec=drv_base.LBaaSDriver)
        ret_pool = obj_lbaas.LBaaSPool(
            name='TEST_NAME',
            project_id='TEST_PROJECT',
            protocol='TCP',
            listener_id='A57B7771-6050-4CA8-A63C-443493EC98AB',
            loadbalancer_id='00EE9E11-91C2-41CF-8FD4-7970579E5C4C')

        m_handler._drv_lbaas.get_pool_by_name.return_value = None
        m_handler._drv_lbaas.ensure_pool_attached_to_lb.return_value = ret_pool

        route = {
            'metadata': {
                'namespace': 'namespace'
            },
            'spec': {
                'host': 'www.test.com',
                'path': 'mypath',
                'to': {
                    'name': 'target_service'
                }
            }
        }
        route_spec = obj_route.RouteSpec(host='www.test.com1',
                                         path='mypath',
                                         to_service='target_service')

        route_state = obj_route.RouteState()

        h_route.OcpRouteHandler._sync_router_pool(m_handler, route, route_spec,
                                                  route_state)
        self.assertEqual(route_state.router_pool, ret_pool)