Exemple #1
0
    def _make_request(self, path, cni_envs, expected_status=None):
        method = 'POST'

        host = CONF.cni_daemon.cni_daemon_host
        port = CONF.cni_daemon.cni_daemon_port
        url = 'http://%s:%s/%s' % (host, port, path)
        try:
            LOG.debug(
                'Making request to CNI Daemon. %(method)s %(path)s\n'
                '%(body)s', {
                    'method': method,
                    'path': url,
                    'body': cni_envs
                })
            resp = requests.post(url,
                                 json=cni_envs,
                                 headers={'Connection': 'close'})
        except requests.ConnectionError:
            LOG.exception(
                'Looks like %s:%s cannot be reached. '
                'Is zun-cni-daemon running?', (host, port))
            raise
        LOG.debug('CNI Daemon returned "%(status)d %(reason)s".', {
            'status': resp.status_code,
            'reason': resp.reason
        })
        if expected_status and resp.status_code != expected_status:
            LOG.error('CNI daemon returned error "%(status)d %(reason)s".', {
                'status': resp.status_code,
                'reason': resp.reason
            })
            raise exception.CNIError('Got invalid status code from CNI daemon')
        return resp
Exemple #2
0
    def _do_work(self, params, fn):
        container_uuid = self._get_container_uuid(params)
        container_type = self._get_container_type(params)

        if container_type == TYPE_CAPSULE:
            container = objects.Capsule.get_by_uuid(self.context,
                                                    container_uuid)
        elif container_type == TYPE_CONTAINER:
            container = objects.Container.get_by_uuid(self.context,
                                                      container_uuid)
        else:
            raise exception.CNIError('Unexpected type: %s' % container_type)

        vifs = cni_utils.get_vifs(container)

        for ifname, vif in vifs.items():
            is_default_gateway = (ifname == consts.DEFAULT_IFNAME)
            if is_default_gateway:
                # NOTE(ygupta): if this is the default interface, we should
                # use the ifname supplied in the CNI ADD request
                ifname = params.CNI_IFNAME

            fn(vif,
               self._get_inst(container),
               ifname,
               params.CNI_NETNS,
               is_default_gateway=is_default_gateway,
               container_id=params.CNI_CONTAINERID)
        return vifs
Exemple #3
0
 def run(self, env, fin, fout):
     try:
         # Prepare params according to calling Object
         params = self.prepare_env(env, fin)
         if env.get('CNI_COMMAND') == 'ADD':
             vif = self._add(params)
             self._write_dict(fout, vif)
         elif env.get('CNI_COMMAND') == 'DEL':
             self._delete(params)
         elif env.get('CNI_COMMAND') == 'VERSION':
             self._write_version(fout)
         else:
             raise exception.CNIError(_("unknown CNI_COMMAND: %s"),
                                      env['CNI_COMMAND'])
         return 0
     except Exception as ex:
         # LOG.exception
         self._write_exception(fout, str(ex))
         return 1