Skip to content

Commit c48778d

Browse files
authored
k8s_json_patch: support the hidden_fields param (#964)
SUMMARY Add support for hidden_fields on k8s_json_patch ISSUE TYPE Feature Pull Request COMPONENT NAME k8s_json_patch ADDITIONAL INFORMATION Works exactly the same as k8s Haven't pushed the doc yet, because of many changes. Will do it on a separate commit if the tests pass. 1st commit here, sorry if I forget some things. Thanks! Reviewed-by: Bianca Henderson <[email protected]> Reviewed-by: Alina Buzachis Reviewed-by: Frank Villaro-Dixon <[email protected]>
1 parent cf3c3a9 commit c48778d

File tree

8 files changed

+125
-2
lines changed

8 files changed

+125
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- Module k8s_json_patch - Add support for `hidden_fields` (https://github.com/ansible-collections/kubernetes.core/pull/964).

plugins/modules/k8s_json_patch.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
aliases:
3434
- api
3535
- version
36+
hidden_fields:
37+
description:
38+
- List of fields to hide from the diff output.
39+
- This is useful for fields that are not relevant to the patch operation, such as `metadata.managedFields`.
40+
type: list
41+
elements: str
42+
default: []
43+
version_added: 6.1.0
3644
kind:
3745
description:
3846
- Use to specify an object model.
@@ -147,6 +155,7 @@
147155
)
148156
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
149157
diff_objects,
158+
hide_fields,
150159
)
151160
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import (
152161
get_waiter,
@@ -174,6 +183,7 @@
174183
"namespace": {"type": "str"},
175184
"name": {"type": "str", "required": True},
176185
"patch": {"type": "list", "required": True, "elements": "dict"},
186+
"hidden_fields": {"type": "list", "elements": "str", "default": []},
177187
}
178188

179189

@@ -203,6 +213,7 @@ def execute_module(module, client):
203213
namespace = module.params.get("namespace")
204214
patch = module.params.get("patch")
205215

216+
hidden_fields = module.params.get("hidden_fields")
206217
wait = module.params.get("wait")
207218
wait_sleep = module.params.get("wait_sleep")
208219
wait_timeout = module.params.get("wait_timeout")
@@ -260,13 +271,13 @@ def build_error_msg(kind, name, msg):
260271
module.fail_json(msg=msg, error=to_native(exc), status="", reason="")
261272

262273
success = True
263-
result = {"result": obj}
274+
result = {"result": hide_fields(obj, hidden_fields)}
264275
if wait and not module.check_mode:
265276
waiter = get_waiter(client, resource, condition=wait_condition)
266277
success, result["result"], result["duration"] = waiter.wait(
267278
wait_timeout, wait_sleep, name, namespace
268279
)
269-
match, diffs = diff_objects(existing.to_dict(), obj)
280+
match, diffs = diff_objects(existing.to_dict(), obj, hidden_fields)
270281
result["changed"] = not match
271282
if module._diff:
272283
result["diff"] = diffs
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
k8s_json_patch
2+
k8s
3+
time=33
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
test_namespace: "k8s-hide-fields"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dependencies:
2+
- setup_namespace
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- connection: local
3+
gather_facts: false
4+
hosts: localhost
5+
roles:
6+
- k8s_json_patch_hide_fields
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
set -eux
3+
export ANSIBLE_CALLBACKS_ENABLED=profile_tasks
4+
export ANSIBLE_ROLES_PATH=../
5+
ansible-playbook playbook.yaml "$@"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
- vars:
2+
pod: json-patch
3+
k8s_wait_timeout: 400
4+
5+
block:
6+
- name: Create a simple pod
7+
kubernetes.core.k8s:
8+
definition:
9+
apiVersion: v1
10+
kind: Pod
11+
metadata:
12+
namespace: "{{ test_namespace }}"
13+
name: "{{ pod }}"
14+
labels:
15+
label1: foo
16+
spec:
17+
containers:
18+
- image: busybox:musl
19+
name: busybox
20+
command:
21+
- sh
22+
- -c
23+
- while true; do echo $(date); sleep 10; done
24+
wait: yes
25+
wait_timeout: "{{ k8s_wait_timeout | default(omit) }}"
26+
27+
28+
- name: Add a label, and hide some fields
29+
kubernetes.core.k8s_json_patch:
30+
kind: Pod
31+
namespace: "{{ test_namespace }}"
32+
name: "{{ pod }}"
33+
patch:
34+
- op: add
35+
path: /metadata/labels/label2
36+
value: bar
37+
hidden_fields:
38+
- metadata.managedFields
39+
register: hf1
40+
41+
- name: Ensure hidden fields are not present
42+
assert:
43+
that:
44+
- "'managedFields' not in hf1.result['metadata']"
45+
46+
47+
- name: Add a label, without hiding our fields
48+
kubernetes.core.k8s_json_patch:
49+
kind: Pod
50+
namespace: "{{ test_namespace }}"
51+
name: "{{ pod }}"
52+
patch:
53+
- op: add
54+
path: /metadata/labels/label3
55+
value: bar
56+
hidden_fields:
57+
- something.else
58+
register: hf2
59+
60+
- name: Ensure hidden fields are present
61+
assert:
62+
that:
63+
- "'managedFields' in hf2.result['metadata']"
64+
65+
66+
- name: Patching the same resource with missing hidden fields should have no effect
67+
kubernetes.core.k8s_json_patch:
68+
kind: Pod
69+
namespace: "{{ test_namespace }}"
70+
name: "{{ pod }}"
71+
patch:
72+
- op: add
73+
path: /metadata/labels/label2
74+
value: bar
75+
hidden_fields:
76+
- does.not.exist
77+
register: hf2
78+
79+
- name: Ensure no change with missing hidden fields
80+
assert:
81+
that:
82+
- not hf2.changed
83+
84+
85+
always:
86+
- name: Remove namespace
87+
k8s:
88+
kind: Namespace
89+
name: "{{ test_namespace }}"
90+
state: absent
91+
ignore_errors: true

0 commit comments

Comments
 (0)