Python kubernetes.client.rest.ApiException() Examples

The following are 30 code examples of kubernetes.client.rest.ApiException(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module kubernetes.client.rest , or try the search function .
Example #1
Source File: actions.py    From chaostoolkit-kubernetes with Apache License 2.0 7 votes vote down vote up
def scale_statefulset(name: str, replicas: int, ns: str = "default",
                      secrets: Secrets = None):
    """
    Scale a stateful set up or down. The `name` is the name of the stateful
    set.
    """
    api = create_k8s_api_client(secrets)

    v1 = client.AppsV1Api(api)
    body = {"spec": {"replicas": replicas}}
    try:
        v1.patch_namespaced_stateful_set(name, namespace=ns, body=body)
    except ApiException as e:
        raise ActivityFailed(
            "failed to scale '{s}' to {r} replicas: {e}".format(
                s=name, r=replicas, e=str(e))) 
Example #2
Source File: k8s.py    From eks-rolling-update with Apache License 2.0 7 votes vote down vote up
def delete_node(node_name):
    """
    Deletes a kubernetes node from the cluster
    """

    try:
        config.load_incluster_config()
    except config.ConfigException:
        try:
            config.load_kube_config()
        except config.ConfigException:
            raise Exception("Could not configure kubernetes python client")

    configuration = client.Configuration()
    # create an instance of the API class
    k8s_api = client.CoreV1Api(client.ApiClient(configuration))
    logger.info("Deleting k8s node {}...".format(node_name))
    try:
        if not app_config['DRY_RUN']:
            k8s_api.delete_node(node_name)
        else:
            k8s_api.delete_node(node_name, dry_run="true")
        logger.info("Node deleted")
    except ApiException as e:
        logger.info("Exception when calling CoreV1Api->delete_node: {}".format(e)) 
Example #3
Source File: test_lock.py    From armada with Apache License 2.0 6 votes vote down vote up
def test_custom_resource_definition_creation(self, _):
        # When the crd doesn't exist yet, Kubernetes responds with a 404 when
        # trying to create a lock
        self.mock_create.side_effect = ApiException(status=404)
        mock_create = self.mock_create
        resp = self.resp

        def clear_side_effect(*args, **kwargs):
            mock_create.side_effect = None
            mock_create.return_value = resp

        # Once the definition is 'created' we need to stop raising err
        self.mock_create_crd.side_effect = clear_side_effect

        try:
            self.test_lock.acquire_lock()
        except lock.LockException:
            self.fail("acquire_lock() raised LockException unexpectedly") 
Example #4
Source File: rhopenshift.py    From wrapanapi with MIT License 6 votes vote down vote up
def get_appliance_version(self, vm_name):
        """Returns appliance version if it is possible

         Args:
            vm_name: the openshift project name of the podified appliance
        Returns: version
        """
        try:
            proj = self.o_api.read_project(vm_name)
            description = proj.metadata.annotations['openshift.io/description']
            return Version(TemplateName.parse_template(description).version)
        except (ApiException, KeyError, ValueError):
            try:
                return Version(TemplateName.parse_template(vm_name).version)
            except ValueError:
                return None 
Example #5
Source File: cluster_monitor.py    From KubeOperator with Apache License 2.0 6 votes vote down vote up
def get_component_status(self):
        component_data = []
        try:
            components = self.api_instance.list_component_status()
            for c in components.items:
                status, msg = '', ''
                for condition in c.conditions:
                    if condition.type == 'Healthy':
                        msg = condition.message
                        if condition.status == 'True':
                            status = 'RUNNING'
                        elif condition.status == 'False':
                            status = 'ERROR'
                        else:
                            status = condition.status
                component = ClusterHealthData(namespace='component', name=c.metadata.name, status=status,
                                              ready='1/1', age=0, msg=msg, restart_count=0)
                component_data.append(component.__dict__)
        except ApiException as e:
            logger.error(msg='list component error ' + e.reason, exc_info=True)

        return component_data 
Example #6
Source File: client.py    From mars with Apache License 2.0 6 votes vote down vote up
def stop(self, wait=False, timeout=0):
        from kubernetes.client import CoreV1Api
        api = CoreV1Api(self._api_client)
        api.delete_namespace(self._namespace)
        if wait:
            start_time = time.time()
            while True:
                try:
                    api.read_namespace(self._namespace)
                except K8SApiException as ex:
                    if ex.status != 404:  # pragma: no cover
                        raise
                    break
                else:
                    time.sleep(1)
                    if timeout and time.time() - start_time > timeout:  # pragma: no cover
                        raise TimeoutError 
Example #7
Source File: actions.py    From chaostoolkit-kubernetes with Apache License 2.0 6 votes vote down vote up
def scale_deployment(name: str, replicas: int, ns: str = "default",
                     secrets: Secrets = None):
    """
    Scale a deployment up or down. The `name` is the name of the deployment.
    """
    api = create_k8s_api_client(secrets)

    v1 = client.ExtensionsV1beta1Api(api)
    body = {"spec": {"replicas": replicas}}
    try:
        v1.patch_namespaced_deployment_scale(name, namespace=ns, body=body)
    except ApiException as e:
        raise ActivityFailed(
            "failed to scale '{s}' to {r} replicas: {e}".format(
                s=name, r=replicas, e=str(e))) 
Example #8
Source File: controller_wrappers.py    From paasta with Apache License 2.0 6 votes vote down vote up
def delete_horizontal_pod_autoscaler(self, kube_client: KubeClient) -> None:
        try:
            kube_client.autoscaling.delete_namespaced_horizontal_pod_autoscaler(
                name=self.item.metadata.name,
                namespace=self.item.metadata.namespace,
                body=V1DeleteOptions(),
            )
        except ApiException as e:
            if e.status == 404:
                # Deployment does not exist, nothing to delete but
                # we can consider this a success.
                self.logging.debug(
                    f"not deleting nonexistent HPA/{self.item.metadata.name} from namespace/{self.item.metadata.namespace}"
                )
            else:
                raise
        else:
            self.logging.info(
                "deleted HPA/{} from namespace/{}".format(
                    self.item.metadata.name, self.item.metadata.namespace
                )
            ) 
Example #9
Source File: client.py    From openshift-restclient-python with Apache License 2.0 6 votes vote down vote up
def meta_request(func):
    """ Handles parsing response structure and translating API Exceptions """
    def inner(self, *args, **kwargs):
        serialize_response = kwargs.pop('serialize', True)
        serializer = kwargs.pop('serializer', ResourceInstance)
        try:
            resp = func(self, *args, **kwargs)
        except ApiException as e:
            raise api_exception(e)
        if serialize_response:
            try:
                if six.PY2:
                    return serializer(self, json.loads(resp.data))
                return serializer(self, json.loads(resp.data.decode('utf8')))
            except ValueError:
                if six.PY2:
                    return resp.data
                return resp.data.decode('utf8')
        return resp

    return inner 
Example #10
Source File: exceptions.py    From openshift-restclient-python with Apache License 2.0 6 votes vote down vote up
def api_exception(e):
    """
    Returns the proper Exception class for the given kubernetes.client.rest.ApiException object
    https://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md#success-codes
    """
    _, _, exc_traceback = sys.exc_info()
    tb = '\n'.join(traceback.format_tb(exc_traceback))
    return {
        400: BadRequestError,
        401: UnauthorizedError,
        403: ForbiddenError,
        404: NotFoundError,
        405: MethodNotAllowedError,
        409: ConflictError,
        410: GoneError,
        422: UnprocessibleEntityError,
        429: TooManyRequestsError,
        500: InternalServerError,
        503: ServiceUnavailableError,
        504: ServerTimeoutError,
    }.get(e.status, DynamicApiError)(e, tb) 
Example #11
Source File: actions.py    From chaostoolkit-kubernetes with Apache License 2.0 6 votes vote down vote up
def uncordon_node(name: str = None, label_selector: str = None,
                  secrets: Secrets = None):
    """
    Uncordon nodes matching the given label name, so that pods can be
    scheduled on them again.
    """
    api = create_k8s_api_client(secrets)

    v1 = client.CoreV1Api(api)

    nodes = _select_nodes(name=name, label_selector=label_selector,
                          secrets=secrets)

    body = {
        "spec": {
            "unschedulable": False
        }
    }

    for n in nodes:
        try:
            v1.patch_node(n.metadata.name, body)
        except ApiException as x:
            logger.debug("Scheduling node '{}' failed: {}".format(
                n.metadata.name, x.body))
            raise ActivityFailed("Failed to schedule node '{}': {}".format(
                n.metadata.name, x.body)) 
Example #12
Source File: k8s.py    From eks-rolling-update with Apache License 2.0 6 votes vote down vote up
def modify_k8s_autoscaler(action):
    """
    Pauses or resumes the Kubernetes autoscaler
    """

    try:
        config.load_incluster_config()
    except config.ConfigException:
        try:
            config.load_kube_config()
        except config.ConfigException:
            raise Exception("Could not configure kubernetes python client")

    # Configure API key authorization: BearerToken
    configuration = client.Configuration()
    # create an instance of the API class
    k8s_api = client.AppsV1Api(client.ApiClient(configuration))
    if action == 'pause':
        logger.info('Pausing k8s autoscaler...')
        body = {'spec': {'replicas': 0}}
    elif action == 'resume':
        logger.info('Resuming k8s autoscaler...')
        body = {'spec': {'replicas': app_config['K8S_AUTOSCALER_REPLICAS']}}
    else:
        logger.info('Invalid k8s autoscaler option')
        sys.exit(1)
    try:
        k8s_api.patch_namespaced_deployment(
            app_config['K8S_AUTOSCALER_DEPLOYMENT'],
            app_config['K8S_AUTOSCALER_NAMESPACE'],
            body
        )
        logger.info('K8s autoscaler modified to replicas: {}'.format(body['spec']['replicas']))
    except ApiException as e:
        logger.info('Scaling of k8s autoscaler failed. Error code was {}, {}. Exiting.'.format(e.reason, e.body))
        sys.exit(1) 
Example #13
Source File: controller_wrappers.py    From paasta with Apache License 2.0 6 votes vote down vote up
def delete_pod_disruption_budget(self, kube_client: KubeClient) -> None:
        try:
            kube_client.policy.delete_namespaced_pod_disruption_budget(
                name=self.item.metadata.name,
                namespace=self.item.metadata.namespace,
                body=V1DeleteOptions(),
            )
        except ApiException as e:
            if e.status == 404:
                # Deployment does not exist, nothing to delete but
                # we can consider this a success.
                self.logging.debug(
                    "not deleting nonexistent pod disruption budget/{} from namespace/{}".format(
                        self.item.metadata.name, self.item.metadata.namespace
                    )
                )
            else:
                raise
        else:
            self.logging.info(
                "deleted pod disruption budget/{} from namespace/{}".format(
                    self.item.metadata.name, self.item.metadata.namespace
                )
            ) 
Example #14
Source File: rhopenshift.py    From wrapanapi with MIT License 6 votes vote down vote up
def unauthenticated_error_handler(method):
    """Fixes issue with 401 error by restoring connection.
        Sometimes connection to openshift api endpoint gets expired and openshift returns 401.
        As a result tasks in some applications like sprout fail.
    """
    @wraps(method)
    def wrap(*args, **kwargs):
        attempts = 3
        for _ in range(attempts):
            try:
                return method(*args, **kwargs)
            except ApiException as e:
                if e.reason == 'Unauthorized':
                    args[0]._connect()
                else:
                    raise e
        return method(*args, **kwargs)
    return wrap 
Example #15
Source File: test_utils.py    From python with Apache License 2.0 6 votes vote down vote up
def test_create_apps_deployment_from_yaml(self):
        """
        Should be able to create an apps/v1 deployment.
        """
        k8s_client = client.api_client.ApiClient(configuration=self.config)
        utils.create_from_yaml(
            k8s_client, self.path_prefix + "apps-deployment.yaml")
        app_api = client.AppsV1Api(k8s_client)
        dep = app_api.read_namespaced_deployment(name="nginx-app",
                                                 namespace="default")
        self.assertIsNotNone(dep)
        while True:
            try:
                app_api.delete_namespaced_deployment(
                    name="nginx-app", namespace="default",
                    body={})
                break
            except ApiException:
                continue 
Example #16
Source File: api_client.py    From python with Apache License 2.0 6 votes vote down vote up
def __deserialize_datatime(self, string):
        """Deserializes string to datetime.

        The string should be in iso8601 datetime format.

        :param string: str.
        :return: datetime.
        """
        try:
            from dateutil.parser import parse
            return parse(string)
        except ImportError:
            return string
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason=(
                    "Failed to parse `{0}` as datetime object"
                    .format(string)
                )
            ) 
