Skip to content

Commit

Permalink
Improve diff_objects to better hide hidden fields
Browse files Browse the repository at this point in the history
Ensure test correctly captures that hidden fields should be hidden
in diffs too
  • Loading branch information
willthames committed Sep 14, 2023
1 parent 4f60378 commit d14b9ca
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 8 additions & 11 deletions plugins/module_utils/k8s/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,23 +510,20 @@ def diff_objects(
result["before"] = diff[0]
result["after"] = diff[1]

if list(result["after"].keys()) != ["metadata"] or list(
if list(result["after"].keys()) == ["metadata"] and list(
result["before"].keys()
) != ["metadata"]:
return False, result
) == ["metadata"]:
# If only metadata.generation and metadata.resourceVersion changed, ignore it
ignored_keys = set(["generation", "resourceVersion"])

# If only metadata.generation and metadata.resourceVersion changed, ignore it
ignored_keys = set(["generation", "resourceVersion"])

if not set(result["after"]["metadata"].keys()).issubset(ignored_keys):
return False, result
if not set(result["before"]["metadata"].keys()).issubset(ignored_keys):
return False, result
if (set(result["after"]["metadata"].keys()).issubset(ignored_keys) and
set(result["before"]["metadata"].keys()).issubset(ignored_keys)):
return True, result

result["before"] = hide_fields(result["before"], hidden_fields)
result["after"] = hide_fields(result["after"], hidden_fields)

return True, result
return False, result


def hide_fields(definition: dict, hidden_fields: Optional[list]) -> dict:
Expand Down
14 changes: 11 additions & 3 deletions tests/integration/targets/k8s_hide_fields/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@
assert:
that:
- >-
'annotations' not in hf6.resources[0].metadata or
q'kubectl.kubernetes.io/last-applied-configuration'
not in hf6.resources[0].metadata.annotations
'annotations' not in hf6.result.metadata or
'kubectl.kubernetes.io/last-applied-configuration'
not in hf6.result.metadata.annotations
- >-
'annotations' not in hf6.diff.before.metadata or
'kubectl.kubernetes.io/last-applied-configuration'
not in hf6.diff.before.metadata.annotations
- >-
'annotations' not in hf6.diff.after.metadata or
'kubectl.kubernetes.io/last-applied-configuration'
not in hf6.diff.after.metadata.annotations
- name: Hidden field should not show up in deletion
k8s:
Expand Down

0 comments on commit d14b9ca

Please sign in to comment.