Exemple #1
0
    def pod_update(self, context, pod_ident, bay_ident, manifest):
        LOG.debug("pod_update %s", pod_ident)
        bay = conductor_utils.retrieve_bay(context, bay_ident)
        self.k8s_api = k8s.create_k8s_api(context, bay)
        if utils.is_uuid_like(pod_ident):
            pod = objects.Pod.get_by_uuid(context, pod_ident, bay.uuid,
                                          self.k8s_api)
        else:
            pod = objects.Pod.get_by_name(context, pod_ident, bay.uuid,
                                          self.k8s_api)
        pod_ident = pod.name
        try:
            resp = self.k8s_api.replace_namespaced_pod(name=str(pod_ident),
                                                       body=manifest,
                                                       namespace='default')
        except rest.ApiException as err:
            raise exception.KubernetesAPIFailed(err=err)

        if resp is None:
            raise exception.PodNotFound(pod=pod.uuid)

        pod['uuid'] = resp.metadata.uid
        pod['name'] = resp.metadata.name
        pod['project_id'] = context.project_id
        pod['user_id'] = context.user_id
        pod['bay_uuid'] = bay.uuid
        pod['images'] = [c.image for c in resp.spec.containers]
        if not resp.metadata.labels:
            pod['labels'] = {}
        else:
            pod['labels'] = ast.literal_eval(resp.metadata.labels)
        pod['status'] = resp.status.phase
        pod['host'] = resp.spec.node_name

        return pod
Exemple #2
0
    def get_by_name(cls, context, name, bay_uuid, k8s_api):
        """Find a pod based on pod name and the uuid for a bay.

        :param context: Security context
        :param name: the name of a pod.
        :param bay_uuid: the UUID of the Bay
        :param k8s_api: k8s API object

        :returns: a :class:`Pod` object.
        """
        try:
            resp = k8s_api.read_namespaced_pod(name=name, namespace='default')
        except rest.ApiException as err:
            raise exception.KubernetesAPIFailed(err=err)

        if resp is None:
            raise exception.PodNotFound(pod=name)

        pod = {}
        pod['uuid'] = resp.metadata.uid
        pod['name'] = resp.metadata.name
        pod['project_id'] = context.project_id
        pod['user_id'] = context.user_id
        pod['bay_uuid'] = bay_uuid
        pod['images'] = [c.image for c in resp.spec.containers]
        if not resp.metadata.labels:
            pod['labels'] = {}
        else:
            pod['labels'] = ast.literal_eval(resp.metadata.labels)
        pod['status'] = resp.status.phase
        pod['host'] = resp.spec.node_name

        pod_obj = Pod(context, **pod)
        return pod_obj
Exemple #3
0
 def destroy_pod(self, pod_id):
     session = get_session()
     with session.begin():
         query = model_query(models.Pod, session=session)
         query = add_identity_filter(query, pod_id)
         count = query.delete()
         if count != 1:
             raise exception.PodNotFound(pod_id)
Exemple #4
0
 def get_pod_by_uuid(self, context, pod_uuid):
     query = model_query(models.Pod)
     query = self._add_tenant_filters(context, query)
     query = query.filter_by(uuid=pod_uuid)
     try:
         return query.one()
     except NoResultFound:
         raise exception.PodNotFound(pod=pod_uuid)
Exemple #5
0
 def get_pod_by_name(self, pod_name):
     query = model_query(models.Pod).filter_by(name=pod_name)
     try:
         return query.one()
     except MultipleResultsFound:
         raise exception.Conflict('Multiple pods exist with same name.'
                                  ' Please use the pod uuid instead.')
     except NoResultFound:
         raise exception.PodNotFound(pod=pod_name)
Exemple #6
0
    def _do_update_pod(self, pod_id, values):
        session = get_session()
        with session.begin():
            query = model_query(models.Pod, session=session)
            query = add_identity_filter(query, pod_id)
            try:
                ref = query.with_lockmode('update').one()
            except NoResultFound:
                raise exception.PodNotFound(pod=pod_id)

            if 'provision_state' in values:
                values['provision_updated_at'] = timeutils.utcnow()

            ref.update(values)
        return ref
Exemple #7
0
    def pod_delete(self, api_address, name):
        LOG.debug("pod_delete %s" % name)
        try:
            out, err = utils.trycmd('kubectl', 'delete', 'pod', name,
                                    '-s', api_address,)
        except Exception as e:
            LOG.error(_LE("Couldn't delete pod %(pod)s due to error "
                          "%(error)s") % {'pod': name, 'error': e})
            return False

        if err:
            if ('"%s" not found' % name) in err:
                raise exception.PodNotFound(pod=name)
            else:
                return False

        return True
Exemple #8
0
    def test_pod_delete_succeeds_when_not_found(self, mock_pod_get_by_uuid,
                                                mock_retrieve_k8s_master_url,
                                                mock_object_has_stack):
        expected_master_url = 'api_address'
        mock_pod = mock.MagicMock()
        mock_pod.name = 'test-pod'
        mock_pod.uuid = 'test-uuid'
        mock_pod_get_by_uuid.return_value = mock_pod

        mock_retrieve_k8s_master_url.return_value = expected_master_url
        mock_object_has_stack.return_value = True
        with patch.object(self.kube_handler, 'kube_cli') as mock_kube_cli:
            mock_kube_cli.pod_delete.side_effect = exception.PodNotFound()

            self.kube_handler.pod_delete(self.context, mock_pod.uuid)

            mock_kube_cli.pod_delete.assert_called_once_with(
                expected_master_url, mock_pod.name)
            mock_pod.destroy.assert_called_once_with(self.context)
Exemple #9
0
 def test_PodNotFound(self):
     self.assertRaises(exception.PodNotFound,
                       lambda: self.raise_(exception.PodNotFound()))