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
@@ -0,0 +1,5 @@
|
||||
---
|
||||
bugfixes:
|
||||
- k8s lookup - ignore ``ENABLE_TURBO_MODE`` on ansible-core 2.19.0 and later, where the ``cloud.common`` collection is not supported.
|
||||
Fall back to the standard ``LookupBase`` instead of failing with a traceback
|
||||
(https://github.com/ansible-collections/kubernetes.core/pull/1253).
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from ansible.plugins.lookup import LookupBase as StandardLookupBase
|
||||
|
||||
|
||||
class TestK8sTurboMode:
|
||||
"""Test turbo mode version checking in k8s lookup plugin."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def cleanup_k8s_module(self):
|
||||
"""Ensure clean module state before each test."""
|
||||
sys.modules.pop("ansible_collections.kubernetes.core.plugins.lookup.k8s", None)
|
||||
|
||||
@pytest.mark.parametrize("ansible_version", ["2.19.0", "2.21.4"])
|
||||
def test_turbo_mode_disabled_on_newer_ansible(self, monkeypatch, ansible_version):
|
||||
"""Test that turbo mode is disabled on ansible-core >= 2.19.0."""
|
||||
monkeypatch.setenv("ENABLE_TURBO_MODE", "true")
|
||||
mock_display = MagicMock()
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"ansible.module_utils.ansible_release": MagicMock(
|
||||
__version__=ansible_version
|
||||
),
|
||||
"ansible.utils.display": MagicMock(Display=lambda: mock_display),
|
||||
},
|
||||
):
|
||||
from ansible_collections.kubernetes.core.plugins.lookup import k8s
|
||||
|
||||
mock_display.deprecated.assert_called()
|
||||
assert (
|
||||
"ENABLE_TURBO_MODE is deprecated"
|
||||
in mock_display.deprecated.call_args[0][0]
|
||||
)
|
||||
|
||||
mock_display.warning.assert_called()
|
||||
assert (
|
||||
f"ENABLE_TURBO_MODE is ignored on ansible-core {ansible_version}"
|
||||
in mock_display.warning.call_args[0][0]
|
||||
)
|
||||
assert "ansible-core < 2.19.0 only" in mock_display.warning.call_args[0][0]
|
||||
|
||||
assert k8s.LookupBase == StandardLookupBase
|
||||
|
||||
def test_turbo_mode_enabled_on_ansible_core_2_18(self, monkeypatch):
|
||||
"""Test that turbo mode is still enabled on ansible-core < 2.19.0."""
|
||||
monkeypatch.setenv("ENABLE_TURBO_MODE", "true")
|
||||
mock_display = MagicMock()
|
||||
mock_turbo_lookup = MagicMock()
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"ansible.module_utils.ansible_release": MagicMock(__version__="2.18.0"),
|
||||
"ansible.utils.display": MagicMock(Display=lambda: mock_display),
|
||||
"ansible_collections.cloud.common.plugins.plugin_utils.turbo.lookup": mock_turbo_lookup,
|
||||
},
|
||||
):
|
||||
from ansible_collections.kubernetes.core.plugins.lookup import ( # noqa: F401
|
||||
k8s,
|
||||
)
|
||||
|
||||
mock_display.deprecated.assert_called()
|
||||
assert not mock_display.warning.called
|
||||
|
||||
def test_turbo_mode_disabled_when_env_not_set(self, monkeypatch):
|
||||
"""Test that turbo mode is disabled when ENABLE_TURBO_MODE is not set."""
|
||||
monkeypatch.delenv("ENABLE_TURBO_MODE", raising=False)
|
||||
mock_display = MagicMock()
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"ansible.module_utils.ansible_release": MagicMock(__version__="2.18.0"),
|
||||
"ansible.utils.display": MagicMock(Display=lambda: mock_display),
|
||||
},
|
||||
):
|
||||
from ansible_collections.kubernetes.core.plugins.lookup import k8s
|
||||
|
||||
mock_display.deprecated.assert_not_called()
|
||||
mock_display.warning.assert_not_called()
|
||||
assert k8s.LookupBase == StandardLookupBase
|
||||
Reference in New Issue
Block a user