mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-07-30 11:24:47 +00:00
action/k8s_info: refactoring to simplify the code base (#331)
Break down the long `run()` method to simplify the readability of the plugin.
This commit is contained in:
@@ -8,6 +8,8 @@ __metaclass__ = type
|
|||||||
|
|
||||||
import copy
|
import copy
|
||||||
import traceback
|
import traceback
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
|
||||||
from ansible.config.manager import ensure_type
|
from ansible.config.manager import ensure_type
|
||||||
from ansible.errors import AnsibleError, AnsibleFileNotFound, AnsibleAction, AnsibleActionFail
|
from ansible.errors import AnsibleError, AnsibleFileNotFound, AnsibleAction, AnsibleActionFail
|
||||||
@@ -34,63 +36,35 @@ class ActionModule(ActionBase):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def run(self, tmp=None, task_vars=None):
|
@contextmanager
|
||||||
''' handler for k8s options '''
|
def get_template_data(self, template_path):
|
||||||
if task_vars is None:
|
|
||||||
task_vars = dict()
|
|
||||||
|
|
||||||
result = super(ActionModule, self).run(tmp, task_vars)
|
|
||||||
del tmp # tmp no longer has any effect
|
|
||||||
|
|
||||||
# Check current transport connection and depending upon
|
|
||||||
# look for kubeconfig and src
|
|
||||||
# 'local' => look files on Ansible Controller
|
|
||||||
# Transport other than 'local' => look files on remote node
|
|
||||||
remote_transport = self._connection.transport != 'local'
|
|
||||||
|
|
||||||
new_module_args = copy.deepcopy(self._task.args)
|
|
||||||
|
|
||||||
kubeconfig = self._task.args.get('kubeconfig', None)
|
|
||||||
# find the kubeconfig in the expected search path
|
|
||||||
if kubeconfig and not remote_transport:
|
|
||||||
# kubeconfig is local
|
|
||||||
try:
|
try:
|
||||||
# find in expected paths
|
source = self._find_needle('templates', template_path)
|
||||||
kubeconfig = self._find_needle('files', kubeconfig)
|
|
||||||
except AnsibleError as e:
|
except AnsibleError as e:
|
||||||
result['failed'] = True
|
raise AnsibleActionFail(to_text(e))
|
||||||
result['msg'] = to_text(e)
|
|
||||||
result['exception'] = traceback.format_exc()
|
|
||||||
return result
|
|
||||||
|
|
||||||
# decrypt kubeconfig found
|
# Get vault decrypted tmp file
|
||||||
actual_file = self._loader.get_real_file(kubeconfig, decrypt=True)
|
|
||||||
new_module_args['kubeconfig'] = actual_file
|
|
||||||
|
|
||||||
# find the file in the expected search path
|
|
||||||
src = self._task.args.get('src', None)
|
|
||||||
|
|
||||||
if src:
|
|
||||||
if remote_transport:
|
|
||||||
# src is on remote node
|
|
||||||
result.update(self._execute_module(module_name=self._task.action, task_vars=task_vars))
|
|
||||||
return self._ensure_invocation(result)
|
|
||||||
|
|
||||||
# src is local
|
|
||||||
try:
|
try:
|
||||||
# find in expected paths
|
tmp_source = self._loader.get_real_file(source)
|
||||||
src = self._find_needle('files', src)
|
except AnsibleFileNotFound as e:
|
||||||
except AnsibleError as e:
|
raise AnsibleActionFail("could not find template=%s, %s" % (source, to_text(e)))
|
||||||
result['failed'] = True
|
b_tmp_source = to_bytes(tmp_source, errors='surrogate_or_strict')
|
||||||
result['msg'] = to_text(e)
|
|
||||||
result['exception'] = traceback.format_exc()
|
|
||||||
return result
|
|
||||||
|
|
||||||
if src:
|
try:
|
||||||
new_module_args['src'] = src
|
with open(b_tmp_source, 'rb') as f:
|
||||||
|
try:
|
||||||
|
template_data = to_text(f.read(), errors='surrogate_or_strict')
|
||||||
|
except UnicodeError:
|
||||||
|
raise AnsibleActionFail("Template source files must be utf-8 encoded")
|
||||||
|
yield template_data
|
||||||
|
except AnsibleAction:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise AnsibleActionFail("%s: %s" % (type(e).__name__, to_text(e)))
|
||||||
|
finally:
|
||||||
|
self._loader.cleanup_tmp_file(b_tmp_source)
|
||||||
|
|
||||||
template = self._task.args.get('template', None)
|
def load_template(self, template, new_module_args, task_vars):
|
||||||
if template:
|
|
||||||
# template is only supported by k8s module.
|
# template is only supported by k8s module.
|
||||||
if self._task.action not in ('k8s', 'community.kubernetes.k8s', 'community.okd.k8s'):
|
if self._task.action not in ('k8s', 'community.kubernetes.k8s', 'community.okd.k8s'):
|
||||||
raise AnsibleActionFail("'template' is only supported parameter for 'k8s' module.")
|
raise AnsibleActionFail("'template' is only supported parameter for 'k8s' module.")
|
||||||
@@ -153,26 +127,8 @@ class ActionModule(ActionBase):
|
|||||||
elif newline_sequence not in allowed_sequences:
|
elif newline_sequence not in allowed_sequences:
|
||||||
raise AnsibleActionFail("newline_sequence needs to be one of: \n, \r or \r\n")
|
raise AnsibleActionFail("newline_sequence needs to be one of: \n, \r or \r\n")
|
||||||
|
|
||||||
try:
|
|
||||||
source = self._find_needle('templates', template_path)
|
|
||||||
except AnsibleError as e:
|
|
||||||
raise AnsibleActionFail(to_text(e))
|
|
||||||
|
|
||||||
# Get vault decrypted tmp file
|
|
||||||
try:
|
|
||||||
tmp_source = self._loader.get_real_file(source)
|
|
||||||
except AnsibleFileNotFound as e:
|
|
||||||
raise AnsibleActionFail("could not find template=%s, %s" % (source, to_text(e)))
|
|
||||||
b_tmp_source = to_bytes(tmp_source, errors='surrogate_or_strict')
|
|
||||||
|
|
||||||
# template the source data locally & get ready to transfer
|
# template the source data locally & get ready to transfer
|
||||||
try:
|
with self.get_template_data(template_path) as template_data:
|
||||||
with open(b_tmp_source, 'rb') as f:
|
|
||||||
try:
|
|
||||||
template_data = to_text(f.read(), errors='surrogate_or_strict')
|
|
||||||
except UnicodeError:
|
|
||||||
raise AnsibleActionFail("Template source files must be utf-8 encoded")
|
|
||||||
|
|
||||||
# add ansible 'template' vars
|
# add ansible 'template' vars
|
||||||
temp_vars = task_vars.copy()
|
temp_vars = task_vars.copy()
|
||||||
old_vars = self._templar.available_variables
|
old_vars = self._templar.available_variables
|
||||||
@@ -195,12 +151,65 @@ class ActionModule(ActionBase):
|
|||||||
if not resource_definition:
|
if not resource_definition:
|
||||||
new_module_args.pop('template')
|
new_module_args.pop('template')
|
||||||
new_module_args['definition'] = resultant
|
new_module_args['definition'] = resultant
|
||||||
except AnsibleAction:
|
|
||||||
raise
|
def run(self, tmp=None, task_vars=None):
|
||||||
except Exception as e:
|
''' handler for k8s options '''
|
||||||
raise AnsibleActionFail("%s: %s" % (type(e).__name__, to_text(e)))
|
if task_vars is None:
|
||||||
finally:
|
task_vars = dict()
|
||||||
self._loader.cleanup_tmp_file(b_tmp_source)
|
|
||||||
|
result = super(ActionModule, self).run(tmp, task_vars)
|
||||||
|
del tmp # tmp no longer has any effect
|
||||||
|
|
||||||
|
# Check current transport connection and depending upon
|
||||||
|
# look for kubeconfig and src
|
||||||
|
# 'local' => look files on Ansible Controller
|
||||||
|
# Transport other than 'local' => look files on remote node
|
||||||
|
remote_transport = self._connection.transport != 'local'
|
||||||
|
|
||||||
|
new_module_args = copy.deepcopy(self._task.args)
|
||||||
|
|
||||||
|
kubeconfig = self._task.args.get('kubeconfig', None)
|
||||||
|
# find the kubeconfig in the expected search path
|
||||||
|
if kubeconfig and not remote_transport:
|
||||||
|
# kubeconfig is local
|
||||||
|
try:
|
||||||
|
# find in expected paths
|
||||||
|
kubeconfig = self._find_needle('files', kubeconfig)
|
||||||
|
except AnsibleError as e:
|
||||||
|
result['failed'] = True
|
||||||
|
result['msg'] = to_text(e)
|
||||||
|
result['exception'] = traceback.format_exc()
|
||||||
|
return result
|
||||||
|
|
||||||
|
# decrypt kubeconfig found
|
||||||
|
actual_file = self._loader.get_real_file(kubeconfig, decrypt=True)
|
||||||
|
new_module_args['kubeconfig'] = actual_file
|
||||||
|
|
||||||
|
# find the file in the expected search path
|
||||||
|
src = self._task.args.get('src', None)
|
||||||
|
|
||||||
|
if src:
|
||||||
|
if remote_transport:
|
||||||
|
# src is on remote node
|
||||||
|
result.update(self._execute_module(module_name=self._task.action, task_vars=task_vars))
|
||||||
|
return self._ensure_invocation(result)
|
||||||
|
|
||||||
|
# src is local
|
||||||
|
try:
|
||||||
|
# find in expected paths
|
||||||
|
src = self._find_needle('files', src)
|
||||||
|
except AnsibleError as e:
|
||||||
|
result['failed'] = True
|
||||||
|
result['msg'] = to_text(e)
|
||||||
|
result['exception'] = traceback.format_exc()
|
||||||
|
return result
|
||||||
|
|
||||||
|
if src:
|
||||||
|
new_module_args['src'] = src
|
||||||
|
|
||||||
|
template = self._task.args.get('template', None)
|
||||||
|
if template:
|
||||||
|
self.load_template(template, new_module_args, task_vars)
|
||||||
|
|
||||||
# Execute the k8s_* module.
|
# Execute the k8s_* module.
|
||||||
module_return = self._execute_module(module_name=self._task.action, module_args=new_module_args, task_vars=task_vars)
|
module_return = self._execute_module(module_name=self._task.action, module_args=new_module_args, task_vars=task_vars)
|
||||||
|
|||||||
Reference in New Issue
Block a user