MINOR: mailers: warn if mailers are configured but not actually used

Now that native mailers configuration is only usable with Lua mailers,
Willy noticed that we lack a way to warn the user if mailers were
previously configured on an older version but Lua mailers were not loaded,
which could trick the user into thinking mailers keep working when
transitionning to 3.2 while it is not.

In this patch we add the 'core.use_native_mailers_config()' Lua function
which should be called in Lua script body before making use of
'Proxy:get_mailers()' function to retrieve legacy mailers configuration
from haproxy main config. This way haproxy effectively knows that the
native mailers config is actually being used from Lua (which indicates
user correctly migrated from native mailers to Lua mailers), else if
mailers are configured but not used from Lua then haproxy warns the user
about the fact that they will be ignored unless they are used from Lua.
(e.g.: using the provided 'examples/lua/mailers.lua' to ease transition)
This commit is contained in:
Aurelien DARRAGON 2025-06-27 16:28:48 +02:00
parent c7c6d8d295
commit 837762e2ee
5 changed files with 57 additions and 2 deletions

View File

@ -1089,6 +1089,14 @@ Core class
perform the heavy job in a dedicated task and allow remaining events to be
processed more quickly.
.. js:function:: core.use_native_mailers_config()
**context**: body
Inform haproxy that the script will make use of the native "mailers"
config section (although legacy). In other words, inform haproxy that
:js:func:`Proxy.get_mailers()` will be used later in the program.
.. _proxy_class:
Proxy class
@ -1216,8 +1224,14 @@ Proxy class
**LEGACY**
Returns a table containing mailers config for the current proxy or nil
if mailers are not available for the proxy.
Returns a table containing legacy mailers config (from haproxy configuration
file) for the current proxy or nil if mailers are not available for the proxy.
.. warning::
When relying on :js:func:`Proxy.get_mailers()` to retrieve mailers
configuration, :js:func:`core.use_native_mailers_config()` must be called
first from body or init context to inform haproxy that Lua makes use of the
legacy mailers config.
:param class_proxy px: A :ref:`proxy_class` which indicates the manipulated
proxy.

View File

@ -364,6 +364,10 @@ local function srv_event_add(event, data)
mailers_track_server_events(data.reference)
end
-- tell haproxy that we do use the legacy native "mailers" config section
-- which allows us to retrieve mailers configuration using Proxy:get_mailers()
core.use_native_mailers_config()
-- event subscriptions are purposely performed in an init function to prevent
-- email alerts from being generated too early (when process is starting up)
core.register_init(function()

View File

@ -31,6 +31,7 @@
#include <haproxy/proxy-t.h>
#include <haproxy/server-t.h>
extern int mailers_used_from_lua;
extern struct mailers *mailers;
int init_email_alert(struct mailers *mailers, struct proxy *p, char **err);

View File

@ -86,6 +86,8 @@ static uint hlua_log_opts = HLUA_LOG_LOGGERS_ON | HLUA_LOG_STDERR_AUTO;
/* set to 1 once some lua was loaded already */
static uint8_t hlua_loaded = 0;
/* set to 1 during body evaluation (very early lua file loading), then 0 */
static uint8_t hlua_body = 1;
#define HLUA_BOOL_SAMPLE_CONVERSION_UNK 0x0
#define HLUA_BOOL_SAMPLE_CONVERSION_NORMAL 0x1
@ -2384,6 +2386,20 @@ __LJMP static int hlua_core_get_var(lua_State *L)
return MAY_LJMP(hlua_smp2lua(L, &smp));
}
/* This function informs haproxy that native mailers config section is
* actually being used from Lua.
*
* It may not be used outside of body context.
*/
__LJMP static int hlua_use_native_mailers_config(lua_State *L)
{
if (!hlua_body)
WILL_LJMP(luaL_error(L, "use_native_mailers_config: "
"not available outside of body context"));
mailers_used_from_lua = 1;
return 0;
}
/* A class is a lot of memory that contain data. This data can be a table,
* an integer or user data. This data is associated with a metatable. This
* metatable have an original version registered in the global context with
@ -13811,6 +13827,8 @@ int hlua_post_init()
struct hlua_function *fcn;
struct hlua_reg_filter *reg_flt;
hlua_body = 0;
#if defined(USE_OPENSSL)
/* Initialize SSL server. */
if (socket_ssl->xprt->prepare_srv) {
@ -14108,6 +14126,7 @@ lua_State *hlua_init_state(int thread_num)
hlua_class_function(L, "Warning", hlua_log_warning);
hlua_class_function(L, "Alert", hlua_log_alert);
hlua_class_function(L, "done", hlua_done);
hlua_class_function(L, "use_native_mailers_config", hlua_use_native_mailers_config);
hlua_fcn_reg_core_fcn(L);
lua_setglobal(L, "core");

View File

@ -23,6 +23,7 @@
#include <haproxy/time.h>
#include <haproxy/tools.h>
int mailers_used_from_lua = 0;
struct mailers *mailers = NULL;
@ -48,3 +49,19 @@ void free_email_alert(struct proxy *p)
ha_free(&p->email_alert.to);
ha_free(&p->email_alert.myhostname);
}
static int mailers_post_check(void)
{
struct mailers *cur;
for (cur = mailers; cur != NULL; cur = cur->next) {
if (cur->users && !mailers_used_from_lua) {
ha_warning("mailers '%s' is referenced on at least one proxy but Lua "
"mailers are not configured so the setting will be ignored. "
"Use 'examples/lua/mailers.lua' file for basic mailers support.\n", cur->id);
return ERR_WARN;
}
}
return ERR_NONE;
}
REGISTER_POST_CHECK(mailers_post_check);