Repository: spotify/luigi

Issue:
KubernetesJobTask bug: Pod status does not have conditions
I have several dozens of `KubernetesJobTask`s every day. And approximately twice a day this error happens:
```
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/luigi/worker.py", line 203, in run
    new_deps = self._run_get_new_deps()
  File "/opt/conda/lib/python3.6/site-packages/luigi/worker.py", line 140, in _run_get_new_deps
    task_gen = self.task.run()
  File "/usr/src/app/phpipeline/luigi_util.py", line 218, in run
    super(PHKubernetesJobTask, self).run()
  File "/opt/conda/lib/python3.6/site-packages/luigi/contrib/kubernetes.py", line 353, in run
    self.__track_job()
  File "/opt/conda/lib/python3.6/site-packages/luigi/contrib/kubernetes.py", line 199, in __track_job
    while not self.__verify_job_has_started():
  File "/opt/conda/lib/python3.6/site-packages/luigi/contrib/kubernetes.py", line 276, in __verify_job_has_started
    for cond in status['conditions']:
KeyError: 'conditions'
```
Unfortunately, I cannot reproduce it.

But the problem is in the way status conditions are accessed:

- how it's done in `luigi`: `status['conditions']` ([line 276](https://github.com/spotify/luigi/blob/38a1d0c4f95a1900e49b64d77cdb90078adc0416/luigi/contrib/kubernetes.py#L276)).
- how it's done in `pykube`: `status.get('conditions', [])` ([line 329](https://github.com/kelproject/pykube/blob/e8a46298a592ad9037587afb707ac75b3114eff9/pykube/objects.py#L329)).

I guess, it can be easily fixed by using the same way of getting status conditions, as in `pykube`.
KubernetesJobTask bug: Pod status does not have conditions
I have several dozens of `KubernetesJobTask`s every day. And approximately twice a day this error happens:
```
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/luigi/worker.py", line 203, in run
    new_deps = self._run_get_new_deps()
  File "/opt/conda/lib/python3.6/site-packages/luigi/worker.py", line 140, in _run_get_new_deps
    task_gen = self.task.run()
  File "/usr/src/app/phpipeline/luigi_util.py", line 218, in run
    super(PHKubernetesJobTask, self).run()
  File "/opt/conda/lib/python3.6/site-packages/luigi/contrib/kubernetes.py", line 353, in run
    self.__track_job()
  File "/opt/conda/lib/python3.6/site-packages/luigi/contrib/kubernetes.py", line 199, in __track_job
    while not self.__verify_job_has_started():
  File "/opt/conda/lib/python3.6/site-packages/luigi/contrib/kubernetes.py", line 276, in __verify_job_has_started
    for cond in status['conditions']:
KeyError: 'conditions'
```
Unfortunately, I cannot reproduce it.

But the problem is in the way status conditions are accessed:

- how it's done in `luigi`: `status['conditions']` ([line 276](https://github.com/spotify/luigi/blob/38a1d0c4f95a1900e49b64d77cdb90078adc0416/luigi/contrib/kubernetes.py#L276)).
- how it's done in `pykube`: `status.get('conditions', [])` ([line 329](https://github.com/kelproject/pykube/blob/e8a46298a592ad9037587afb707ac75b3114eff9/pykube/objects.py#L329)).

I guess, it can be easily fixed by using the same way of getting status conditions, as in `pykube`.


Discussion:

Resolving patch:
diff --git a/luigi/contrib/kubernetes.py b/luigi/contrib/kubernetes.py
--- a/luigi/contrib/kubernetes.py
+++ b/luigi/contrib/kubernetes.py
@@ -273,7 +273,7 @@ def __verify_job_has_started(self):
                     assert wr == 'ContainerCreating', "Pod %s %s. Logs: `kubectl logs pod/%s`" % (
                         pod.name, wr, pod.name)
 
-            for cond in status['conditions']:
+            for cond in status.get('conditions', []):
                 if 'message' in cond:
                     if cond['reason'] == 'ContainersNotReady':
                         return False


Test patch: