From 523285ca1fb7c37ddadb20e8a46deb6408755efe Mon Sep 17 00:00:00 2001 From: Felix Matouschek Date: Tue, 23 Apr 2024 11:49:46 +0200 Subject: [PATCH] k8s: Display warnings when applying objects Parse the unserialized response from the K8S API and display warnings sent by the K8S API to the user. Signed-off-by: Felix Matouschek --- plugins/module_utils/k8s/runner.py | 2 +- plugins/module_utils/k8s/service.py | 36 +++++++++++++++-------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/plugins/module_utils/k8s/runner.py b/plugins/module_utils/k8s/runner.py index 50a4604d32..ade87d5749 100644 --- a/plugins/module_utils/k8s/runner.py +++ b/plugins/module_utils/k8s/runner.py @@ -173,7 +173,7 @@ def perform_action(svc, definition: Dict, params: Dict) -> Dict: return result if params.get("apply"): - instance = svc.apply(resource, definition, existing) + instance, warnings = svc.apply(resource, definition, existing) result["method"] = "apply" elif not existing: if state == "patched": diff --git a/plugins/module_utils/k8s/service.py b/plugins/module_utils/k8s/service.py index e4bc7a199d..149f33fc78 100644 --- a/plugins/module_utils/k8s/service.py +++ b/plugins/module_utils/k8s/service.py @@ -366,32 +366,34 @@ def apply( resource: Resource, definition: Dict, existing: Optional[ResourceInstance] = None, - ) -> Dict: + ) -> Tuple[Dict, List[str]]: namespace = definition["metadata"].get("namespace") server_side_apply = self.module.params.get("server_side_apply") if server_side_apply: requires("kubernetes", "19.15.0", reason="to use server side apply") + if self._client_side_dry_run: ignored, patch = apply_object(resource, _encode_stringdata(definition)) if existing: - k8s_obj = dict_merge(existing.to_dict(), patch) + return dict_merge(existing.to_dict(), patch), [] else: - k8s_obj = patch - else: - try: - params = {} - if server_side_apply: - params["server_side"] = True - params.update(server_side_apply) - k8s_obj = self.client.apply( - resource, definition, namespace=namespace, **params - ).to_dict() - except Exception as e: - reason = e.body if hasattr(e, "body") else e - msg = "Failed to apply object: {0}".format(reason) - raise CoreException(msg) from e - return k8s_obj + return patch, [] + + try: + params = {} + if server_side_apply: + params["server_side"] = True + params.update(server_side_apply) + return decode_response( + self.client.apply( + resource, definition, namespace=namespace, serialize=False, **params + ) + ) + except Exception as e: + reason = e.body if hasattr(e, "body") else e + msg = "Failed to apply object: {0}".format(reason) + raise CoreException(msg) from e def replace( self,