| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: k8s_info |
| 14 |
|
| 15 |
short_description: Describe Kubernetes (K8s) objects |
| 16 |
|
| 17 |
author: |
| 18 |
- "Will Thames (@willthames)" |
| 19 |
|
| 20 |
description: |
| 21 |
- Use the Kubernetes Python client to perform read operations on K8s objects. |
| 22 |
- Access to the full range of K8s APIs. |
| 23 |
- Authenticate using either a config file, certificates, password or token. |
| 24 |
- Supports check mode. |
| 25 |
- This module was called C(k8s_facts) before Ansible 2.9. The usage did not change. |
| 26 |
|
| 27 |
options: |
| 28 |
kind: |
| 29 |
description: |
| 30 |
- Use to specify an object model. |
| 31 |
- Use to create, delete, or discover an object without providing a full resource definition. |
| 32 |
- Use in conjunction with I(api_version), I(name), and I(namespace) to identify a specific object. |
| 33 |
- If I(resource definition) is provided, the I(kind) value from the I(resource_definition) |
| 34 |
will override this option. |
| 35 |
type: str |
| 36 |
required: True |
| 37 |
label_selectors: |
| 38 |
description: List of label selectors to use to filter results |
| 39 |
type: list |
| 40 |
elements: str |
| 41 |
default: [] |
| 42 |
field_selectors: |
| 43 |
description: List of field selectors to use to filter results |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
default: [] |
| 47 |
hidden_fields: |
| 48 |
description: |
| 49 |
- Hide fields matching any of the field definitions in the result |
| 50 |
- An example might be C(hidden_fields=[metadata.managedFields]) |
| 51 |
or V(hidden_fields=[spec.containers[0].env[3].value]) |
| 52 |
or V(hidden_fields=[metadata.annotations[kubectl.kubernetes.io/last-applied-configuration]]) |
| 53 |
type: list |
| 54 |
elements: str |
| 55 |
version_added: 3.0.0 |
| 56 |
|
| 57 |
extends_documentation_fragment: |
| 58 |
- kubernetes.core.k8s_auth_options |
| 59 |
- kubernetes.core.k8s_name_options |
| 60 |
- kubernetes.core.k8s_wait_options |
| 61 |
|
| 62 |
requirements: |
| 63 |
- "python >= 3.9" |
| 64 |
- "kubernetes >= 24.2.0" |
| 65 |
- "PyYAML >= 3.11" |
| 66 |
""" |
| 67 |
|
| 68 |
EXAMPLES = r""" |
| 69 |
- name: Get an existing Service object |
| 70 |
kubernetes.core.k8s_info: |
| 71 |
api_version: v1 |
| 72 |
kind: Service |
| 73 |
name: web |
| 74 |
namespace: testing |
| 75 |
register: web_service |
| 76 |
|
| 77 |
- name: Get a list of all service objects |
| 78 |
kubernetes.core.k8s_info: |
| 79 |
api_version: v1 |
| 80 |
kind: Service |
| 81 |
namespace: testing |
| 82 |
register: service_list |
| 83 |
|
| 84 |
- name: Get a list of all pods from any namespace |
| 85 |
kubernetes.core.k8s_info: |
| 86 |
kind: Pod |
| 87 |
register: pod_list |
| 88 |
|
| 89 |
- name: Search for all Pods labelled app=web |
| 90 |
kubernetes.core.k8s_info: |
| 91 |
kind: Pod |
| 92 |
label_selectors: |
| 93 |
- app = web |
| 94 |
- tier in (dev, test) |
| 95 |
|
| 96 |
- name: Using vars while using label_selectors |
| 97 |
kubernetes.core.k8s_info: |
| 98 |
kind: Pod |
| 99 |
label_selectors: |
| 100 |
- "app = {{ app_label_web }}" |
| 101 |
vars: |
| 102 |
app_label_web: web |
| 103 |
|
| 104 |
- name: Search for all running pods |
| 105 |
kubernetes.core.k8s_info: |
| 106 |
kind: Pod |
| 107 |
field_selectors: |
| 108 |
- status.phase=Running |
| 109 |
|
| 110 |
- name: List custom objects created using CRD |
| 111 |
kubernetes.core.k8s_info: |
| 112 |
kind: MyCustomObject |
| 113 |
api_version: "stable.example.com/v1" |
| 114 |
|
| 115 |
- name: Wait till the Object is created |
| 116 |
kubernetes.core.k8s_info: |
| 117 |
kind: Pod |
| 118 |
wait: yes |
| 119 |
name: pod-not-yet-created |
| 120 |
namespace: default |
| 121 |
wait_sleep: 10 |
| 122 |
wait_timeout: 360 |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = r""" |
| 126 |
api_found: |
| 127 |
description: |
| 128 |
- Whether the specified api_version and kind were successfully mapped to an existing API on the targeted cluster. |
| 129 |
- Version added 1.2.0. |
| 130 |
returned: always |
| 131 |
type: bool |
| 132 |
resources: |
| 133 |
description: |
| 134 |
- The object(s) that exists |
| 135 |
returned: success |
| 136 |
type: complex |
| 137 |
contains: |
| 138 |
api_version: |
| 139 |
description: The versioned schema of this representation of an object. |
| 140 |
returned: success |
| 141 |
type: str |
| 142 |
kind: |
| 143 |
description: Represents the REST resource this object represents. |
| 144 |
returned: success |
| 145 |
type: str |
| 146 |
metadata: |
| 147 |
description: Standard object metadata. Includes name, namespace, annotations, labels, etc. |
| 148 |
returned: success |
| 149 |
type: dict |
| 150 |
spec: |
| 151 |
description: Specific attributes of the object. Will vary based on the I(api_version) and I(kind). |
| 152 |
returned: success |
| 153 |
type: dict |
| 154 |
status: |
| 155 |
description: Current status details for the object. |
| 156 |
returned: success |
| 157 |
type: dict |
| 158 |
""" |
| 159 |
|
| 160 |
import copy |
| 161 |
|
| 162 |
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( |
| 163 |
AnsibleModule, |
| 164 |
) |
| 165 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 166 |
AUTH_ARG_SPEC, |
| 167 |
WAIT_ARG_SPEC, |
| 168 |
) |
| 169 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( |
| 170 |
get_api_client, |
| 171 |
) |
| 172 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( |
| 173 |
AnsibleK8SModule, |
| 174 |
) |
| 175 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( |
| 176 |
CoreException, |
| 177 |
) |
| 178 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( |
| 179 |
K8sService, |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
def execute_module(module, svc): |
| 184 |
facts = svc.find( |
| 185 |
module.params["kind"], |
| 186 |
module.params["api_version"], |
| 187 |
name=module.params["name"], |
| 188 |
namespace=module.params["namespace"], |
| 189 |
label_selectors=module.params["label_selectors"], |
| 190 |
field_selectors=module.params["field_selectors"], |
| 191 |
wait=module.params["wait"], |
| 192 |
wait_sleep=module.params["wait_sleep"], |
| 193 |
wait_timeout=module.params["wait_timeout"], |
| 194 |
condition=module.params["wait_condition"], |
| 195 |
hidden_fields=module.params["hidden_fields"], |
| 196 |
) |
| 197 |
module.exit_json(changed=False, **facts) |
| 198 |
|
| 199 |
|
| 200 |
def argspec(): |
| 201 |
args = copy.deepcopy(AUTH_ARG_SPEC) |
| 202 |
args.update(WAIT_ARG_SPEC) |
| 203 |
args.update( |
| 204 |
dict( |
| 205 |
kind=dict(required=True), |
| 206 |
api_version=dict(default="v1", aliases=["api", "version"]), |
| 207 |
name=dict(), |
| 208 |
namespace=dict(), |
| 209 |
label_selectors=dict(type="list", elements="str", default=[]), |
| 210 |
field_selectors=dict(type="list", elements="str", default=[]), |
| 211 |
hidden_fields=dict(type="list", elements="str"), |
| 212 |
) |
| 213 |
) |
| 214 |
return args |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
module = AnsibleK8SModule( |
| 219 |
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True |
| 220 |
) |
| 221 |
try: |
| 222 |
client = get_api_client(module) |
| 223 |
svc = K8sService(client, module) |
| 224 |
execute_module(module, svc) |
| 225 |
except CoreException as e: |
| 226 |
module.fail_from_exception(e) |
| 227 |
|
| 228 |
|
| 229 |
if __name__ == "__main__": |
| 230 |
main() |