mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-06 13:02:37 +00:00
refactoring for ansible_module.turbo integration (#313)
* refactoring for ansible_module.turbo integration This refactoring prepares the integration of `ansible_module.turbo` - Delay the loading of `common.py`, move the shared structure in `args_common`. - Avoid the use of one single object per module, this to increase the amount of Python structure that we can cache. - Cache the Kubernetes client. See: https://github.com/ansible-collections/community.kubernetes/pull/270 Co-authored-by: Jill Rouleau <jill.rouleau@bespokess.com>
This commit is contained in:
6
plugins/module_utils/ansiblemodule.py
Normal file
6
plugins/module_utils/ansiblemodule.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule # noqa: F401
|
||||
133
plugins/module_utils/args_common.py
Normal file
133
plugins/module_utils/args_common.py
Normal file
@@ -0,0 +1,133 @@
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
|
||||
from ansible.module_utils.six import string_types
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
def list_dict_str(value):
|
||||
if isinstance(value, (list, dict, string_types)):
|
||||
return value
|
||||
raise TypeError
|
||||
|
||||
|
||||
AUTH_ARG_SPEC = {
|
||||
'kubeconfig': {
|
||||
'type': 'path',
|
||||
},
|
||||
'context': {},
|
||||
'host': {},
|
||||
'api_key': {
|
||||
'no_log': True,
|
||||
},
|
||||
'username': {},
|
||||
'password': {
|
||||
'no_log': True,
|
||||
},
|
||||
'validate_certs': {
|
||||
'type': 'bool',
|
||||
'aliases': ['verify_ssl'],
|
||||
},
|
||||
'ca_cert': {
|
||||
'type': 'path',
|
||||
'aliases': ['ssl_ca_cert'],
|
||||
},
|
||||
'client_cert': {
|
||||
'type': 'path',
|
||||
'aliases': ['cert_file'],
|
||||
},
|
||||
'client_key': {
|
||||
'type': 'path',
|
||||
'aliases': ['key_file'],
|
||||
},
|
||||
'proxy': {
|
||||
'type': 'str',
|
||||
},
|
||||
'persist_config': {
|
||||
'type': 'bool',
|
||||
},
|
||||
}
|
||||
|
||||
WAIT_ARG_SPEC = dict(
|
||||
wait=dict(type='bool', default=False),
|
||||
wait_sleep=dict(type='int', default=5),
|
||||
wait_timeout=dict(type='int', default=120),
|
||||
wait_condition=dict(
|
||||
type='dict',
|
||||
default=None,
|
||||
options=dict(
|
||||
type=dict(),
|
||||
status=dict(default=True, choices=[True, False, "Unknown"]),
|
||||
reason=dict()
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
# Map kubernetes-client parameters to ansible parameters
|
||||
AUTH_ARG_MAP = {
|
||||
'kubeconfig': 'kubeconfig',
|
||||
'context': 'context',
|
||||
'host': 'host',
|
||||
'api_key': 'api_key',
|
||||
'username': 'username',
|
||||
'password': 'password',
|
||||
'verify_ssl': 'validate_certs',
|
||||
'ssl_ca_cert': 'ca_cert',
|
||||
'cert_file': 'client_cert',
|
||||
'key_file': 'client_key',
|
||||
'proxy': 'proxy',
|
||||
'persist_config': 'persist_config',
|
||||
}
|
||||
|
||||
NAME_ARG_SPEC = {
|
||||
'kind': {},
|
||||
'name': {},
|
||||
'namespace': {},
|
||||
'api_version': {
|
||||
'default': 'v1',
|
||||
'aliases': ['api', 'version'],
|
||||
},
|
||||
}
|
||||
|
||||
COMMON_ARG_SPEC = {
|
||||
'state': {
|
||||
'default': 'present',
|
||||
'choices': ['present', 'absent'],
|
||||
},
|
||||
'force': {
|
||||
'type': 'bool',
|
||||
'default': False,
|
||||
},
|
||||
}
|
||||
|
||||
RESOURCE_ARG_SPEC = {
|
||||
'resource_definition': {
|
||||
'type': list_dict_str,
|
||||
'aliases': ['definition', 'inline']
|
||||
},
|
||||
'src': {
|
||||
'type': 'path',
|
||||
},
|
||||
}
|
||||
|
||||
ARG_ATTRIBUTES_BLACKLIST = ('property_path',)
|
||||
|
||||
DELETE_OPTS_ARG_SPEC = {
|
||||
'propagationPolicy': {
|
||||
'choices': ['Foreground', 'Background', 'Orphan'],
|
||||
},
|
||||
'gracePeriodSeconds': {
|
||||
'type': 'int',
|
||||
},
|
||||
'preconditions': {
|
||||
'type': 'dict',
|
||||
'options': {
|
||||
'resourceVersion': {
|
||||
'type': 'str',
|
||||
},
|
||||
'uid': {
|
||||
'type': 'str',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import sys
|
||||
from datetime import datetime
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
from ansible_collections.community.kubernetes.plugins.module_utils.args_common import (AUTH_ARG_MAP, AUTH_ARG_SPEC)
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
||||
from ansible.module_utils.six import iteritems, string_types
|
||||
@@ -99,201 +100,107 @@ except ImportError as e:
|
||||
K8S_IMP_ERR = traceback.format_exc()
|
||||
|
||||
|
||||
def list_dict_str(value):
|
||||
if isinstance(value, (list, dict, string_types)):
|
||||
return value
|
||||
raise TypeError
|
||||
def configuration_digest(configuration):
|
||||
import hashlib
|
||||
m = hashlib.sha256()
|
||||
for k in AUTH_ARG_MAP:
|
||||
if not hasattr(configuration, k):
|
||||
v = None
|
||||
else:
|
||||
v = getattr(configuration, k)
|
||||
if v and k in ["ssl_ca_cert", "cert_file", "key_file"]:
|
||||
with open(str(v), "r") as fd:
|
||||
content = fd.read()
|
||||
m.update(content.encode())
|
||||
else:
|
||||
m.update(str(v).encode())
|
||||
digest = m.hexdigest()
|
||||
return digest
|
||||
|
||||
|
||||
ARG_ATTRIBUTES_BLACKLIST = ('property_path',)
|
||||
def get_api_client(module=None):
|
||||
auth = {}
|
||||
|
||||
COMMON_ARG_SPEC = {
|
||||
'state': {
|
||||
'default': 'present',
|
||||
'choices': ['present', 'absent'],
|
||||
},
|
||||
'force': {
|
||||
'type': 'bool',
|
||||
'default': False,
|
||||
},
|
||||
}
|
||||
def _raise_or_fail(exc, msg):
|
||||
if module:
|
||||
module.fail_json(msg % to_native(exc))
|
||||
else:
|
||||
raise exc
|
||||
|
||||
RESOURCE_ARG_SPEC = {
|
||||
'resource_definition': {
|
||||
'type': list_dict_str,
|
||||
'aliases': ['definition', 'inline']
|
||||
},
|
||||
'src': {
|
||||
'type': 'path',
|
||||
},
|
||||
}
|
||||
# If authorization variables aren't defined, look for them in environment variables
|
||||
for true_name, arg_name in AUTH_ARG_MAP.items():
|
||||
if module and module.params.get(arg_name):
|
||||
auth[true_name] = module.params.get(arg_name)
|
||||
else:
|
||||
env_value = os.getenv('K8S_AUTH_{0}'.format(arg_name.upper()), None) or os.getenv('K8S_AUTH_{0}'.format(true_name.upper()), None)
|
||||
if env_value is not None:
|
||||
if AUTH_ARG_SPEC[arg_name].get('type') == 'bool':
|
||||
env_value = env_value.lower() not in ['0', 'false', 'no']
|
||||
auth[true_name] = env_value
|
||||
|
||||
NAME_ARG_SPEC = {
|
||||
'kind': {},
|
||||
'name': {},
|
||||
'namespace': {},
|
||||
'api_version': {
|
||||
'default': 'v1',
|
||||
'aliases': ['api', 'version'],
|
||||
},
|
||||
}
|
||||
def auth_set(*names):
|
||||
return all([auth.get(name) for name in names])
|
||||
|
||||
AUTH_ARG_SPEC = {
|
||||
'kubeconfig': {
|
||||
'type': 'path',
|
||||
},
|
||||
'context': {},
|
||||
'host': {},
|
||||
'api_key': {
|
||||
'no_log': True,
|
||||
},
|
||||
'username': {},
|
||||
'password': {
|
||||
'no_log': True,
|
||||
},
|
||||
'validate_certs': {
|
||||
'type': 'bool',
|
||||
'aliases': ['verify_ssl'],
|
||||
},
|
||||
'ca_cert': {
|
||||
'type': 'path',
|
||||
'aliases': ['ssl_ca_cert'],
|
||||
},
|
||||
'client_cert': {
|
||||
'type': 'path',
|
||||
'aliases': ['cert_file'],
|
||||
},
|
||||
'client_key': {
|
||||
'type': 'path',
|
||||
'aliases': ['key_file'],
|
||||
},
|
||||
'proxy': {
|
||||
'type': 'str',
|
||||
},
|
||||
'persist_config': {
|
||||
'type': 'bool',
|
||||
},
|
||||
}
|
||||
if auth_set('username', 'password', 'host') or auth_set('api_key', 'host'):
|
||||
# We have enough in the parameters to authenticate, no need to load incluster or kubeconfig
|
||||
pass
|
||||
elif auth_set('kubeconfig') or auth_set('context'):
|
||||
try:
|
||||
kubernetes.config.load_kube_config(auth.get('kubeconfig'), auth.get('context'), persist_config=auth.get('persist_config'))
|
||||
except Exception as err:
|
||||
_raise_or_fail(err, 'Failed to load kubeconfig due to %s')
|
||||
|
||||
WAIT_ARG_SPEC = dict(
|
||||
wait=dict(type='bool', default=False),
|
||||
wait_sleep=dict(type='int', default=5),
|
||||
wait_timeout=dict(type='int', default=120),
|
||||
wait_condition=dict(
|
||||
type='dict',
|
||||
default=None,
|
||||
options=dict(
|
||||
type=dict(),
|
||||
status=dict(type='str', default="True", choices=["True", "False", "Unknown"]),
|
||||
reason=dict()
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
# First try to do incluster config, then kubeconfig
|
||||
try:
|
||||
kubernetes.config.load_incluster_config()
|
||||
except kubernetes.config.ConfigException:
|
||||
try:
|
||||
kubernetes.config.load_kube_config(auth.get('kubeconfig'), auth.get('context'), persist_config=auth.get('persist_config'))
|
||||
except Exception as err:
|
||||
_raise_or_fail(err, 'Failed to load kubeconfig due to %s')
|
||||
|
||||
DELETE_OPTS_ARG_SPEC = {
|
||||
'propagationPolicy': {
|
||||
'choices': ['Foreground', 'Background', 'Orphan'],
|
||||
},
|
||||
'gracePeriodSeconds': {
|
||||
'type': 'int',
|
||||
},
|
||||
'preconditions': {
|
||||
'type': 'dict',
|
||||
'options': {
|
||||
'resourceVersion': {
|
||||
'type': 'str',
|
||||
},
|
||||
'uid': {
|
||||
'type': 'str',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
# Override any values in the default configuration with Ansible parameters
|
||||
# As of kubernetes-client v12.0.0, get_default_copy() is required here
|
||||
try:
|
||||
configuration = kubernetes.client.Configuration().get_default_copy()
|
||||
except AttributeError:
|
||||
configuration = kubernetes.client.Configuration()
|
||||
|
||||
for key, value in iteritems(auth):
|
||||
if key in AUTH_ARG_MAP.keys() and value is not None:
|
||||
if key == 'api_key':
|
||||
setattr(configuration, key, {'authorization': "Bearer {0}".format(value)})
|
||||
else:
|
||||
setattr(configuration, key, value)
|
||||
|
||||
digest = configuration_digest(configuration)
|
||||
if digest in get_api_client._pool:
|
||||
client = get_api_client._pool[digest]
|
||||
return client
|
||||
|
||||
try:
|
||||
client = DynamicClient(kubernetes.client.ApiClient(configuration))
|
||||
except Exception as err:
|
||||
_raise_or_fail(err, 'Failed to get client due to %s')
|
||||
|
||||
get_api_client._pool[digest] = client
|
||||
return client
|
||||
|
||||
|
||||
# Map kubernetes-client parameters to ansible parameters
|
||||
AUTH_ARG_MAP = {
|
||||
'kubeconfig': 'kubeconfig',
|
||||
'context': 'context',
|
||||
'host': 'host',
|
||||
'api_key': 'api_key',
|
||||
'username': 'username',
|
||||
'password': 'password',
|
||||
'verify_ssl': 'validate_certs',
|
||||
'ssl_ca_cert': 'ca_cert',
|
||||
'cert_file': 'client_cert',
|
||||
'key_file': 'client_key',
|
||||
'proxy': 'proxy',
|
||||
'persist_config': 'persist_config',
|
||||
}
|
||||
get_api_client._pool = {}
|
||||
|
||||
|
||||
class K8sAnsibleMixin(object):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
def __init__(self, module, *args, **kwargs):
|
||||
if not HAS_K8S_MODULE_HELPER:
|
||||
self.fail_json(msg=missing_required_lib('openshift'), exception=K8S_IMP_ERR,
|
||||
error=to_native(k8s_import_exception))
|
||||
module.fail_json(msg=missing_required_lib('openshift'), exception=K8S_IMP_ERR,
|
||||
error=to_native(k8s_import_exception))
|
||||
self.openshift_version = openshift.__version__
|
||||
|
||||
if not HAS_YAML:
|
||||
self.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR)
|
||||
|
||||
def get_api_client(self, **auth_params):
|
||||
auth_params = auth_params or getattr(self, 'params', {})
|
||||
auth = {}
|
||||
|
||||
# If authorization variables aren't defined, look for them in environment variables
|
||||
for true_name, arg_name in AUTH_ARG_MAP.items():
|
||||
if auth_params.get(arg_name) is None:
|
||||
env_value = os.getenv('K8S_AUTH_{0}'.format(arg_name.upper()), None) or os.getenv('K8S_AUTH_{0}'.format(true_name.upper()), None)
|
||||
if env_value is not None:
|
||||
if AUTH_ARG_SPEC[arg_name].get('type') == 'bool':
|
||||
env_value = env_value.lower() not in ['0', 'false', 'no']
|
||||
auth[true_name] = env_value
|
||||
else:
|
||||
auth[true_name] = auth_params[arg_name]
|
||||
|
||||
def auth_set(*names):
|
||||
return all([auth.get(name) for name in names])
|
||||
|
||||
if auth_set('username', 'password', 'host') or auth_set('api_key', 'host'):
|
||||
# We have enough in the parameters to authenticate, no need to load incluster or kubeconfig
|
||||
pass
|
||||
elif auth_set('kubeconfig') or auth_set('context'):
|
||||
try:
|
||||
kubernetes.config.load_kube_config(auth.get('kubeconfig'), auth.get('context'), persist_config=auth.get('persist_config'))
|
||||
except Exception as err:
|
||||
self.fail(msg='Failed to load kubeconfig due to %s' % to_native(err))
|
||||
else:
|
||||
# First try to do incluster config, then kubeconfig
|
||||
try:
|
||||
kubernetes.config.load_incluster_config()
|
||||
except kubernetes.config.ConfigException:
|
||||
try:
|
||||
kubernetes.config.load_kube_config(auth.get('kubeconfig'), auth.get('context'), persist_config=auth.get('persist_config'))
|
||||
except Exception as err:
|
||||
self.fail(msg='Failed to load kubeconfig due to %s' % to_native(err))
|
||||
|
||||
# Override any values in the default configuration with Ansible parameters
|
||||
# As of kubernetes-client v12.0.0, get_default_copy() is required here
|
||||
try:
|
||||
configuration = kubernetes.client.Configuration().get_default_copy()
|
||||
except AttributeError:
|
||||
configuration = kubernetes.client.Configuration()
|
||||
|
||||
for key, value in iteritems(auth):
|
||||
if key in AUTH_ARG_MAP.keys() and value is not None:
|
||||
if key == 'api_key':
|
||||
setattr(configuration, key, {'authorization': "Bearer {0}".format(value)})
|
||||
else:
|
||||
setattr(configuration, key, value)
|
||||
|
||||
kubernetes.client.Configuration.set_default(configuration)
|
||||
try:
|
||||
return DynamicClient(kubernetes.client.ApiClient(configuration))
|
||||
except Exception as err:
|
||||
self.fail(msg='Failed to get client due to %s' % to_native(err))
|
||||
module.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR)
|
||||
|
||||
def find_resource(self, kind, api_version, fail=False):
|
||||
for attribute in ['kind', 'name', 'singular_name']:
|
||||
@@ -513,8 +420,8 @@ class K8sAnsibleMixin(object):
|
||||
predicate = _resource_absent
|
||||
return self._wait_for(resource, definition['metadata']['name'], definition['metadata'].get('namespace'), predicate, sleep, timeout, state)
|
||||
|
||||
def set_resource_definitions(self):
|
||||
resource_definition = self.params.get('resource_definition')
|
||||
def set_resource_definitions(self, module):
|
||||
resource_definition = module.params.get('resource_definition')
|
||||
|
||||
self.resource_definitions = []
|
||||
|
||||
@@ -529,7 +436,7 @@ class K8sAnsibleMixin(object):
|
||||
else:
|
||||
self.resource_definitions = [resource_definition]
|
||||
|
||||
src = self.params.get('src')
|
||||
src = module.params.get('src')
|
||||
if src:
|
||||
self.resource_definitions = self.load_resource_definitions(src)
|
||||
try:
|
||||
@@ -539,12 +446,12 @@ class K8sAnsibleMixin(object):
|
||||
|
||||
if not resource_definition and not src:
|
||||
implicit_definition = dict(
|
||||
kind=self.kind,
|
||||
apiVersion=self.api_version,
|
||||
metadata=dict(name=self.name)
|
||||
kind=module.params['kind'],
|
||||
apiVersion=module.params['api_version'],
|
||||
metadata=dict(name=module.params['name'])
|
||||
)
|
||||
if self.namespace:
|
||||
implicit_definition['metadata']['namespace'] = self.namespace
|
||||
if module.params.get('namespace'):
|
||||
implicit_definition['metadata']['namespace'] = module.params.get('namespace')
|
||||
self.resource_definitions = [implicit_definition]
|
||||
|
||||
def check_library_version(self):
|
||||
@@ -577,7 +484,7 @@ class K8sAnsibleMixin(object):
|
||||
changed = False
|
||||
results = []
|
||||
try:
|
||||
self.client = self.get_api_client()
|
||||
self.client = get_api_client()
|
||||
# Hopefully the kubernetes client will provide its own exception class one day
|
||||
except (urllib3.exceptions.RequestError) as e:
|
||||
self.fail_json(msg="Couldn't connect to Kubernetes: %s" % str(e))
|
||||
|
||||
@@ -1,166 +0,0 @@
|
||||
#
|
||||
# Copyright 2018 Red Hat | Ansible
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import copy
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible_collections.community.kubernetes.plugins.module_utils.common import (
|
||||
AUTH_ARG_SPEC, RESOURCE_ARG_SPEC, NAME_ARG_SPEC, K8sAnsibleMixin)
|
||||
|
||||
try:
|
||||
from openshift.dynamic.exceptions import NotFoundError
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
SCALE_ARG_SPEC = {
|
||||
'replicas': {'type': 'int', 'required': True},
|
||||
'current_replicas': {'type': 'int'},
|
||||
'resource_version': {},
|
||||
'wait': {'type': 'bool', 'default': True},
|
||||
'wait_timeout': {'type': 'int', 'default': 20},
|
||||
}
|
||||
|
||||
|
||||
class KubernetesAnsibleScaleModule(K8sAnsibleMixin):
|
||||
|
||||
def __init__(self, k8s_kind=None, *args, **kwargs):
|
||||
self.client = None
|
||||
self.warnings = []
|
||||
|
||||
mutually_exclusive = [
|
||||
('resource_definition', 'src'),
|
||||
]
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec=self.argspec,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
self.module = module
|
||||
self.params = self.module.params
|
||||
self.check_mode = self.module.check_mode
|
||||
self.fail_json = self.module.fail_json
|
||||
self.fail = self.module.fail_json
|
||||
self.exit_json = self.module.exit_json
|
||||
super(KubernetesAnsibleScaleModule, self).__init__()
|
||||
|
||||
self.kind = k8s_kind or self.params.get('kind')
|
||||
self.api_version = self.params.get('api_version')
|
||||
self.name = self.params.get('name')
|
||||
self.namespace = self.params.get('namespace')
|
||||
self.set_resource_definitions()
|
||||
|
||||
def execute_module(self):
|
||||
definition = self.resource_definitions[0]
|
||||
|
||||
self.client = self.get_api_client()
|
||||
|
||||
name = definition['metadata']['name']
|
||||
namespace = definition['metadata'].get('namespace')
|
||||
api_version = definition['apiVersion']
|
||||
kind = definition['kind']
|
||||
current_replicas = self.params.get('current_replicas')
|
||||
replicas = self.params.get('replicas')
|
||||
resource_version = self.params.get('resource_version')
|
||||
|
||||
wait = self.params.get('wait')
|
||||
wait_time = self.params.get('wait_timeout')
|
||||
existing = None
|
||||
existing_count = None
|
||||
return_attributes = dict(changed=False, result=dict(), diff=dict())
|
||||
if wait:
|
||||
return_attributes['duration'] = 0
|
||||
|
||||
resource = self.find_resource(kind, api_version, fail=True)
|
||||
|
||||
try:
|
||||
existing = resource.get(name=name, namespace=namespace)
|
||||
return_attributes['result'] = existing.to_dict()
|
||||
except NotFoundError as exc:
|
||||
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc),
|
||||
error=exc.value.get('status'))
|
||||
|
||||
if self.kind == 'job':
|
||||
existing_count = existing.spec.parallelism
|
||||
elif hasattr(existing.spec, 'replicas'):
|
||||
existing_count = existing.spec.replicas
|
||||
|
||||
if existing_count is None:
|
||||
self.fail_json(msg='Failed to retrieve the available count for the requested object.')
|
||||
|
||||
if resource_version and resource_version != existing.metadata.resourceVersion:
|
||||
self.exit_json(**return_attributes)
|
||||
|
||||
if current_replicas is not None and existing_count != current_replicas:
|
||||
self.exit_json(**return_attributes)
|
||||
|
||||
if existing_count != replicas:
|
||||
return_attributes['changed'] = True
|
||||
if not self.check_mode:
|
||||
if self.kind == 'job':
|
||||
existing.spec.parallelism = replicas
|
||||
return_attributes['result'] = resource.patch(existing.to_dict()).to_dict()
|
||||
else:
|
||||
return_attributes = self.scale(resource, existing, replicas, wait, wait_time)
|
||||
|
||||
self.exit_json(**return_attributes)
|
||||
|
||||
@property
|
||||
def argspec(self):
|
||||
args = copy.deepcopy(SCALE_ARG_SPEC)
|
||||
args.update(RESOURCE_ARG_SPEC)
|
||||
args.update(NAME_ARG_SPEC)
|
||||
args.update(AUTH_ARG_SPEC)
|
||||
return args
|
||||
|
||||
def scale(self, resource, existing_object, replicas, wait, wait_time):
|
||||
name = existing_object.metadata.name
|
||||
namespace = existing_object.metadata.namespace
|
||||
kind = existing_object.kind
|
||||
|
||||
if not hasattr(resource, 'scale'):
|
||||
self.fail_json(
|
||||
msg="Cannot perform scale on resource of kind {0}".format(resource.kind)
|
||||
)
|
||||
|
||||
scale_obj = {'kind': kind, 'metadata': {'name': name, 'namespace': namespace}, 'spec': {'replicas': replicas}}
|
||||
|
||||
existing = resource.get(name=name, namespace=namespace)
|
||||
|
||||
try:
|
||||
resource.scale.patch(body=scale_obj)
|
||||
except Exception as exc:
|
||||
self.fail_json(msg="Scale request failed: {0}".format(exc))
|
||||
|
||||
k8s_obj = resource.get(name=name, namespace=namespace).to_dict()
|
||||
match, diffs = self.diff_objects(existing.to_dict(), k8s_obj)
|
||||
result = dict()
|
||||
result['result'] = k8s_obj
|
||||
result['changed'] = not match
|
||||
result['diff'] = diffs
|
||||
|
||||
if wait:
|
||||
success, result['result'], result['duration'] = self.wait(resource, scale_obj, 5, wait_time)
|
||||
if not success:
|
||||
self.fail_json(msg="Resource scaling timed out", **result)
|
||||
return result
|
||||
Reference in New Issue
Block a user