oc_objectvalidator.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # pylint: skip-file
  2. # flake8: noqa
  3. # pylint: disable=too-many-instance-attributes
  4. class OCObjectValidator(OpenShiftCLI):
  5. ''' Class to wrap the oc command line tools '''
  6. def __init__(self, kubeconfig):
  7. ''' Constructor for OCObjectValidator '''
  8. # namespace has no meaning for object validation, hardcode to 'default'
  9. super(OCObjectValidator, self).__init__('default', kubeconfig)
  10. def get_invalid(self, kind, invalid_filter):
  11. ''' return invalid object information '''
  12. rval = self._get(kind)
  13. if rval['returncode'] != 0:
  14. return False, rval, []
  15. return True, rval, list(filter(invalid_filter, rval['results'][0]['items'])) # wrap filter with list for py3
  16. # pylint: disable=too-many-return-statements
  17. @staticmethod
  18. def run_ansible(params):
  19. ''' run the oc_objectvalidator module
  20. params comes from the ansible portion of this module
  21. '''
  22. objectvalidator = OCObjectValidator(params['kubeconfig'])
  23. all_invalid = {}
  24. failed = False
  25. def _is_invalid_namespace(namespace):
  26. # check if it uses a reserved name
  27. name = namespace['metadata']['name']
  28. if not any((name == 'kube',
  29. name == 'kubernetes',
  30. name == 'openshift',
  31. name.startswith('kube-'),
  32. name.startswith('kubernetes-'),
  33. name.startswith('openshift-'),)):
  34. return False
  35. # determine if the namespace was created by a user
  36. if 'annotations' not in namespace['metadata']:
  37. return False
  38. return 'openshift.io/requester' in namespace['metadata']['annotations']
  39. checks = (
  40. (
  41. 'hostsubnet',
  42. lambda x: x['metadata']['name'] != x['host'],
  43. u'hostsubnets where metadata.name != host',
  44. ),
  45. (
  46. 'netnamespace',
  47. lambda x: x['metadata']['name'] != x['netname'],
  48. u'netnamespaces where metadata.name != netname',
  49. ),
  50. (
  51. 'namespace',
  52. _is_invalid_namespace,
  53. u'namespaces that use reserved names and were not created by infrastructure components',
  54. ),
  55. )
  56. for resource, invalid_filter, invalid_msg in checks:
  57. success, rval, invalid = objectvalidator.get_invalid(resource, invalid_filter)
  58. if not success:
  59. return {'failed': True, 'msg': 'Failed to GET {}.'.format(resource), 'state': 'list', 'results': rval}
  60. if invalid:
  61. failed = True
  62. all_invalid[invalid_msg] = invalid
  63. if failed:
  64. return {
  65. 'failed': True,
  66. 'msg': (
  67. "All objects are not valid. If you are a supported customer please contact "
  68. "Red Hat Support providing the complete output above. If you are not a customer "
  69. "please contact users@lists.openshift.redhat.com for assistance."
  70. ),
  71. 'state': 'list',
  72. 'results': all_invalid
  73. }
  74. return {'msg': 'All objects are valid.'}