Browse Source

Merge "wait: Remove test pods from wait"

Zuul 5 months ago
parent
commit
ab8f98b543
3 changed files with 88 additions and 1 deletions
  1. 2
    0
      armada/const.py
  2. 40
    1
      armada/handlers/wait.py
  3. 46
    0
      armada/tests/unit/handlers/test_wait.py

+ 2
- 0
armada/const.py View File

@@ -27,6 +27,8 @@ DEFAULT_CHART_TIMEOUT = 900
27 27
 
28 28
 # Tiller
29 29
 DEFAULT_TILLER_TIMEOUT = 300
30
+HELM_HOOK_ANNOTATION = 'helm.sh/hook'
31
+HELM_TEST_HOOKS = ['test-success', 'test-failure']
30 32
 STATUS_UNKNOWN = 'UNKNOWN'
31 33
 STATUS_DEPLOYED = 'DEPLOYED'
32 34
 STATUS_DELETED = 'DELETED'

+ 40
- 1
armada/handlers/wait.py View File

@@ -148,6 +148,16 @@ class ResourceWait(ABC):
148 148
         '''
149 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 161
     def handle_resource(self, resource):
152 162
         resource_name = resource.metadata.name
153 163
 
@@ -259,7 +269,9 @@ class ResourceWait(ABC):
259 269
 
260 270
         resource_list = self.get_resources(**kwargs)
261 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 275
         if not resource_list.items:
264 276
             if self.skip_if_none_found:
265 277
                 msg = 'Skipping wait, no %s resources found.'
@@ -279,6 +291,11 @@ class ResourceWait(ABC):
279 291
             resource = event['object']
280 292
             resource_name = resource.metadata.name
281 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 299
             msg = ('Watch event: type=%s, name=%s, namespace=%s,'
283 300
                    'resource_version=%s')
284 301
             LOG.debug(msg, event_type, resource_name,
@@ -330,6 +347,28 @@ class PodWait(ResourceWait):
330 347
             resource_type, chart_wait, labels,
331 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 372
     def is_resource_ready(self, resource):
334 373
         pod = resource
335 374
         name = pod.metadata.name

+ 46
- 0
armada/tests/unit/handlers/test_wait.py View File

@@ -185,3 +185,49 @@ class ChartWaitTestCase(base.ArmadaTestCase):
185 185
         self.assertEqual(2, len(unit.waits))
186 186
         for w in unit.waits:
187 187
             w.wait.assert_called_once()
188
+
189
+
190
+class 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))

Loading…
Cancel
Save