Replace passing `warnings to exit_json with AnsibleModule.warn` for the few modules (#1033)

SUMMARY
Using exit_json or fail_json for warnings is deprecated in ansible-core>=2.19.0 and will be removed in ansible-core>=2.23.0
Tested with ansible-core 2.19.3 as the latest released version at the time of the start of this PR and with 2.16.0 as the lowest version supported by kubernetes.core 6.x
Resolves: #1031
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
k8s_drain
k8s_rollback
k8s_scale
ADDITIONAL INFORMATION
The initial version of this PR covers only the module k8s_drain, with the following commits extended to k8s_rollback
k8s_scale

Reviewed-by: Bianca Henderson <beeankha@gmail.com>
Reviewed-by: Mike Graves <mgraves@redhat.com>
Reviewed-by: Alina Buzachis
This commit is contained in:
Yuriy Novostavskiy
2026-01-26 20:52:15 +01:00
committed by GitHub
parent 13791ec7bf
commit 3e32c12c40
8 changed files with 30 additions and 11 deletions

View File

@@ -0,0 +1,2 @@
bugfixes:
- Replace passing ``warnings`` to ``exit_json`` with ``AnsibleModule.warn`` in the ``k8s_drain``, ``k8s_rollback.py`` and ``k8s_scale.py`` modules as it deprecated in ``ansible-core>=2.19.0`` and will be removed in ``ansible-core>=2.23.0`` (https://github.com/ansible-collections/kubernetes.core/pull/1033).

View File

@@ -278,11 +278,15 @@ class K8SCopyFromPod(K8SCopy):
def run(self):
self.files_to_copy = self.list_remote_files()
if self.files_to_copy == []:
# Using warn method instead of passing warnings to exit_json as it is
# deprecated in ansible-core>=2.19.0
self._module.warn(
"No file found from directory '{0}' into remote Pod.".format(
self.remote_path
)
)
self.module.exit_json(
changed=False,
warning="No file found from directory '{0}' into remote Pod.".format(
self.remote_path
),
)
self.copy()

View File

@@ -441,7 +441,8 @@ class K8sDrainAnsible(object):
warnings.append(warn)
result.append("{0} Pod(s) deleted from node.".format(number_pod))
if warnings:
return dict(result=" ".join(result), warnings=warnings)
for warning in warnings:
self._module.warn(warning)
return dict(result=" ".join(result))
def patch_node(self, unschedulable):

View File

@@ -168,7 +168,9 @@ def perform_action(svc, resource):
module.params["kind"],
resource["metadata"]["name"],
)
result = {"changed": False, "warnings": [warn]}
if warn:
module.warn(warn)
result = {"changed": False}
return result
if module.params["kind"] == "Deployment":

View File

@@ -243,10 +243,12 @@ def execute_module(client, module):
module.fail_json(msg=error, **return_attributes)
def _continue_or_exit(warn):
if warn:
module.warn(warn)
if multiple_scale:
return_attributes["results"].append({"warning": warn, "changed": False})
return_attributes["results"].append({"changed": False})
else:
module.exit_json(warning=warn, **return_attributes)
module.exit_json(**return_attributes)
for existing in existing_items:
if kind.lower() == "job":

View File

@@ -81,6 +81,7 @@
replicas: 1
current_replicas: 3
wait: true
wait_timeout: 60
register: scale
- name: Read deployment

View File

@@ -402,12 +402,16 @@
namespace: "{{ namespace }}"
register: result
- name: Debug result
debug:
var: result
- name: Assert warning is returned for no rollout history
assert:
that:
- not result.changed
- result.rollback_info[0].warnings is defined
- "'No rollout history found' in result.rollback_info[0].warnings[0]"
- result.warnings is defined
- "'No rollout history found' in result.warnings[0]"
- name: Create a service for unsupported resource test
k8s:

View File

@@ -49,6 +49,7 @@
namespace: "{{ scale_namespace }}"
replicas: 0
wait: true
wait_timeout: 60
register: scale_down
check_mode: true
@@ -80,6 +81,7 @@
namespace: "{{ scale_namespace }}"
replicas: 0
wait: true
wait_timeout: 60
register: scale_down
check_mode: true
@@ -143,6 +145,7 @@
namespace: "{{ scale_namespace }}"
replicas: 0
wait: true
wait_timeout: 60
register: scale_down_idempotency
diff: true
@@ -233,6 +236,7 @@
namespace: "{{ scale_namespace }}"
replicas: 2
wait: yes
wait_timeout: 60
register: scale_up_noop
diff: false
@@ -308,12 +312,12 @@
resource_version: 0
label_selectors:
- app=nginx
wait_timeout: 60
register: scale_out
- assert:
that:
- not scale_out.changed
- scale_out.results | selectattr('warning', 'defined') | list | length == 2
- name: scale deployment using current replicas (wrong value)
kubernetes.core.k8s_scale:
@@ -328,7 +332,6 @@
- assert:
that:
- not scale_out.changed
- scale_out.results | selectattr('warning', 'defined') | list | length == 2
- name: scale deployment using current replicas (right value)
kubernetes.core.k8s_scale: