]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/mgr_module: make get_mgr_ip() return mgr's IP from mgrmap
authorSage Weil <sage@newdream.net>
Wed, 2 Jun 2021 02:31:11 +0000 (22:31 -0400)
committerSage Weil <sage@newdream.net>
Thu, 3 Jun 2021 16:11:31 +0000 (12:11 -0400)
The previous approach was convoluted: we tried to do a DNS lookup on the
hostname, which would fail if /etc/hosts had an entry.  Which, with podman,
it does.  And the IP it has will vary in all sorts of weird ways.  For
example, CNI on my host means that I get a dynamic address in 10.88.0.0/24.

Avoid all of that nonsense and use the IP that is in the mgrmap.  There
may be multiple IPs (v2 + v1, or maybe even IPv4 + v6 in the future); in
that case, use the first one.

Signed-off-by: Sage Weil <sage@newdream.net>
src/mgr/ActivePyModules.cc
src/pybind/mgr/mgr_module.py

index eb7c81b47e22a3ea6717fb969d45cc9dc388510a..eeb2d8cba587ca83e062440129e0dee519d60e4b 100644 (file)
@@ -425,6 +425,23 @@ PyObject *ActivePyModules::get_python(const std::string &what)
       mgr_map.dump(&f);
       return f.get();
     });
+  } else if (what == "mgr_ips") {
+    return cluster_state.with_mgrmap([&](const MgrMap &mgr_map) {
+      with_gil_t with_gil{no_gil};
+      f.open_array_section("ips");
+      std::set<std::string> did;
+      for (auto& i : server.get_myaddrs().v) {
+       std::string ip = i.ip_only_to_str();
+       if (did.count(ip)) {
+         continue;
+       }
+       did.insert(ip);
+       f.dump_string("ip", ip);
+      }
+      f.close_section();
+      return f.get();
+    });
+
   } else if (what == "have_local_config_map") {
     with_gil_t with_gil{no_gil};
     f.dump_bool("have_local_config_map", have_local_config_map);
index 1cb384c8fae17078f2f081b4db98cae83d29ef0a..0e73b0d879feba865c0b22f0be0d494e82b4c8d9 100644 (file)
@@ -800,19 +800,8 @@ class MgrStandbyModule(ceph_module.BaseMgrStandbyModule, MgrModuleLoggingMixin):
         return self._ceph_get_active_uri()
 
     def get_mgr_ip(self) -> str:
-        hostname = socket.gethostname()
-        try:
-            r = socket.getaddrinfo(hostname, None, flags=socket.AI_CANONNAME,
-                                   type=socket.SOCK_STREAM)
-            # pick first v4 IP, if present, as long as it is not 127.0.{0,1}.1
-            for a in r:
-                if a[4][0] in ['127.0.1.1', '127.0.0.1']:
-                    continue
-                if a[0] == socket.AF_INET:
-                    return a[4][0]
-        except socket.gaierror as e:
-            pass
-        return hostname
+        # we don't have get() for standby modules; make do with the hostname
+        return socket.gethostname()
 
     def get_localized_module_option(self, key: str, default: OptionValue = None) -> OptionValue:
         r = self._ceph_get_module_option(key, self.get_mgr_id())
@@ -1396,19 +1385,10 @@ class MgrModule(ceph_module.BaseMgrModule, MgrModuleLoggingMixin):
         return self._ceph_get_ceph_conf_path()
 
     def get_mgr_ip(self) -> str:
-        hostname = socket.gethostname()
-        try:
-            r = socket.getaddrinfo(hostname, None, flags=socket.AI_CANONNAME,
-                                   type=socket.SOCK_STREAM)
-            # pick first v4 IP, if present, as long as it is not 127.0.{0,1}.1
-            for a in r:
-                if a[4][0] in ['127.0.1.1', '127.0.0.1']:
-                    continue
-                if a[0] == socket.AF_INET:
-                    return a[4][0]
-        except socket.gaierror as e:
-            pass
-        return hostname
+        ips = self.get("mgr_ips").get('ips', [])
+        if not ips:
+            return socket.gethostname()
+        return ips[0]
 
     def get_ceph_option(self, key: str) -> OptionValue:
         return self._ceph_get_option(key)