<p>I'd like to use <a href="https://airflow.apache.org/docs/stable/howto/connection/index.html" rel="noreferrer">connections saved in <code>airflow</code></a> in a task which uses the  <code>KubernetesPodOperator</code>.</p>

<p>When developing the image I've used environment variables to pass database connection information down to the container, but the production environment has the databases saved as connection hooks.</p>

<p>What is the best way to extract the database connection information and pass it down to the container?</p>

<pre class="lang-py prettyprint-override"><code>env_vars = {'database_usr': 'xxx', 'database_pas': 'xxx'}
</code></pre>

<pre class="lang-py prettyprint-override"><code>KubernetesPodOperator(
        dag=dag,
        task_id="example-task",
        name="example-task",
        namespace="default",
        image="eu.gcr.io/repo/image:tag",
        image_pull_policy="Always",
        arguments=["-v", "image-command", "image-arg"],
        env_vars=env_vars,
    )
</code></pre>

<p>My current solution is to grab the variables from the connection using <code>BaseHook</code>:</p>

<pre><code>from airflow.hooks.base_hook import BaseHook


def connection_to_dict(connection_id):
    """Returns connection params from Airflow as a dictionary.

    Parameters
    ----------
    connection_id : str
        Name of the connection in Airflow, e.g. `mysql_default`

    Returns
    -------
    dict
        Unencrypted values.
    """
    conn_obj = BaseHook.get_connection(connection_id)
    d = conn_obj.__dict__
    if ('is_encrypted', True) in d.items():
        d['password'] = conn_obj.get_password()
    return d
</code></pre>

<p>and then passing those as environment variables to the Kubernetes pod operator.</p>
