2016-03-18 14:07:33 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: kube
|
|
|
|
short_description: Manage Kubernetes Cluster
|
|
|
|
description:
|
|
|
|
- Create, replace, remove, and stop resources within a Kubernetes Cluster
|
|
|
|
version_added: "2.0"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- The name associated with resource
|
|
|
|
filename:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
2018-03-07 20:50:09 +00:00
|
|
|
- The path and filename of the resource(s) definition file(s).
|
|
|
|
- To operate on several files this can accept a comma separated list of files or a list of files.
|
|
|
|
aliases: [ 'files', 'file', 'filenames' ]
|
2016-03-18 14:07:33 +00:00
|
|
|
kubectl:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- The path to the kubectl bin
|
|
|
|
namespace:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- The namespace associated with the resource(s)
|
|
|
|
resource:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- The resource to perform an action on. pods (po), replicationControllers (rc), services (svc)
|
|
|
|
label:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- The labels used to filter specific resources.
|
|
|
|
server:
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
description:
|
|
|
|
- The url for the API server that commands are executed against.
|
|
|
|
force:
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
description:
|
|
|
|
- A flag to indicate to force delete, replace, or stop.
|
2020-09-03 11:01:43 +00:00
|
|
|
wait:
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
description:
|
|
|
|
- A flag to indicate to wait for resources to be created before continuing to the next step
|
2016-03-18 14:07:33 +00:00
|
|
|
all:
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
description:
|
|
|
|
- A flag to indicate delete all, stop all, or all namespaces when checking exists.
|
|
|
|
log_level:
|
|
|
|
required: false
|
|
|
|
default: 0
|
|
|
|
description:
|
|
|
|
- Indicates the level of verbosity of logging by kubectl.
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
choices: ['present', 'absent', 'latest', 'reloaded', 'stopped']
|
|
|
|
default: present
|
|
|
|
description:
|
|
|
|
- present handles checking existence or creating if definition file provided,
|
|
|
|
absent handles deleting resource(s) based on other options,
|
2017-05-08 13:55:34 +00:00
|
|
|
latest handles creating or updating based on existence,
|
2016-03-18 14:07:33 +00:00
|
|
|
reloaded handles updating resource(s) definition using definition file,
|
|
|
|
stopped handles stopping resource(s) based on other options.
|
2019-02-26 06:17:23 +00:00
|
|
|
recursive:
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
description:
|
|
|
|
- Process the directory used in -f, --filename recursively.
|
|
|
|
Useful when you want to manage related manifests organized
|
|
|
|
within the same directory.
|
2016-03-18 14:07:33 +00:00
|
|
|
requirements:
|
|
|
|
- kubectl
|
|
|
|
author: "Kenny Jones (@kenjones-cisco)"
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: test nginx is present
|
|
|
|
kube: name=nginx resource=rc state=present
|
|
|
|
|
|
|
|
- name: test nginx is stopped
|
|
|
|
kube: name=nginx resource=rc state=stopped
|
|
|
|
|
|
|
|
- name: test nginx is absent
|
|
|
|
kube: name=nginx resource=rc state=absent
|
|
|
|
|
|
|
|
- name: test nginx is present
|
|
|
|
kube: filename=/tmp/nginx.yml
|
2018-03-07 20:50:09 +00:00
|
|
|
|
|
|
|
- name: test nginx and postgresql are present
|
|
|
|
kube: files=/tmp/nginx.yml,/tmp/postgresql.yml
|
|
|
|
|
|
|
|
- name: test nginx and postgresql are present
|
|
|
|
kube:
|
|
|
|
files:
|
|
|
|
- /tmp/nginx.yml
|
|
|
|
- /tmp/postgresql.yml
|
2016-03-18 14:07:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class KubeManager(object):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
|
|
|
|
self.module = module
|
|
|
|
|
|
|
|
self.kubectl = module.params.get('kubectl')
|
|
|
|
if self.kubectl is None:
|
|
|
|
self.kubectl = module.get_bin_path('kubectl', True)
|
|
|
|
self.base_cmd = [self.kubectl]
|
|
|
|
|
|
|
|
if module.params.get('server'):
|
|
|
|
self.base_cmd.append('--server=' + module.params.get('server'))
|
|
|
|
|
|
|
|
if module.params.get('log_level'):
|
|
|
|
self.base_cmd.append('--v=' + str(module.params.get('log_level')))
|
|
|
|
|
|
|
|
if module.params.get('namespace'):
|
|
|
|
self.base_cmd.append('--namespace=' + module.params.get('namespace'))
|
|
|
|
|
2019-02-26 06:17:23 +00:00
|
|
|
|
2016-03-18 14:07:33 +00:00
|
|
|
self.all = module.params.get('all')
|
|
|
|
self.force = module.params.get('force')
|
2020-09-03 11:01:43 +00:00
|
|
|
self.wait = module.params.get('wait')
|
2016-03-18 14:07:33 +00:00
|
|
|
self.name = module.params.get('name')
|
2018-03-07 20:50:09 +00:00
|
|
|
self.filename = [f.strip() for f in module.params.get('filename') or []]
|
2016-03-18 14:07:33 +00:00
|
|
|
self.resource = module.params.get('resource')
|
|
|
|
self.label = module.params.get('label')
|
2019-02-26 06:17:23 +00:00
|
|
|
self.recursive = module.params.get('recursive')
|
2016-03-18 14:07:33 +00:00
|
|
|
|
|
|
|
def _execute(self, cmd):
|
|
|
|
args = self.base_cmd + cmd
|
|
|
|
try:
|
|
|
|
rc, out, err = self.module.run_command(args)
|
|
|
|
if rc != 0:
|
|
|
|
self.module.fail_json(
|
2018-03-07 20:50:09 +00:00
|
|
|
msg='error running kubectl (%s) command (rc=%d), out=\'%s\', err=\'%s\'' % (' '.join(args), rc, out, err))
|
2016-03-18 14:07:33 +00:00
|
|
|
except Exception as exc:
|
|
|
|
self.module.fail_json(
|
|
|
|
msg='error running kubectl (%s) command: %s' % (' '.join(args), str(exc)))
|
|
|
|
return out.splitlines()
|
|
|
|
|
|
|
|
def _execute_nofail(self, cmd):
|
|
|
|
args = self.base_cmd + cmd
|
|
|
|
rc, out, err = self.module.run_command(args)
|
|
|
|
if rc != 0:
|
|
|
|
return None
|
|
|
|
return out.splitlines()
|
|
|
|
|
2017-09-13 18:00:51 +00:00
|
|
|
def create(self, check=True, force=True):
|
2016-03-18 14:07:33 +00:00
|
|
|
if check and self.exists():
|
|
|
|
return []
|
|
|
|
|
2017-09-06 06:36:54 +00:00
|
|
|
cmd = ['apply']
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2017-09-13 18:00:51 +00:00
|
|
|
if force:
|
|
|
|
cmd.append('--force')
|
|
|
|
|
2020-09-03 11:01:43 +00:00
|
|
|
if self.wait:
|
|
|
|
cmd.append('--wait')
|
|
|
|
|
2019-02-26 06:17:23 +00:00
|
|
|
if self.recursive:
|
|
|
|
cmd.append('--recursive={}'.format(self.recursive))
|
|
|
|
|
2016-03-18 14:07:33 +00:00
|
|
|
if not self.filename:
|
|
|
|
self.module.fail_json(msg='filename required to create')
|
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
cmd.append('--filename=' + ','.join(self.filename))
|
2016-03-18 14:07:33 +00:00
|
|
|
|
|
|
|
return self._execute(cmd)
|
|
|
|
|
2017-09-13 18:00:51 +00:00
|
|
|
def replace(self, force=True):
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2017-09-06 06:36:54 +00:00
|
|
|
cmd = ['apply']
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2017-09-13 18:00:51 +00:00
|
|
|
if force:
|
2016-03-18 14:07:33 +00:00
|
|
|
cmd.append('--force')
|
|
|
|
|
2020-09-03 11:01:43 +00:00
|
|
|
if self.wait:
|
|
|
|
cmd.append('--wait')
|
|
|
|
|
2019-02-26 06:17:23 +00:00
|
|
|
if self.recursive:
|
|
|
|
cmd.append('--recursive={}'.format(self.recursive))
|
|
|
|
|
2016-03-18 14:07:33 +00:00
|
|
|
if not self.filename:
|
|
|
|
self.module.fail_json(msg='filename required to reload')
|
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
cmd.append('--filename=' + ','.join(self.filename))
|
2016-03-18 14:07:33 +00:00
|
|
|
|
|
|
|
return self._execute(cmd)
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
|
|
|
|
if not self.force and not self.exists():
|
|
|
|
return []
|
|
|
|
|
|
|
|
cmd = ['delete']
|
|
|
|
|
|
|
|
if self.filename:
|
2018-03-07 20:50:09 +00:00
|
|
|
cmd.append('--filename=' + ','.join(self.filename))
|
2019-02-26 06:17:23 +00:00
|
|
|
if self.recursive:
|
|
|
|
cmd.append('--recursive={}'.format(self.recursive))
|
2016-03-18 14:07:33 +00:00
|
|
|
else:
|
|
|
|
if not self.resource:
|
|
|
|
self.module.fail_json(msg='resource required to delete without filename')
|
|
|
|
|
|
|
|
cmd.append(self.resource)
|
|
|
|
|
|
|
|
if self.name:
|
|
|
|
cmd.append(self.name)
|
|
|
|
|
|
|
|
if self.label:
|
|
|
|
cmd.append('--selector=' + self.label)
|
|
|
|
|
|
|
|
if self.all:
|
|
|
|
cmd.append('--all')
|
|
|
|
|
|
|
|
if self.force:
|
|
|
|
cmd.append('--ignore-not-found')
|
|
|
|
|
2019-02-26 06:17:23 +00:00
|
|
|
if self.recursive:
|
|
|
|
cmd.append('--recursive={}'.format(self.recursive))
|
|
|
|
|
2016-03-18 14:07:33 +00:00
|
|
|
return self._execute(cmd)
|
|
|
|
|
|
|
|
def exists(self):
|
|
|
|
cmd = ['get']
|
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
if self.filename:
|
|
|
|
cmd.append('--filename=' + ','.join(self.filename))
|
2019-02-26 06:17:23 +00:00
|
|
|
if self.recursive:
|
|
|
|
cmd.append('--recursive={}'.format(self.recursive))
|
2018-03-07 20:50:09 +00:00
|
|
|
else:
|
|
|
|
if not self.resource:
|
|
|
|
self.module.fail_json(msg='resource required without filename')
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
cmd.append(self.resource)
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
if self.name:
|
|
|
|
cmd.append(self.name)
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
if self.label:
|
|
|
|
cmd.append('--selector=' + self.label)
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
if self.all:
|
|
|
|
cmd.append('--all-namespaces')
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
cmd.append('--no-headers')
|
2016-03-18 14:07:33 +00:00
|
|
|
|
|
|
|
result = self._execute_nofail(cmd)
|
|
|
|
if not result:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-03-07 20:50:09 +00:00
|
|
|
# TODO: This is currently unused, perhaps convert to 'scale' with a replicas param?
|
2016-03-18 14:07:33 +00:00
|
|
|
def stop(self):
|
|
|
|
|
|
|
|
if not self.force and not self.exists():
|
|
|
|
return []
|
|
|
|
|
|
|
|
cmd = ['stop']
|
|
|
|
|
|
|
|
if self.filename:
|
2018-03-07 20:50:09 +00:00
|
|
|
cmd.append('--filename=' + ','.join(self.filename))
|
2019-02-26 06:17:23 +00:00
|
|
|
if self.recursive:
|
|
|
|
cmd.append('--recursive={}'.format(self.recursive))
|
2016-03-18 14:07:33 +00:00
|
|
|
else:
|
|
|
|
if not self.resource:
|
|
|
|
self.module.fail_json(msg='resource required to stop without filename')
|
|
|
|
|
|
|
|
cmd.append(self.resource)
|
|
|
|
|
|
|
|
if self.name:
|
|
|
|
cmd.append(self.name)
|
|
|
|
|
|
|
|
if self.label:
|
|
|
|
cmd.append('--selector=' + self.label)
|
|
|
|
|
|
|
|
if self.all:
|
|
|
|
cmd.append('--all')
|
|
|
|
|
|
|
|
if self.force:
|
|
|
|
cmd.append('--ignore-not-found')
|
|
|
|
|
|
|
|
return self._execute(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(),
|
2018-03-07 20:50:09 +00:00
|
|
|
filename=dict(type='list', aliases=['files', 'file', 'filenames']),
|
2016-03-18 14:07:33 +00:00
|
|
|
namespace=dict(),
|
|
|
|
resource=dict(),
|
|
|
|
label=dict(),
|
|
|
|
server=dict(),
|
|
|
|
kubectl=dict(),
|
|
|
|
force=dict(default=False, type='bool'),
|
2020-09-03 11:01:43 +00:00
|
|
|
wait=dict(default=False, type='bool'),
|
2016-03-18 14:07:33 +00:00
|
|
|
all=dict(default=False, type='bool'),
|
|
|
|
log_level=dict(default=0, type='int'),
|
2022-06-20 07:34:31 +00:00
|
|
|
state=dict(default='present', choices=['present', 'absent', 'latest', 'reloaded', 'stopped', 'exists']),
|
2019-02-26 06:17:23 +00:00
|
|
|
recursive=dict(default=False, type='bool'),
|
2018-03-07 20:50:09 +00:00
|
|
|
),
|
|
|
|
mutually_exclusive=[['filename', 'list']]
|
2016-03-18 14:07:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
manager = KubeManager(module)
|
|
|
|
state = module.params.get('state')
|
|
|
|
if state == 'present':
|
2017-09-06 06:36:54 +00:00
|
|
|
result = manager.create(check=False)
|
2016-03-18 14:07:33 +00:00
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
result = manager.delete()
|
|
|
|
|
|
|
|
elif state == 'reloaded':
|
|
|
|
result = manager.replace()
|
|
|
|
|
|
|
|
elif state == 'stopped':
|
|
|
|
result = manager.stop()
|
|
|
|
|
|
|
|
elif state == 'latest':
|
2017-09-06 06:36:54 +00:00
|
|
|
result = manager.replace()
|
2016-03-18 14:07:33 +00:00
|
|
|
|
2022-06-20 07:34:31 +00:00
|
|
|
elif state == 'exists':
|
|
|
|
result = manager.exists()
|
|
|
|
module.exit_json(changed=changed,
|
|
|
|
msg='%s' % result)
|
|
|
|
|
2016-03-18 14:07:33 +00:00
|
|
|
else:
|
|
|
|
module.fail_json(msg='Unrecognized state %s.' % state)
|
|
|
|
|
|
|
|
module.exit_json(changed=changed,
|
|
|
|
msg='success: %s' % (' '.join(result))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import * # noqa
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|