]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
controller.c: Implement ready/health endpoints 3788/head
authorManuel Rüger <manuel@rueg.eu>
Tue, 8 Jun 2021 21:18:21 +0000 (23:18 +0200)
committerManuel Rüger <manuel@rueg.eu>
Tue, 8 Jun 2021 22:12:00 +0000 (00:12 +0200)
These endpoints allow an orchestrator like kubernetes to verify the
status of rspamd (https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/)

Current implementation is very minimal.

The health endpoint allows to verify rspamd internally. It could check
for internal configuration and ensure that rspamd itself is healthy
and available.
The ready endpoint signals that rspamd is ready to receive and process
traffic and thus ensures that configured external components are available.
The readiness check could for example test if configured redis servers or
at least one rspamd upstreams is available.

src/controller.c

index 185970efa435ed6ebf1fed702322453aa2fc4329..e209d9320e7ae307643e32e3aebb996048a7bfb2 100644 (file)
 #define PATH_GET_MAP "/getmap"
 #define PATH_GRAPH "/graph"
 #define PATH_PIE_CHART "/pie"
+#define PATH_HEALTHY "/healthy"
 #define PATH_HISTORY "/history"
 #define PATH_HISTORY_RESET "/historyreset"
 #define PATH_LEARN_SPAM "/learnspam"
 #define PATH_LEARN_HAM "/learnham"
 #define PATH_METRICS "/metrics"
+#define PATH_READY "/ready"
 #define PATH_SAVE_ACTIONS "/saveactions"
 #define PATH_SAVE_SYMBOLS "/savesymbols"
 #define PATH_SAVE_MAP "/savemap"
@@ -1579,6 +1581,46 @@ err:
        rspamd_controller_send_error (conn_ent, 500, "Internal error");
 }
 
+/*
+ * Healthy command handler:
+ * request: /healthy
+ * headers: Password
+ * reply: json {"success":true}
+ */
+static int
+rspamd_controller_handle_healthy (struct rspamd_http_connection_entry *conn_ent,
+        struct rspamd_http_message *msg)
+{
+    struct rspamd_controller_session *session = conn_ent->ud;
+
+    if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
+        return 0;
+    }
+
+    rspamd_controller_send_string (conn_ent, "{\"success\":true}");
+    return 0;
+}
+
+/*
+ * Ready command handler:
+ * request: /ready
+ * headers: Password
+ * reply: json {"success":true} or {"error":"error message"}
+ */
+static int
+rspamd_controller_handle_ready (struct rspamd_http_connection_entry *conn_ent,
+        struct rspamd_http_message *msg)
+{
+    struct rspamd_controller_session *session = conn_ent->ud;
+
+    if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
+        return 0;
+    }
+
+    rspamd_controller_send_string (conn_ent, "{\"success\":true}");
+    return 0;
+}
+
 /*
  * History command handler:
  * request: /history
@@ -3894,6 +3936,12 @@ start_controller_worker (struct rspamd_worker *worker)
        rspamd_http_router_add_path (ctx->http,
                        PATH_GRAPH,
                        rspamd_controller_handle_graph);
+       rspamd_http_router_add_path (ctx->http,
+                       PATH_HEALTHY,
+                       rspamd_controller_handle_healthy);
+       rspamd_http_router_add_path (ctx->http,
+                       PATH_READY,
+                       rspamd_controller_handle_ready);
        rspamd_http_router_add_path (ctx->http,
                        PATH_HISTORY,
                        rspamd_controller_handle_history);