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 2022/10/09 12:28:40 UTC

[GitHub] [airflow] sshivprasad opened a new pull request, #26951: Add env-file parameter to Docker Operator

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

   The change allows for passing a templateable `.env` file to Docker Operator to set the mentioned environment variables in the container. Passing more than a handful env variables might be cumbersome with `environment`, `env-file` would make it easier. 
   
   ---
   **Add meaningful description above**
   
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


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


[GitHub] [airflow] uranusjr commented on a diff in pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
uranusjr commented on code in PR #26951:
URL: https://github.com/apache/airflow/pull/26951#discussion_r993104861


##########
airflow/providers/docker/operators/docker.py:
##########
@@ -304,10 +311,13 @@ def _run_image_with_mounts(self, target_mounts, add_tmp_variable: bool) -> list[
             docker_log_config['max-size'] = self.log_opts_max_size
         if self.log_opts_max_file is not None:
             docker_log_config['max-file'] = self.log_opts_max_file
+        env_file_vars = {}
+        if self.env_file is not None:
+            env_file_vars = self.unpack_environment_variables(self.env_file)
         self.container = self.cli.create_container(
             command=self.format_command(self.command),
             name=self.container_name,
-            environment={**self.environment, **self._private_environment},
+            environment={**self.environment, **self._private_environment, **env_file_vars},

Review Comment:
   Some documentation would be needed to describe the fact that vars in `env_var` overrides those from `environment` (And honestly Iā€™m not sure this is the right behaviour? Personally Iā€™d expect the other way around, but that may not be a universal expectation.)



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


[GitHub] [airflow] uranusjr commented on a diff in pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
uranusjr commented on code in PR #26951:
URL: https://github.com/apache/airflow/pull/26951#discussion_r993171775


##########
airflow/providers/docker/operators/docker.py:
##########
@@ -304,10 +311,13 @@ def _run_image_with_mounts(self, target_mounts, add_tmp_variable: bool) -> list[
             docker_log_config['max-size'] = self.log_opts_max_size
         if self.log_opts_max_file is not None:
             docker_log_config['max-file'] = self.log_opts_max_file
+        env_file_vars = {}
+        if self.env_file is not None:
+            env_file_vars = self.unpack_environment_variables(self.env_file)
         self.container = self.cli.create_container(
             command=self.format_command(self.command),
             name=self.container_name,
-            environment={**self.environment, **self._private_environment},
+            environment={**self.environment, **self._private_environment, **env_file_vars},

Review Comment:
   Me either, Iā€™d say just go with instincts šŸ˜› Your suggested code looks good to me. (But the behaviour should be documented not matter what is chosen.)



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


[GitHub] [airflow] potiuk commented on pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
potiuk commented on PR #26951:
URL: https://github.com/apache/airflow/pull/26951#issuecomment-1295035073

   Cool addition to the Docker Operator. Thanks!


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


[GitHub] [airflow] sshivprasad commented on pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
sshivprasad commented on PR #26951:
URL: https://github.com/apache/airflow/pull/26951#issuecomment-1295017739

   Thank you @pierrejeambrun and @eladkal 


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


[GitHub] [airflow] boring-cyborg[bot] commented on pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
boring-cyborg[bot] commented on PR #26951:
URL: https://github.com/apache/airflow/pull/26951#issuecomment-1295035033

   Awesome work, congrats on your first merged pull request!
   


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


[GitHub] [airflow] potiuk merged pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
potiuk merged PR #26951:
URL: https://github.com/apache/airflow/pull/26951


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


[GitHub] [airflow] sshivprasad commented on pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
sshivprasad commented on PR #26951:
URL: https://github.com/apache/airflow/pull/26951#issuecomment-1295072815

   Thank you for the guidance @potiuk !


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


[GitHub] [airflow] boring-cyborg[bot] commented on pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
boring-cyborg[bot] commented on PR #26951:
URL: https://github.com/apache/airflow/pull/26951#issuecomment-1272531988

   Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
   Here are some useful points:
   - Pay attention to the quality of your code (flake8, mypy and type annotations). Our [pre-commits]( https://github.com/apache/airflow/blob/main/STATIC_CODE_CHECKS.rst#prerequisites-for-pre-commit-hooks) will help you with that.
   - In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/docs/apache-airflow/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
   - Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/BREEZE.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
   - Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
   - Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
   - Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#coding-style-and-best-practices).
   Apache Airflow is a community-driven project and together we are making it better šŸš€.
   In case of doubts contact the developers at:
   Mailing List: dev@airflow.apache.org
   Slack: https://s.apache.org/airflow-slack
   


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


[GitHub] [airflow] sshivprasad commented on a diff in pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
sshivprasad commented on code in PR #26951:
URL: https://github.com/apache/airflow/pull/26951#discussion_r993157721


##########
airflow/providers/docker/operators/docker.py:
##########
@@ -304,10 +311,13 @@ def _run_image_with_mounts(self, target_mounts, add_tmp_variable: bool) -> list[
             docker_log_config['max-size'] = self.log_opts_max_size
         if self.log_opts_max_file is not None:
             docker_log_config['max-file'] = self.log_opts_max_file
+        env_file_vars = {}
+        if self.env_file is not None:
+            env_file_vars = self.unpack_environment_variables(self.env_file)
         self.container = self.cli.create_container(
             command=self.format_command(self.command),
             name=self.container_name,
-            environment={**self.environment, **self._private_environment},
+            environment={**self.environment, **self._private_environment, **env_file_vars},

Review Comment:
   Thank you @uranusjr
   Didn't think about the overriding.
   I am assuming that either environment or env-file would be used and not both at once since instead of setting in environment, it can as well be mentioned in the env-file with the rest of the variables, but of course that's just an assumption. I am unsure of what the prescribed norms are. If it is that environment has to override the env-file and private-environment overrides the rest, then I will change the order to `environment={**env_file_vars, **self.environment, **self._private_environment}`, mention the overriding bit in the docstring, and add a test case for that, if you think that's the right way to go? 



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


[GitHub] [airflow] sshivprasad commented on a diff in pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
sshivprasad commented on code in PR #26951:
URL: https://github.com/apache/airflow/pull/26951#discussion_r993444896


##########
airflow/providers/docker/operators/docker.py:
##########
@@ -304,10 +311,13 @@ def _run_image_with_mounts(self, target_mounts, add_tmp_variable: bool) -> list[
             docker_log_config['max-size'] = self.log_opts_max_size
         if self.log_opts_max_file is not None:
             docker_log_config['max-file'] = self.log_opts_max_file
+        env_file_vars = {}
+        if self.env_file is not None:
+            env_file_vars = self.unpack_environment_variables(self.env_file)
         self.container = self.cli.create_container(
             command=self.format_command(self.command),
             name=self.container_name,
-            environment={**self.environment, **self._private_environment},
+            environment={**self.environment, **self._private_environment, **env_file_vars},

Review Comment:
   Okay :-) 
   I tried checking the precedence by running `docker run -it --env MYVAR2=bar --env-file .\config\sample.env ubuntu bash` where MYVAR2 is set to foo inside `sample.env` and on printing `MYVAR2` it gives `bar`, apparently giving precedence to `env` over `env-file`. So I believe, can go with `environment={**env_file_vars, **self.environment, **self._private_environment}`
   Yes, will make sure to include the behaviour bit in the docstring



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


[GitHub] [airflow] sshivprasad commented on a diff in pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
sshivprasad commented on code in PR #26951:
URL: https://github.com/apache/airflow/pull/26951#discussion_r993444896


##########
airflow/providers/docker/operators/docker.py:
##########
@@ -304,10 +311,13 @@ def _run_image_with_mounts(self, target_mounts, add_tmp_variable: bool) -> list[
             docker_log_config['max-size'] = self.log_opts_max_size
         if self.log_opts_max_file is not None:
             docker_log_config['max-file'] = self.log_opts_max_file
+        env_file_vars = {}
+        if self.env_file is not None:
+            env_file_vars = self.unpack_environment_variables(self.env_file)
         self.container = self.cli.create_container(
             command=self.format_command(self.command),
             name=self.container_name,
-            environment={**self.environment, **self._private_environment},
+            environment={**self.environment, **self._private_environment, **env_file_vars},

Review Comment:
   Okay :-) 
   I tried checking the precedence by running `docker run -it --env MYVAR2=bar --env-file .\config\sample.env ubuntu bash` where MYVAR2 is set to foo inside `sample.env` and on printing `MYVAR2` it gives `bar`, apparently giving precedence to `env` over `env-file`. So I believe, can go with `environment={**env_file_vars, **self.environment, **self._private_environment}`
   On the other hand, I wonder if having `env-file` override `environment` is useful when having different environments (test, dev, prod). But will go with `environment` overriding `env-file`. 
   Yes, will make sure to include the behaviour bit in the docstring



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


[GitHub] [airflow] potiuk commented on pull request #26951: Add env-file parameter to Docker Operator

Posted by GitBox <gi...@apache.org>.
potiuk commented on PR #26951:
URL: https://github.com/apache/airflow/pull/26951#issuecomment-1288160260

   I think it would be great to get it rebased if the tests fail. 


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