From 4ffd90c2f00afe56354d019ff00ad1574f4d8682 Mon Sep 17 00:00:00 2001 From: Amaury Denoyelle Date: Tue, 4 Aug 2026 17:33:24 +0200 Subject: [PATCH] MINOR: proxy: implement "show defaults" Implement a new command "show defaults" whose purpose is to list the existing named defaults section. This may be needed when adding a new backend via the CLI. For now, only the names of the defaults instances are listed. In the future, it could be useful to have at least some details about their configuration. Command does not take any argument and is implemented via a single io_handler. No need for a watcher as iteration is performed over defaults instances which cannot be removed at runtime. --- doc/configuration.txt | 5 +++-- doc/management.txt | 9 ++++++++- reg-tests/proxy/cli_add_backend.vtc | 3 +++ src/proxy.c | 31 ++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index 3050e155c..ec3073e07 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -4238,7 +4238,7 @@ tune.defaults.purge [] Default-server instances are kept in memory after parsing as they may be reused for dynamic servers added at runtime. Similarly, named defaults proxies sections are preserved as dynamic backends rely on them. The command - "show default-server" may be used to list them. + "show default-server" and "show defaults" may be used to list them. This may consume a noticeable amount of memory if the number of default instances is important. It's possible to reclaim this memory after the @@ -5939,7 +5939,8 @@ timeouts) but can quickly become confusing to follow. By default, named defaults sections are preserved after configuration parsing. This allows to reuse them for dynamic backends creation. This behavior can be -changed globally via "tune.defaults.purge" keyword. +changed globally via "tune.defaults.purge" keyword. It is possible to list the +preserved instances via the command "show defaults". All proxy names must be formed from upper and lower case letters, digits, '-' (dash), '_' (underscore) , '.' (dot) and ':' (colon). ACL names are diff --git a/doc/management.txt b/doc/management.txt index 8c2b8abd5..e5bf389a1 100644 --- a/doc/management.txt +++ b/doc/management.txt @@ -1766,7 +1766,9 @@ add backend from [mode ] [guid ] All named default proxies can be used, given that they validate the same inheritance rules applied during configuration parsing. There is some - exceptions though, for example when the mode is neither TCP nor HTTP. + exceptions though, for example when the mode is neither TCP nor HTTP. The + available default proxy instances can be listed via the "show defaults" + additional command. This command is restricted and can only be issued on sockets configured for level "admin". @@ -3071,6 +3073,11 @@ show default-server [] configuration parsing. These instances can be referenced via the server keyword from during "add server". +show defaults + Dump the list of named defaults proxies instances still present after + configuration parsing. These instances can be reused with "add backend" + command to serve as configuration for the newly created proxy. + show dev This command is meant to centralize some information that HAProxy developers might need to better understand the causes of a given problem. It generally diff --git a/reg-tests/proxy/cli_add_backend.vtc b/reg-tests/proxy/cli_add_backend.vtc index 8bc8b3754..8afaad4b7 100644 --- a/reg-tests/proxy/cli_add_backend.vtc +++ b/reg-tests/proxy/cli_add_backend.vtc @@ -59,6 +59,9 @@ haproxy h1 -cli { send "add backend be2 from def mode http" expect ~ "New backend registered." + send "show defaults" + expect ~ "def\ndef_http" + send "add backend be from def_http" expect ~ "New backend registered." diff --git a/src/proxy.c b/src/proxy.c index 34a99aaac..957c31ca8 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -92,7 +92,7 @@ unsigned int error_snapshot_id = 0; /* global ID assigned to each error then unsigned int dynpx_next_id = 0; /* lowest ID assigned to dynamic proxies */ -/* CLI context used during "show backend" and "show default-server" */ +/* CLI context used during "show backend" and "show default-server/defaults" */ struct show_be_ctx { struct proxy *px; struct watcher px_watch; /* watcher to automatically update px pointer on backend deletion */ @@ -4493,6 +4493,34 @@ static int cli_io_handler_show_default_server(struct appctx *appctx) return 1; } +/* Handler for "show defaults" command. */ +static int cli_io_handler_show_defaults(struct appctx *appctx) +{ + struct show_be_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); + + if (!ctx->px) { + /* No need to use ctx as defaults proxies cannot be removed at runtime. */ + ctx->px = !LIST_ISEMPTY(&defaults_list) ? + LIST_ELEM(defaults_list.n, struct proxy *, el) : NULL; + } + + while (ctx->px) { + chunk_reset(&trash); + chunk_appendf(&trash, "%s\n", ctx->px->id); + + if (STRESS_RUN1(applet_putchk_stress(appctx, &trash) == -1, + applet_putchk(appctx, &trash) == -1)) { + return 0; + } + + if (ctx->px->el.n == &defaults_list) + break; + ctx->px = LIST_ELEM(ctx->px->el.n, struct proxy *, el); + } + + return 1; +} + /* parse a "show servers [state|conn]" CLI line, returns 0 if it wants to start * the dump or 1 if it stops immediately. If an argument is specified, it will * reserve a show_srv_ctx context and set the proxy pointer into ->px, its ID @@ -5523,6 +5551,7 @@ static struct cli_kw_list cli_kws = {{ },{ { { "publish", "backend", NULL }, "publish backend : mark backend as ready for traffic", cli_parse_publish_backend, NULL, NULL }, { { "set", "maxconn", "frontend", NULL }, "set maxconn frontend : change a frontend's maxconn setting", cli_parse_set_maxconn_frontend, NULL }, { { "show", "default-server", NULL }, "show default-server [] : list default-server instances in all or a single backend", cli_parse_show_default_server, cli_io_handler_show_default_server, }, + { { "show", "defaults", NULL }, "show defaults : list all proxies defaults sections", NULL, cli_io_handler_show_defaults }, { { "show","servers", "conn", NULL }, "show servers conn [] : dump server connections status (all or for a single backend)", cli_parse_show_servers, cli_io_handler_servers_state }, { { "show","servers", "state", NULL }, "show servers state [] : dump volatile server information (all or for a single backend)", cli_parse_show_servers, cli_io_handler_servers_state }, { { "show", "backend", NULL }, "show backend : list backends in the current running config", NULL, cli_io_handler_show_backend }, -- 2.47.3