Example #17
Source File: api_client.py    From python with Apache License 2.0 6 votes vote down vote up
def __deserialize_date(self, string):
        """Deserializes string to date.

        :param string: str.
        :return: date.
        """
        try:
            from dateutil.parser import parse
            return parse(string).date()
        except ImportError:
            return string
        except ValueError:
            raise rest.ApiException(
                status=0,
                reason="Failed to parse `{0}` as date object".format(string)
            ) 
Example #18
Source File: kubernetes.py    From paasta with Apache License 2.0 6 votes vote down vote up
def set_cr_desired_state(
    kube_client: kubernetes_tools.KubeClient,
    service: str,
    instance: str,
    instance_type: str,
    desired_state: str,
):
    try:
        kubernetes_tools.set_cr_desired_state(
            kube_client=kube_client,
            cr_id=cr_id(service, instance, instance_type),
            desired_state=desired_state,
        )
    except ApiException as e:
        error_message = (
            f"Error while setting state {desired_state} of "
            f"{service}.{instance}: {e}"
        )
        raise RuntimeError(error_message) 
Example #19
Source File: adapters.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def get(self):
        self._validate()

        try:
            if self.namespace:
                return self.api.get_namespaced_custom_object(
                    self.group, self.version, self.namespace, self.plural, self.name
                )

            return self.api.get_cluster_custom_object(self.group, self.version, self.plural, self.name)

        except ApiException as e:
            if e.reason == 'Not Found':
                return None

            log.error('{}'.format(add_indent(e.body)))
            raise ProvisioningError(e) 
