From 5ee8cfd8dbcf51be563b4538127e45bef9bdc3ce Mon Sep 17 00:00:00 2001 From: Federico Chiacchiaretta Date: Fri, 17 Oct 2025 13:57:01 +0200 Subject: [PATCH 1/6] Support for metadata-only fetches in k8s_info module --- plugins/module_utils/args_common.py | 9 +++++ plugins/module_utils/k8s/client.py | 1 + plugins/modules/k8s_info.py | 36 ++++++++++++++++++- .../targets/k8s_info/tasks/main.yml | 1 + .../k8s_info/tasks/metadata-only-fetches.yml | 22 ++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml diff --git a/plugins/module_utils/args_common.py b/plugins/module_utils/args_common.py index 0ea022c52c..fa7e701109 100644 --- a/plugins/module_utils/args_common.py +++ b/plugins/module_utils/args_common.py @@ -144,3 +144,12 @@ def _extract_recursive(data, current_path=""): "options": {"resourceVersion": {"type": "str"}, "uid": {"type": "str"}}, }, } + +METADATA_ONLY_HEADERS_SPEC = dict( + accept=dict(type="str"), +) + +METADATA_ONLY_HEADER_VALUES = { + "partial_object": "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1, application/json;q=0.9", + "partial_object_list":"application/json;as=PartialObjectMetadataList;g=meta.k8s.io;v=v1, application/json;q=0.9" +} diff --git a/plugins/module_utils/k8s/client.py b/plugins/module_utils/k8s/client.py index 197c34b967..2ab2eed651 100644 --- a/plugins/module_utils/k8s/client.py +++ b/plugins/module_utils/k8s/client.py @@ -181,6 +181,7 @@ def _create_headers(module=None, **kwargs): header_map = { "impersonate_user": "Impersonate-User", "impersonate_groups": "Impersonate-Group", + "accept": "Accept", } headers = {} diff --git a/plugins/modules/k8s_info.py b/plugins/modules/k8s_info.py index 306425ad01..928a558b24 100644 --- a/plugins/modules/k8s_info.py +++ b/plugins/modules/k8s_info.py @@ -53,6 +53,15 @@ type: list elements: str version_added: 3.0.0 + metadata_only: + description: + - Request metadata only response from the Kubernetes API server. + - This feature is useful for clients that only need to check for the existence of an object, + - or that only need to read its metadata. + - It can significantly reduce the size of the response from the API server. + type: bool + default: false + version_added: 6.3.0 extends_documentation_fragment: - kubernetes.core.k8s_auth_options @@ -120,6 +129,12 @@ namespace: default wait_sleep: 10 wait_timeout: 360 + +- name: Get a list of all pods metadata only + kubernetes.core.k8s_info: + kind: Pod + metadata_only: true + register: pod_metadata_list """ RETURN = r""" @@ -165,6 +180,8 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_SPEC, WAIT_ARG_SPEC, + METADATA_ONLY_HEADERS_SPEC, + METADATA_ONLY_HEADER_VALUES ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, @@ -209,6 +226,7 @@ def argspec(): label_selectors=dict(type="list", elements="str", default=[]), field_selectors=dict(type="list", elements="str", default=[]), hidden_fields=dict(type="list", elements="str"), + metadata_only=dict(type="bool", default=False), ) ) return args @@ -218,8 +236,24 @@ def main(): module = AnsibleK8SModule( module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True ) + + metadata_only_headers = {} + if module.params["metadata_only"]: + metadata_only_headers = copy.deepcopy(METADATA_ONLY_HEADERS_SPEC) + metadata_only_headers.update( + dict( + accept=METADATA_ONLY_HEADER_VALUES["partial_object_list"] + ) + ) + if module.params["name"]: + metadata_only_headers.update( + dict( + accept=METADATA_ONLY_HEADER_VALUES["partial_object"] + ) + ) + try: - client = get_api_client(module) + client = get_api_client(module, **metadata_only_headers) svc = K8sService(client, module) execute_module(module, svc) except CoreException as e: diff --git a/tests/integration/targets/k8s_info/tasks/main.yml b/tests/integration/targets/k8s_info/tasks/main.yml index 7235564033..3b58e89ff3 100644 --- a/tests/integration/targets/k8s_info/tasks/main.yml +++ b/tests/integration/targets/k8s_info/tasks/main.yml @@ -4,3 +4,4 @@ - wait - api-server-caching - discovery + - metadata-only-fetches diff --git a/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml b/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml new file mode 100644 index 0000000000..de638a5ee7 --- /dev/null +++ b/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml @@ -0,0 +1,22 @@ +--- +- name: Retrieve Node list with metadata_only + kubernetes.core.k8s_info: + kind: Node + metadata_only: true + register: node_metadata_only_list + +- name: Assert response has kind PartialObjectMetadata + assert: + that: + - node_metadata_only_list.resources[0].kind == "PartialObjectMetadata" + +- name: Retrieve full Node list + kubernetes.core.k8s_info: + kind: Node + metadata_only: false + register: node_full_list + +- name: Assert response has kind Node + assert: + that: + - node_metadata_only_list.resources[0].kind == "Node" From d3f94da612ee436eee2ffd71b0a9df2aa652f8bf Mon Sep 17 00:00:00 2001 From: Federico Chiacchiaretta Date: Fri, 17 Oct 2025 16:42:21 +0200 Subject: [PATCH 2/6] Fix integration test assert --- .../targets/k8s_info/tasks/metadata-only-fetches.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml b/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml index de638a5ee7..57791c3900 100644 --- a/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml +++ b/tests/integration/targets/k8s_info/tasks/metadata-only-fetches.yml @@ -19,4 +19,4 @@ - name: Assert response has kind Node assert: that: - - node_metadata_only_list.resources[0].kind == "Node" + - node_full_list.resources[0].kind == "Node" From 1328c13ef04b612b90905f68c346d408f4b6649e Mon Sep 17 00:00:00 2001 From: Federico Chiacchiaretta Date: Mon, 27 Oct 2025 20:38:35 +0100 Subject: [PATCH 3/6] Add missing changelog fragment --- changelogs/fragments/20251027-metadata-only-fetches.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/20251027-metadata-only-fetches.yaml diff --git a/changelogs/fragments/20251027-metadata-only-fetches.yaml b/changelogs/fragments/20251027-metadata-only-fetches.yaml new file mode 100644 index 0000000000..4fac2cfd03 --- /dev/null +++ b/changelogs/fragments/20251027-metadata-only-fetches.yaml @@ -0,0 +1,2 @@ +minor_changes: + - k8s_info - Support for metadata-only fetches in k8s_info module (https://github.com/ansible-collections/kubernetes.core/pull/1030) From 6b04827d223dab14b8492091de628587080cb8a8 Mon Sep 17 00:00:00 2001 From: Federico Chiacchiaretta Date: Mon, 27 Oct 2025 20:39:05 +0100 Subject: [PATCH 4/6] Updated k8s_info docs --- docs/kubernetes.core.k8s_info_module.rst | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/kubernetes.core.k8s_info_module.rst b/docs/kubernetes.core.k8s_info_module.rst index cbd56febd3..d55617aedf 100644 --- a/docs/kubernetes.core.k8s_info_module.rst +++ b/docs/kubernetes.core.k8s_info_module.rst @@ -280,6 +280,29 @@ Parameters
List of label selectors to use to filter results
+ + +
+ metadata_only + +
+ boolean +
+
added in 6.3.0
+ + +
    Choices: +
  • no ←
  • +
  • yes
  • +
+ + +
Request metadata only response from the Kubernetes API server.
+
This feature is useful for clients that only need to check for the existence of an object,
+
or that only need to read its metadata.
+
It can significantly reduce the size of the response from the API server.
+ +
@@ -701,6 +724,12 @@ Examples wait_sleep: 10 wait_timeout: 360 + - name: Get a list of all pods metadata only + kubernetes.core.k8s_info: + kind: Pod + metadata_only: true + register: pod_metadata_list + Return Values From 1becf1c64621799746a79f96065dbc5688a4f10e Mon Sep 17 00:00:00 2001 From: Federico Chiacchiaretta Date: Mon, 27 Oct 2025 22:43:05 +0100 Subject: [PATCH 5/6] Fix linters and sanity checks --- plugins/module_utils/args_common.py | 10 ++++++++-- plugins/modules/k8s_info.py | 10 +++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/plugins/module_utils/args_common.py b/plugins/module_utils/args_common.py index fa7e701109..60b87badaf 100644 --- a/plugins/module_utils/args_common.py +++ b/plugins/module_utils/args_common.py @@ -150,6 +150,12 @@ def _extract_recursive(data, current_path=""): ) METADATA_ONLY_HEADER_VALUES = { - "partial_object": "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1, application/json;q=0.9", - "partial_object_list":"application/json;as=PartialObjectMetadataList;g=meta.k8s.io;v=v1, application/json;q=0.9" + "partial_object": ( + "application/json;as=PartialObjectMetadata;g=meta.k8s.io;v=v1," + "application/json;q=0.9" + ), + "partial_object_list": ( + "application/json;as=PartialObjectMetadataList;g=meta.k8s.io;v=v1," + "application/json;q=0.9" + ), } diff --git a/plugins/modules/k8s_info.py b/plugins/modules/k8s_info.py index 928a558b24..6b0b97a361 100644 --- a/plugins/modules/k8s_info.py +++ b/plugins/modules/k8s_info.py @@ -181,7 +181,7 @@ AUTH_ARG_SPEC, WAIT_ARG_SPEC, METADATA_ONLY_HEADERS_SPEC, - METADATA_ONLY_HEADER_VALUES + METADATA_ONLY_HEADER_VALUES, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client, @@ -241,15 +241,11 @@ def main(): if module.params["metadata_only"]: metadata_only_headers = copy.deepcopy(METADATA_ONLY_HEADERS_SPEC) metadata_only_headers.update( - dict( - accept=METADATA_ONLY_HEADER_VALUES["partial_object_list"] - ) + dict(accept=METADATA_ONLY_HEADER_VALUES["partial_object_list"]) ) if module.params["name"]: metadata_only_headers.update( - dict( - accept=METADATA_ONLY_HEADER_VALUES["partial_object"] - ) + dict(accept=METADATA_ONLY_HEADER_VALUES["partial_object"]) ) try: From e5e1487a0e6d5e67602a152b04407fc5e9f226f1 Mon Sep 17 00:00:00 2001 From: Federico Chiacchiaretta Date: Wed, 29 Oct 2025 08:38:20 +0100 Subject: [PATCH 6/6] Fix linters --- plugins/modules/k8s_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/k8s_info.py b/plugins/modules/k8s_info.py index 6b0b97a361..d2c5bbe016 100644 --- a/plugins/modules/k8s_info.py +++ b/plugins/modules/k8s_info.py @@ -179,9 +179,9 @@ ) from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( AUTH_ARG_SPEC, - WAIT_ARG_SPEC, METADATA_ONLY_HEADERS_SPEC, METADATA_ONLY_HEADER_VALUES, + WAIT_ARG_SPEC, ) from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( get_api_client,