| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: k8s_cluster_info |
| 13 |
|
| 14 |
version_added: 0.11.1 |
| 15 |
|
| 16 |
short_description: Describe Kubernetes (K8s) cluster, APIs available and their respective versions |
| 17 |
|
| 18 |
author: |
| 19 |
- Abhijeet Kasurde (@Akasurde) |
| 20 |
|
| 21 |
description: |
| 22 |
- Use the Kubernetes Python client to perform read operations on K8s objects. |
| 23 |
- Authenticate using either a config file, certificates, password or token. |
| 24 |
- Supports check mode. |
| 25 |
|
| 26 |
options: |
| 27 |
invalidate_cache: |
| 28 |
description: |
| 29 |
- Invalidate cache before retrieving information about cluster. |
| 30 |
type: bool |
| 31 |
default: True |
| 32 |
|
| 33 |
extends_documentation_fragment: |
| 34 |
- kubernetes.core.k8s_auth_options |
| 35 |
|
| 36 |
requirements: |
| 37 |
- "python >= 3.9" |
| 38 |
- "kubernetes >= 24.2.0" |
| 39 |
- "PyYAML >= 3.11" |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = r""" |
| 43 |
- name: Get Cluster information |
| 44 |
kubernetes.core.k8s_cluster_info: |
| 45 |
register: api_status |
| 46 |
|
| 47 |
- name: Do not invalidate cache before getting information |
| 48 |
kubernetes.core.k8s_cluster_info: |
| 49 |
invalidate_cache: False |
| 50 |
register: api_status |
| 51 |
""" |
| 52 |
|
| 53 |
RETURN = r""" |
| 54 |
connection: |
| 55 |
description: |
| 56 |
- Connection information |
| 57 |
returned: success |
| 58 |
type: dict |
| 59 |
contains: |
| 60 |
cert_file: |
| 61 |
description: |
| 62 |
- Path to client certificate. |
| 63 |
type: str |
| 64 |
returned: success |
| 65 |
host: |
| 66 |
description: |
| 67 |
- Host URL |
| 68 |
type: str |
| 69 |
returned: success |
| 70 |
password: |
| 71 |
description: |
| 72 |
- User password |
| 73 |
type: str |
| 74 |
returned: success |
| 75 |
proxy: |
| 76 |
description: |
| 77 |
- Proxy details |
| 78 |
type: str |
| 79 |
returned: success |
| 80 |
ssl_ca_cert: |
| 81 |
description: |
| 82 |
- Path to CA certificate |
| 83 |
type: str |
| 84 |
returned: success |
| 85 |
username: |
| 86 |
description: |
| 87 |
- Username |
| 88 |
type: str |
| 89 |
returned: success |
| 90 |
verify_ssl: |
| 91 |
description: |
| 92 |
- SSL verification status |
| 93 |
type: bool |
| 94 |
returned: success |
| 95 |
version: |
| 96 |
description: |
| 97 |
- Information about server and client version |
| 98 |
returned: success |
| 99 |
type: dict |
| 100 |
contains: |
| 101 |
server: |
| 102 |
description: Server version |
| 103 |
returned: success |
| 104 |
type: dict |
| 105 |
client: |
| 106 |
description: Client version |
| 107 |
returned: success |
| 108 |
type: str |
| 109 |
apis: |
| 110 |
description: |
| 111 |
- dictionary of group + version of resource found from cluster |
| 112 |
returned: success |
| 113 |
type: dict |
| 114 |
elements: dict |
| 115 |
contains: |
| 116 |
categories: |
| 117 |
description: API categories |
| 118 |
returned: success |
| 119 |
type: list |
| 120 |
name: |
| 121 |
description: Resource short name |
| 122 |
returned: success |
| 123 |
type: str |
| 124 |
namespaced: |
| 125 |
description: If resource is namespaced |
| 126 |
returned: success |
| 127 |
type: bool |
| 128 |
preferred: |
| 129 |
description: If resource version preferred |
| 130 |
returned: success |
| 131 |
type: bool |
| 132 |
short_names: |
| 133 |
description: Resource short names |
| 134 |
returned: success |
| 135 |
type: str |
| 136 |
singular_name: |
| 137 |
description: Resource singular name |
| 138 |
returned: success |
| 139 |
type: str |
| 140 |
""" |
| 141 |
|
| 142 |
|
| 143 |
import copy |
| 144 |
from collections import defaultdict |
| 145 |
|
| 146 |
try: |
| 147 |
from ansible_collections.kubernetes.core.plugins.module_utils.client.resource import ( |
| 148 |
ResourceList, |
| 149 |
) |
| 150 |
except ImportError: |
| 151 |
|
| 152 |
pass |
| 153 |
|
| 154 |
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( |
| 155 |
AnsibleModule, |
| 156 |
) |
| 157 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 158 |
AUTH_ARG_SPEC, |
| 159 |
) |
| 160 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( |
| 161 |
AnsibleK8SModule, |
| 162 |
) |
| 163 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( |
| 164 |
CoreException, |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
def execute_module(module, client): |
| 169 |
if module.params.get("invalidate_cache"): |
| 170 |
client.resources.invalidate_cache() |
| 171 |
results = defaultdict(dict) |
| 172 |
for resource in list(client.resources): |
| 173 |
resource = resource[0] |
| 174 |
if isinstance(resource, ResourceList): |
| 175 |
continue |
| 176 |
key = ( |
| 177 |
resource.group_version |
| 178 |
if resource.group == "" |
| 179 |
else "/".join([resource.group, resource.group_version.split("/")[-1]]) |
| 180 |
) |
| 181 |
results[key][resource.kind] = { |
| 182 |
"categories": resource.categories if resource.categories else [], |
| 183 |
"name": resource.name, |
| 184 |
"namespaced": resource.namespaced, |
| 185 |
"preferred": resource.preferred, |
| 186 |
"short_names": resource.short_names if resource.short_names else [], |
| 187 |
"singular_name": resource.singular_name, |
| 188 |
} |
| 189 |
configuration = client.configuration |
| 190 |
connection = { |
| 191 |
"cert_file": configuration.cert_file, |
| 192 |
"host": configuration.host, |
| 193 |
"password": configuration.password, |
| 194 |
"proxy": configuration.proxy, |
| 195 |
"ssl_ca_cert": configuration.ssl_ca_cert, |
| 196 |
"username": configuration.username, |
| 197 |
"verify_ssl": configuration.verify_ssl, |
| 198 |
} |
| 199 |
from kubernetes import __version__ as version |
| 200 |
|
| 201 |
version_info = { |
| 202 |
"client": version, |
| 203 |
"server": client.client.version, |
| 204 |
} |
| 205 |
module.exit_json( |
| 206 |
changed=False, apis=results, connection=connection, version=version_info |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
def argspec(): |
| 211 |
spec = copy.deepcopy(AUTH_ARG_SPEC) |
| 212 |
spec["invalidate_cache"] = dict(type="bool", default=True) |
| 213 |
return spec |
| 214 |
|
| 215 |
|
| 216 |
def main(): |
| 217 |
module = AnsibleK8SModule( |
| 218 |
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True |
| 219 |
) |
| 220 |
|
| 221 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( |
| 222 |
get_api_client, |
| 223 |
) |
| 224 |
|
| 225 |
try: |
| 226 |
execute_module(module, client=get_api_client(module=module)) |
| 227 |
except CoreException as e: |
| 228 |
module.fail_from_exception(e) |
| 229 |
|
| 230 |
|
| 231 |
if __name__ == "__main__": |
| 232 |
main() |