| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from typing import Dict |
| 5 |
|
| 6 |
from ansible.module_utils.common.text.converters import to_native |
| 7 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( |
| 8 |
get_api_client, |
| 9 |
) |
| 10 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( |
| 11 |
CoreException, |
| 12 |
ResourceTimeout, |
| 13 |
) |
| 14 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import ( |
| 15 |
create_definitions, |
| 16 |
) |
| 17 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import ( |
| 18 |
K8sService, |
| 19 |
diff_objects, |
| 20 |
hide_fields, |
| 21 |
) |
| 22 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import exists |
| 23 |
from ansible_collections.kubernetes.core.plugins.module_utils.selector import ( |
| 24 |
LabelSelectorFilter, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def validate(client, module, resource): |
| 29 |
def _prepend_resource_info(resource, msg): |
| 30 |
return "%s %s: %s" % (resource["kind"], resource["metadata"]["name"], msg) |
| 31 |
|
| 32 |
module.requires("kubernetes-validate") |
| 33 |
|
| 34 |
warnings, errors = client.validate( |
| 35 |
resource, |
| 36 |
module.params["validate"].get("version"), |
| 37 |
module.params["validate"].get("strict"), |
| 38 |
) |
| 39 |
|
| 40 |
if errors and module.params["validate"]["fail_on_error"]: |
| 41 |
module.fail_json( |
| 42 |
msg="\n".join([_prepend_resource_info(resource, error) for error in errors]) |
| 43 |
) |
| 44 |
return [_prepend_resource_info(resource, msg) for msg in warnings + errors] |
| 45 |
|
| 46 |
|
| 47 |
def get_definitions(svc, params): |
| 48 |
try: |
| 49 |
definitions = create_definitions(params) |
| 50 |
except Exception as e: |
| 51 |
msg = "Failed to load resource definition: {0}".format(e) |
| 52 |
raise CoreException(msg) from e |
| 53 |
|
| 54 |
delete_all = params.get("delete_all") |
| 55 |
src = params.get("src") |
| 56 |
resource_definition = params.get("resource_definition") |
| 57 |
name = params.get("name") |
| 58 |
state = params.get("state") |
| 59 |
|
| 60 |
if ( |
| 61 |
delete_all |
| 62 |
and state == "absent" |
| 63 |
and name is None |
| 64 |
and resource_definition is None |
| 65 |
and src is None |
| 66 |
): |
| 67 |
|
| 68 |
if params.get("kind") is None: |
| 69 |
raise CoreException( |
| 70 |
"'kind' option is required to specify the resource type." |
| 71 |
) |
| 72 |
|
| 73 |
resource = svc.find_resource( |
| 74 |
params.get("kind"), params.get("api_version"), fail=True |
| 75 |
) |
| 76 |
definitions = svc.retrieve_all( |
| 77 |
resource, |
| 78 |
params.get("namespace"), |
| 79 |
params.get("label_selectors"), |
| 80 |
) |
| 81 |
|
| 82 |
return definitions |
| 83 |
|
| 84 |
|
| 85 |
def run_module(module) -> None: |
| 86 |
results = [] |
| 87 |
changed = False |
| 88 |
client = get_api_client(module) |
| 89 |
svc = K8sService(client, module) |
| 90 |
|
| 91 |
definitions = get_definitions(svc, module.params) |
| 92 |
|
| 93 |
for definition in definitions: |
| 94 |
result = {"changed": False, "result": {}} |
| 95 |
warnings = [] |
| 96 |
|
| 97 |
if module.params.get("validate") is not None: |
| 98 |
warnings = validate(client, module, definition) |
| 99 |
|
| 100 |
try: |
| 101 |
result = perform_action(svc, definition, module.params) |
| 102 |
except Exception as e: |
| 103 |
try: |
| 104 |
error = e.result |
| 105 |
except AttributeError: |
| 106 |
error = {} |
| 107 |
try: |
| 108 |
error["reason"] = e.__cause__.reason |
| 109 |
except AttributeError: |
| 110 |
pass |
| 111 |
error["msg"] = to_native(e) |
| 112 |
if warnings: |
| 113 |
error.setdefault("warnings", []).extend(warnings) |
| 114 |
|
| 115 |
if module.params.get("continue_on_error"): |
| 116 |
result["error"] = error |
| 117 |
else: |
| 118 |
module.fail_json(**error) |
| 119 |
|
| 120 |
if warnings: |
| 121 |
result.setdefault("warnings", []).extend(warnings) |
| 122 |
changed |= result["changed"] |
| 123 |
results.append(result) |
| 124 |
|
| 125 |
if len(results) == 1: |
| 126 |
module.exit_json(**results[0]) |
| 127 |
|
| 128 |
module.exit_json(**{"changed": changed, "result": {"results": results}}) |
| 129 |
|
| 130 |
|
| 131 |
def perform_action(svc, definition: Dict, params: Dict) -> Dict: |
| 132 |
origin_name = definition["metadata"].get("name") |
| 133 |
namespace = definition["metadata"].get("namespace") |
| 134 |
label_selectors = params.get("label_selectors") |
| 135 |
state = params.get("state", None) |
| 136 |
kind = definition.get("kind") |
| 137 |
api_version = definition.get("apiVersion") |
| 138 |
hidden_fields = params.get("hidden_fields") |
| 139 |
|
| 140 |
result = {"changed": False, "result": {}} |
| 141 |
instance = {} |
| 142 |
warnings = [] |
| 143 |
|
| 144 |
resource = svc.find_resource(kind, api_version, fail=True) |
| 145 |
definition["kind"] = resource.kind |
| 146 |
definition["apiVersion"] = resource.group_version |
| 147 |
existing = svc.retrieve(resource, definition) |
| 148 |
|
| 149 |
if state == "absent": |
| 150 |
if exists(existing) and existing.kind.endswith("List"): |
| 151 |
instance = [] |
| 152 |
for item in existing.items: |
| 153 |
r = svc.delete(resource, item, existing) |
| 154 |
instance.append(r) |
| 155 |
else: |
| 156 |
instance = svc.delete(resource, definition, existing) |
| 157 |
result["method"] = "delete" |
| 158 |
if exists(existing): |
| 159 |
result["changed"] = True |
| 160 |
else: |
| 161 |
if label_selectors: |
| 162 |
filter_selector = LabelSelectorFilter(label_selectors) |
| 163 |
if not filter_selector.isMatching(definition): |
| 164 |
result["changed"] = False |
| 165 |
result["msg"] = ( |
| 166 |
"resource 'kind={kind},name={name},namespace={namespace}' " |
| 167 |
"filtered by label_selectors.".format( |
| 168 |
kind=kind, |
| 169 |
name=origin_name, |
| 170 |
namespace=namespace, |
| 171 |
) |
| 172 |
) |
| 173 |
return result |
| 174 |
|
| 175 |
if params.get("apply"): |
| 176 |
instance, warnings = svc.apply(resource, definition, existing) |
| 177 |
result["method"] = "apply" |
| 178 |
elif not existing: |
| 179 |
if state == "patched": |
| 180 |
result.setdefault("warnings", []).append( |
| 181 |
"resource 'kind={kind},name={name}' was not found but will not be " |
| 182 |
"created as 'state' parameter has been set to '{state}'".format( |
| 183 |
kind=kind, name=definition["metadata"].get("name"), state=state |
| 184 |
) |
| 185 |
) |
| 186 |
return result |
| 187 |
instance, warnings = svc.create(resource, definition) |
| 188 |
result["method"] = "create" |
| 189 |
result["changed"] = True |
| 190 |
elif params.get("force", False): |
| 191 |
instance, warnings = svc.replace(resource, definition, existing) |
| 192 |
result["method"] = "replace" |
| 193 |
else: |
| 194 |
instance, warnings = svc.update(resource, definition, existing) |
| 195 |
result["method"] = "update" |
| 196 |
|
| 197 |
if warnings: |
| 198 |
result["warnings"] = warnings |
| 199 |
|
| 200 |
|
| 201 |
success = True |
| 202 |
|
| 203 |
if result["method"] == "delete": |
| 204 |
|
| 205 |
if params.get("wait") and not svc.module.check_mode: |
| 206 |
success, waited, duration = svc.wait(resource, definition) |
| 207 |
result["duration"] = duration |
| 208 |
else: |
| 209 |
if params.get("wait") and not svc.module.check_mode: |
| 210 |
success, instance, duration = svc.wait(resource, instance) |
| 211 |
result["duration"] = duration |
| 212 |
|
| 213 |
if result["method"] not in ("create", "delete"): |
| 214 |
if existing: |
| 215 |
existing = existing.to_dict() |
| 216 |
else: |
| 217 |
existing = {} |
| 218 |
match, diffs = diff_objects(existing, instance, hidden_fields) |
| 219 |
if match and diffs: |
| 220 |
result.setdefault("warnings", []).append( |
| 221 |
"No meaningful diff was generated, but the API may not be idempotent " |
| 222 |
"(only metadata.generation or metadata.resourceVersion were changed)" |
| 223 |
) |
| 224 |
result["changed"] = not match |
| 225 |
if svc.module._diff: |
| 226 |
result["diff"] = diffs |
| 227 |
|
| 228 |
result["result"] = hide_fields(instance, hidden_fields) |
| 229 |
if not success: |
| 230 |
raise ResourceTimeout( |
| 231 |
'"{0}" "{1}": Timed out waiting on resource'.format( |
| 232 |
definition["kind"], origin_name |
| 233 |
), |
| 234 |
result, |
| 235 |
) |
| 236 |
|
| 237 |
return result |