Example #20
Source File: adapters.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def delete(self):
        self._validate()

        try:
            if self.namespace:
                return self.api.delete_namespaced_custom_object(
                    self.group, self.version, self.namespace, self.plural, self.name,
                    client.V1DeleteOptions(propagation_policy='Foreground')
                )

            return self.api.delete_cluster_custom_object(
                self.group, self.version, self.plural, self.name,
                client.V1DeleteOptions(propagation_policy='Foreground')
            )

        except ApiException as e:
            if e.reason == 'Not Found':
                return None

            log.error(
                '{}'.format(add_indent(e.body)))
            raise ProvisioningError(e) 
Example #21
Source File: test_lock.py    From armada with Apache License 2.0 6 votes vote down vote up
def test_timeout_getting_lock(self, mock_time, _):
        # The timestamp on the 'lock' will be new to avoid expiring
        last_update = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
        self.resp['data']['lastUpdated'] = str(last_update)
        # Mocking time.time() so that acquire_lock() is run through once, and
        # once the time is checked again the timeout will be reached
        test_time = 1550510151.792119
        mock_time.time = mock.Mock()

        def set_time():
            nonlocal test_time
            test_time += self.test_lock.timeout / 2
            return test_time

        mock_time.time.side_effect = set_time

        # Creating large expire time so the lock doesn't get overwritten
        self.test_lock.expire_time = 60
        # Updating mocks so that there is always a 'lock'
        self.mock_create.side_effect = ApiException(status=409)
        self.mock_read.return_value = self.resp

        # It should fail to acquire the lock before the attempt times out
        self.assertRaises(lock.LockException, self.test_lock.acquire_lock) 
