Ensure CoreExceptions are handled gracefully (#476)

Ensure CoreExceptions are handled gracefully

SUMMARY

CoreExceptions, when raised, should have a reasonably helpful and
actionable message associated with them. This adds a final check in
module execution to gracefully fail from these exceptions. A new
fail_from_exception method is added both to simplify exiting the module,
and to ensure that any chained exceptions are available when using -vvv.

ISSUE TYPE

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Joseph Torcasso <None>
This commit is contained in:
Mike Graves
2022-06-15 09:26:24 -04:00
committed by GitHub
parent 92785f58da
commit beb53652db
14 changed files with 104 additions and 26 deletions

View File

@@ -394,6 +394,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.args_common import
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.runner import (
run_module,
)
@@ -458,7 +461,10 @@ def main():
mutually_exclusive=mutually_exclusive,
supports_check_mode=True,
)
run_module(module)
try:
run_module(module)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -157,6 +157,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule impo
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import (
AUTH_ARG_SPEC,
)
@@ -219,7 +222,10 @@ def main():
get_api_client,
)
execute_module(module, client=get_api_client(module=module))
try:
execute_module(module, client=get_api_client(module=module))
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -148,6 +148,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
@@ -212,7 +215,10 @@ def main():
supports_check_mode=True,
)
execute_module(module)
try:
execute_module(module)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -142,6 +142,10 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible.module_utils._text import to_native
try:
@@ -497,9 +501,12 @@ def main():
error=to_native(k8s_import_exception),
)
client = get_api_client(module=module)
k8s_drain = K8sDrainAnsible(module, client.client)
k8s_drain.execute_module()
try:
client = get_api_client(module=module)
k8s_drain = K8sDrainAnsible(module, client.client)
k8s_drain.execute_module()
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -144,6 +144,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
try:
from kubernetes.client.apis import core_v1_api
@@ -240,8 +243,11 @@ def main():
supports_check_mode=True,
)
client = get_api_client(module)
execute_module(module, client.client)
try:
client = get_api_client(module)
execute_module(module, client.client)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -161,6 +161,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
@@ -202,9 +205,12 @@ def main():
module = AnsibleK8SModule(
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True
)
client = get_api_client(module)
svc = K8sService(client, module)
execute_module(module, svc)
try:
client = get_api_client(module)
svc = K8sService(client, module)
execute_module(module, svc)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -142,6 +142,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
AnsibleK8SModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
diff_objects,
)
@@ -283,8 +286,11 @@ def main():
module = AnsibleK8SModule(
module_class=AnsibleModule, argument_spec=args, supports_check_mode=True
)
client = get_api_client(module)
execute_module(module, client)
try:
client = get_api_client(module)
execute_module(module, client)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -272,7 +272,7 @@ def main():
result = execute_module(svc, module.params)
module.exit_json(**result)
except CoreException as e:
module.fail_json(msg=e)
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -262,9 +262,12 @@ def main():
module_class=AnsibleModule, argument_spec=argspec(), supports_check_mode=True
)
client = get_api_client(module=module)
svc = K8sService(client, module)
execute_module(svc)
try:
client = get_api_client(module=module)
svc = K8sService(client, module)
execute_module(svc)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -411,8 +411,11 @@ def main():
supports_check_mode=True,
)
client = get_api_client(module=module)
execute_module(client, module)
try:
client = get_api_client(module=module)
execute_module(client, module)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":

View File

@@ -160,6 +160,9 @@ from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import (
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import (
get_api_client,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import (
CoreException,
)
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.service import (
K8sService,
)
@@ -274,9 +277,12 @@ def main():
supports_check_mode=True,
)
client = get_api_client(module=module)
svc = K8sService(client, module)
execute_module(svc)
try:
client = get_api_client(module=module)
svc = K8sService(client, module)
execute_module(svc)
except CoreException as e:
module.fail_from_exception(e)
if __name__ == "__main__":