-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix plugin macros not being exposed through airflow.macros #12788
Conversation
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/master/CONTRIBUTING.rst)
|
# Verify that the symbol table in airflow.macros has been updated with an entry for | ||
# this plugin, this is necessary in order to allow the plugin's macros to be used when | ||
# rendering templates. | ||
assert hasattr(macros, MacroPlugin.name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
integrate_macros_plugins()
has the side-effect that it modifies the airflow.macros
module. Do we need to add additional cleanup logic to reset the contents of airflow.macros
through a finalizer here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will importlib.reload
help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply reloading the module using importlib.reload()
will not be sufficient as the module's symbol table (dictionary) is retained.
When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains.
I think we'll have to delete the module from sys.modules
and import it again in order to properly remove the entries that are being added by this test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've implemented this in c2b5354 -- I'm curious to your thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this change.😻 I'm just wondering if it's worth moving this code to the context manager mock_plugins_manager. This allows us to limit the side effects in other tests as well. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered doing this, but the __doc__
comment on the mock_plugin_manager
seems to suggest that the scope of that mock is limited to the airflow.plugins
module.
While that mock does in fact clear out the macros_modules
variable in airflow.plugins
, it does not actually attempt to reverse any (side) effects that are caused by invoking integrate_macros_plugins()
. I did a bit of searching through the code base, and it doesn't really appear that there is any test coverage for this function beyond the test I've just added.
Because I want to avoid scope creep for the mock_plugin_manager
fixture, and this is the only test case that actually appears to have to deal with side effects cause by calling integrate_macros_plugins()
, I'm a bit hesitant to make changes beyond what's being proposed here.
The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest master at your convenience, or amend the last commit of the PR, and push it with --force-with-lease. |
@RikHeijdens Can you do a rebase? I want to make sure that |
c2b5354
to
6ce365f
Compare
@mik-laj I've just rebased my branch on master. |
@mik-laj I noticed the change proposed here causes the The subprocess in which the Python callable runs however does not integrate the macros provided by plugins. I was able to get this test to pass locally by adding the following two lines of code to # Ensure airflow.macros has been loaded properly.
import airflow.plugins_manager
airflow.plugins_manager.integrate_macros_plugins() Doing this however, seems like a bit of a nasty solution to me and also causes additional test failures. I'm curious whether you have any thoughts on what would be a better solution? What is the use-case of allowing subprocess spawned by PythonVirtualenvOperator to access plugin-provided macros? Edit: I went ahead and removed |
I guess this code should only be called if you have airflow installed in your environment. airflow/airflow/operators/python.py Lines 536 to 537 in 39ea872
The use case is that we want to allow the user to perform any operation, but in a virtual environment. The virtual environment will allow they to use additional libraries. |
@mik-laj Got it. I've restored access to |
@RikHeijdens Got it. Thanks for your work. |
@RikHeijdens Can you document this py2 virtualenv limiation somewhere please? (VirtualEnvOperator docs?) LGTM after that. |
I've documented the limitation in d17e98f, but I'm a bit skeptical as to whether any of the I believe anything that will require to import |
The Workflow run is cancelling this PR. It has some failed jobs matching ^Pylint$,^Static checks,^Build docs$,^Spell check docs$,^Backport packages$,^Provider packages,^Checks: Helm tests$,^Test OpenAPI*. |
@RikHeijdens Yes. Please, do it. That should fix the problem. |
Added a test case to reproduce the issue reported in apache#12785
In order to allow a plugin-provided macro to be used at templating time, it needs to be exposed through the airflow.macros module. This commit fixes GitHub issue airflow#12785.
This test-case has side-effects in the sense that the symbol table of the airflow.macros module is altered when integrate_macros_plugins() is invoked. This commit adds a finalizer to the test case that ensures that that module is being reloaded completely in order to prevent impact on other tests.
The process that runs in the virtual environment may not be able to access macros that have been provided by plugins. E.g. in cases where the virtual environment uses a different major version of Python than Airflow itself. In order to prevent this from being an issue macros has been removed from the serializable context.
This reverts commit 5a6c3cfd19c584a2b5ab2220ee580f8fd85ddb17.
When Airflow is available in a virtual environment, and when this environment runs at least Python 3, then plugin-provided macros should be made available to the Python callable that is being executed in this environment.
Plugin-provided macros can not be used on Python 2 when using PythonVirtualenvOperator any longer.
d17e98f
to
cb64e96
Compare
Almost there. It almost feels like someone is running a slowloris attack against GitHub actions today. |
✔️ |
Awesome work, congrats on your first merged pull request! |
This PR fixes an issue where macros that are being provided through plugins can not be used at template time because they are not accessible through the
airflow.macros
module.This PR consists out of two commits. In the first commit I add a test-case that reproduces the issue as outlined in #12785, and the second commit introduces the fix which fixes the issue and allows the test-case to pass.
Fixes: #12785
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.