]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: srv: extract tracking server config function
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Tue, 13 Jul 2021 08:35:23 +0000 (10:35 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 16 Jul 2021 08:08:55 +0000 (10:08 +0200)
Extract the post-config tracking setup in a dedicated function
srv_apply_track. This will be useful to implement track support for
dynamic servers.

include/haproxy/server.h
src/cfgparse.c
src/server.c

index b807a8a04c1c165485044e39a97d6917854700e9..c39b0e3c0a46ec339ef3c32cf3ecaa5e69db16ae 100644 (file)
@@ -75,6 +75,8 @@ struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char
 struct task *srv_cleanup_idle_conns(struct task *task, void *ctx, unsigned int state);
 struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state);
 
+int srv_apply_track(struct server *srv, struct proxy *curproxy);
+
 /*
  * Registers the server keyword list <kwl> as a list of valid keywords for next
  * parsing sessions.
index 6bf8e6d59c63d111accff76c28d7c03230e85d6a..5e21467951d634ecca9defa59c03ff1c893ff170 100644 (file)
@@ -3445,72 +3445,10 @@ out_uri_auth_compat:
                                ha_warning("server has tfo activated, the backend should be configured with at least 'conn-failure', 'empty-response' and 'response-timeout' or we wouldn't be able to retry the connection on failure.\n");
 
                        if (newsrv->trackit) {
-                               struct proxy *px;
-                               struct server *srv, *loop;
-                               char *pname, *sname;
-
-                               pname = newsrv->trackit;
-                               sname = strrchr(pname, '/');
-
-                               if (sname)
-                                       *sname++ = '\0';
-                               else {
-                                       sname = pname;
-                                       pname = NULL;
-                               }
-
-                               if (pname) {
-                                       px = proxy_be_by_name(pname);
-                                       if (!px) {
-                                               ha_alert("unable to find required proxy '%s' for tracking.\n",
-                                                        pname);
-                                               cfgerr++;
-                                               goto next_srv;
-                                       }
-                               } else
-                                       px = curproxy;
-
-                               srv = findserver(px, sname);
-                               if (!srv) {
-                                       ha_alert("unable to find required server '%s' for tracking.\n",
-                                                sname);
-                                       cfgerr++;
+                               if (srv_apply_track(newsrv, curproxy)) {
+                                       ++cfgerr;
                                        goto next_srv;
                                }
-
-                               if (!srv->do_check && !srv->do_agent && !srv->track && !srv->trackit) {
-                                       ha_alert("unable to use %s/%s for "
-                                                "tracking as it does not have any check nor agent enabled.\n",
-                                                px->id, srv->id);
-                                       cfgerr++;
-                                       goto next_srv;
-                               }
-
-                               for (loop = srv->track; loop && loop != newsrv; loop = loop->track);
-
-                               if (newsrv == srv || loop) {
-                                       ha_alert("unable to track %s/%s as it "
-                                                "belongs to a tracking chain looping back to %s/%s.\n",
-                                                px->id, srv->id, px->id,
-                                                newsrv == srv ? srv->id : loop->id);
-                                       cfgerr++;
-                                       goto next_srv;
-                               }
-
-                               if (curproxy != px &&
-                                       (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
-                                       ha_alert("unable to use %s/%s for"
-                                                "tracking: disable-on-404 option inconsistency.\n",
-                                                px->id, srv->id);
-                                       cfgerr++;
-                                       goto next_srv;
-                               }
-
-                               newsrv->track = srv;
-                               newsrv->tracknext = srv->trackers;
-                               srv->trackers = newsrv;
-
-                               ha_free(&newsrv->trackit);
                        }
 
                next_srv:
index 073d3beb7558dd74931dea7d12bcad79eac49e63..1ad4c2e6ab1cb0cbd7b6ea71d9142a14360b9136 100644 (file)
@@ -4715,6 +4715,91 @@ static struct cli_kw_list cli_kws = {{ },{
 
 INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
 
+/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
+ * field is used to retrieve the identifier of the tracked server, either with
+ * the format "proxy/server" or just "server". <curproxy> must point to the
+ * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
+ * to find the tracked server.
+ *
+ * Returns 0 if the server track has been activated else non-zero.
+ *
+ * Not thread-safe.
+ */
+int srv_apply_track(struct server *srv, struct proxy *curproxy)
+{
+       struct proxy *px;
+       struct server *strack, *loop;
+       char *pname, *sname;
+
+       if (!srv->trackit)
+               return 1;
+
+       pname = srv->trackit;
+       sname = strrchr(pname, '/');
+
+       if (sname) {
+               *sname++ = '\0';
+       }
+       else {
+               sname = pname;
+               pname = NULL;
+       }
+
+       if (pname) {
+               px = proxy_be_by_name(pname);
+               if (!px) {
+                       ha_alert("unable to find required proxy '%s' for tracking.\n",
+                                pname);
+                       return 1;
+               }
+       }
+       else {
+               px = curproxy;
+       }
+
+       strack = findserver(px, sname);
+       if (!strack) {
+               ha_alert("unable to find required server '%s' for tracking.\n",
+                        sname);
+               return 1;
+       }
+
+       if (!strack->do_check && !strack->do_agent && !strack->track &&
+           !strack->trackit) {
+               ha_alert("unable to use %s/%s for "
+                        "tracking as it does not have any check nor agent enabled.\n",
+                        px->id, strack->id);
+               return 1;
+       }
+
+       for (loop = strack->track; loop && loop != srv; loop = loop->track)
+               ;
+
+       if (srv == strack || loop) {
+               ha_alert("unable to track %s/%s as it "
+                        "belongs to a tracking chain looping back to %s/%s.\n",
+                        px->id, strack->id, px->id,
+                        srv == strack ? strack->id : loop->id);
+               return 1;
+       }
+
+       if (curproxy != px &&
+           (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
+               ha_alert("unable to use %s/%s for"
+                        "tracking: disable-on-404 option inconsistency.\n",
+                        px->id, strack->id);
+               return 1;
+       }
+
+       srv->track = strack;
+       srv->tracknext = strack->trackers;
+       strack->trackers = srv;
+
+       ha_free(&srv->trackit);
+
+       return 0;
+}
+
 /*
  * This function applies server's status changes, it is
  * is designed to be called asynchronously.