mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-07-28 10:24:45 +00:00
[backport/2.2] Update k8s_scale to use module_utils refactored code
This commit is contained in:
committed by
Mike Graves
parent
08a3d951d0
commit
61faa1079e
@@ -143,6 +143,14 @@ result:
|
|||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
try:
|
||||||
|
from kubernetes.dynamic.exceptions import NotFoundError
|
||||||
|
except ImportError:
|
||||||
|
# Handled in module setup
|
||||||
|
pass
|
||||||
|
|
||||||
|
from ansible.module_utils._text import to_native
|
||||||
|
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
|
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import (
|
||||||
AnsibleModule,
|
AnsibleModule,
|
||||||
)
|
)
|
||||||
@@ -151,7 +159,25 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import
|
|||||||
RESOURCE_ARG_SPEC,
|
RESOURCE_ARG_SPEC,
|
||||||
NAME_ARG_SPEC,
|
NAME_ARG_SPEC,
|
||||||
)
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
|
||||||
|
get_api_client,
|
||||||
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
|
||||||
|
AnsibleK8SModule,
|
||||||
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
|
||||||
|
CoreException,
|
||||||
|
ResourceTimeout,
|
||||||
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
|
||||||
|
K8sService,
|
||||||
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import (
|
||||||
|
get_waiter,
|
||||||
|
)
|
||||||
|
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.resource import (
|
||||||
|
create_definitions,
|
||||||
|
)
|
||||||
|
|
||||||
SCALE_ARG_SPEC = {
|
SCALE_ARG_SPEC = {
|
||||||
"replicas": {"type": "int", "required": True},
|
"replicas": {"type": "int", "required": True},
|
||||||
@@ -163,27 +189,20 @@ SCALE_ARG_SPEC = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def execute_module(
|
def execute_module(client, module):
|
||||||
module,
|
|
||||||
k8s_ansible_mixin,
|
|
||||||
):
|
|
||||||
k8s_ansible_mixin.set_resource_definitions(module)
|
|
||||||
|
|
||||||
definition = k8s_ansible_mixin.resource_definitions[0]
|
|
||||||
|
|
||||||
name = definition["metadata"]["name"]
|
|
||||||
namespace = definition["metadata"].get("namespace")
|
|
||||||
api_version = definition["apiVersion"]
|
|
||||||
kind = definition["kind"]
|
|
||||||
current_replicas = module.params.get("current_replicas")
|
current_replicas = module.params.get("current_replicas")
|
||||||
replicas = module.params.get("replicas")
|
replicas = module.params.get("replicas")
|
||||||
resource_version = module.params.get("resource_version")
|
resource_version = module.params.get("resource_version")
|
||||||
|
definitions = create_definitions(module.params)
|
||||||
|
definition = definitions[0]
|
||||||
|
name = definition["metadata"].get("name")
|
||||||
|
namespace = definition["metadata"].get("namespace")
|
||||||
|
api_version = definition["apiVersion"]
|
||||||
|
kind = definition["kind"]
|
||||||
label_selectors = module.params.get("label_selectors")
|
label_selectors = module.params.get("label_selectors")
|
||||||
if not label_selectors:
|
if not label_selectors:
|
||||||
label_selectors = []
|
label_selectors = []
|
||||||
continue_on_error = module.params.get("continue_on_error")
|
continue_on_error = module.params.get("continue_on_error")
|
||||||
|
|
||||||
wait = module.params.get("wait")
|
wait = module.params.get("wait")
|
||||||
wait_time = module.params.get("wait_timeout")
|
wait_time = module.params.get("wait_timeout")
|
||||||
wait_sleep = module.params.get("wait_sleep")
|
wait_sleep = module.params.get("wait_sleep")
|
||||||
@@ -195,12 +214,8 @@ def execute_module(
|
|||||||
if wait:
|
if wait:
|
||||||
return_attributes["duration"] = 0
|
return_attributes["duration"] = 0
|
||||||
|
|
||||||
resource = k8s_ansible_mixin.find_resource(kind, api_version, fail=True)
|
svc = K8sService(client, module)
|
||||||
|
resource = svc.find_resource(kind, api_version, fail=True)
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
|
||||||
NotFoundError,
|
|
||||||
)
|
|
||||||
|
|
||||||
multiple_scale = False
|
multiple_scale = False
|
||||||
try:
|
try:
|
||||||
existing = resource.get(
|
existing = resource.get(
|
||||||
@@ -211,11 +226,10 @@ def execute_module(
|
|||||||
multiple_scale = len(existing_items) > 1
|
multiple_scale = len(existing_items) > 1
|
||||||
else:
|
else:
|
||||||
existing_items = [existing]
|
existing_items = [existing]
|
||||||
except NotFoundError as exc:
|
except NotFoundError as e:
|
||||||
module.fail_json(
|
reason = e.body if hasattr(e, "body") else e
|
||||||
msg="Failed to retrieve requested object: {0}".format(exc),
|
msg = "Failed to retrieve requested object: {0}".format(reason)
|
||||||
error=exc.value.get("status"),
|
raise CoreException(msg) from e
|
||||||
)
|
|
||||||
|
|
||||||
if multiple_scale:
|
if multiple_scale:
|
||||||
# when scaling multiple resource, the 'result' is changed to 'results' and is a list
|
# when scaling multiple resource, the 'result' is changed to 'results' and is a list
|
||||||
@@ -278,23 +292,26 @@ def execute_module(
|
|||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
result["result"] = existing.to_dict()
|
result["result"] = existing.to_dict()
|
||||||
else:
|
else:
|
||||||
result["result"] = resource.patch(existing.to_dict()).to_dict()
|
result["result"] = client.patch(resource, existing.to_dict()).to_dict()
|
||||||
else:
|
else:
|
||||||
result = scale(
|
try:
|
||||||
module,
|
result = scale(
|
||||||
k8s_ansible_mixin,
|
client,
|
||||||
resource,
|
svc,
|
||||||
existing,
|
resource,
|
||||||
replicas,
|
existing,
|
||||||
wait,
|
replicas,
|
||||||
wait_time,
|
wait,
|
||||||
wait_sleep,
|
wait_time,
|
||||||
)
|
wait_sleep,
|
||||||
|
)
|
||||||
|
except CoreException as e:
|
||||||
|
module.fail_json(msg=to_native(e))
|
||||||
changed = changed or result["changed"]
|
changed = changed or result["changed"]
|
||||||
else:
|
else:
|
||||||
name = existing.metadata.name
|
name = existing.metadata.name
|
||||||
namespace = existing.metadata.namespace
|
namespace = existing.metadata.namespace
|
||||||
existing = resource.get(name=name, namespace=namespace)
|
existing = client.get(resource, name=name, namespace=namespace)
|
||||||
result = {"changed": False, "result": existing.to_dict()}
|
result = {"changed": False, "result": existing.to_dict()}
|
||||||
if module._diff:
|
if module._diff:
|
||||||
result["diff"] = {}
|
result["diff"] = {}
|
||||||
@@ -320,22 +337,16 @@ def argspec():
|
|||||||
|
|
||||||
|
|
||||||
def scale(
|
def scale(
|
||||||
module,
|
client, svc, resource, existing_object, replicas, wait, wait_time, wait_sleep,
|
||||||
k8s_ansible_mixin,
|
|
||||||
resource,
|
|
||||||
existing_object,
|
|
||||||
replicas,
|
|
||||||
wait,
|
|
||||||
wait_time,
|
|
||||||
wait_sleep,
|
|
||||||
):
|
):
|
||||||
|
module = svc.module
|
||||||
name = existing_object.metadata.name
|
name = existing_object.metadata.name
|
||||||
namespace = existing_object.metadata.namespace
|
namespace = existing_object.metadata.namespace
|
||||||
kind = existing_object.kind
|
kind = existing_object.kind
|
||||||
|
|
||||||
if not hasattr(resource, "scale"):
|
if not hasattr(resource, "scale"):
|
||||||
module.fail_json(
|
raise CoreException(
|
||||||
msg="Cannot perform scale on resource of kind {0}".format(resource.kind)
|
"Cannot perform scale on resource of kind {0}".format(resource.kind)
|
||||||
)
|
)
|
||||||
|
|
||||||
scale_obj = {
|
scale_obj = {
|
||||||
@@ -344,32 +355,35 @@ def scale(
|
|||||||
"spec": {"replicas": replicas},
|
"spec": {"replicas": replicas},
|
||||||
}
|
}
|
||||||
|
|
||||||
existing = resource.get(name=name, namespace=namespace)
|
existing = client.get(resource, name=name, namespace=namespace)
|
||||||
|
|
||||||
result = dict()
|
result = dict()
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
k8s_obj = copy.deepcopy(existing.to_dict())
|
k8s_obj = copy.deepcopy(existing.to_dict())
|
||||||
k8s_obj["spec"]["replicas"] = replicas
|
k8s_obj["spec"]["replicas"] = replicas
|
||||||
match, diffs = k8s_ansible_mixin.diff_objects(existing.to_dict(), k8s_obj)
|
match, diffs = svc.diff_objects(existing.to_dict(), k8s_obj)
|
||||||
if wait:
|
if wait:
|
||||||
result["duration"] = 0
|
result["duration"] = 0
|
||||||
result["result"] = k8s_obj
|
result["result"] = k8s_obj
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
resource.scale.patch(body=scale_obj)
|
resource.scale.patch(body=scale_obj)
|
||||||
except Exception as exc:
|
except Exception as e:
|
||||||
module.fail_json(msg="Scale request failed: {0}".format(exc))
|
reason = e.body if hasattr(e, "body") else e
|
||||||
|
msg = "Scale request failed: {0}".format(reason)
|
||||||
|
raise CoreException(msg) from e
|
||||||
|
|
||||||
k8s_obj = resource.get(name=name, namespace=namespace).to_dict()
|
k8s_obj = client.get(resource, name=name, namespace=namespace).to_dict()
|
||||||
result["result"] = k8s_obj
|
result["result"] = k8s_obj
|
||||||
if wait and not module.check_mode:
|
if wait:
|
||||||
success, result["result"], result["duration"] = k8s_ansible_mixin.wait(
|
waiter = get_waiter(svc.client, resource)
|
||||||
resource, scale_obj, wait_sleep, wait_time
|
success, result["result"], result["duration"] = waiter.wait(
|
||||||
|
timeout=wait_time, sleep=wait_sleep, name=name, namespace=namespace,
|
||||||
)
|
)
|
||||||
if not success:
|
if not success:
|
||||||
module.fail_json(msg="Resource scaling timed out", **result)
|
raise ResourceTimeout("Resource scaling timed out", **result)
|
||||||
|
|
||||||
match, diffs = k8s_ansible_mixin.diff_objects(existing.to_dict(), k8s_obj)
|
match, diffs = svc.diff_objects(existing.to_dict(), k8s_obj)
|
||||||
result["changed"] = not match
|
result["changed"] = not match
|
||||||
if module._diff:
|
if module._diff:
|
||||||
result["diff"] = diffs
|
result["diff"] = diffs
|
||||||
@@ -381,19 +395,15 @@ def main():
|
|||||||
mutually_exclusive = [
|
mutually_exclusive = [
|
||||||
("resource_definition", "src"),
|
("resource_definition", "src"),
|
||||||
]
|
]
|
||||||
module = AnsibleModule(
|
module = AnsibleK8SModule(
|
||||||
|
module_class=AnsibleModule,
|
||||||
argument_spec=argspec(),
|
argument_spec=argspec(),
|
||||||
mutually_exclusive=mutually_exclusive,
|
mutually_exclusive=mutually_exclusive,
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
|
||||||
K8sAnsibleMixin,
|
|
||||||
get_api_client,
|
|
||||||
)
|
|
||||||
|
|
||||||
k8s_ansible_mixin = K8sAnsibleMixin(module)
|
client = get_api_client(module=module)
|
||||||
k8s_ansible_mixin.client = get_api_client(module=module)
|
execute_module(client, module)
|
||||||
execute_module(module, k8s_ansible_mixin)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user