mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-03-26 19:03:16 +00:00
Merge pull request #178 from 0xFelix/hidden_fields
feat(modules): Add hidden_fields argument
This commit is contained in:
43
plugins/module_utils/diff.py
Normal file
43
plugins/module_utils/diff.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2025 Red Hat, Inc.
|
||||
# Apache License 2.0 (see LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
|
||||
from typing import Dict, Tuple, Optional
|
||||
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s import service
|
||||
|
||||
|
||||
# Copied from
|
||||
# https://github.com/ansible-collections/kubernetes.core/blob/d329e7ee42799ae9d86b54cf2c7dfc8059103504/plugins/module_utils/k8s/service.py#L493
|
||||
# Removed this once this fix was merged into kubernetes.core.
|
||||
def _diff_objects(
|
||||
existing: Dict, new: Dict, hidden_fields: Optional[list] = None
|
||||
) -> Tuple[bool, Dict]:
|
||||
result = {}
|
||||
diff = service.recursive_diff(existing, new)
|
||||
if not diff:
|
||||
return True, result
|
||||
|
||||
result["before"] = service.hide_fields(diff[0], hidden_fields)
|
||||
result["after"] = service.hide_fields(diff[1], hidden_fields)
|
||||
|
||||
if list(result["after"].keys()) == ["metadata"] and list(
|
||||
result["before"].keys()
|
||||
) == ["metadata"]:
|
||||
# If only metadata.generation and metadata.resourceVersion changed, ignore it
|
||||
ignored_keys = set(["generation", "resourceVersion"])
|
||||
|
||||
if set(result["after"]["metadata"].keys()).issubset(ignored_keys) and set(
|
||||
result["before"]["metadata"].keys()
|
||||
).issubset(ignored_keys):
|
||||
return True, result
|
||||
|
||||
return False, result
|
||||
|
||||
|
||||
service.diff_objects = _diff_objects
|
||||
|
||||
|
||||
def _patch_diff_objects():
|
||||
"""_dummy is required to satisfy the unused import linter and the ansible-doc sanity check."""
|
||||
pass
|
||||
@@ -43,6 +43,7 @@ def execute_info_module(module, kind, wait_condition):
|
||||
wait=module.params["wait"],
|
||||
wait_sleep=module.params["wait_sleep"],
|
||||
wait_timeout=module.params["wait_timeout"],
|
||||
hidden_fields=module.params["hidden_fields"],
|
||||
condition=wait_condition,
|
||||
)
|
||||
module.exit_json(changed=False, **facts)
|
||||
|
||||
@@ -158,6 +158,15 @@ options:
|
||||
- If set to O(force=yes), and O(state=present) is set, an existing object will be replaced.
|
||||
type: bool
|
||||
default: no
|
||||
hidden_fields:
|
||||
description:
|
||||
- Hide fields matching this option in the result.
|
||||
- An example might be O(hidden_fields=[metadata.managedFields])
|
||||
or O(hidden_fields=[metadata.annotations[kubemacpool.io/transaction-timestamp]]).
|
||||
type: list
|
||||
elements: str
|
||||
default: ['metadata.annotations[kubemacpool.io/transaction-timestamp]', metadata.managedFields]
|
||||
version_added: 2.2.0
|
||||
|
||||
requirements:
|
||||
- "python >= 3.9"
|
||||
@@ -273,6 +282,11 @@ result:
|
||||
type: str
|
||||
"""
|
||||
|
||||
# Monkey patch service.diff_objects to temporarily fix the changed logic
|
||||
from ansible_collections.kubevirt.core.plugins.module_utils.diff import (
|
||||
_patch_diff_objects,
|
||||
)
|
||||
|
||||
from copy import deepcopy
|
||||
from typing import Dict
|
||||
|
||||
@@ -402,6 +416,14 @@ def arg_spec() -> Dict:
|
||||
},
|
||||
},
|
||||
},
|
||||
"hidden_fields": {
|
||||
"type": "list",
|
||||
"elements": "str",
|
||||
"default": [
|
||||
"metadata.annotations[kubemacpool.io/transaction-timestamp]",
|
||||
"metadata.managedFields",
|
||||
],
|
||||
},
|
||||
}
|
||||
spec.update(deepcopy(AUTH_ARG_SPEC))
|
||||
spec.update(deepcopy(COMMON_ARG_SPEC))
|
||||
@@ -440,4 +462,5 @@ def main() -> None:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_patch_diff_objects()
|
||||
main()
|
||||
|
||||
@@ -74,6 +74,15 @@ options:
|
||||
- Ignored if O(wait) is not set.
|
||||
default: 120
|
||||
type: int
|
||||
hidden_fields:
|
||||
description:
|
||||
- Hide fields matching this option in the result.
|
||||
- An example might be O(hidden_fields=[metadata.managedFields])
|
||||
or O(hidden_fields=[metadata.annotations[kubemacpool.io/transaction-timestamp]]).
|
||||
type: list
|
||||
elements: str
|
||||
default: ['metadata.annotations[kubemacpool.io/transaction-timestamp]', metadata.managedFields]
|
||||
version_added: 2.2.0
|
||||
|
||||
requirements:
|
||||
- "python >= 3.9"
|
||||
@@ -150,6 +159,11 @@ resources:
|
||||
type: dict
|
||||
"""
|
||||
|
||||
# Monkey patch service.diff_objects to temporarily fix the changed logic
|
||||
from ansible_collections.kubevirt.core.plugins.module_utils.diff import (
|
||||
_patch_diff_objects,
|
||||
)
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
|
||||
@@ -174,6 +188,14 @@ def arg_spec():
|
||||
"""
|
||||
spec = {
|
||||
"running": {"type": "bool"},
|
||||
"hidden_fields": {
|
||||
"type": "list",
|
||||
"elements": "str",
|
||||
"default": [
|
||||
"metadata.annotations[kubemacpool.io/transaction-timestamp]",
|
||||
"metadata.managedFields",
|
||||
],
|
||||
},
|
||||
}
|
||||
spec.update(deepcopy(INFO_ARG_SPEC))
|
||||
spec.update(deepcopy(AUTH_ARG_SPEC))
|
||||
@@ -206,4 +228,5 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_patch_diff_objects()
|
||||
main()
|
||||
|
||||
@@ -66,6 +66,15 @@ options:
|
||||
- Ignored if O(wait) is not set.
|
||||
default: 120
|
||||
type: int
|
||||
hidden_fields:
|
||||
description:
|
||||
- Hide fields matching this option in the result.
|
||||
- An example might be O(hidden_fields=[metadata.managedFields])
|
||||
or O(hidden_fields=[metadata.annotations[kubemacpool.io/transaction-timestamp]]).
|
||||
type: list
|
||||
elements: str
|
||||
default: ['metadata.annotations[kubemacpool.io/transaction-timestamp]', metadata.managedFields]
|
||||
version_added: 2.2.0
|
||||
|
||||
requirements:
|
||||
- "python >= 3.9"
|
||||
@@ -135,6 +144,11 @@ resources:
|
||||
type: dict
|
||||
"""
|
||||
|
||||
# Monkey patch service.diff_objects to temporarily fix the changed logic
|
||||
from ansible_collections.kubevirt.core.plugins.module_utils.diff import (
|
||||
_patch_diff_objects,
|
||||
)
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
|
||||
@@ -157,7 +171,16 @@ def arg_spec():
|
||||
"""
|
||||
arg_spec defines the argument spec of this module.
|
||||
"""
|
||||
spec = {}
|
||||
spec = {
|
||||
"hidden_fields": {
|
||||
"type": "list",
|
||||
"elements": "str",
|
||||
"default": [
|
||||
"metadata.annotations[kubemacpool.io/transaction-timestamp]",
|
||||
"metadata.managedFields",
|
||||
],
|
||||
},
|
||||
}
|
||||
spec.update(deepcopy(INFO_ARG_SPEC))
|
||||
spec.update(deepcopy(AUTH_ARG_SPEC))
|
||||
|
||||
@@ -184,4 +207,5 @@ def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
_patch_diff_objects()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user