diff options
author | Kenny Woodson <kwoodson@redhat.com> | 2017-01-25 14:40:17 -0500 |
---|---|---|
committer | Kenny Woodson <kwoodson@redhat.com> | 2017-01-26 14:20:19 -0500 |
commit | 0db4215a5570b550251af614e8e944b9f2db7094 (patch) | |
tree | c8f9adac3d9e1c62bace6106a56f118acccb787f /roles/lib_openshift/src | |
parent | 8f2b3f132bc6cd03640c31c0c33cffb01f80138c (diff) | |
download | openshift-0db4215a5570b550251af614e8e944b9f2db7094.tar.gz openshift-0db4215a5570b550251af614e8e944b9f2db7094.tar.bz2 openshift-0db4215a5570b550251af614e8e944b9f2db7094.tar.xz openshift-0db4215a5570b550251af614e8e944b9f2db7094.zip |
Adding oadm_manage_node to lib_openshift.
Diffstat (limited to 'roles/lib_openshift/src')
-rw-r--r-- | roles/lib_openshift/src/ansible/oadm_manage_node.py | 38 | ||||
-rw-r--r-- | roles/lib_openshift/src/class/oadm_manage_node.py | 191 | ||||
-rw-r--r-- | roles/lib_openshift/src/doc/manage_node | 88 | ||||
-rw-r--r-- | roles/lib_openshift/src/sources.yml | 9 | ||||
-rwxr-xr-x | roles/lib_openshift/src/test/unit/oadm_manage_node.py | 131 |
5 files changed, 457 insertions, 0 deletions
diff --git a/roles/lib_openshift/src/ansible/oadm_manage_node.py b/roles/lib_openshift/src/ansible/oadm_manage_node.py new file mode 100644 index 000000000..86ab3ec31 --- /dev/null +++ b/roles/lib_openshift/src/ansible/oadm_manage_node.py @@ -0,0 +1,38 @@ +# pylint: skip-file +# flake8: noqa + + +def main(): + ''' + ansible oadm module for manage-node + ''' + + module = AnsibleModule( + argument_spec=dict( + debug=dict(default=False, type='bool'), + kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'), + node=dict(default=None, type='list'), + selector=dict(default=None, type='str'), + pod_selector=dict(default=None, type='str'), + schedulable=dict(default=False, type='bool'), + list_pods=dict(default=False, type='bool'), + evacuate=dict(default=False, type='bool'), + dry_run=dict(default=False, type='bool'), + force=dict(default=False, type='bool'), + grace_period=dict(default=None, type='int'), + ), + mutually_exclusive=[["selector", "node"], ['evacuate', 'list_pods'], ['list_pods', 'schedulable']], + required_one_of=[["node", "selector"]], + + supports_check_mode=True, + ) + results = ManageNode.run_ansible(module.params, module.check_mode) + + if 'failed' in results: + module.fail_json(**results) + + module.exit_json(**results) + + +if __name__ == "__main__": + main() diff --git a/roles/lib_openshift/src/class/oadm_manage_node.py b/roles/lib_openshift/src/class/oadm_manage_node.py new file mode 100644 index 000000000..bf750d83f --- /dev/null +++ b/roles/lib_openshift/src/class/oadm_manage_node.py @@ -0,0 +1,191 @@ +# pylint: skip-file +# flake8: noqa + + +class ManageNodeException(Exception): + ''' manage-node exception class ''' + pass + + +class ManageNodeConfig(OpenShiftCLIConfig): + ''' ManageNodeConfig is a DTO for the manage-node command.''' + def __init__(self, kubeconfig, node_options): + super(ManageNodeConfig, self).__init__(None, None, kubeconfig, node_options) + + +# pylint: disable=too-many-instance-attributes +class ManageNode(OpenShiftCLI): + ''' Class to wrap the oc command line tools ''' + + # pylint allows 5 + # pylint: disable=too-many-arguments + def __init__(self, + config, + verbose=False): + ''' Constructor for OCVolume ''' + super(ManageNode, self).__init__(None, config.kubeconfig) + self.config = config + + def evacuate(self): + ''' formulate the params and run oadm manage-node ''' + return self._evacuate(node=self.config.config_options['node']['value'], + selector=self.config.config_options['selector']['value'], + pod_selector=self.config.config_options['pod_selector']['value'], + dry_run=self.config.config_options['dry_run']['value'], + grace_period=self.config.config_options['grace_period']['value'], + force=self.config.config_options['force']['value'], + ) + def get_nodes(self, node=None, selector=''): + '''perform oc get node''' + _node = None + _sel = None + if node: + _node = node + if selector: + _sel = selector + + results = self._get('node', rname=_node, selector=_sel) + if results['returncode'] != 0: + return results + + nodes = [] + items = None + if results['results'][0]['kind'] == 'List': + items = results['results'][0]['items'] + else: + items = results['results'] + + for node in items: + _node = {} + _node['name'] = node['metadata']['name'] + _node['schedulable'] = True + if node['spec'].has_key('unschedulable'): + _node['schedulable'] = False + nodes.append(_node) + + return nodes + + def get_pods_from_node(self, node, pod_selector=None): + '''return pods for a node''' + results = self._list_pods(node=[node], pod_selector=pod_selector) + + if results['returncode'] != 0: + return results + + # When a selector or node is matched it is returned along with the json. + # We are going to split the results based on the regexp and then + # load the json for each matching node. + # Before we return we are going to loop over the results and pull out the node names. + # {'node': [pod, pod], 'node': [pod, pod]} + # 3.2 includes the following lines in stdout: "Listing matched pods on node:" + all_pods = [] + if "Listing matched" in results['results']: + listing_match = re.compile('\n^Listing matched.*$\n', flags=re.MULTILINE) + pods = listing_match.split(results['results']) + for pod in pods: + if pod: + all_pods.extend(json.loads(pod)['items']) + + # 3.3 specific + else: + # this is gross but I filed a bug... + # build our own json from the output. + all_pods = json.loads(results['results'])['items'] + + return all_pods + + def list_pods(self): + ''' run oadm manage-node --list-pods''' + _nodes = self.config.config_options['node']['value'] + _selector = self.config.config_options['selector']['value'] + _pod_selector = self.config.config_options['pod_selector']['value'] + + if not _nodes: + _nodes = self.get_nodes(selector=_selector) + else: + _nodes = [{'name': name} for name in _nodes] + + all_pods = {} + for node in _nodes: + results = self.get_pods_from_node(node['name'], pod_selector=_pod_selector) + if isinstance(results, dict): + return results + all_pods[node['name']] = results + + results = {} + results['nodes'] = all_pods + results['returncode'] = 0 + return results + + def schedulable(self): + '''oadm manage-node call for making nodes unschedulable''' + nodes = self.config.config_options['node']['value'] + selector = self.config.config_options['selector']['value'] + + if not nodes: + nodes = self.get_nodes(selector=selector) + else: + tmp_nodes = [] + for name in nodes: + tmp_result = self.get_nodes(name) + if isinstance(tmp_result, dict): + tmp_nodes.append(tmp_result) + continue + tmp_nodes.extend(self.get_nodes(name)) + nodes = tmp_nodes + + for node in nodes: + if isinstance(node, dict) and node.has_key('returncode'): + return {'results': nodes, 'returncode': node['returncode']} + if isinstance(node, list) and node[0].has_key('returncode'): + return {'results': nodes, 'returncode': node[0]['returncode']} + # check all the nodes that were returned and verify they are: + # node['schedulable'] == self.config.config_options['schedulable']['value'] + if any([node['schedulable'] != self.config.config_options['schedulable']['value'] for node in nodes]): + + return self._schedulable(node=self.config.config_options['node']['value'], + selector=self.config.config_options['selector']['value'], + schedulable=self.config.config_options['schedulable']['value'], + ) + + results = {} + results['returncode'] = 0 + results['changed'] = False + results['nodes'] = nodes + + return results + + @staticmethod + def run_ansible(params, check_mode): + '''run the idempotent ansible code''' + nconfig = ManageNodeConfig(params['kubeconfig'], + {'node': {'value': params['node'], 'include': True}, + 'selector': {'value': params['selector'], 'include': True}, + 'pod_selector': {'value': params['pod_selector'], 'include': True}, + 'schedulable': {'value': params['schedulable'], 'include': True}, + 'list_pods': {'value': params['list_pods'], 'include': True}, + 'evacuate': {'value': params['evacuate'], 'include': True}, + 'dry_run': {'value': params['dry_run'], 'include': True}, + 'force': {'value': params['force'], 'include': True}, + 'grace_period': {'value': params['grace_period'], 'include': True}, + }) + + oadm_mn = ManageNode(nconfig) + # Run the oadm manage-node commands + results = None + changed = False + if params['schedulable'] != None: + results = oadm_mn.schedulable() + if not results.has_key('changed'): + changed = True + + if params['evacuate']: + results = oadm_mn.evacuate() + changed = True + elif params['list_pods']: + results = oadm_mn.list_pods() + + if not results or results['returncode'] != 0: + return {'failed': True, 'msg': results} + + return {'changed': changed, 'results': results, 'state': "present"} diff --git a/roles/lib_openshift/src/doc/manage_node b/roles/lib_openshift/src/doc/manage_node new file mode 100644 index 000000000..ff5a21ba5 --- /dev/null +++ b/roles/lib_openshift/src/doc/manage_node @@ -0,0 +1,88 @@ +# flake8: noqa +# pylint: skip-file + +DOCUMENTATION = ''' +--- +module: oadm_manage_node +short_description: Module to manage openshift nodes +description: + - Manage openshift nodes programmatically. +options: + kubeconfig: + description: + - The path for the kubeconfig file to use for authentication + required: false + default: /etc/origin/master/admin.kubeconfig + aliases: [] + debug: + description: + - Turn on debug output. + required: false + default: False + aliases: [] + node: + description: + - A list of the nodes being managed + required: false + default: None + aliases: [] + selector: + description: + - The selector when filtering on node labels + required: false + default: str + aliases: [] + pod_selector: + description: + - A selector when filtering on pod labels. + required: false + default: None + aliases: [] + evacuate: + description: + - Remove all pods from a node. + required: false + default: False + aliases: [] + schedulable: + description: + - whether or not openshift can schedule pods on this node + required: False + default: None + aliases: [] + dry_run: + description: + - This shows the pods that would be migrated if evacuate were called + required: False + default: False + aliases: [] + grace_period: + description: + - Grace period (seconds) for pods being deleted. + required: false + default: None + aliases: [] + force: + description: + - Whether or not to attempt to force this action in openshift + required: false + default: None + aliases: [] +author: +- "Kenny Woodson <kwoodson@redhat.com>" +extends_documentation_fragment: [] +''' + +EXAMPLES = ''' +- name: oadm manage-node --schedulable=true --selector=ops_node=new + oadm_manage_node: + selector: ops_node=new + schedulable: True + register: schedout + +- name: oadm manage-node my-k8s-node-5 --evacuate + oadm_manage_node: + node: my-k8s-node-5 + evacuate: True + force: True +''' diff --git a/roles/lib_openshift/src/sources.yml b/roles/lib_openshift/src/sources.yml index 5afcdc55d..b0835784b 100644 --- a/roles/lib_openshift/src/sources.yml +++ b/roles/lib_openshift/src/sources.yml @@ -1,4 +1,13 @@ --- +oadm_manage_node.py: +- doc/generated +- doc/license +- lib/import.py +- doc/manage_node +- ../../lib_utils/src/class/yedit.py +- lib/base.py +- class/oadm_manage_node.py +- ansible/oadm_manage_node.py oc_edit.py: - doc/generated - doc/license diff --git a/roles/lib_openshift/src/test/unit/oadm_manage_node.py b/roles/lib_openshift/src/test/unit/oadm_manage_node.py new file mode 100755 index 000000000..4c1819d66 --- /dev/null +++ b/roles/lib_openshift/src/test/unit/oadm_manage_node.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python2 +''' + Unit tests for oadm_manage_node +''' +# To run +# python -m unittest version +# +# . +# Ran 1 test in 0.597s +# +# OK + +import os +import sys +import unittest +import mock + +# Removing invalid variable names for tests so that I can +# keep them brief +# pylint: disable=invalid-name,no-name-in-module +# Disable import-error b/c our libraries aren't loaded in jenkins +# pylint: disable=import-error +# place class in our python path +module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 +sys.path.insert(0, module_path) +from oadm_manage_node import ManageNode # noqa: E402 + + +class ManageNodeTest(unittest.TestCase): + ''' + Test class for oadm_manage_node + ''' + + def setUp(self): + ''' setup method will create a file and set to known configuration ''' + pass + + @mock.patch('oadm_manage_node.ManageNode.openshift_cmd') + def test_state_list(self, mock_openshift_cmd): + ''' Testing a get ''' + params = {'node': 'test-node-1', + 'namespace': 'default', + 'selector': None, + 'pod_selector': None, + 'list_pods': True, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'evacuate': False, + 'grace_period': False, + 'dry_run': False, + 'force': False} + + dc = '''{"kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "router", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router", + "uid": "a441eedc-e1ae-11e6-a2d5-0e6967f34d42", + "resourceVersion": "6558", + "generation": 8, + "creationTimestamp": "2017-01-23T20:58:07Z", + "labels": { + "router": "router" + } + }, + "spec": { + "replicas": 2, + } + }''' + + mock_openshift_cmd.side_effect = [ + {"cmd": '/usr/bin/oc get dc router -n default', + 'results': dc, + 'returncode': 0}] + + results = OCScale.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertEqual(results['result'][0], 2) + + @mock.patch('oc_scale.OCScale.openshift_cmd') + def test_scale(self, mock_openshift_cmd): + ''' Testing a get ''' + params = {'name': 'router', + 'namespace': 'default', + 'replicas': 3, + 'state': 'list', + 'kind': 'dc', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + dc = '''{"kind": "DeploymentConfig", + "apiVersion": "v1", + "metadata": { + "name": "router", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router", + "uid": "a441eedc-e1ae-11e6-a2d5-0e6967f34d42", + "resourceVersion": "6558", + "generation": 8, + "creationTimestamp": "2017-01-23T20:58:07Z", + "labels": { + "router": "router" + } + }, + "spec": { + "replicas": 3, + } + }''' + + mock_openshift_cmd.side_effect = [ + {"cmd": '/usr/bin/oc get dc router -n default', + 'results': dc, + 'returncode': 0}, + {"cmd": '/usr/bin/oc create -f /tmp/router -n default', + 'results': '', + 'returncode': 0} + ] + + results = OCScale.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertEqual(results['result'][0], 3) + + def tearDown(self): + '''TearDown method''' + pass + + +if __name__ == "__main__": + unittest.main() |