Source code for airflow.providers.cncf.kubernetes.operators.job
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Executes a Kubernetes Job."""from__future__importannotationsimportcopyimportjsonimportloggingimportosfromcollections.abcimportSequencefromfunctoolsimportcached_propertyfromtypingimportTYPE_CHECKINGfromkubernetes.clientimportBatchV1Api,modelsask8sfromkubernetes.client.api_clientimportApiClientfromkubernetes.client.restimportApiExceptionfromairflow.configurationimportconffromairflow.exceptionsimportAirflowExceptionfromairflow.modelsimportBaseOperatorfromairflow.providers.cncf.kubernetes.hooks.kubernetesimportKubernetesHookfromairflow.providers.cncf.kubernetes.kubernetes_helper_functionsimport(add_unique_suffix,create_unique_id,)fromairflow.providers.cncf.kubernetes.operators.podimportKubernetesPodOperatorfromairflow.providers.cncf.kubernetes.pod_generatorimportPodGenerator,merge_objectsfromairflow.providers.cncf.kubernetes.triggers.jobimportKubernetesJobTriggerfromairflow.providers.cncf.kubernetes.utils.pod_managerimportEMPTY_XCOM_RESULT,PodNotFoundExceptionfromairflow.utilsimportyamlfromairflow.utils.contextimportContextifTYPE_CHECKING:fromairflow.utils.contextimportContext
[docs]classKubernetesJobOperator(KubernetesPodOperator):""" Executes a Kubernetes Job. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:KubernetesJobOperator` .. note:: If you use `Google Kubernetes Engine <https://cloud.google.com/kubernetes-engine/>`__ and Airflow is not running in the same cluster, consider using :class:`~airflow.providers.google.cloud.operators.kubernetes_engine.GKEStartJobOperator`, which simplifies the authorization process. :param job_template_file: path to job template file (templated) :param full_job_spec: The complete JodSpec :param backoff_limit: Specifies the number of retries before marking this job failed. Defaults to 6 :param completion_mode: CompletionMode specifies how Pod completions are tracked. It can be `NonIndexed` (default) or `Indexed`. :param completions: Specifies the desired number of successfully finished pods the job should be run with. :param manual_selector: manualSelector controls generation of pod labels and pod selectors. :param parallelism: Specifies the maximum desired number of pods the job should run at any given time. :param selector: The selector of this V1JobSpec. :param suspend: Suspend specifies whether the Job controller should create Pods or not. :param ttl_seconds_after_finished: ttlSecondsAfterFinished limits the lifetime of a Job that has finished execution (either Complete or Failed). :param wait_until_job_complete: Whether to wait until started job finished execution (either Complete or Failed). Default is False. :param job_poll_interval: Interval in seconds between polling the job status. Default is 10. Used if the parameter `wait_until_job_complete` set True. :param deferrable: Run operator in the deferrable mode. Note that the parameter `wait_until_job_complete` must be set True. """
[docs]defexecute(self,context:Context):ifself.deferrableandnotself.wait_until_job_complete:self.log.warning("Deferrable mode is available only with parameter `wait_until_job_complete=True`. ""Please, set it up.")if(self.get_logsorself.do_xcom_push)andnotself.wait_until_job_complete:self.log.warning("Getting Logs and pushing to XCom are available only with parameter `wait_until_job_complete=True`. ""Please, set it up.")self.job_request_obj=self.build_job_request_obj(context)self.job=self.create_job(# must set `self.job` for `on_kill`job_request_obj=self.job_request_obj)ti=context["ti"]ti.xcom_push(key="job_name",value=self.job.metadata.name)ti.xcom_push(key="job_namespace",value=self.job.metadata.namespace)self.pod:k8s.V1Pod|Noneifself.podisNone:self.pod=self.get_or_create_pod(# must set `self.pod` for `on_kill`pod_request_obj=self.pod_request_obj,context=context,)ifself.wait_until_job_completeandself.deferrable:self.execute_deferrable()returnifself.wait_until_job_complete:ifself.do_xcom_push:self.pod_manager.await_container_completion(pod=self.pod,container_name=self.base_container_name)self.pod_manager.await_xcom_sidecar_container_start(pod=self.pod)xcom_result=self.extract_xcom(pod=self.pod)self.job=self.hook.wait_until_job_complete(job_name=self.job.metadata.name,namespace=self.job.metadata.namespace,job_poll_interval=self.job_poll_interval,)ifself.get_logs:self.pod_manager.fetch_requested_container_logs(pod=self.pod,containers=self.container_logs,follow_logs=True,)ti.xcom_push(key="job",value=self.job.to_dict())ifself.wait_until_job_complete:iferror_message:=self.hook.is_job_failed(job=self.job):raiseAirflowException(f"Kubernetes job '{self.job.metadata.name}' is failed with error '{error_message}'")ifself.do_xcom_push:returnxcom_result
[docs]defexecute_complete(self,context:Context,event:dict,**kwargs):ti=context["ti"]ti.xcom_push(key="job",value=event["job"])ifevent["status"]=="error":raiseAirflowException(event["message"])ifself.get_logs:pod_name=event["pod_name"]pod_namespace=event["pod_namespace"]self.pod=self.hook.get_pod(pod_name,pod_namespace)ifnotself.pod:raisePodNotFoundException("Could not find pod after resuming from deferral")self._write_logs(self.pod)ifself.do_xcom_push:xcom_result=event["xcom_result"]ifisinstance(xcom_result,str)andxcom_result.rstrip()==EMPTY_XCOM_RESULT:self.log.info("xcom result file is empty.")returnNoneself.log.info("xcom result: \n%s",xcom_result)returnjson.loads(xcom_result)
@staticmethod
[docs]defdeserialize_job_template_file(path:str)->k8s.V1Job:""" Generate a Job from a file. Unfortunately we need access to the private method ``_ApiClient__deserialize_model`` from the kubernetes client. This issue is tracked here: https://github.com/kubernetes-client/python/issues/977. :param path: Path to the file :return: a kubernetes.client.models.V1Job """ifos.path.exists(path):withopen(path)asstream:job=yaml.safe_load(stream)else:job=Nonelog.warning("Template file %s does not exist",path)api_client=ApiClient()returnapi_client._ApiClient__deserialize_model(job,k8s.V1Job)
[docs]defbuild_job_request_obj(self,context:Context|None=None)->k8s.V1Job:""" Return V1Job object based on job template file, full job spec, and other operator parameters. The V1Job attributes are derived (in order of precedence) from operator params, full job spec, job template file. """self.log.debug("Creating job for KubernetesJobOperator task %s",self.task_id)ifself.job_template_file:self.log.debug("Job template file found, will parse for base job")job_template=self.deserialize_job_template_file(self.job_template_file)ifself.full_job_spec:job_template=self.reconcile_jobs(job_template,self.full_job_spec)elifself.full_job_spec:job_template=self.full_job_specelse:job_template=k8s.V1Job(metadata=k8s.V1ObjectMeta())pod_template=super().build_pod_request_obj(context)pod_template_spec=k8s.V1PodTemplateSpec(metadata=pod_template.metadata,spec=pod_template.spec,)self.pod_request_obj=pod_templatejob=k8s.V1Job(api_version="batch/v1",kind="Job",metadata=k8s.V1ObjectMeta(namespace=self.namespace,labels=self.labels,name=self.name,annotations=self.annotations,),spec=k8s.V1JobSpec(active_deadline_seconds=self.active_deadline_seconds,backoff_limit=self.backoff_limit,completion_mode=self.completion_mode,completions=self.completions,manual_selector=self.manual_selector,parallelism=self.parallelism,selector=self.selector,suspend=self.suspend,template=pod_template_spec,ttl_seconds_after_finished=self.ttl_seconds_after_finished,),)job=self.reconcile_jobs(job_template,job)ifnotjob.metadata.name:job.metadata.name=create_unique_id(task_id=self.task_id,unique=self.random_name_suffix,max_length=80)elifself.random_name_suffix:# user has supplied job name, we're just adding suffixjob.metadata.name=add_unique_suffix(name=job.metadata.name)job.metadata.name=f"job-{job.metadata.name}"ifnotjob.metadata.namespace:hook_namespace=self.hook.get_namespace()job_namespace=self.namespaceorhook_namespaceorself._incluster_namespaceor"default"job.metadata.namespace=job_namespaceself.log.info("Building job %s ",job.metadata.name)returnjob
@staticmethod
[docs]defreconcile_jobs(base_job:k8s.V1Job,client_job:k8s.V1Job|None)->k8s.V1Job:""" Merge Kubernetes Job objects. :param base_job: has the base attributes which are overwritten if they exist in the client job and remain if they do not exist in the client_job :param client_job: the job that the client wants to create. :return: the merged jobs This can't be done recursively as certain fields are overwritten and some are concatenated. """ifclient_jobisNone:returnbase_jobclient_job_cp=copy.deepcopy(client_job)client_job_cp.spec=KubernetesJobOperator.reconcile_job_specs(base_job.spec,client_job_cp.spec)client_job_cp.metadata=PodGenerator.reconcile_metadata(base_job.metadata,client_job_cp.metadata)client_job_cp=merge_objects(base_job,client_job_cp)returnclient_job_cp
@staticmethod
[docs]defreconcile_job_specs(base_spec:k8s.V1JobSpec|None,client_spec:k8s.V1JobSpec|None)->k8s.V1JobSpec|None:""" Merge Kubernetes JobSpec objects. :param base_spec: has the base attributes which are overwritten if they exist in the client_spec and remain if they do not exist in the client_spec :param client_spec: the spec that the client wants to create. :return: the merged specs """ifbase_specandnotclient_spec:returnbase_specifnotbase_specandclient_spec:returnclient_specelifclient_specandbase_spec:client_spec.template.spec=PodGenerator.reconcile_specs(base_spec.template.spec,client_spec.template.spec)client_spec.template.metadata=PodGenerator.reconcile_metadata(base_spec.template.metadata,client_spec.template.metadata)returnmerge_objects(base_spec,client_spec)returnNone
[docs]classKubernetesDeleteJobOperator(BaseOperator):""" Delete a Kubernetes Job. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:KubernetesDeleteJobOperator` :param name: name of the Job. :param namespace: the namespace to run within kubernetes. :param kubernetes_conn_id: The :ref:`kubernetes connection id <howto/connection:kubernetes>` for the Kubernetes cluster. :param config_file: The path to the Kubernetes config file. (templated) If not specified, default value is ``~/.kube/config`` :param in_cluster: run kubernetes client with in_cluster configuration. :param cluster_context: context that points to kubernetes cluster. Ignored when in_cluster is True. If None, current-context is used. (templated) :param delete_on_status: Condition for performing delete operation depending on the job status. Values: ``None`` - delete the job regardless of its status, "Complete" - delete only successfully completed jobs, "Failed" - delete only failed jobs. (default: ``None``) :param wait_for_completion: Whether to wait for the job to complete. (default: ``False``) :param poll_interval: Interval in seconds between polling the job status. Used when the `delete_on_status` parameter is set. (default: 10.0) """
[docs]defexecute(self,context:Context):try:ifself.delete_on_statusnotin("Complete","Failed",None):raiseAirflowException("The `delete_on_status` parameter must be one of 'Complete', 'Failed' or None. ""The current value is %s",str(self.delete_on_status),)ifself.wait_for_completion:job=self.hook.wait_until_job_complete(job_name=self.name,namespace=self.namespace,job_poll_interval=self.poll_interval)else:job=self.hook.get_job_status(job_name=self.name,namespace=self.namespace)if(self.delete_on_statusisNoneor(self.delete_on_status=="Complete"andself.hook.is_job_successful(job=job))or(self.delete_on_status=="Failed"andself.hook.is_job_failed(job=job))):self.log.info("Deleting kubernetes Job: %s",self.name)self.client.delete_namespaced_job(name=self.name,namespace=self.namespace)self.log.info("Kubernetes job was deleted.")else:self.log.info("Deletion of the job %s was skipped due to settings of on_status=%s",self.name,self.delete_on_status,)exceptApiExceptionase:ife.status==404:self.log.info("The Kubernetes job %s does not exist.",self.name)else:raisee
[docs]classKubernetesPatchJobOperator(BaseOperator):""" Update a Kubernetes Job. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:KubernetesPatchJobOperator` :param name: name of the Job :param namespace: the namespace to run within kubernetes :param body: Job json object with parameters for update https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.25/#job-v1-batch e.g. ``{"spec": {"suspend": True}}`` :param kubernetes_conn_id: The :ref:`kubernetes connection id <howto/connection:kubernetes>` for the Kubernetes cluster. :param config_file: The path to the Kubernetes config file. (templated) If not specified, default value is ``~/.kube/config`` :param in_cluster: run kubernetes client with in_cluster configuration. :param cluster_context: context that points to kubernetes cluster. Ignored when in_cluster is True. If None, current-context is used. (templated) """