]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: backend: add function to check support for dynamic servers
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 6 Feb 2026 06:40:59 +0000 (07:40 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 6 Feb 2026 13:35:19 +0000 (14:35 +0100)
Move backend compatibility checks performed during 'add server' in a
dedicated function be_supports_dynamic_srv(). This should simplify
addition of future restriction.

This function will be reused when implementing backend creation at
runtime.

include/haproxy/backend.h
reg-tests/server/cli_add_server.vtc
src/backend.c
src/server.c

index 12027073076d6c385d9339e0d4813c7a71a56d0d..6930ff256d3f5b7533dcf29a00e7ed94f389792b 100644 (file)
@@ -69,6 +69,7 @@ int backend_parse_balance(const char **args, char **err, struct proxy *curproxy)
 int tcp_persist_rdp_cookie(struct stream *s, struct channel *req, int an_bit);
 
 int be_downtime(struct proxy *px);
+int be_supports_dynamic_srv(struct proxy *px, char **msg);
 void recount_servers(struct proxy *px);
 void update_backend_weight(struct proxy *px);
 
index 8081312ba901f8b0c88c60464acb834a801fc4e2..93331a2bf7416e7fb981c8be147516aeb133ad9d 100644 (file)
@@ -51,7 +51,7 @@ haproxy h1 -cli {
 
        # invalid load-balancing algo
        send "add server other/s1 ${s1_addr}:${s1_port}"
-       expect ~ "Backend must use a dynamic load balancing to support dynamic servers."
+       expect ~ "backend 'other' uses a non dynamic load balancing method"
 
        # invalid mux proto
        send "add server other2/s1 ${s1_addr}:${s1_port} proto h2"
index 73b39306b9bac917c57cdf25297f5ffa8b777286..b531843fc276107b042068c4ea65bdde0887e654 100644 (file)
@@ -59,6 +59,7 @@
 #include <haproxy/task.h>
 #include <haproxy/ticks.h>
 #include <haproxy/time.h>
+#include <haproxy/tools.h>
 #include <haproxy/trace.h>
 
 #define TRACE_SOURCE &trace_strm
@@ -3055,6 +3056,27 @@ int be_downtime(struct proxy *px) {
        return ns_to_sec(now_ns) - px->last_change + px->down_time;
 }
 
+/* Checks if <px> backend supports the addition of servers at runtime. Either a
+ * backend or a defaults proxy are supported. If proxy is incompatible, <msg>
+ * will be allocated to contain a textual explaination.
+ */
+int be_supports_dynamic_srv(struct proxy *px, char **msg)
+{
+       if (px->lbprm.algo && !(px->lbprm.algo & BE_LB_PROP_DYN)) {
+               memprintf(msg, "%s '%s' uses a non dynamic load balancing method",
+                         proxy_cap_str(px->cap), px->id);
+               return 0;
+       }
+
+       if (px->mode == PR_MODE_SYSLOG) {
+               memprintf(msg, "%s '%s' uses mode log",
+                         proxy_cap_str(px->cap), px->id);
+               return 0;
+       }
+
+       return 1;
+}
+
 /*
  * This function returns a string containing the balancing
  * mode of the proxy in a format suitable for stats.
index d1bb43825cd7abfbd10eceddd3be1ac3be950499..a475c881bbdb238b975bba93f637d826545f5ff9 100644 (file)
@@ -6104,7 +6104,7 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
        struct add_srv_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
        struct proxy *be;
        struct server *srv;
-       char *be_name, *sv_name;
+       char *be_name, *sv_name, *errmsg;
        int errcode, argc;
        int next_id;
        const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
@@ -6140,13 +6140,9 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
        if (!be)
                return cli_err(appctx, "No such backend.\n");
 
-       if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
-               cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.\n");
-               return 1;
-       }
-
-       if (be->mode == PR_MODE_SYSLOG) {
-               cli_err(appctx," Dynamic servers cannot be used with log backends.\n");
+       errmsg = NULL;
+       if (!be_supports_dynamic_srv(be, &errmsg)) {
+               cli_dynerr(appctx, memprintf(&errmsg, "Backend does not support dynamic servers : %s.\n", errmsg));
                return 1;
        }