Move diff and wait to perform_action (#375)

This primarily moves the diff and wait logic from the various service
methods to perform_action to eliminate code duplication. I also moved
the diff_objects function out of the service object and moved most of
the find_resource logic to a new resource client method. We ended up
with several modules creating a service object just to use one of these
methods, so it seemed to make sense to make these more accessible.
This commit is contained in:
Mike Graves
2022-02-14 08:19:36 -05:00
parent 3bf147580f
commit 25644ac192
11 changed files with 356 additions and 448 deletions

View File

@@ -143,7 +143,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
diff_objects,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import (
get_waiter,
@@ -194,7 +194,7 @@ def json_patch(existing, patch):
return None, error
def execute_module(module, svc):
def execute_module(module, client):
kind = module.params.get("kind")
api_version = module.params.get("api_version")
name = module.params.get("name")
@@ -213,8 +213,7 @@ def execute_module(module, svc):
def build_error_msg(kind, name, msg):
return "%s %s: %s" % (kind, name, msg)
client = svc.client
resource = svc.find_resource(kind, api_version, fail=True)
resource = client.resource(kind, api_version)
try:
existing = client.get(resource, name=name, namespace=namespace)
@@ -265,7 +264,7 @@ def execute_module(module, svc):
success, result["result"], result["duration"] = waiter.wait(
wait_timeout, wait_sleep, name, namespace
)
match, diffs = svc.diff_objects(existing.to_dict(), obj)
match, diffs = diff_objects(existing.to_dict(), obj)
result["changed"] = not match
if module._diff:
result["diff"] = diffs
@@ -285,8 +284,7 @@ def main():
module_class=AnsibleModule, argument_spec=args, supports_check_mode=True
)
client = get_api_client(module)
svc = K8sService(client, module)
execute_module(module, svc)
execute_module(module, client)
if __name__ == "__main__":

View File

@@ -170,7 +170,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions imp
ResourceTimeout,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
diff_objects,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.waiter import (
get_waiter,
@@ -214,8 +214,7 @@ def execute_module(client, module):
if wait:
return_attributes["duration"] = 0
svc = K8sService(client, module)
resource = svc.find_resource(kind, api_version, fail=True)
resource = client.resource(kind, api_version)
multiple_scale = False
try:
existing = resource.get(
@@ -297,7 +296,7 @@ def execute_module(client, module):
try:
result = scale(
client,
svc,
module,
resource,
existing,
replicas,
@@ -337,9 +336,8 @@ def argspec():
def scale(
client, svc, resource, existing_object, replicas, wait, wait_time, wait_sleep,
client, module, resource, existing_object, replicas, wait, wait_time, wait_sleep,
):
module = svc.module
name = existing_object.metadata.name
namespace = existing_object.metadata.namespace
kind = existing_object.kind
@@ -361,7 +359,6 @@ def scale(
if module.check_mode:
k8s_obj = copy.deepcopy(existing.to_dict())
k8s_obj["spec"]["replicas"] = replicas
match, diffs = svc.diff_objects(existing.to_dict(), k8s_obj)
if wait:
result["duration"] = 0
result["result"] = k8s_obj
@@ -383,7 +380,7 @@ def scale(
if not success:
raise ResourceTimeout("Resource scaling timed out", **result)
match, diffs = svc.diff_objects(existing.to_dict(), k8s_obj)
match, diffs = diff_objects(existing.to_dict(), result["result"])
result["changed"] = not match
if module._diff:
result["diff"] = diffs