MINOR: cfgparse-global: add cfg_parse_global_localpeer

This commit prepares the parsing of localpeer keyword in MODE_DISCOVERY. We
need this, as HAPROXY_LOCALPEER environment variable could be checked in the
configuration in order to enable some backend or frontend settings.

So, let's at first add a dedicated parser for localpeer. At second, we no
longer need to check, if cfg_peers is valid pointer, as in MODE_DISCOVERY we
parse only the "global" section.

In addition, let's make the code of localpeer parser a little bit more
readable.
This commit is contained in:
Valentine Krasnobaeva 2024-11-21 17:25:09 +01:00 committed by Willy Tarreau
parent bfe0f9d02d
commit d253f30823

View file

@ -41,7 +41,7 @@ static const char *common_kw_list[] = {
"description", "node", "unix-bind", "log",
"log-send-hostname", "server-state-base", "server-state-file",
"log-tag", "spread-checks", "max-spread-checks", "cpu-map",
"strict-limits", "localpeer",
"strict-limits",
"numa-cpu-mapping", "defaults", "listen", "frontend", "backend",
"peers", "resolvers", "cluster-secret", "no-quic", "limited-quic",
"stats-file",
@ -798,39 +798,6 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
if (kwm == KWM_NO)
global.tune.options &= ~GTUNE_STRICT_LIMITS;
}
else if (strcmp(args[0], "localpeer") == 0) {
if (alertif_too_many_args(1, file, linenum, args, &err_code))
goto out;
if (*(args[1]) == 0) {
ha_alert("parsing [%s:%d] : '%s' expects a name as an argument.\n",
file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
if (global.localpeer_cmdline != 0) {
ha_warning("parsing [%s:%d] : '%s' ignored since it is already set by using the '-L' "
"command line argument.\n", file, linenum, args[0]);
err_code |= ERR_WARN;
goto out;
}
if (cfg_peers) {
ha_warning("parsing [%s:%d] : '%s' ignored since it is used after 'peers' section.\n",
file, linenum, args[0]);
err_code |= ERR_WARN;
goto out;
}
free(localpeer);
if ((localpeer = strdup(args[1])) == NULL) {
ha_alert("parsing [%s:%d]: cannot allocate memory for '%s'.\n",
file, linenum, args[0]);
err_code |= ERR_ALERT | ERR_FATAL;
goto out;
}
}
else if (strcmp(args[0], "numa-cpu-mapping") == 0) {
global.numa_cpu_mapping = (kwm == KWM_NO) ? 0 : 1;
}
@ -1644,6 +1611,37 @@ static int cfg_parse_global_chroot(char **args, int section_type, struct proxy *
return 0;
}
static int cfg_parse_global_localpeer(char **args, int section_type, struct proxy *curpx,
const struct proxy *defpx, const char *file, int line,
char **err)
{
if (!(global.mode & MODE_DISCOVERY))
return 0;
if (too_many_args(1, args, err, NULL))
return -1;
if (*(args[1]) == 0) {
memprintf(err, "'%s' expects a name as an argument.\n", args[0]);
return -1;
}
if (global.localpeer_cmdline != 0) {
memprintf(err, "'%s' ignored since it is already set by using the '-L' "
"command line argument.\n", args[0]);
return -1;
}
free(localpeer);
localpeer = strdup(args[1]);
if (localpeer == NULL) {
memprintf(err, "cannot allocate memory for '%s'.\n", args[0]);
return -1;
}
return 0;
}
static struct cfg_kw_list cfg_kws = {ILH, {
{ CFG_GLOBAL, "prealloc-fd", cfg_parse_prealloc_fd },
{ CFG_GLOBAL, "force-cfg-parser-pause", cfg_parse_global_parser_pause, KWF_EXPERIMENTAL },
@ -1688,6 +1686,7 @@ static struct cfg_kw_list cfg_kws = {ILH, {
{ CFG_GLOBAL, "resetenv", cfg_parse_global_env_opts, KWF_DISCOVERY },
{ CFG_GLOBAL, "presetenv", cfg_parse_global_env_opts, KWF_DISCOVERY },
{ CFG_GLOBAL, "chroot", cfg_parse_global_chroot },
{ CFG_GLOBAL, "localpeer", cfg_parse_global_localpeer, KWF_DISCOVERY },
{ 0, NULL, NULL },
}};