]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proxy: implement "show defaults" master
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Tue, 4 Aug 2026 15:33:24 +0000 (17:33 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 5 Aug 2026 09:38:21 +0000 (11:38 +0200)
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
doc/management.txt
reg-tests/proxy/cli_add_backend.vtc
src/proxy.c

index 3050e155c622993a81f03f7196781f8553542fd1..ec3073e07d593934f2720840db8b2dd1e28f433d 100644 (file)
@@ -4238,7 +4238,7 @@ tune.defaults.purge [<element[,...]>]
   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
index 8c2b8abd5ad03d7eed3a56e1022b906a2d147499..e5bf389a1e1242bfa4d0375d25ecb93fd28458e2 100644 (file)
@@ -1766,7 +1766,9 @@ add backend <name> from <defproxy> [mode <mode>] [guid <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 [<backend>]
   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
index 8bc8b37546d0477c11c3e559e088ffe560ed4262..8afaad4b7881423c3d590ad392f2c5ebf2bca8f1 100644 (file)
@@ -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."
 
index 34a99aaac448d6c36152a29e2c03233fba8cf424..957c31ca81e8f9bc57ff3a137cf3faeddbdc73a6 100644 (file)
@@ -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 <px_watch> 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 <backend>               : mark backend as ready for traffic",                              cli_parse_publish_backend, NULL, NULL },
        { { "set", "maxconn", "frontend",  NULL },          "set maxconn frontend <frontend> <value> : change a frontend's maxconn setting",                            cli_parse_set_maxconn_frontend, NULL },
        { { "show", "default-server", NULL },               "show default-server [<backend>]         : 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 [<backend>]           : 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 [<backend>]          : 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 },