mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-11-21 14:48:37 +00:00
Add ansible-core version check to k8s lookup turbo mode (#1253)
* feat(lookup): add ansible-core version check for turbo mode in k8s lookup Add ansible-core version checking to the k8s lookup plugin to match the behavior already implemented in module_utils/ansiblemodule.py. When ENABLE_TURBO_MODE is set and ansible-core >= 2.19.0: - Emit deprecation warning for ENABLE_TURBO_MODE - Emit version warning explaining turbo mode is unsupported - Disable turbo mode and fall back to standard LookupBase This prevents confusing tracebacks when cloud.common (which declares requires_ansible '>=2.15.0,<2.19') is used on newer ansible-core versions where it is not fully compatible. Changes: - Import ansible_version and LooseVersion - Add TURBO_UNSUPPORTED_CORE = "2.19.0" constant - Check version before enabling turbo mode - Disable turbo mode and warn if version >= 2.19.0 Addresses reviewer feedback on PR #1251 to add version testing to the lookup plugin matching the module implementation. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * test(lookup): add unit tests for k8s turbo mode version check Add comprehensive unit tests for the ansible-core version checking logic in the k8s lookup plugin's turbo mode handling. Tests validate that when ENABLE_TURBO_MODE is set: - Turbo mode is disabled on ansible-core >= 2.19.0 (tests 2.19.0 and 2.21.4) - Turbo mode is enabled on ansible-core < 2.19.0 (tests 2.18.0) - Appropriate deprecation and version warnings are shown - Standard LookupBase is used when turbo mode is disabled Tests also verify that when ENABLE_TURBO_MODE is not set: - No deprecation or warning messages are shown - Turbo mode remains disabled - Standard LookupBase is used Implementation follows pytest best practices: - Autouse fixture for module cleanup between tests - Parametrized tests to avoid duplication (2.19.0 and 2.21.4 share logic) - Mock assertion methods (assert_called, assert_not_called) - Module-level import of StandardLookupBase to avoid repetition Simplified and maintainable: 71 lines vs initial 139-line implementation (49% reduction through deduplication and better abstractions). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * style: format with black * fixup! test(lookup): add unit tests for k8s turbo mode version check * docs: add changelog fragment for k8s lookup turbo version check Add changelog entry documenting that the k8s lookup plugin now ignores ENABLE_TURBO_MODE on ansible-core >= 2.19.0 and falls back to standard LookupBase instead of failing with a traceback. This complements the existing deprecate-enable-turbo-mode.yml fragment which covers the module implementation, by documenting the lookup plugin fix specifically. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * fixup! fixup! test(lookup): add unit tests for k8s turbo mode version check * fixup! fixup! fixup! test(lookup): add unit tests for k8s turbo mode version check --------- Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.5
parent
8a9dd54fe7
commit
c46180bb6f
@@ -212,6 +212,7 @@ import os
|
||||
from collections.abc import KeysView
|
||||
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils.ansible_release import __version__ as ansible_version
|
||||
from ansible.module_utils.common.validation import check_type_bool
|
||||
from ansible.utils.display import Display
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
|
||||
@@ -220,9 +221,17 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import (
|
||||
create_definitions,
|
||||
)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.version import (
|
||||
LooseVersion,
|
||||
)
|
||||
|
||||
display = Display()
|
||||
|
||||
# cloud.common declares requires_ansible '>=2.15.0,<2.19'. On newer ansible-core the
|
||||
# import still succeeds, so the version has to be checked explicitly: relying on
|
||||
# ImportError alone lets turbo mode fail later with an unrelated traceback.
|
||||
TURBO_UNSUPPORTED_CORE = "2.19.0"
|
||||
|
||||
try:
|
||||
enable_turbo_mode = check_type_bool(os.environ.get("ENABLE_TURBO_MODE"))
|
||||
except TypeError:
|
||||
@@ -236,6 +245,15 @@ if enable_turbo_mode:
|
||||
version="8.0.0",
|
||||
collection_name="kubernetes.core",
|
||||
)
|
||||
if LooseVersion(ansible_version) >= LooseVersion(TURBO_UNSUPPORTED_CORE):
|
||||
display.warning(
|
||||
"ENABLE_TURBO_MODE is ignored on ansible-core %s: Ansible Turbo mode requires "
|
||||
"the cloud.common collection, which supports ansible-core < %s only. "
|
||||
"Continuing without Turbo mode." % (ansible_version, TURBO_UNSUPPORTED_CORE)
|
||||
)
|
||||
enable_turbo_mode = False
|
||||
|
||||
if enable_turbo_mode:
|
||||
try:
|
||||
from ansible_collections.cloud.common.plugins.plugin_utils.turbo.lookup import (
|
||||
TurboLookupBase as LookupBase,
|
||||
|
||||
Reference in New Issue
Block a user