def clean_experiment_pods(): pod_names = (x.metadata.name for x in running_experiment_pods(include_attacker=True)) for pod_name in pod_names: print("Deleting " + pod_name) runner.api.delete_namespaced_pod(pod_name, "default", V1DeleteOptions())
def invokeApi(k8sApi, action, obj, **args): """find a suitalbe function and perform the actual API invocation. @param k8sApi: client object for the invocation, wired to correct API version @param action: either 'create' (to inject a new objet) or 'replace','patch','delete' @param obj: the full object spec to be passed into the API invocation @param args: (optional) extraneous arguments to pass @return: response object from Kubernetes API call """ # transform ActionType from Yaml into action_type for swagger API kind = camel2snake(obj["kind"]) # determine namespace to place the object in, supply default try: namespace = obj["metadata"]["namespace"] except: namespace = "default" functionName = "%s_%s" % (action, kind) if hasattr(k8sApi, functionName): # namespace agnostic API function = getattr(k8sApi, functionName) else: functionName = "%s_namespaced_%s" % (action, kind) function = getattr(k8sApi, functionName) args["namespace"] = namespace if not "create" in functionName: args["name"] = obj["metadata"]["name"] if "delete" in functionName: from kubernetes.client.models.v1_delete_options import V1DeleteOptions obj = V1DeleteOptions() return function(body=obj, **args)
def stop_batch_workflow_run(self): """Stop a batch workflow run along with all its dependent jobs.""" workflow_run_name = self._workflow_run_name_generator("batch") to_delete = self.get_workflow_running_jobs_as_backend_ids() + [ workflow_run_name ] error = False for job in to_delete: try: current_k8s_batchv1_api_client.delete_namespaced_job( job, REANA_RUNTIME_KUBERNETES_NAMESPACE, body=V1DeleteOptions(grace_period_seconds=0, propagation_policy="Background"), ) except ApiException: logging.error( f"Error while trying to stop {self.workflow.id_}" f": Kubernetes job {job} could not be deleted.", exc_info=True, ) error = True continue if error: raise REANAWorkflowStopError( f"Workflow {self.workflow.id_} could not be stopped.")
def evict_pod(self, pod): '''Trigger the eviction process for a single pod. Args: - pod: the Kubernetes API pod object to evict Returns: None Raises: CommandExecutionError in case of API error ''' delete_options = V1DeleteOptions() if self.grace_period >= 0: delete_options.grace_period = self.grace_period object_meta = V1ObjectMeta(name=pod.metadata.name, namespace=pod.metadata.namespace) eviction = V1beta1Eviction( delete_options=delete_options, metadata=object_meta, ) api_call = "CoreV1Api->create_namespaced_pod_eviction" api_instance = kubernetes.client.CoreV1Api() try: return api_instance.create_namespaced_pod_eviction( name=eviction.metadata.name, namespace=eviction.metadata.namespace, body=eviction, ) except (ApiException, HTTPError) as exc: if isinstance(exc, ApiException) and exc.status == 404: return None log.exception('Exception when calling %s', api_call) raise CommandExecutionError(exc)
def stop_batch_workflow_run(self): """Stop a batch workflow run along with all its dependent jobs.""" workflow_run_name = self._workflow_run_name_generator('batch') to_delete = self.get_workflow_running_jobs_as_backend_ids() + \ [workflow_run_name] for job in to_delete: current_k8s_batchv1_api_client.delete_namespaced_job( job, KubernetesWorkflowRunManager.default_namespace, body=V1DeleteOptions(propagation_policy='Background'))
def stop_batch_workflow_run(self, workflow_run_jobs=None): """Stop a batch workflow run along with all its dependent jobs. :param workflow_run_jobs: List of active job id's spawned by the workflow run. """ workflow_run_name = self._workflow_run_name_generator('batch') workflow_run_jobs = workflow_run_jobs or [] to_delete = workflow_run_jobs + [workflow_run_name] for job in to_delete: current_k8s_batchv1_api_client.delete_namespaced_job( job, KubernetesWorkflowRunManager.default_namespace, V1DeleteOptions(propagation_policy='Background'))
def __delete_resource(self, gvk, name, namespace='default', **kwargs): try: delete_options = V1DeleteOptions() if self.model_registry.requires_namespace(gvk): if namespace is None: namespace = 'default' return self.model_registry.invoke_delete_api( gvk, name=name, namespace=namespace, body=delete_options, **kwargs) else: return self.model_registry.invoke_delete_api( gvk, name=name, body=delete_options, **kwargs) except ApiException, ex: model = self.parser.parse(ex.body) return model
def stop(backend_job_id, asynchronous=True): """Stop Kubernetes job execution. :param backend_job_id: Kubernetes job id. :param asynchronous: Whether the function waits for the action to be performed or does it asynchronously. """ try: propagation_policy = 'Background' if asynchronous else 'Foreground' delete_options = V1DeleteOptions( propagation_policy=propagation_policy) current_k8s_batchv1_api_client.delete_namespaced_job( backend_job_id, K8S_DEFAULT_NAMESPACE, body=delete_options) except ApiException as e: logging.error( 'An error has occurred while connecting to Kubernetes API ' 'Server \n {}'.format(e)) raise ComputingBackendSubmissionError(e.reason)
def k8s_delete_job(job, asynchronous=True): """Delete Kubernetes job. :param job: The :class:`kubernetes.client.models.v1_job.V1Job` to be deleted. :param asynchronous: Whether the function waits for the action to be performed or does it asynchronously. """ try: propagation_policy = 'Background' if asynchronous else 'Foreground' delete_options = V1DeleteOptions(propagation_policy=propagation_policy) current_k8s_batchv1_api_client.delete_namespaced_job( job.metadata.name, job.metadata.namespace, delete_options) except ApiException as e: logging.error( 'An error has occurred while connecting to Kubernetes API Server' ' \n {}'.format(e)) raise ComputingBackendSubmissionError(e.reason)
def evict_pod(name, namespace='default', grace_period=1, **kwargs): '''Trigger the eviction process for a single pod. Args: - name : the name of the pod to evict - namespace : the namespace of the pod to evict - grace_period : the time to wait before killing the pod Returns: api response Raises: CommandExecutionError in case of API error ''' kind_info = __utils__['metalk8s_kubernetes.get_kind_info']({ 'kind': 'PodEviction', 'apiVersion': 'v1' }) delete_options = V1DeleteOptions() if grace_period >= 0: delete_options.grace_period = grace_period object_meta = V1ObjectMeta(name=name, namespace=namespace) kubeconfig, context = __salt__['metalk8s_kubernetes.get_kubeconfig']( **kwargs) client = kind_info.client client.configure(config_file=kubeconfig, context=context) try: result = client.create(name=name, namespace=namespace, body=V1beta1Eviction( delete_options=delete_options, metadata=object_meta)) except (ApiException, HTTPError) as exc: if isinstance(exc, ApiException) and exc.status == 404: return None raise CommandExecutionError( 'Failed to evict pod "{}" in namespace "{}": {!s}'.format( name, namespace, exc)) return result.to_dict()
def test_delete_secret(): cont = CredStashController("none", "none", "none", "none", "none") cont.v1core = MagicMock() credstash_secret = { "metadata": { "namespace": "test", "name": "boom" }, "spec": [], } metadata = MagicMock() metadata.annotations = {"credstash-fully-managed": "true"} obj = MagicMock() obj.metadata = metadata cont.v1core.read_namespaced_secret = MagicMock(return_value=obj) cont.delete_secret(credstash_secret, resource_version=1) cont.v1core.delete_namespaced_secret.assert_called_once_with( "boom", "test", V1DeleteOptions())
def stop_batch_workflow_run(self): """Stop a batch workflow run along with all its dependent jobs.""" workflow_run_name = self._workflow_run_name_generator('batch') to_delete = self.get_workflow_running_jobs_as_backend_ids() + \ [workflow_run_name] error = False for job in to_delete: try: current_k8s_batchv1_api_client.delete_namespaced_job( job, KubernetesWorkflowRunManager.default_namespace, body=V1DeleteOptions(propagation_policy='Background')) except ApiException: logging.error( f'Error while trying to stop {self.workflow.id_}' f': Kubernetes job {job} could not be deleted.', exc_info=True) error = True continue if error: raise REANAWorkflowStopError( f'Workflow {self.workflow.id_} could not be stopped.')