Manage a pool of nodes for a distributed test infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0KB

  1. # Copyright 2018 Red Hat
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. #
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import math
  17. import voluptuous as v
  18. from nodepool.driver import ConfigPool
  19. from nodepool.driver import ConfigValue
  20. from nodepool.driver import ProviderConfig
  21. class OpenshiftLabel(ConfigValue):
  22. def __eq__(self, other):
  23. if isinstance(other, OpenshiftLabel):
  24. return (other.name == self.name and
  25. other.type == self.type and
  26. other.image_pull == self.image_pull and
  27. other.image == self.image and
  28. other.cpu == self.cpu and
  29. other.memory == self.memory)
  30. return False
  31. def __repr__(self):
  32. return "<OpenshiftLabel %s>" % self.name
  33. class OpenshiftPool(ConfigPool):
  34. def __eq__(self, other):
  35. if isinstance(other, OpenshiftPool):
  36. return (super().__eq__(other) and
  37. other.name == self.name and
  38. other.labels == self.labels)
  39. return False
  40. def __repr__(self):
  41. return "<OpenshiftPool %s>" % self.name
  42. def load(self, pool_config, full_config):
  43. super().load(pool_config)
  44. self.name = pool_config['name']
  45. self.labels = {}
  46. for label in pool_config.get('labels', []):
  47. pl = OpenshiftLabel()
  48. pl.name = label['name']
  49. pl.type = label['type']
  50. pl.image = label.get('image')
  51. pl.image_pull = label.get('image-pull', 'IfNotPresent')
  52. pl.cpu = label.get('cpu')
  53. pl.memory = label.get('memory')
  54. pl.pool = self
  55. self.labels[pl.name] = pl
  56. full_config.labels[label['name']].pools.append(self)
  57. class OpenshiftProviderConfig(ProviderConfig):
  58. def __init__(self, driver, provider):
  59. self.driver_object = driver
  60. self.__pools = {}
  61. super().__init__(provider)
  62. def __eq__(self, other):
  63. if isinstance(other, OpenshiftProviderConfig):
  64. return (super().__eq__(other) and
  65. other.context == self.context and
  66. other.pools == self.pools)
  67. return False
  68. @property
  69. def pools(self):
  70. return self.__pools
  71. @property
  72. def manage_images(self):
  73. return False
  74. def load(self, config):
  75. self.launch_retries = int(self.provider.get('launch-retries', 3))
  76. self.context = self.provider['context']
  77. self.max_projects = self.provider.get('max-projects', math.inf)
  78. for pool in self.provider.get('pools', []):
  79. pp = OpenshiftPool()
  80. pp.load(pool, config)
  81. pp.provider = self
  82. self.pools[pp.name] = pp
  83. def getSchema(self):
  84. openshift_label = {
  85. v.Required('name'): str,
  86. v.Required('type'): str,
  87. 'image': str,
  88. 'image-pull': str,
  89. 'cpu': int,
  90. 'memory': int,
  91. }
  92. pool = {
  93. v.Required('name'): str,
  94. v.Required('labels'): [openshift_label],
  95. }
  96. schema = ProviderConfig.getCommonSchemaDict()
  97. schema.update({
  98. v.Required('pools'): [pool],
  99. v.Required('context'): str,
  100. 'launch-retries': int,
  101. 'max-projects': int,
  102. })
  103. return v.Schema(schema)
  104. def getSupportedLabels(self, pool_name=None):
  105. labels = set()
  106. for pool in self.pools.values():
  107. if not pool_name or (pool.name == pool_name):
  108. labels.update(pool.labels.keys())
  109. return labels