async def terminate_resource(self, resource_attributes: AttributeDict): response = None try: response = await self.client.delete_namespaced_deployment( name=resource_attributes.drone_uuid, namespace=self.machine_type_configuration.namespace, body=k8s_client.V1DeleteOptions( propagation_policy="Foreground", grace_period_seconds=5 ), ) except K8SApiException as ex: if ex.status != 404: logger.warning(f"deleting deployment failed: {ex}") raise if self.machine_type_configuration.hpa: try: await self.hpa_client.delete_namespaced_horizontal_pod_autoscaler( name=resource_attributes.drone_uuid, namespace=self.machine_type_configuration.namespace, ) except K8SApiException as ex: if ex.status != 404: logger.warning(f"deleting hpa failed: {ex}") raise return response
def test_terminate_resource(self, mocked_aiohttp): run_async( self.kubernetes_adapter.terminate_resource, resource_attributes=AttributeDict(drone_uuid="testsite-089123"), ) self.mock_kubernetes_api.return_value.delete_namespaced_deployment.assert_called_with( # noqa: B950 name="testsite-089123", namespace="default", body=client.V1DeleteOptions( propagation_policy="Foreground", grace_period_seconds=5 ), ) self.update_delete_side_effect(exception=K8SApiException(status=500)) with self.assertLogs(level=logging.WARNING): with self.assertRaises(K8SApiException): run_async( self.kubernetes_adapter.terminate_resource, resource_attributes=AttributeDict(drone_uuid="testsite-089123"), ) self.update_delete_side_effect(exception=None) self.update_delete_hpa_side_effect(exception=K8SApiException(status=500)) with self.assertLogs(level=logging.WARNING): with self.assertRaises(K8SApiException): run_async( self.kubernetes_adapter.terminate_resource, resource_attributes=AttributeDict(drone_uuid="testsite-089123"), ) self.update_delete_hpa_side_effect(exception=None)
async def do_delete_broken(self): v1 = client.CoreV1Api() to_delete = await self.list_pods(lambda conds: 'Ready' not in conds) confirm = input('Do you want to delete these pods [YES/NO]?') if confirm == 'YES': for i, pod in enumerate(to_delete): pod_namespace = pod.metadata.namespace pod_name = pod.metadata.name print('{}/{} {}/{} deleting...'.format(i + 1, len(to_delete), pod_namespace, pod_name)) await v1.delete_namespaced_pod(pod_name, pod_namespace, client.V1DeleteOptions())
async def delete_custom_object(self, name, group, version, plural, reraise=False): try: await self.k8s_custom_object_api.delete_namespaced_custom_object( name=name, group=group, version=version, plural=plural, namespace=self.namespace, body=client.V1DeleteOptions(), ) logger.debug("Custom object `{}` deleted".format(name)) except ApiException as e: if reraise: raise PolyaxonK8SError("Connection error: %s" % e) from e else: logger.debug("Custom object `{}` was not found".format(name))