mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-11-21 14:48:37 +00:00
Support for metadata-only fetches in k8s_info module (#1030)
* Support for metadata-only fetches in k8s_info module * Fix integration test assert * Add missing changelog fragment * Fix linters and sanity checks * Fix linters * Apply suggestion from @abikouo * fix: resolve linter errors after rebase Fix import sorting and apply black formatting to k8s_info.py and args_common.py to pass tox -e linters. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Bikouo Aubin <79859644+abikouo@users.noreply.github.com> Co-authored-by: aubin bikouo <abikouo@redhat.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Bikouo Aubin
aubin bikouo
Claude Opus 4.6
parent
dd4ca7a8b0
commit
08a761fc17
@@ -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)
|
||||
@@ -144,3 +144,18 @@ DELETE_OPTS_ARG_SPEC = {
|
||||
"options": {"resourceVersion": {"type": "str"}, "uid": {"type": "str"}},
|
||||
},
|
||||
}
|
||||
|
||||
METADATA_ONLY_HEADERS_SPEC = {
|
||||
"accept": {"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"
|
||||
),
|
||||
}
|
||||
|
||||
@@ -181,6 +181,7 @@ def _create_headers(module=None, **kwargs):
|
||||
header_map = {
|
||||
"impersonate_user": "Impersonate-User",
|
||||
"impersonate_groups": "Impersonate-Group",
|
||||
"accept": "Accept",
|
||||
}
|
||||
|
||||
headers = {}
|
||||
|
||||
+40
-10
@@ -53,6 +53,15 @@ options:
|
||||
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.6.0
|
||||
|
||||
extends_documentation_fragment:
|
||||
- kubernetes.core.k8s_auth_options
|
||||
@@ -135,6 +144,12 @@ EXAMPLES = r"""
|
||||
(ocp_bootstrap_status.resources[0].data.status == 'complete')
|
||||
retries: 60
|
||||
delay: 15
|
||||
|
||||
- name: Get a list of all pods metadata only
|
||||
kubernetes.core.k8s_info:
|
||||
kind: Pod
|
||||
metadata_only: true
|
||||
register: pod_metadata_list
|
||||
"""
|
||||
|
||||
RETURN = r"""
|
||||
@@ -179,6 +194,8 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule impo
|
||||
)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
|
||||
AUTH_ARG_SPEC,
|
||||
METADATA_ONLY_HEADER_VALUES,
|
||||
METADATA_ONLY_HEADERS_SPEC,
|
||||
WAIT_ARG_SPEC,
|
||||
)
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
|
||||
@@ -216,15 +233,16 @@ def argspec():
|
||||
args = copy.deepcopy(AUTH_ARG_SPEC)
|
||||
args.update(WAIT_ARG_SPEC)
|
||||
args.update(
|
||||
dict(
|
||||
kind=dict(required=True),
|
||||
api_version=dict(default="v1", aliases=["api", "version"]),
|
||||
name=dict(),
|
||||
namespace=dict(),
|
||||
label_selectors=dict(type="list", elements="str", default=[]),
|
||||
field_selectors=dict(type="list", elements="str", default=[]),
|
||||
hidden_fields=dict(type="list", elements="str"),
|
||||
)
|
||||
{
|
||||
"kind": {"required": True},
|
||||
"api_version": {"default": "v1", "aliases": ["api", "version"]},
|
||||
"name": {},
|
||||
"namespace": {},
|
||||
"label_selectors": {"type": "list", "elements": "str", "default": []},
|
||||
"field_selectors": {"type": "list", "elements": "str", "default": []},
|
||||
"hidden_fields": {"type": "list", "elements": "str"},
|
||||
"metadata_only": {"type": "bool", "default": False},
|
||||
}
|
||||
)
|
||||
return args
|
||||
|
||||
@@ -233,8 +251,20 @@ 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(
|
||||
{"accept": METADATA_ONLY_HEADER_VALUES["partial_object_list"]}
|
||||
)
|
||||
if module.params["name"]:
|
||||
metadata_only_headers.update(
|
||||
{"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:
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
- wait
|
||||
- api-server-caching
|
||||
- discovery
|
||||
- metadata-only-fetches
|
||||
|
||||
@@ -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_full_list.resources[0].kind == "Node"
|
||||
Reference in New Issue
Block a user