diff --git a/changelogs/fragments/1033-warnings-deprecations.yaml b/changelogs/fragments/1033-warnings-deprecations.yaml new file mode 100644 index 0000000000..729cdceea1 --- /dev/null +++ b/changelogs/fragments/1033-warnings-deprecations.yaml @@ -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). diff --git a/plugins/module_utils/copy.py b/plugins/module_utils/copy.py index 0e19e17d2a..2dd04d1832 100644 --- a/plugins/module_utils/copy.py +++ b/plugins/module_utils/copy.py @@ -278,11 +278,15 @@ def copy(self): 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() diff --git a/plugins/modules/k8s_drain.py b/plugins/modules/k8s_drain.py index 0f1c290b59..2ed5ee4df3 100644 --- a/plugins/modules/k8s_drain.py +++ b/plugins/modules/k8s_drain.py @@ -441,7 +441,7 @@ def _revert_node_patch(): warnings.append(warn) result.append("{0} Pod(s) deleted from node.".format(number_pod)) if warnings: - return dict(result=" ".join(result), warnings=warnings) + self._module.warn(warnings) return dict(result=" ".join(result)) def patch_node(self, unschedulable): diff --git a/plugins/modules/k8s_rollback.py b/plugins/modules/k8s_rollback.py index 4fbf610491..308f99e583 100644 --- a/plugins/modules/k8s_rollback.py +++ b/plugins/modules/k8s_rollback.py @@ -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": diff --git a/plugins/modules/k8s_scale.py b/plugins/modules/k8s_scale.py index 6e7d8c44a2..b2873c878b 100644 --- a/plugins/modules/k8s_scale.py +++ b/plugins/modules/k8s_scale.py @@ -243,10 +243,12 @@ def _continue_or_fail(error): 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": diff --git a/tests/integration/targets/k8s_rollback/tasks/main.yml b/tests/integration/targets/k8s_rollback/tasks/main.yml index 1758384c2d..92f3bf2ba2 100644 --- a/tests/integration/targets/k8s_rollback/tasks/main.yml +++ b/tests/integration/targets/k8s_rollback/tasks/main.yml @@ -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]" + - "'No rollout history found' in result.warnings.warnings[0]" - name: Create a service for unsupported resource test k8s: