oadm_manage_node.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # pylint: skip-file
  2. # flake8: noqa
  3. class ManageNodeException(Exception):
  4. ''' manage-node exception class '''
  5. pass
  6. class ManageNodeConfig(OpenShiftCLIConfig):
  7. ''' ManageNodeConfig is a DTO for the manage-node command.'''
  8. def __init__(self, kubeconfig, node_options):
  9. super(ManageNodeConfig, self).__init__(None, None, kubeconfig, node_options)
  10. # pylint: disable=too-many-instance-attributes
  11. class ManageNode(OpenShiftCLI):
  12. ''' Class to wrap the oc command line tools '''
  13. # pylint allows 5
  14. # pylint: disable=too-many-arguments
  15. def __init__(self,
  16. config,
  17. verbose=False):
  18. ''' Constructor for OCVolume '''
  19. super(ManageNode, self).__init__(None, config.kubeconfig)
  20. self.config = config
  21. def evacuate(self):
  22. ''' formulate the params and run oadm manage-node '''
  23. return self._evacuate(node=self.config.config_options['node']['value'],
  24. selector=self.config.config_options['selector']['value'],
  25. pod_selector=self.config.config_options['pod_selector']['value'],
  26. dry_run=self.config.config_options['dry_run']['value'],
  27. grace_period=self.config.config_options['grace_period']['value'],
  28. force=self.config.config_options['force']['value'],
  29. )
  30. def get_nodes(self, node=None, selector=''):
  31. '''perform oc get node'''
  32. _node = None
  33. _sel = None
  34. if node:
  35. _node = node
  36. if selector:
  37. _sel = selector
  38. results = self._get('node', rname=_node, selector=_sel)
  39. if results['returncode'] != 0:
  40. return results
  41. nodes = []
  42. items = None
  43. if results['results'][0]['kind'] == 'List':
  44. items = results['results'][0]['items']
  45. else:
  46. items = results['results']
  47. for node in items:
  48. _node = {}
  49. _node['name'] = node['metadata']['name']
  50. _node['schedulable'] = True
  51. if 'unschedulable' in node['spec']:
  52. _node['schedulable'] = False
  53. nodes.append(_node)
  54. return nodes
  55. def get_pods_from_node(self, node, pod_selector=None):
  56. '''return pods for a node'''
  57. results = self._list_pods(node=[node], pod_selector=pod_selector)
  58. if results['returncode'] != 0:
  59. return results
  60. # When a selector or node is matched it is returned along with the json.
  61. # We are going to split the results based on the regexp and then
  62. # load the json for each matching node.
  63. # Before we return we are going to loop over the results and pull out the node names.
  64. # {'node': [pod, pod], 'node': [pod, pod]}
  65. # 3.2 includes the following lines in stdout: "Listing matched pods on node:"
  66. all_pods = []
  67. if "Listing matched" in results['results']:
  68. listing_match = re.compile('\n^Listing matched.*$\n', flags=re.MULTILINE)
  69. pods = listing_match.split(results['results'])
  70. for pod in pods:
  71. if pod:
  72. all_pods.extend(json.loads(pod)['items'])
  73. # 3.3 specific
  74. else:
  75. # this is gross but I filed a bug...
  76. # build our own json from the output.
  77. all_pods = json.loads(results['results'])['items']
  78. return all_pods
  79. def list_pods(self):
  80. ''' run oadm manage-node --list-pods'''
  81. _nodes = self.config.config_options['node']['value']
  82. _selector = self.config.config_options['selector']['value']
  83. _pod_selector = self.config.config_options['pod_selector']['value']
  84. if not _nodes:
  85. _nodes = self.get_nodes(selector=_selector)
  86. else:
  87. _nodes = [{'name': name} for name in _nodes]
  88. all_pods = {}
  89. for node in _nodes:
  90. results = self.get_pods_from_node(node['name'], pod_selector=_pod_selector)
  91. if isinstance(results, dict):
  92. return results
  93. all_pods[node['name']] = results
  94. results = {}
  95. results['nodes'] = all_pods
  96. results['returncode'] = 0
  97. return results
  98. def schedulable(self):
  99. '''oadm manage-node call for making nodes unschedulable'''
  100. nodes = self.config.config_options['node']['value']
  101. selector = self.config.config_options['selector']['value']
  102. if not nodes:
  103. nodes = self.get_nodes(selector=selector)
  104. else:
  105. tmp_nodes = []
  106. for name in nodes:
  107. tmp_result = self.get_nodes(name)
  108. if isinstance(tmp_result, dict):
  109. tmp_nodes.append(tmp_result)
  110. continue
  111. tmp_nodes.extend(tmp_result)
  112. nodes = tmp_nodes
  113. # This is a short circuit based on the way we fetch nodes.
  114. # If node is a dict/list then we've already fetched them.
  115. for node in nodes:
  116. if isinstance(node, dict) and 'returncode' in node:
  117. return {'results': nodes, 'returncode': node['returncode']}
  118. if isinstance(node, list) and 'returncode' in node[0]:
  119. return {'results': nodes, 'returncode': node[0]['returncode']}
  120. # check all the nodes that were returned and verify they are:
  121. # node['schedulable'] == self.config.config_options['schedulable']['value']
  122. if any([node['schedulable'] != self.config.config_options['schedulable']['value'] for node in nodes]):
  123. results = self._schedulable(node=self.config.config_options['node']['value'],
  124. selector=self.config.config_options['selector']['value'],
  125. schedulable=self.config.config_options['schedulable']['value'])
  126. # 'NAME STATUS AGE\\nip-172-31-49-140.ec2.internal Ready 4h\\n' # E501
  127. # normalize formatting with previous return objects
  128. if results['results'].startswith('NAME'):
  129. nodes = []
  130. # removing header line and trailing new line character of node lines
  131. for node_results in results['results'].split('\n')[1:-1]:
  132. parts = node_results.split()
  133. nodes.append({'name': parts[0], 'schedulable': parts[1] == 'Ready'})
  134. results['nodes'] = nodes
  135. return results
  136. results = {}
  137. results['returncode'] = 0
  138. results['changed'] = False
  139. results['nodes'] = nodes
  140. return results
  141. @staticmethod
  142. def run_ansible(params, check_mode):
  143. '''run the idempotent ansible code'''
  144. nconfig = ManageNodeConfig(params['kubeconfig'],
  145. {'node': {'value': params['node'], 'include': True},
  146. 'selector': {'value': params['selector'], 'include': True},
  147. 'pod_selector': {'value': params['pod_selector'], 'include': True},
  148. 'schedulable': {'value': params['schedulable'], 'include': True},
  149. 'list_pods': {'value': params['list_pods'], 'include': True},
  150. 'evacuate': {'value': params['evacuate'], 'include': True},
  151. 'dry_run': {'value': params['dry_run'], 'include': True},
  152. 'force': {'value': params['force'], 'include': True},
  153. 'grace_period': {'value': params['grace_period'], 'include': True},
  154. })
  155. oadm_mn = ManageNode(nconfig)
  156. # Run the oadm manage-node commands
  157. results = None
  158. changed = False
  159. if params['schedulable'] != None:
  160. if check_mode:
  161. # schedulable returns results after the fact.
  162. # We need to redo how this works to support check_mode completely.
  163. return {'changed': True, 'msg': 'CHECK_MODE: would have called schedulable.'}
  164. results = oadm_mn.schedulable()
  165. if 'changed' not in results:
  166. changed = True
  167. if params['evacuate']:
  168. results = oadm_mn.evacuate()
  169. changed = True
  170. elif params['list_pods']:
  171. results = oadm_mn.list_pods()
  172. if not results or results['returncode'] != 0:
  173. return {'failed': True, 'msg': results}
  174. return {'changed': changed, 'results': results, 'state': "present"}