ashb commented on a change in pull request #6377: [AIRFLOW-5589] monitor pods by labels instead of names URL: https://github.com/apache/airflow/pull/6377#discussion_r337075124
########## File path: airflow/contrib/operators/kubernetes_pod_operator.py ########## @@ -112,55 +113,54 @@ class KubernetesPodOperator(BaseOperator): # pylint: disable=too-many-instance- """ template_fields = ('cmds', 'arguments', 'env_vars', 'config_file') + @staticmethod + def create_labels_for_pod(context): + """ + Generate labels for the pod s.t. we can track it in case of Operator crash + :param context: + :return: + """ + labels = { + 'dag_id': context['dag'].dag_id, + 'task_id': context['task'].task_id, + 'exec_date': context['ts'] + } + # In the case of sub dags this is just useful + if context['dag'].parent_dag: + labels['parent_dag_id'] = context['dag'].parent_dag.dag_id + # Replace unsupported characters with dashes & trim at 63 chars (max allowed) + for label_id, label in labels.items(): + safe_label = pod_generator.make_safe_label_value(label) + labels[label_id] = safe_label + return labels + def execute(self, context): try: client = kube_client.get_kube_client(in_cluster=self.in_cluster, cluster_context=self.cluster_context, config_file=self.config_file) - pod = pod_generator.PodGenerator( - image=self.image, - namespace=self.namespace, - cmds=self.cmds, - args=self.arguments, - labels=self.labels, - name=self.name, - envs=self.env_vars, - extract_xcom=self.do_xcom_push, - image_pull_policy=self.image_pull_policy, - node_selectors=self.node_selectors, - annotations=self.annotations, - affinity=self.affinity, - image_pull_secrets=self.image_pull_secrets, - service_account_name=self.service_account_name, - hostnetwork=self.hostnetwork, - tolerations=self.tolerations, - configmaps=self.configmaps, - security_context=self.security_context, - dnspolicy=self.dnspolicy, - resources=self.resources, - pod=self.full_pod_spec, - ).gen_pod() - - pod = append_to_pod(pod, self.ports) - pod = append_to_pod(pod, self.pod_runtime_info_envs) - pod = append_to_pod(pod, self.volumes) - pod = append_to_pod(pod, self.volume_mounts) - pod = append_to_pod(pod, self.secrets) + # Add combination of labels to uniquely identify a running pod + labels = self.create_labels_for_pod(context) - self.pod = pod + label_selector = ','.join([label_id + '=' + label for label_id, label in labels.items()]) + + pod_list = client.list_namespaced_pod(self.namespace, label_selector=label_selector) + + if len(pod_list.items) > 1: + raise AirflowException( + 'More than one pod running with labels: ' + '{label_selector}'.format(label_selector=label_selector)) + + launcher = pod_launcher.PodLauncher(kube_client=client, extract_xcom=self.do_xcom_push) - launcher = pod_launcher.PodLauncher(kube_client=client, - extract_xcom=self.do_xcom_push) + if len(pod_list.items) == 1: + pod = pod_list.items[0] + final_state, result = self.monitor_launched_pod(launcher, pod) Review comment: Add an info log line here about found/monitoring existing pod ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services