Example #1
0
 def _test_watch_create_watcher(path, handler, timeout=0):
     watcher_obj = watcher.Watcher(handler, timeout=timeout)
     watcher_obj._running = True
     watcher_obj._resources.add(path)
     watcher_obj._idle[path] = True
     watcher_obj._watching[path] = None
     return watcher_obj
    def __init__(self):
        super(KuryrK8sService, self).__init__()

        pipeline = h_pipeline.ControllerPipeline(self.tg)
        self.watcher = watcher.Watcher(pipeline, self.tg)
        # TODO(ivc): pluggable resource/handler registration
        for resource in ["pods", "services", "endpoints"]:
            self.watcher.add("%s/%s" % (constants.K8S_API_BASE, resource))
        pipeline.register(h_vif.VIFHandler())
Example #3
0
    def test_start_watch(self, m_watch):
        path = '/test'
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)

        watcher_obj._start_watch(path)

        m_watch.assert_called_once_with(path)
        self.assertTrue(watcher_obj._idle.get(path))
        self.assertIn(path, watcher_obj._watching)
Example #4
0
    def test_add(self, m_start_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)

        for path in paths:
            watcher_obj.add(path)

        self.assertEqual(set(paths), watcher_obj._resources)
        m_start_watch.assert_not_called()
Example #5
0
 def run(self):
     self.pipeline = h_cni.CNIPipeline()
     self.pipeline.register(h_cni.CallbackHandler(self.on_done))
     self.watcher = k_watcher.Watcher(self.pipeline)
     self.watcher.add(
         "%(base)s/pods?fieldSelector=spec.nodeName=%(node_name)s" % {
             'base': k_const.K8S_API_BASE,
             'node_name': self._get_nodename()
         })
     self.watcher.start()
Example #6
0
 def test_watch_restart(self):
     tg = mock.Mock()
     w = watcher.Watcher(lambda e: None, tg)
     w.add('/test')
     w.start()
     tg.add_thread.assert_called_once_with(mock.ANY, '/test')
     w.stop()
     tg.add_thread = mock.Mock()  # Reset mock.
     w.start()
     tg.add_thread.assert_called_once_with(mock.ANY, '/test')
Example #7
0
    def test_remove(self, m_stop_watch):
        path = '/test'
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._resources.add(path)

        watcher_obj.remove(path)

        self.assertEqual(set(), watcher_obj._resources)
        m_stop_watch.assert_not_called()
Example #8
0
 def _setup(self, params):
     clients.setup_kubernetes_client()
     self._pipeline = h_cni.CNIPipeline()
     self._watcher = k_watcher.Watcher(self._pipeline)
     self._watcher.add(
         "%(base)s/namespaces/%(namespace)s/pods"
         "?fieldSelector=metadata.name=%(pod)s" % {
             'base': k_const.K8S_API_BASE,
             'namespace': params.args.K8S_POD_NAMESPACE,
             'pod': params.args.K8S_POD_NAME})
