# Buggy code
def _convert_to_airflow_pod(pod):
    """
    Converts a k8s V1Pod object into an `airflow.kubernetes.pod.Pod` object.
    This function is purely for backwards compatibility
    """
    base_container = pod.spec.containers[0]  # type: k8s.V1Container
    env_vars, secrets = _extract_env_vars_and_secrets(base_container.env)
    volumes = _extract_volumes(pod.spec.volumes)
    api_client = ApiClient()
    init_containers = pod.spec.init_containers
    image_pull_secrets = pod.spec.image_pull_secrets or []
    if pod.spec.init_containers is not None:
        init_containers = [
            api_client.sanitize_for_serialization(i) for i in pod.spec.init_containers
        ]
    dummy_pod = Pod(
        image=base_container.image,
        envs=env_vars,
        cmds=base_container.command,
        args=base_container.args,
        labels=pod.metadata.labels,
        annotations=pod.metadata.annotations,
        node_selectors=pod.spec.node_selector,
        name=pod.metadata.name,
        ports=_extract_ports(base_container.ports),
        volumes=volumes,
        volume_mounts=_extract_volume_mounts(base_container.volume_mounts),
        namespace=pod.metadata.namespace,
        image_pull_policy=base_container.image_pull_policy or "IfNotPresent",
        tolerations=pod.spec.tolerations,
        init_containers=init_containers,
        image_pull_secrets=",".join([i.name for i in image_pull_secrets]),
        resources=base_container.resources,
        service_account_name=pod.spec.service_account_name,
        secrets=secrets,
        affinity=pod.spec.affinity,
        hostnetwork=pod.spec.host_network,
        security_context=_extract_security_context(pod.spec.security_context),
    )
    return dummy_pod


# Traceback (AttributeError)
[2020-10-21 20:55:11,879] {scheduler_job.py:1401} ERROR - Exception when executing execute_helper
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/airflow/kubernetes/pod_launcher.py", line 114, in _mutate_pod_backcompat
settings.pod_mutation_hook(dummy_pod)
File "/usr/local/airflow/config/airflow_local_settings.py", line 21, in pod_mutation_hook
pod.affinity.update({})
AttributeError: 'V1Affinity' object has no attribute 'update'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/airflow/kubernetes/pod_launcher.py", line 124, in _mutate_pod_backcompat
settings.pod_mutation_hook(pod)
File "/usr/local/airflow/config/airflow_local_settings.py", line 13, in pod_mutation_hook
if 'airflow-worker' in pod.labels.keys() or \
AttributeError: 'V1Pod' object has no attribute 'labels'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1399, in _execute
self._execute_helper()
File "/usr/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1470, in _execute_helper
if not self._validate_and_run_task_instances(simple_dag_bag=simple_dag_bag):
File "/usr/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1532, in _validate_and_run_task_instances
self.executor.heartbeat()
File "/usr/lib/python3.7/site-packages/airflow/executors/base_executor.py", line 134, in heartbeat
self.sync()
File "/usr/lib/python3.7/site-packages/airflow/executors/kubernetes_executor.py", line 870, in sync
self.kube_scheduler.run_next(task)
File "/usr/lib/python3.7/site-packages/airflow/executors/kubernetes_executor.py", line 497, in run_next
self.launcher.run_pod_async(pod, **self.kube_config.kube_client_request_args)
File "/usr/lib/python3.7/site-packages/airflow/kubernetes/pod_launcher.py", line 93, in run_pod_async
pod = self._mutate_pod_backcompat(pod)
File "/usr/lib/python3.7/site-packages/airflow/kubernetes/pod_launcher.py", line 127, in _mutate_pod_backcompat
raise Exception([e, e2])
Exception: [AttributeError("'V1Affinity' object has no attribute 'update'"), AttributeError("'V1Pod' object has no attribute 'labels'")]

# Fixed code
def _convert_to_airflow_pod(pod):
    """
    Converts a k8s V1Pod object into an `airflow.kubernetes.pod.Pod` object.
    This function is purely for backwards compatibility
    """
    base_container = pod.spec.containers[0]  # type: k8s.V1Container
    env_vars, secrets = _extract_env_vars_and_secrets(base_container.env)
    volumes = _extract_volumes(pod.spec.volumes)
    api_client = ApiClient()
    init_containers = pod.spec.init_containers
    image_pull_secrets = pod.spec.image_pull_secrets or []
    if pod.spec.init_containers is not None:
        init_containers = [
            api_client.sanitize_for_serialization(i) for i in pod.spec.init_containers
        ]
    dummy_pod = Pod(
        image=base_container.image,
        envs=env_vars,
        cmds=base_container.command,
        args=base_container.args,
        labels=pod.metadata.labels,
        annotations=pod.metadata.annotations,
        node_selectors=pod.spec.node_selector,
        name=pod.metadata.name,
        ports=_extract_ports(base_container.ports),
        volumes=volumes,
        volume_mounts=_extract_volume_mounts(base_container.volume_mounts),
        namespace=pod.metadata.namespace,
        image_pull_policy=base_container.image_pull_policy or "IfNotPresent",
        tolerations=pod.spec.tolerations,
        init_containers=init_containers,
        image_pull_secrets=",".join([i.name for i in image_pull_secrets]),
        resources=base_container.resources,
        service_account_name=pod.spec.service_account_name,
        secrets=secrets,
        affinity=api_client.sanitize_for_serialization(pod.spec.affinity),
        hostnetwork=pod.spec.host_network,
        security_context=_extract_security_context(pod.spec.security_context),
    )
    return dummy_pod