You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "hussein-awala (via GitHub)" <gi...@apache.org> on 2023/02/25 20:46:57 UTC

[GitHub] [airflow] hussein-awala commented on a diff in pull request #29760: `KubernetesPodOperator._render_nested_template_fields` improved by changing the conditionals for a map

hussein-awala commented on code in PR #29760:
URL: https://github.com/apache/airflow/pull/29760#discussion_r1117977100


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -378,22 +378,16 @@ def _render_nested_template_fields(
         seen_oids: set,
     ) -> None:
         if id(content) not in seen_oids:
-            template_fields: tuple | None = None
-
-            if isinstance(content, k8s.V1EnvVar):
-                template_fields = ("value", "name")
-
-            if isinstance(content, k8s.V1ResourceRequirements):
-                template_fields = ("limits", "requests")
-
-            if isinstance(content, k8s.V1Volume):
-                template_fields = ("name", "persistent_volume_claim")
-
-            if isinstance(content, k8s.V1VolumeMount):
-                template_fields = ("name",)
-
-            if isinstance(content, k8s.V1PersistentVolumeClaimVolumeSource):
-                template_fields = ("claim_name",)
+            try:

Review Comment:
   @jose-lpa Thank you for opening this PR!
   
   I agree with @potiuk about the need to check if the object is derived from the type, where some people extend the k8s classes to define their own default values or simplifying the creating of its instances, ex:
   ```python
   class CompanyResourceRequirements(k8s.V1ResourceRequirements):
       def __init__(self, cpu, memory):
           resources = {"cpu": cpu, "memory": memory}
           super().__init__(limits=resources, requests=resources)
   
   resources_requirements = CompanyResourceRequirements(cpu="1", memory="512m")
   isinstance(resource_requirements, k8s.V1ResourceRequirements)
   >>> True
   ```
   Also we will not gain a lot because python needs resources to create the dictionary and create the hashes. Instead we can replace the `if` by `elif` and avoid the useless checks when we find the class of our instance:
   ```python
               if isinstance(content, k8s.V1EnvVar):
                   template_fields = ("value", "name")
               elif isinstance(content, k8s.V1ResourceRequirements):
                   template_fields = ("limits", "requests")
               elif isinstance(content, k8s.V1Volume):
                   template_fields = ("name", "persistent_volume_claim")
               elif isinstance(content, k8s.V1VolumeMount):
                   template_fields = ("name",)
               elif isinstance(content, k8s.V1PersistentVolumeClaimVolumeSource):
                   template_fields = ("claim_name",)
   ```
   WDYT



-- 
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.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org