Example #9
0
    def test_stop(self, m_stop_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._resources.update(paths)

        watcher_obj.stop()

        self.assertFalse(watcher_obj._running)
        m_stop_watch.assert_not_called()
Example #10
0
    def test_start(self, m_start_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._resources.update(paths)

        watcher_obj.start()

        self.assertTrue(watcher_obj._running)
        m_start_watch.assert_has_calls([mock.call(path) for path in paths],
                                       any_order=True)
Example #11
0
    def test_remove_watching(self, m_stop_watch):
        path = '/test'
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._resources.add(path)
        m_watching = watcher_obj._watching = mock.MagicMock()
        m_watching.__contains__.return_value = True

        watcher_obj.remove(path)

        self.assertEqual(set(), watcher_obj._resources)
        m_stop_watch.assert_called_once_with(path)
Example #12
0
    def __init__(self):
        super(KuryrK8sService, self).__init__()

        objects.register_locally_defined_vifs()
        pipeline = h_pipeline.ControllerPipeline(self.tg)
        self.watcher = watcher.Watcher(pipeline, self.tg)
        self.health_manager = health.HealthServer()

        handlers = _load_kuryr_ctrlr_handlers()
        for handler in handlers:
            self.watcher.add(handler.get_watch_path())
            pipeline.register(handler)
Example #13
0
    def test_stop_watch_threaded(self):
        path = '/test'
        m_tg = mock.Mock()
        m_th = mock.Mock()
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler, m_tg)
        watcher_obj._idle[path] = True
        watcher_obj._watching[path] = m_th

        watcher_obj._stop_watch(path)

        m_th.stop.assert_called()
Example #14
0
    def test_add_running(self, m_start_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._running = True

        for path in paths:
            watcher_obj.add(path)

        self.assertEqual(set(paths), watcher_obj._resources)
        m_start_watch.assert_has_calls([mock.call(path) for path in paths],
                                       any_order=True)
Example #15
0
    def test_stop_watch_idle(self):
        path = '/test'
        m_tg = mock.Mock()
        m_th = mock.Mock()
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler, m_tg)
        watcher_obj._idle[path] = False
        watcher_obj._watching[path] = m_th

        watcher_obj._stop_watch(path)

        m_th.kill.assert_not_called()
Example #16
0
    def test_start_already_watching(self, m_start_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._resources.update(paths)
        m_watching = watcher_obj._watching = mock.MagicMock()
        m_watching.__iter__.return_value = paths

        watcher_obj.start()

        self.assertTrue(watcher_obj._running)
        m_start_watch.assert_not_called()
Example #17
0
    def __init__(self):
        super(KuryrK8sService, self).__init__()

        objects.register_locally_defined_vifs()
        pipeline = h_pipeline.ControllerPipeline(self.tg)
        self.watcher = watcher.Watcher(pipeline, self.tg)
        self.health_manager = health.HealthServer()
        # TODO(ivc): pluggable resource/handler registration
        for resource in ["pods", "services", "endpoints"]:
            self.watcher.add("%s/%s" % (constants.K8S_API_BASE, resource))
        pipeline.register(h_vif.VIFHandler())
        pipeline.register(h_lbaas.LBaaSSpecHandler())
        pipeline.register(h_lbaas.LoadBalancerHandler())
Example #18
0
    def test_add_watching(self, m_start_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._running = True
        m_watching = watcher_obj._watching = mock.MagicMock()
        m_watching.__contains__.return_value = True

        for path in paths:
            watcher_obj.add(path)

        self.assertEqual(set(paths), watcher_obj._resources)
        m_start_watch.assert_not_called()
    def run(self):
        self.pipeline = handlers.CNIPipeline()
        self.pipeline.register(self.handler)
        self.watcher = k_watcher.Watcher(self.pipeline)
        self.watcher.add(self.path)

        self.is_running = True

        self.health_thread = threading.Thread(
            target=self._start_watcher_health_checker)
        self.health_thread.start()

        self.watcher.start()
Example #20
0
    def test_start_watch_threaded(self):
        path = '/test'
        m_tg = mock.Mock()
        m_tg.add_thread.return_value = mock.sentinel.watch_thread
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler, m_tg)

        watcher_obj._start_watch(path)

        m_tg.add_thread.assert_called_once_with(watcher_obj._watch, path)
        self.assertTrue(watcher_obj._idle.get(path))
        self.assertEqual(mock.sentinel.watch_thread,
                         watcher_obj._watching.get(path))
Example #21
0
    def test_stop_watching(self, m_stop_watch):
        paths = ['/test%s' % i for i in range(3)]
        m_handler = mock.Mock()
        watcher_obj = watcher.Watcher(m_handler)
        watcher_obj._resources.update(paths)
        m_watching = watcher_obj._watching = mock.MagicMock()
        m_watching.__iter__.return_value = paths

        watcher_obj.stop()

        self.assertFalse(watcher_obj._running)
        m_stop_watch.assert_has_calls([mock.call(path) for path in paths],
                                      any_order=True)
Example #22
0
 def run(self):
     self.pipeline = h_cni.CNIPipeline()
     self.pipeline.register(h_cni.CallbackHandler(self.on_done,
                                                  self.on_deleted))
     self.watcher = k_watcher.Watcher(self.pipeline)
     self.watcher.add(
         "%(base)s/pods?fieldSelector=spec.nodeName=%(node_name)s" % {
             'base': k_const.K8S_API_BASE,
             'node_name': self._get_nodename()})
     self.is_running = True
     self.health_thread = threading.Thread(
         target=self._start_watcher_health_checker)
     self.health_thread.start()
     self.watcher.start()
Example #23
0
    def run(self):
        self.pipeline = h_cni.CNIPipeline()
        self.pipeline.register(
            h_cni.CallbackHandler(self.on_done, self.on_deleted))
        self.watcher = k_watcher.Watcher(self.pipeline)
        query_label = urllib.parse.quote_plus(f'{k_const.KURYRPORT_LABEL}='
                                              f'{self._get_nodename()}')

        self.watcher.add(f'{k_const.K8S_API_CRD_KURYRPORTS}'
                         f'?labelSelector={query_label}')

        self.is_running = True
        self.health_thread = threading.Thread(
            target=self._start_watcher_health_checker)
        self.health_thread.start()
        self.watcher.start()
    def __init__(self):
        super(KuryrK8sService, self).__init__()
        periodic_task.PeriodicTasks.__init__(self, CONF)

        objects.register_locally_defined_vifs()
        pipeline = h_pipeline.ControllerPipeline(self.tg)
        self.watcher = watcher.Watcher(pipeline, self.tg, exit_on_stop=True)
        self.health_manager = health.HealthServer()
        self.current_leader = None
        self.node_name = utils.get_node_name()

        handlers = _load_kuryr_ctrlr_handlers()
        for handler in handlers:
            self.watcher.add(handler.get_watch_path())
            pipeline.register(handler)
        self.pool_driver = drivers.VIFPoolDriver.get_instance(
            specific_driver='multi_pool')
        self.pool_driver.set_vif_driver()
Example #25
0
    def _setup(self, params):
        args = ['--config-file', params.config.kuryr_conf]

        try:
            if params.config.debug:
                args.append('-d')
        except AttributeError:
            pass

        config.init(args)
        config.setup_logging()
        os_vif.initialize()
        clients.setup_kubernetes_client()
        self._pipeline = h_cni.CNIPipeline()
        self._watcher = k_watcher.Watcher(self._pipeline)
        self._watcher.add(
            "%(base)s/namespaces/%(namespace)s/pods"
            "?fieldSelector=metadata.name=%(pod)s" % {
                'base': k_const.K8S_API_BASE,
                'namespace': params.args.K8S_POD_NAMESPACE,
                'pod': params.args.K8S_POD_NAME
            })