Skip to content

Commit

Permalink
k8s: Display warnings when applying objects
Browse files Browse the repository at this point in the history
Parse the unserialized response from the K8S API and display warnings
sent by the K8S API to the user.

Signed-off-by: Felix Matouschek <[email protected]>
  • Loading branch information
0xFelix committed Apr 25, 2024
1 parent a9bbd93 commit 523285c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion plugins/module_utils/k8s/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
36 changes: 19 additions & 17 deletions plugins/module_utils/k8s/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 523285c

Please sign in to comment.