You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2021/02/18 11:00:54 UTC

[GitHub] [airflow] gm55 commented on issue #14287: Migration of env_vars from dict to a List[V1EnvVar] for KubernetesPodOperator breaks Jinja template processing of env_vars with templates like {{ dags.conf['val1'] }} in KubernetesPodOperator

gm55 commented on issue #14287:
URL: https://github.com/apache/airflow/issues/14287#issuecomment-781262440


   As a temporary solution it can be something like that
   ```diff
   --- airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py	(date 1612472719000)
   +++ airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py	(date 1613645533626)
   @@ -17,8 +17,9 @@
    """Executes task in a Kubernetes POD"""
    import re
    import warnings
   -from typing import Any, Dict, Iterable, List, Optional, Tuple
   +from typing import Any, Dict, Iterable, List, Optional, Tuple, Set
    
   +import jinja2
    import yaml
    from kubernetes.client import CoreV1Api, models as k8s
    
   @@ -532,3 +533,39 @@
                if self.termination_grace_period is not None:
                    kwargs = {"grace_period_seconds": self.termination_grace_period}
                self.client.delete_namespaced_pod(name=name, namespace=namespace, **kwargs)
   +
   +    def render_template(  # pylint: disable=too-many-return-statements
   +        self,
   +        content: Any,
   +        context: Dict,
   +        jinja_env: Optional[jinja2.Environment] = None,
   +        seen_oids: Optional[Set] = None,
   +    ) -> Any:
   +        """
   +        Render a templated string. The content can be a collection holding multiple templated strings and will
   +        be templated recursively.
   +
   +        :param content: Content to template. Only strings can be templated (may be inside collection).
   +        :type content: Any
   +        :param context: Dict with values to apply on templated content
   +        :type context: dict
   +        :param jinja_env: Jinja environment. Can be provided to avoid re-creating Jinja environments during
   +            recursion.
   +        :type jinja_env: jinja2.Environment
   +        :param seen_oids: template fields already rendered (to avoid RecursionError on circular dependencies)
   +        :type seen_oids: set
   +        :return: Templated content
   +        """
   +        if not jinja_env:
   +            jinja_env = self.get_template_env()
   +
   +        if isinstance(content, k8s.V1EnvVar):
   +            if isinstance(content.value, str):
   +                return k8s.V1EnvVar(name=content.name, value=jinja_env.from_string(content.value).render(**context), value_from=content.value_from)
   +            else:
   +                return k8s.V1EnvVar(name=content.name, value=content.value, value_from=content.value_from)
   +
   +        elif isinstance(content, list):
   +            return [self.render_template(element, context, jinja_env) for element in content]
   +        else:
   +            return super(KubernetesPodOperator, self).render_template(content, context, jinja_env, seen_oids))
   ```


----------------------------------------------------------------
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:
users@infra.apache.org