brouberol opened a new pull request, #47864:
URL: https://github.com/apache/airflow/pull/47864

   I would like to propose adding `base_container_name` to the 
`KubernetesPodOperator` templated fields.
   
   The rationale is that the base container name is part of the log lines 
emitted by the KubernetesPodManager, which is a good opportunity to have it 
give as much context as possible.
   
   <img width="1127" alt="Screenshot 2025-03-17 at 15 44 22" 
src="https://github.com/user-attachments/assets/5760c162-09aa-4835-8223-7efb41ec6883";
 />
   
   
   For example, in a Wikimedia DAG, we defined the following operators:
   
   ```python
   class WikimediaDumpOperator(KubernetesPodOperator):
       """
       Base class for all types of wiki dumps run as Kubernetes Pods.
       """
   
       dump_type = "generic"
   
       def __init__(self, wiki: str, *args, **kwargs):
           super().__init__(*args, **kwargs)
           self.wiki = wiki
   
           # Name of the "dumps" container (default is base, which isn't super 
telling)
           self.base_container_name = f"mediawiki-{self.dump_type}-dump"
   
           # name of the pod itself
           # made templated in https://github.com/apache/airflow/pull/46268
           self.name = f"{self.base_container_name}-{wiki}"
   
   class WikimediaSqlXmlDumpsOperator(WikimediaDumpOperator):
       """Operator class running the sql/xml wiki dumps as Kubernetes Pods"""
   
       dump_type = "sql-xml"
   
   class WikimediaWikidataDumpsOperator(WikimediaDumpOperator):
       """Operator class running the wikidata dumps as Kubernetes Pods"""
   
       dump_type = "wikidata"
   
   ```
   
   Adding `base_container_name` to the templated fields would allow us to 
rewrite the `WikimediaDumpOperator` to the following:
   
   ```python
   class WikimediaDumpOperator(KubernetesPodOperator):
       """
       Base class for all types of wiki dumps run as Kubernetes Pods.
       """
   
       dump_type = "generic"
   
       def __init__(self, wiki: str, *args, **kwargs):
           super().__init__(*args, **kwargs)
           self.wiki = wiki
   ```
   
   and we could invoke the operator as such:
   
   ```python
   WikimediaSqlXmlOperator(
       ...,
       wiki="value",
       base_container_name='mediawiki-{{ task.dump_type }}-dump',
       name='{{ task.base_container_name }}-{{ task.wiki }}'
       ...
   )
   ```
   
   The endgame would be to have our logs contain as much context as possible 
while avoiding mixing passing both keyword args to the conttructor _and_ 
infering some attributes _within_ the `__init__` method itself.
   
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to