Skip to content

Comments

Fix ModuleNotFoundError in kubernetes cleanup cronjob#61673

Merged
shahar1 merged 3 commits intoapache:mainfrom
Eason09053360:fix/kubernetes-cleanup-import-error
Feb 10, 2026
Merged

Fix ModuleNotFoundError in kubernetes cleanup cronjob#61673
shahar1 merged 3 commits intoapache:mainfrom
Eason09053360:fix/kubernetes-cleanup-import-error

Conversation

@Eason09053360
Copy link
Contributor

The cleanup cronjob in apache-airflow-providers-cncf-kubernetes >= 10.12.2 fails in production because it imports from tests_common.test_utils.taskinstance, which is not available outside the dev repository.

Solution: Remove test utility import and use TaskInstance directly, manually setting dag_version_id for Airflow 3.0+ compatibility.

related: #60112
closed: #61360

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst or {issue_number}.significant.rst, in airflow-core/newsfragments.

The cleanup cronjob in apache-airflow-providers-cncf-kubernetes >= 10.12.2
fails in production because it imports from tests_common.test_utils.taskinstance,
which is not available outside the dev repository.

Solution: Remove test utility import and use TaskInstance directly,
manually setting dag_version_id for Airflow 3.0+ compatibility.
The kubernetes cleanup cronjob command was importing from tests_common
which is not available in production environments, causing
ModuleNotFoundError when running the cronjob.

Changes:
- Import create_scheduler_operator conditionally within AIRFLOW_V_3_0_PLUS
  block to avoid import errors in older versions where it may not exist
- Use create_scheduler_operator to serialize tasks before creating
  TaskInstance, ensuring compatibility with Airflow 3.0+ which requires
  serialized operators for proper TaskInstance initialization
- Pass dag_version_id as constructor parameter (required in Airflow 3.0+)

This ensures the cleanup cronjob works correctly in production Kubernetes
environments running Airflow 3.0+.
@jscheffl
Copy link
Contributor

jscheffl commented Feb 9, 2026

Looks good, just one back-compat test is failing, so 3.0.x-line probably needs another entry.

Missing expertise... @amoghrajesh Do you have a hint what is also working in 3.0.0? Would be great fixing it before next provider wave tomorrow.

Moved the try-except import of create_scheduler_operator from inside
the for loop to the top-level import section. This follows Python best
practices and improves performance by:
- Executing the import only once instead of on every loop iteration
- Making dependencies more visible at the top of the file
- Matching the pattern used in tests_common.test_utils.taskinstance

The fallback lambda ensures compatibility with Airflow versions where
create_scheduler_operator doesn't exist (e.g., 3.0.6).
@shahar1 shahar1 added the priority:critical Showstopper bug that should be patched immediately label Feb 10, 2026
Copy link
Contributor

@shahar1 shahar1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!

@shahar1 shahar1 merged commit dcb4050 into apache:main Feb 10, 2026
105 checks passed
Copy link
Contributor

@amoghrajesh amoghrajesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this looks good.

@shahar1
Copy link
Contributor

shahar1 commented Feb 10, 2026

Created #61713 with a pre-commit to avoid such cases for the next times

@smktpd
Copy link

smktpd commented Feb 17, 2026

Which release this fix will get into?

@jscheffl
Copy link
Contributor

Which release this fix will get into?

See https://airflow.apache.org/docs/apache-airflow-providers-cncf-kubernetes/stable/changelog.html - is released with 10.12.4

choo121600 pushed a commit to choo121600/airflow that referenced this pull request Feb 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers priority:critical Showstopper bug that should be patched immediately provider:cncf-kubernetes Kubernetes (k8s) provider related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cleanup job using test-only module at runtime

5 participants