From: Sage Weil Date: Wed, 2 Jun 2021 02:31:11 +0000 (-0400) Subject: pybind/mgr/mgr_module: make get_mgr_ip() return mgr's IP from mgrmap X-Git-Tag: v17.1.0~1755^2~1 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=72d72fc5db33110effdbfcdfd19e086d4dd831be;p=ceph-ci.git pybind/mgr/mgr_module: make get_mgr_ip() return mgr's IP from mgrmap 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 --- diff --git a/src/mgr/ActivePyModules.cc b/src/mgr/ActivePyModules.cc index eb7c81b47e2..eeb2d8cba58 100644 --- a/src/mgr/ActivePyModules.cc +++ b/src/mgr/ActivePyModules.cc @@ -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 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); diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 1cb384c8fae..0e73b0d879f 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -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)