BUG/MINOR: server: Set server without addr but with dns in RMAINT on startup
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 26 Oct 2020 09:31:17 +0000 (10:31 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 3 Nov 2020 15:18:47 +0000 (16:18 +0100)
On startup, if a server has no address but the dns resolutions are configured,
"none" method is added to the default init-addr methods, in addition to "last"
and "libc". Thus on startup, this server is set to RMAINT mode if no address is
found. It is only performed if no other init-addr method is configured.

Setting the RMAINT mode on startup is important to inhibit the health checks.

For instance, following servers will now be set to RMAINT mode on startup :

  server srv nofound.tld:80 check resolvers mydns
  server srv _http._tcp.service.local check resolvers mydns
  server-template srv 1-3 _http._tcp.service.local check resolvers mydns

while followings ones will trigger an error :

  server srv nofound.tld:80 check
  server srv nofound.tld:80 check resolvers mydns init-addr libc
  server srv _http._tcp.service.local check
  server srv _http._tcp.service.local check resolvers mydns init-addr libc
  server-template srv 1-3 _http._tcp.service.local check resolvers mydns init-addr libc

This patch must be backported as far as 1.8.

(cherry picked from commit ac1c60fd9c63dd9ac4bab7e9a9f24fb347a5fbdf)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 13704ca2d0de919935c91246ce582d7f22160051)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

src/server.c

index 635c977..0ee5a50 100644 (file)
@@ -4335,14 +4335,24 @@ static int srv_apply_lastaddr(struct server *srv, int *err_code)
 /* returns 0 if no error, otherwise a combination of ERR_* flags */
 static int srv_iterate_initaddr(struct server *srv)
 {
+       char *name = srv->hostname;
        int return_code = 0;
        int err_code;
        unsigned int methods;
 
+       /* If no addr and no hostname set, get the name from the DNS SRV request */
+       if (!name && srv->srvrq)
+               name = srv->srvrq->name;
+
        methods = srv->init_addr_methods;
-       if (!methods) { // default to "last,libc"
+       if (!methods) {
+               /* otherwise default to "last,libc" */
                srv_append_initaddr(&methods, SRV_IADDR_LAST);
                srv_append_initaddr(&methods, SRV_IADDR_LIBC);
+               if (srv->resolvers_id) {
+                       /* dns resolution is configured, add "none" to not fail on startup */
+                       srv_append_initaddr(&methods, SRV_IADDR_NONE);
+               }
        }
 
        /* "-dr" : always append "none" so that server addresses resolution
@@ -4375,7 +4385,7 @@ static int srv_iterate_initaddr(struct server *srv)
                        srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
                        if (return_code) {
                                ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
-                                          srv->conf.file, srv->conf.line, srv->id, srv->hostname);
+                                          srv->conf.file, srv->conf.line, srv->id, name);
                        }
                        return return_code;
 
@@ -4383,7 +4393,7 @@ static int srv_iterate_initaddr(struct server *srv)
                        ipcpy(&srv->init_addr, &srv->addr);
                        if (return_code) {
                                ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
-                                          srv->conf.file, srv->conf.line, srv->id, srv->hostname);
+                                          srv->conf.file, srv->conf.line, srv->id, name);
                        }
                        goto out;
 
@@ -4394,11 +4404,11 @@ static int srv_iterate_initaddr(struct server *srv)
 
        if (!return_code) {
                ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
-                     srv->conf.file, srv->conf.line, srv->id, srv->hostname);
+                     srv->conf.file, srv->conf.line, srv->id, name);
        }
        else {
                ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
-                     srv->conf.file, srv->conf.line, srv->id, srv->hostname);
+                     srv->conf.file, srv->conf.line, srv->id, name);
        }
 
        return_code |= ERR_ALERT | ERR_FATAL;
@@ -4430,7 +4440,7 @@ int srv_init_addr(void)
                        goto srv_init_addr_next;
 
                for (srv = curproxy->srv; srv; srv = srv->next)
-                       if (srv->hostname)
+                       if (srv->hostname || srv->srvrq)
                                return_code |= srv_iterate_initaddr(srv);
 
  srv_init_addr_next: