openshift_master_facts_default_predicates.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # pylint: disable=missing-docstring
  2. import re
  3. from ansible.errors import AnsibleError
  4. from ansible.plugins.lookup import LookupBase
  5. class LookupModule(LookupBase):
  6. # pylint: disable=too-many-branches,too-many-statements,too-many-arguments
  7. def run(self, terms, variables=None, regions_enabled=True, short_version=None,
  8. deployment_type=None, **kwargs):
  9. predicates = []
  10. if short_version is None or deployment_type is None:
  11. if 'openshift' not in variables:
  12. raise AnsibleError("This lookup module requires openshift_facts to be run prior to use")
  13. if deployment_type is None:
  14. if 'common' not in variables['openshift'] or 'deployment_type' not in variables['openshift']['common']:
  15. raise AnsibleError("This lookup module requires that the deployment_type be set")
  16. deployment_type = variables['openshift']['common']['deployment_type']
  17. if short_version is None:
  18. if 'short_version' in variables['openshift']['common']:
  19. short_version = variables['openshift']['common']['short_version']
  20. elif 'openshift_release' in variables:
  21. release = variables['openshift_release']
  22. if release.startswith('v'):
  23. short_version = release[1:]
  24. else:
  25. short_version = release
  26. short_version = '.'.join(short_version.split('.')[0:2])
  27. elif 'openshift_version' in variables:
  28. version = variables['openshift_version']
  29. short_version = '.'.join(version.split('.')[0:2])
  30. else:
  31. # pylint: disable=line-too-long
  32. raise AnsibleError("Either OpenShift needs to be installed or openshift_release needs to be specified")
  33. if deployment_type == 'origin':
  34. if short_version not in ['1.1', '1.2', '1.3', '1.4', '1.5', '3.6', '3.7', '3.8', '3.9', 'latest']:
  35. raise AnsibleError("Unknown short_version %s" % short_version)
  36. elif deployment_type == 'openshift-enterprise':
  37. if short_version not in ['3.1', '3.2', '3.3', '3.4', '3.5', '3.6', '3.7', '3.8', '3.9', 'latest']:
  38. raise AnsibleError("Unknown short_version %s" % short_version)
  39. else:
  40. raise AnsibleError("Unknown deployment_type %s" % deployment_type)
  41. if deployment_type == 'origin':
  42. # convert short_version to enterprise short_version
  43. short_version = re.sub('^1.', '3.', short_version)
  44. if short_version == 'latest':
  45. short_version = '3.9'
  46. # Predicates ordered according to OpenShift Origin source:
  47. # origin/vendor/k8s.io/kubernetes/plugin/pkg/scheduler/algorithmprovider/defaults/defaults.go
  48. if short_version == '3.1':
  49. predicates.extend([
  50. {'name': 'PodFitsHostPorts'},
  51. {'name': 'PodFitsResources'},
  52. {'name': 'NoDiskConflict'},
  53. {'name': 'MatchNodeSelector'},
  54. ])
  55. if short_version == '3.2':
  56. predicates.extend([
  57. {'name': 'PodFitsHostPorts'},
  58. {'name': 'PodFitsResources'},
  59. {'name': 'NoDiskConflict'},
  60. {'name': 'NoVolumeZoneConflict'},
  61. {'name': 'MatchNodeSelector'},
  62. {'name': 'MaxEBSVolumeCount'},
  63. {'name': 'MaxGCEPDVolumeCount'}
  64. ])
  65. if short_version == '3.3':
  66. predicates.extend([
  67. {'name': 'NoDiskConflict'},
  68. {'name': 'NoVolumeZoneConflict'},
  69. {'name': 'MaxEBSVolumeCount'},
  70. {'name': 'MaxGCEPDVolumeCount'},
  71. {'name': 'GeneralPredicates'},
  72. {'name': 'PodToleratesNodeTaints'},
  73. {'name': 'CheckNodeMemoryPressure'}
  74. ])
  75. if short_version == '3.4':
  76. predicates.extend([
  77. {'name': 'NoDiskConflict'},
  78. {'name': 'NoVolumeZoneConflict'},
  79. {'name': 'MaxEBSVolumeCount'},
  80. {'name': 'MaxGCEPDVolumeCount'},
  81. {'name': 'GeneralPredicates'},
  82. {'name': 'PodToleratesNodeTaints'},
  83. {'name': 'CheckNodeMemoryPressure'},
  84. {'name': 'CheckNodeDiskPressure'},
  85. {'name': 'MatchInterPodAffinity'}
  86. ])
  87. if short_version in ['3.5', '3.6']:
  88. predicates.extend([
  89. {'name': 'NoVolumeZoneConflict'},
  90. {'name': 'MaxEBSVolumeCount'},
  91. {'name': 'MaxGCEPDVolumeCount'},
  92. {'name': 'MatchInterPodAffinity'},
  93. {'name': 'NoDiskConflict'},
  94. {'name': 'GeneralPredicates'},
  95. {'name': 'PodToleratesNodeTaints'},
  96. {'name': 'CheckNodeMemoryPressure'},
  97. {'name': 'CheckNodeDiskPressure'},
  98. ])
  99. if short_version in ['3.7', '3.8', '3.9']:
  100. predicates.extend([
  101. {'name': 'NoVolumeZoneConflict'},
  102. {'name': 'MaxEBSVolumeCount'},
  103. {'name': 'MaxGCEPDVolumeCount'},
  104. {'name': 'MaxAzureDiskVolumeCount'},
  105. {'name': 'MatchInterPodAffinity'},
  106. {'name': 'NoDiskConflict'},
  107. {'name': 'GeneralPredicates'},
  108. {'name': 'PodToleratesNodeTaints'},
  109. {'name': 'CheckNodeMemoryPressure'},
  110. {'name': 'CheckNodeDiskPressure'},
  111. {'name': 'NoVolumeNodeConflict'},
  112. ])
  113. if regions_enabled:
  114. region_predicate = {
  115. 'name': 'Region',
  116. 'argument': {
  117. 'serviceAffinity': {
  118. 'labels': ['region']
  119. }
  120. }
  121. }
  122. predicates.append(region_predicate)
  123. return predicates