Example #22
Source File: api_extensions.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def list_api_resources(self, version):
        try:
            return self.api_client.call_api(
                resource_path='/api/{}'.format(version),
                method='GET',
                header_params={
                    'Accept': self.api_client.select_header_accept(
                        ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']
                    ),
                    'Content-Type': self.api_client.select_header_content_type(
                        ['application/json', 'application/yaml', 'application/vnd.kubernetes.protobuf']
                    )
                },
                response_type='V1APIResourceList',
                auth_settings=['BearerToken'],
                _return_http_data_only=True,
                _preload_content=True,
            )
        except ApiException as e:
            if e.reason == 'Not Found':
                log.error('The resource definition with the specified version was not found')
                return None

            log.error('{}'.format(add_indent(e.body)))
            raise ProvisioningError(e) 
Example #23
Source File: mocks.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def delete_namespaced_deployment(self, name, body, namespace):
        if self.name == 'fail':
            raise ApiException('Delete deployment fail')
        if self.name == '404' or name == '404':
            raise ApiException(reason='Not Found')

        if self.name == 'test1' or name == 'test1':
            my_response = namedtuple('my_response', 'message')
            return my_response(message='Failed')

        if self.name == 'test2' or name == 'test2':
            my_response = namedtuple('my_response', 'message')
            return my_response(message=None)

        return {'key1': 'value1'}

    # Service 
