def _adopt_completed_pods(self, kube_client: client.CoreV1Api) -> None: """ Patch completed pod so that the KubernetesJobWatcher can delete it. :param kube_client: kubernetes client for speaking to kube API """ kwargs = { 'field_selector': "status.phase=Succeeded", 'label_selector': 'kubernetes_executor=True', } pod_list = kube_client.list_namespaced_pod( namespace=self.kube_config.kube_namespace, **kwargs) for pod in pod_list.items: self.log.info("Attempting to adopt pod %s", pod.metadata.name) pod.metadata.labels[ 'airflow-worker'] = pod_generator.make_safe_label_value( str(self.scheduler_job_id)) try: kube_client.patch_namespaced_pod( name=pod.metadata.name, namespace=pod.metadata.namespace, body=PodGenerator.serialize_pod(pod), ) except ApiException as e: self.log.info("Failed to adopt pod %s. Reason: %s", pod.metadata.name, e)
def adopt_launched_task(self, kube_client: client.CoreV1Api, pod: k8s.V1Pod, pod_ids: Dict[TaskInstanceKey, k8s.V1Pod]) -> None: """ Patch existing pod so that the current KubernetesJobWatcher can monitor it via label selectors :param kube_client: kubernetes client for speaking to kube API :param pod: V1Pod spec that we will patch with new label :param pod_ids: pod_ids we expect to patch. """ self.log.info("attempting to adopt pod %s", pod.metadata.name) pod.metadata.labels[ 'airflow-worker'] = pod_generator.make_safe_label_value( str(self.scheduler_job_id)) pod_id = annotations_to_key(pod.metadata.annotations) if pod_id not in pod_ids: self.log.error( "attempting to adopt taskinstance which was not specified by database: %s", pod_id) return try: kube_client.patch_namespaced_pod( name=pod.metadata.name, namespace=pod.metadata.namespace, body=PodGenerator.serialize_pod(pod), ) pod_ids.pop(pod_id) self.running.add(pod_id) except ApiException as e: self.log.info("Failed to adopt pod %s. Reason: %s", pod.metadata.name, e)