fix(k8s): emit warnings via AnsibleModule.warn instead of exit_json (#1267)

SUMMARY
runner.py put warnings into the result dict that run_module() and k8s_service unpack into exit_json()/fail_json(). Passing warnings that way is deprecated since ansible-core 2.19 and goes away in 2.23, so every k8s or k8s_service run that produced a warning also emitted a deprecation warning. All five sites now use AnsibleModule.warn.
This also fixes a quieter bug in the same code. For a multi-document manifest run_module() returns {"changed": ..., "result": {"results": results}}, so warnings ended up nested at result.results[*].warnings, where ansible-core never looked. They were silently dropped whenever a task handled more than one resource, and are now shown.
#1033 fixed this class of bug in k8s_drain, k8s_rollback and k8s_scale for #1031, but never touched runner.py.
Fixes #1264
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
k8s
k8s_service
ADDITIONAL INFORMATION
Verified on kind (Kubernetes v1.37.0) with ansible-core 2.21.4
AI Assistance Disclosure
Generated: unit test is AI-generated,
Human Verified: test on the kind cluster performed by a human

Reviewed-by: Don Naro <dnaro@redhat.com>
Reviewed-by: Bikouo Aubin
(cherry picked from commit 11b861f77e)
This commit is contained in:
Yuriy Novostavskiy
2026-11-16 17:23:12 +00:00
committed by patchback[bot]
parent 2e5f3198fc
commit cd631923aa
3 changed files with 71 additions and 43 deletions
+6 -10
View File
@@ -92,10 +92,10 @@ def run_module(module) -> None:
for definition in definitions:
result = {"changed": False, "result": {}}
warnings = []
if module.params.get("validate") is not None:
warnings = validate(client, module, definition)
for warning in validate(client, module, definition):
module.warn(warning)
try:
result = perform_action(svc, definition, module.params)
@@ -109,16 +109,12 @@ def run_module(module) -> None:
except AttributeError:
pass
error["msg"] = to_native(e)
if warnings:
error.setdefault("warnings", []).extend(warnings)
if module.params.get("continue_on_error"):
result["error"] = error
else:
module.fail_json(**error)
if warnings:
result.setdefault("warnings", []).extend(warnings)
changed |= result["changed"]
results.append(result)
@@ -177,7 +173,7 @@ def perform_action(svc, definition: Dict, params: Dict) -> Dict:
result["method"] = "apply"
elif not existing:
if state == "patched":
result.setdefault("warnings", []).append(
svc.module.warn(
"resource 'kind={kind},name={name}' was not found but will not be "
"created as 'state' parameter has been set to '{state}'".format(
kind=kind, name=definition["metadata"].get("name"), state=state
@@ -194,8 +190,8 @@ def perform_action(svc, definition: Dict, params: Dict) -> Dict:
instance, warnings = svc.update(resource, definition, existing)
result["method"] = "update"
if warnings:
result["warnings"] = warnings
for warning in warnings:
svc.module.warn(warning)
# If needed, wait and/or create diff
success = True
@@ -217,7 +213,7 @@ def perform_action(svc, definition: Dict, params: Dict) -> Dict:
existing = {}
match, diffs = diff_objects(existing, instance, hidden_fields)
if match and diffs:
result.setdefault("warnings", []).append(
svc.module.warn(
"No meaningful diff was generated, but the API may not be idempotent "
"(only metadata.generation or metadata.resourceVersion were changed)"
)