Example #24
Source File: lock.py    From armada with Apache License 2.0 6 votes vote down vote up
def delete_lock(self):
        """Deletes the Lock custom resource

        :return: whether it was successfully deleted
        :rtype: bool
        """
        try:
            self.k8s.delete_custom_resource(
                group=LOCK_GROUP,
                version=LOCK_VERSION,
                namespace=LOCK_NAMESPACE,
                plural=LOCK_PLURAL,
                name=self.full_name,
                body=self.delete_options)

            return True
        except ApiException as err:
            # If it returns 404 then something else deleted it
            if err.status == 404:
                return True
            raise 
Example #25
Source File: lock.py    From armada with Apache License 2.0 6 votes vote down vote up
def get_lock(self):
        """Retrieves the Lock custom resource object

        :return: the Lock custom resource object
        :rtype: object
        """
        try:
            return self.k8s.read_custom_resource(
                group=LOCK_GROUP,
                version=LOCK_VERSION,
                namespace=LOCK_NAMESPACE,
                plural=LOCK_PLURAL,
                name=self.full_name)

        except ApiException as err:
            if err.status == 404:
                return None
            raise 
Example #26
Source File: mocks.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def read_namespaced_job(self, name, namespace):
        if self.name == 'fail':
            raise ApiException('Get daemonset fail')
        if self.name == '404':
            raise ApiException(reason='Not Found')

        my_response = namedtuple('my_response', 'metadata status')
        my_status = namedtuple('my_status', 'failed conditions')

        if self.name == 'test1':
            return my_response(metadata={}, status=my_status(failed='Failed',
                                                             conditions=[]))
        if self.name == 'test2':
            my_conditions = namedtuple('my_conditions', 'type')
            return my_response(metadata={}, status=my_status(failed=None,
                                                             conditions=[my_conditions(type='Failed')]))
        if self.name == 'test3':
            my_conditions = namedtuple('my_conditions', 'type')
            return my_response(metadata={}, status=my_status(failed=None,
                                                             conditions=[my_conditions(type='Complete')]))

        return my_response(metadata={'key1': 'value1'}, status={'key1': 'value1'})

    # StorageClass 
