dimberman commented on a change in pull request #6377:
URL: https://github.com/apache/airflow/pull/6377#discussion_r421113363
##########
File path: airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py
##########
@@ -235,84 +259,45 @@ def execute(self, context):
client =
kube_client.get_kube_client(cluster_context=self.cluster_context,
config_file=self.config_file)
- if not (self.full_pod_spec or self.pod_template_file):
- # Add Airflow Version to the label
- # And a label to identify that pod is launched by
KubernetesPodOperator
- self.labels.update(
- {
- 'airflow_version': airflow_version.replace('+', '-'),
- 'kubernetes_pod_operator': 'True',
- }
- )
- 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,
- schedulername=self.schedulername,
- init_containers=self.init_containers,
- restart_policy='Never',
- priority_class_name=self.priority_class_name,
- pod_template_file=self.pod_template_file,
- pod=self.full_pod_spec,
- ).gen_pod()
+ # Add combination of labels to uniquely identify a running pod
+ labels = self.create_labels_for_pod(context)
- pod = append_to_pod(
- pod,
- self.pod_runtime_info_envs +
- self.ports +
- self.resources +
- self.secrets +
- self.volumes +
- self.volume_mounts
- )
+ label_selector = self._get_pod_identifying_label_string(labels)
- self.pod = pod
+ pod_list = client.list_namespaced_pod(self.namespace,
label_selector=label_selector)
- launcher = pod_launcher.PodLauncher(kube_client=client,
- extract_xcom=self.do_xcom_push)
+ if len(pod_list.items) > 1:
+ raise AirflowException(
+ 'More than one pod running with labels: '
+ '{label_selector}'.format(label_selector=label_selector))
- try:
- (final_state, result) = launcher.run_pod(
- pod,
- startup_timeout=self.startup_timeout_seconds,
- get_logs=self.get_logs)
- except AirflowException:
- if self.log_events_on_failure:
- for event in launcher.read_pod_events(pod).items:
- self.log.error("Pod Event: %s - %s", event.reason,
event.message)
- raise
- finally:
- if self.is_delete_operator_pod:
- launcher.delete_pod(pod)
+ launcher = pod_launcher.PodLauncher(kube_client=client,
extract_xcom=self.do_xcom_push)
+ if len(pod_list.items) == 1 and
self._try_numbers_do_not_match(context, pod_list.items[0]):
+ launcher.delete_pod(pod_list.items[0])
+ final_state, _, result =
self.create_new_pod_for_operator(labels, launcher)
+ elif len(pod_list.items) == 1:
+ self.log.info("found a running pod with labels %s."
+ "Will monitor instead of starting new one",
labels)
+ final_state, result = self.monitor_launched_pod(launcher,
pod_list[0])
+ else:
+ final_state, _, result =
self.create_new_pod_for_operator(labels, launcher)
if final_state != State.SUCCESS:
- if self.log_events_on_failure:
- for event in launcher.read_pod_events(pod).items:
- self.log.error("Pod Event: %s - %s", event.reason,
event.message)
raise AirflowException(
- 'Pod returned a failure: {state}'.format(state=final_state)
- )
-
+ 'Pod returned a failure:
{state}'.format(state=final_state))
return result
except AirflowException as ex:
raise AirflowException('Pod Launching failed:
{error}'.format(error=ex))
+ @staticmethod
+ def _get_pod_identifying_label_string(labels):
+ filtered_labels = {label_id: label for label_id, label in
labels.items() if label_id != 'try_number'}
Review comment:
@kaxil why do we need to sort? this is for creating the label_selector
for kubernetes which I don't believe is order specific.
----------------------------------------------------------------
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:
[email protected]