Skip to content

Commit 6215ac7

Browse files
fix(k8s,service): Hide fields first before creating diffs (#915) (#962)
This is a backport of PR #915 as merged into main (6a0635a). SUMMARY By hiding fields first before creating a diff hidden fields will not be shown in the resulting diffs and therefore will also not trigger the changed condition. The issue can only be reproduced when a mutating webhook changes the object while the kubernetes.core.k8s module is working with it. kubevirt/kubevirt.core#145 ISSUE TYPE Bugfix Pull Request COMPONENT NAME kubernetes.core.module_utils.k8s.service ADDITIONAL INFORMATION Run kubernetes.core.k8s and create object with hidden fields. After run kubernetes.core.k8s again and let a webhook mutate the object that the module is working with. The module should return with changed: no. Reviewed-by: Bianca Henderson <[email protected]>
1 parent d5599f1 commit 6215ac7

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- module_utils/k8s/service - hide fields first before creating diffs (https://github.com/ansible-collections/kubernetes.core/pull/915).

plugins/module_utils/k8s/service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ def diff_objects(
498498
if not diff:
499499
return True, result
500500

501-
result["before"] = diff[0]
502-
result["after"] = diff[1]
501+
result["before"] = hide_fields(diff[0], hidden_fields)
502+
result["after"] = hide_fields(diff[1], hidden_fields)
503503

504504
if list(result["after"].keys()) == ["metadata"] and list(
505505
result["before"].keys()
@@ -512,9 +512,6 @@ def diff_objects(
512512
).issubset(ignored_keys):
513513
return True, result
514514

515-
result["before"] = hide_fields(result["before"], hidden_fields)
516-
result["after"] = hide_fields(result["after"], hidden_fields)
517-
518515
return False, result
519516

520517

tests/integration/targets/k8s_hide_fields/tasks/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
- "'managedFields' not in hf4.resources[0]['metadata']"
5959

6060

61-
- name: Hiding a changed field should still result in a change
61+
- name: Hiding a changed field should not result in a change
6262
k8s:
6363
definition: "{{ hide_fields_base_configmap | combine({'data':{'hello':'different'}}) }}"
6464
hidden_fields:
@@ -67,10 +67,10 @@
6767
register: hf5
6868
diff: true
6969

70-
- name: Ensure that hidden changed field changed
70+
- name: Ensure that hidden changed field not changed
7171
assert:
7272
that:
73-
- hf5.changed
73+
- not hf5.changed
7474

7575
- name: Apply works with hidden fields
7676
k8s:

0 commit comments

Comments
 (0)