From 17ea5a6445392ec721f20b3c3d8bf801a01fb258 Mon Sep 17 00:00:00 2001
From: Markus Scheidgen <markus.scheidgen@gmail.com>
Date: Fri, 1 Feb 2019 13:16:58 +0100
Subject: [PATCH] Added basic liveness/readiness for api+worker

---
 nomad/api/app.py                              |  6 ++++++
 ops/helm/nomad/templates/api-deployment.yaml  | 12 +++++++++++
 .../nomad/templates/worker-deployment.yaml    | 20 +++++++++++++++++++
 tests/test_api.py                             |  5 +++++
 4 files changed, 43 insertions(+)

diff --git a/nomad/api/app.py b/nomad/api/app.py
index 69bc14a224..68daaaef3c 100644
--- a/nomad/api/app.py
+++ b/nomad/api/app.py
@@ -82,6 +82,12 @@ def handle(error: Exception):
     return response
 
 
+@app.route("%s/alive" % base_path)
+def alive():
+    """ Simply endpoint to utilize kubernetes liveness/readiness probing. """
+    return "I am, alive!"
+
+
 def with_logger(func):
     """
     Decorator for endpoint implementations that provides a pre configured logger and
diff --git a/ops/helm/nomad/templates/api-deployment.yaml b/ops/helm/nomad/templates/api-deployment.yaml
index ad7c74d056..bbd3c32265 100644
--- a/ops/helm/nomad/templates/api-deployment.yaml
+++ b/ops/helm/nomad/templates/api-deployment.yaml
@@ -71,6 +71,18 @@ spec:
         - name: NOMAD_COE_REPO_DB_NAME
           value: "{{ .Values.dbname }}"
         command: ["python", "-m", "gunicorn.app.wsgiapp", "-b 0.0.0.0:8000", "nomad.api:app"]
+        livenessProbe:
+          httpGet:
+            path: "{{ .Values.proxy.external.path }}/alive"
+            port: 8000
+          initialDelaySeconds: 15
+          periodSeconds: 15
+        readinessProbe:
+          httpGet:
+            path: "{{ .Values.proxy.external.path }}/alive"
+            port: 8000
+          initialDelaySeconds: 5
+          periodSeconds: 5
       imagePullSecrets:
       - name: {{ .Values.images.secret }}
       imagePullPolicy: always
diff --git a/ops/helm/nomad/templates/worker-deployment.yaml b/ops/helm/nomad/templates/worker-deployment.yaml
index 92dd0546ba..c28b04761a 100644
--- a/ops/helm/nomad/templates/worker-deployment.yaml
+++ b/ops/helm/nomad/templates/worker-deployment.yaml
@@ -59,6 +59,26 @@ spec:
         - name: NOMAD_COE_REPO_DB_NAME
           value: "{{ .Values.dbname }}"
         command: ["python", "-m", "celery", "worker", "-l", "info", "-A", "nomad.processing"]
+        livenessProbe:
+          exec:
+            env:
+            - name: NOMAD_LOGSTASH_LEVEL
+            - value: 30
+            command:
+            - sh -c
+            - celery -A nomad.processing status
+          initialDelaySeconds: 30
+          periodSeconds: 30
+        readinessProbe:
+          exec:
+            env:
+            - name: NOMAD_LOGSTASH_LEVEL
+            - value: 30
+            command:
+            - sh -c
+            - celery -A nomad.processing status
+          initialDelaySeconds: 5
+          periodSeconds: 5
       imagePullSecrets:
       - name: {{ .Values.images.secret }}
       imagePullPolicy: always
diff --git a/tests/test_api.py b/tests/test_api.py
index b593e6797c..0e2a981926 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -56,6 +56,11 @@ def client(mockmongo):
     Upload._get_collection().drop()
 
 
+def test_alive(client):
+    rv = client.get('/alive')
+    assert rv.status_code == 200
+
+
 def create_auth_headers(user):
     basic_auth_str = '%s:password' % user.email
     basic_auth_bytes = basic_auth_str.encode('utf-8')
-- 
GitLab