summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-11-07 17:50:13 +0000
committerGerrit Code Review <review@openstack.org>2018-11-07 17:50:14 +0000
commitab8f98b5432c48411d5016091c7cba7ca7603f44 (patch)
treefccfcc454423a9e67a37321cedcbbe636f04f321
parentd35896537b998fe9771f115c6489a81a8ae77d14 (diff)
parent1a28e6b72fe7d330ff2ebde2fc4afacd35bf4a3b (diff)
Merge "wait: Remove test pods from wait"
-rw-r--r--armada/const.py2
-rw-r--r--armada/handlers/wait.py41
-rw-r--r--armada/tests/unit/handlers/test_wait.py46
3 files changed, 88 insertions, 1 deletions
diff --git a/armada/const.py b/armada/const.py
index e253881..30d3149 100644
--- a/armada/const.py
+++ b/armada/const.py
@@ -27,6 +27,8 @@ DEFAULT_CHART_TIMEOUT = 900
27 27
28# Tiller 28# Tiller
29DEFAULT_TILLER_TIMEOUT = 300 29DEFAULT_TILLER_TIMEOUT = 300
30HELM_HOOK_ANNOTATION = 'helm.sh/hook'
31HELM_TEST_HOOKS = ['test-success', 'test-failure']
30STATUS_UNKNOWN = 'UNKNOWN' 32STATUS_UNKNOWN = 'UNKNOWN'
31STATUS_DEPLOYED = 'DEPLOYED' 33STATUS_DEPLOYED = 'DEPLOYED'
32STATUS_DELETED = 'DELETED' 34STATUS_DELETED = 'DELETED'
diff --git a/armada/handlers/wait.py b/armada/handlers/wait.py
index a131265..7a27945 100644
--- a/armada/handlers/wait.py
+++ b/armada/handlers/wait.py
@@ -148,6 +148,16 @@ class ResourceWait(ABC):
148 ''' 148 '''
149 pass 149 pass
150 150
151 def include_resource(self, resource):
152 '''
153 Test to include or exclude a resource in a wait operation. This method
154 can be used to exclude resources that should not be included in wait
155 operations (e.g. test pods).
156 :param resource: resource to test
157 :returns: boolean representing test result
158 '''
159 return True
160
151 def handle_resource(self, resource): 161 def handle_resource(self, resource):
152 resource_name = resource.metadata.name 162 resource_name = resource.metadata.name
153 163
@@ -259,7 +269,9 @@ class ResourceWait(ABC):
259 269
260 resource_list = self.get_resources(**kwargs) 270 resource_list = self.get_resources(**kwargs)
261 for resource in resource_list.items: 271 for resource in resource_list.items:
262 ready[resource.metadata.name] = self.handle_resource(resource) 272 # Only include resources that should be included in wait ops
273 if self.include_resource(resource):
274 ready[resource.metadata.name] = self.handle_resource(resource)
263 if not resource_list.items: 275 if not resource_list.items:
264 if self.skip_if_none_found: 276 if self.skip_if_none_found:
265 msg = 'Skipping wait, no %s resources found.' 277 msg = 'Skipping wait, no %s resources found.'
@@ -279,6 +291,11 @@ class ResourceWait(ABC):
279 resource = event['object'] 291 resource = event['object']
280 resource_name = resource.metadata.name 292 resource_name = resource.metadata.name
281 resource_version = resource.metadata.resource_version 293 resource_version = resource.metadata.resource_version
294
295 # Skip resources that should be excluded from wait operations
296 if not self.include_resource(resource):
297 continue
298
282 msg = ('Watch event: type=%s, name=%s, namespace=%s,' 299 msg = ('Watch event: type=%s, name=%s, namespace=%s,'
283 'resource_version=%s') 300 'resource_version=%s')
284 LOG.debug(msg, event_type, resource_name, 301 LOG.debug(msg, event_type, resource_name,
@@ -330,6 +347,28 @@ class PodWait(ResourceWait):
330 resource_type, chart_wait, labels, 347 resource_type, chart_wait, labels,
331 chart_wait.k8s.client.list_namespaced_pod, **kwargs) 348 chart_wait.k8s.client.list_namespaced_pod, **kwargs)
332 349
350 def include_resource(self, resource):
351 pod = resource
352 annotations = pod.metadata.annotations
353
354 # Retrieve pod's Helm test hooks
355 test_hooks = None
356 if annotations:
357 hook_string = annotations.get(const.HELM_HOOK_ANNOTATION)
358 if hook_string:
359 hooks = hook_string.split(',')
360 test_hooks = [h for h in hooks if h in const.HELM_TEST_HOOKS]
361
362 # NOTE(drewwalters96): Test pods may cause wait operations to fail
363 # when old resources remain from previous upgrades/tests. Indicate that
364 # test pods should not be included in wait operations.
365 if test_hooks:
366 LOG.debug('Pod %s will be skipped during wait operations.',
367 pod.metadata.name)
368 return False
369 else:
370 return True
371
333 def is_resource_ready(self, resource): 372 def is_resource_ready(self, resource):
334 pod = resource 373 pod = resource
335 name = pod.metadata.name 374 name = pod.metadata.name
diff --git a/armada/tests/unit/handlers/test_wait.py b/armada/tests/unit/handlers/test_wait.py
index ed074f1..cc2db38 100644
--- a/armada/tests/unit/handlers/test_wait.py
+++ b/armada/tests/unit/handlers/test_wait.py
@@ -185,3 +185,49 @@ class ChartWaitTestCase(base.ArmadaTestCase):
185 self.assertEqual(2, len(unit.waits)) 185 self.assertEqual(2, len(unit.waits))
186 for w in unit.waits: 186 for w in unit.waits:
187 w.wait.assert_called_once() 187 w.wait.assert_called_once()
188
189
190class PodWaitTestCase(base.ArmadaTestCase):
191
192 def get_unit(self, labels):
193 return wait.PodWait(
194 resource_type='pod', chart_wait=mock.MagicMock(), labels=labels)
195
196 def test_include_resource(self):
197
198 def mock_resource(annotations):
199 resource = mock.Mock()
200 resource.metadata.annotations = annotations
201 return resource
202
203 test_resources = [
204 mock_resource({
205 'key': 'value',
206 'helm.sh/hook': 'test-success'
207 }),
208 mock_resource({
209 'helm.sh/hook': 'test-failure'
210 }),
211 mock_resource({
212 'helm.sh/hook': 'test-success,pre-install'
213 })
214 ]
215 non_test_resources = [
216 mock_resource({
217 'helm.sh/hook': 'pre-install'
218 }),
219 mock_resource({
220 'key': 'value'
221 }),
222 mock_resource({})
223 ]
224
225 unit = self.get_unit({})
226
227 # Validate test resources excluded
228 for resource in test_resources:
229 self.assertFalse(unit.include_resource(resource))
230
231 # Validate other resources included
232 for resource in non_test_resources:
233 self.assertTrue(unit.include_resource(resource))