Example #27
Source File: mocks.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def read_storage_class(self, name):
        if self.name == 'fail':
            raise ApiException('Get storage class fail')
        if self.name == '404' or name == '404':
            raise ApiException(reason='Not Found')

        my_response = namedtuple('my_response', 'metadata status')
        my_status = namedtuple('my_status',
                               'replicas available_replicas ready_replicas updated_replicas unavailable_replicas')

        if self.name == 'test1':
            return my_response(metadata={}, status=my_status(replicas=3,
                                                             available_replicas=2,
                                                             ready_replicas=1,
                                                             updated_replicas=None,
                                                             unavailable_replicas=1))
        if self.name == 'test2' or name == 'test2':
            return my_response(metadata={}, status=my_status(replicas=1,
                                                             available_replicas=1,
                                                             ready_replicas=1,
                                                             updated_replicas=1,
                                                             unavailable_replicas=None))

        return my_response(metadata={'key1': 'value1'}, status={'key1': 'value1'}) 
Example #28
Source File: kubernetes_executor.py    From airflow with Apache License 2.0 6 votes vote down vote up
def _inject_secrets(self) -> None:
        def _create_or_update_secret(secret_name, secret_path):
            try:
                return self.kube_client.create_namespaced_secret(
                    self.kube_config.executor_namespace, kubernetes.client.V1Secret(
                        data={
                            'key.json': base64.b64encode(open(secret_path, 'r').read())},
                        metadata=kubernetes.client.V1ObjectMeta(name=secret_name)),
                    **self.kube_config.kube_client_request_args)
            except ApiException as e:
                if e.status == 409:
                    return self.kube_client.replace_namespaced_secret(
                        secret_name, self.kube_config.executor_namespace,
                        kubernetes.client.V1Secret(
                            data={'key.json': base64.b64encode(
                                open(secret_path, 'r').read())},
                            metadata=kubernetes.client.V1ObjectMeta(name=secret_name)),
                        **self.kube_config.kube_client_request_args)
                self.log.exception(
                    'Exception while trying to inject secret. '
                    'Secret name: %s, error details: %s',
                    secret_name, e
                )
                raise 
Example #29
Source File: mocks.py    From k8s-handle with Apache License 2.0 6 votes vote down vote up
def delete_storage_class(self, name, body):
        if self.name == 'fail':
            raise ApiException('Delete storage class fail')
        if self.name == '404' or name == '404':
            raise ApiException(reason='Not Found')

        if self.name == 'test1' or name == 'test1':
            my_response = namedtuple('my_response', 'message')
            return my_response(message='Failed')

        if self.name == 'test2' or name == 'test2':
            my_response = namedtuple('my_response', 'message')
            return my_response(message=None)

        return {'key1': 'value1'}

    # PersistentVolumeClaim 
Example #30
Source File: test_kubernetes_pod_operator.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_faulty_service_account(self):
        bad_service_account_name = "foobar"
        k = KubernetesPodOperator(
            namespace='default',
            image="ubuntu:16.04",
            cmds=["bash", "-cx"],
            arguments=["echo 10"],
            labels={"foo": "bar"},
            name="test",
            task_id="task",
            in_cluster=False,
            do_xcom_push=False,
            startup_timeout_seconds=5,
            service_account_name=bad_service_account_name,
        )
        with self.assertRaises(ApiException):
            context = create_context(k)
            k.execute(context)
            actual_pod = self.api_client.sanitize_for_serialization(k.pod)
            self.expected_pod['spec']['serviceAccountName'] = bad_service_account_name
            self.assertEqual(self.expected_pod, actual_pod)