From: Adam King Date: Thu, 29 Feb 2024 18:04:43 +0000 (-0500) Subject: mgr/cephadm: allow having prometheus bind on specific network X-Git-Tag: v19.1.0~89^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=74364710acca719bbff77db27195a57fe6a3fd71;p=ceph.git mgr/cephadm: allow having prometheus bind on specific network To avoid port conflicts in certain setups where something else is using the same port on a different network on the host. Signed-off-by: Adam King (cherry picked from commit e6943dd61374ee8ce337751e9ef9ba55f10a83ed) --- diff --git a/src/cephadm/cephadmlib/daemons/monitoring.py b/src/cephadm/cephadmlib/daemons/monitoring.py index e0667853dd7b5..e6ad2412a7cba 100644 --- a/src/cephadm/cephadmlib/daemons/monitoring.py +++ b/src/cephadm/cephadmlib/daemons/monitoring.py @@ -251,9 +251,11 @@ class Monitoring(ContainerDaemonForm): ip = meta['ip'] if 'ports' in meta and meta['ports']: port = meta['ports'][0] - r += [f'--web.listen-address={ip}:{port}'] if daemon_type == 'prometheus': config = fetch_configs(ctx) + ip_to_bind_to = config.get('ip_to_bind_to', '') + if ip_to_bind_to: + ip = ip_to_bind_to retention_time = config.get('retention_time', '15d') retention_size = config.get( 'retention_size', '0' @@ -270,6 +272,7 @@ class Monitoring(ContainerDaemonForm): addr = next(iter(ipv4_addrs or ipv6_addrs), None) host = wrap_ipv6(addr) if addr else host r += [f'--web.external-url={scheme}://{host}:{port}'] + r += [f'--web.listen-address={ip}:{port}'] if daemon_type == 'alertmanager': config = fetch_configs(ctx) peers = config.get('peers', list()) # type: ignore diff --git a/src/cephadm/tests/test_deploy.py b/src/cephadm/tests/test_deploy.py index cfde3fbce0aab..9a6da3aad4b95 100644 --- a/src/cephadm/tests/test_deploy.py +++ b/src/cephadm/tests/test_deploy.py @@ -305,6 +305,7 @@ def test_deploy_a_monitoring_container(cephadm_fs, funkypatch): 'files': { 'prometheus.yml': 'bettercallherc', }, + 'ip_to_bind_to': '1.2.3.4' } _cephadm._common_deploy(ctx) @@ -314,7 +315,7 @@ def test_deploy_a_monitoring_container(cephadm_fs, funkypatch): runfile_lines = f.read().splitlines() assert 'podman' in runfile_lines[-1] assert runfile_lines[-1].endswith( - 'quay.io/titans/prometheus:latest --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --web.listen-address=:9095 --storage.tsdb.retention.time=15d --storage.tsdb.retention.size=0 --web.external-url=http://10.10.10.10:9095' + 'quay.io/titans/prometheus:latest --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --storage.tsdb.retention.time=15d --storage.tsdb.retention.size=0 --web.external-url=http://10.10.10.10:9095 --web.listen-address=1.2.3.4:9095' ) assert '--user 8765' in runfile_lines[-1] assert f'-v /var/lib/ceph/{fsid}/prometheus.fire/etc/prometheus:/etc/prometheus:Z' in runfile_lines[-1] diff --git a/src/pybind/mgr/cephadm/services/monitoring.py b/src/pybind/mgr/cephadm/services/monitoring.py index 9aa503b5bcefa..cc8ebd1d628ed 100644 --- a/src/pybind/mgr/cephadm/services/monitoring.py +++ b/src/pybind/mgr/cephadm/services/monitoring.py @@ -431,6 +431,13 @@ class PrometheusService(CephadmService): 'ceph_exporter_sd_url': ceph_exporter_sd_url } + ip_to_bind_to = '' + if spec.only_bind_port_on_networks and spec.networks: + assert daemon_spec.host is not None + ip_to_bind_to = self.mgr.get_first_matching_network_ip(daemon_spec.host, spec) or '' + if ip_to_bind_to: + daemon_spec.port_ips = {str(port): ip_to_bind_to} + web_context = { 'prometheus_web_user': prometheus_user, 'prometheus_web_password': password_hash(prometheus_password), @@ -457,6 +464,7 @@ class PrometheusService(CephadmService): }, 'retention_time': retention_time, 'retention_size': retention_size, + 'ip_to_bind_to': ip_to_bind_to, 'web_config': '/etc/prometheus/web.yml' } else: @@ -465,7 +473,8 @@ class PrometheusService(CephadmService): 'prometheus.yml': self.mgr.template.render('services/prometheus/prometheus.yml.j2', context) }, 'retention_time': retention_time, - 'retention_size': retention_size + 'retention_size': retention_size, + 'ip_to_bind_to': ip_to_bind_to } # include alerts, if present in the container diff --git a/src/pybind/mgr/cephadm/tests/test_services.py b/src/pybind/mgr/cephadm/tests/test_services.py index dbab022058d42..73e9135338453 100644 --- a/src/pybind/mgr/cephadm/tests/test_services.py +++ b/src/pybind/mgr/cephadm/tests/test_services.py @@ -682,7 +682,9 @@ class TestMonitoring: keepalived_password='12345', virtual_ip="1.2.3.4/32", backend_service='rgw.foo')) as _, \ - with_service(cephadm_module, PrometheusSpec('prometheus')) as _: + with_service(cephadm_module, PrometheusSpec('prometheus', + networks=['1.2.3.0/24'], + only_bind_port_on_networks=True)) as _: y = dedent(""" # This file is generated by cephadm. @@ -730,11 +732,12 @@ class TestMonitoring: "deploy_arguments": [], "params": { 'tcp_ports': [9095], + 'port_ips': {'8765': '1.2.3.1'} }, "meta": { 'service_name': 'prometheus', 'ports': [9095], - 'ip': None, + 'ip': '1.2.3.1', 'deployed_by': [], 'rank': None, 'rank_generation': None, @@ -748,6 +751,7 @@ class TestMonitoring: }, 'retention_time': '15d', 'retention_size': '0', + 'ip_to_bind_to': '1.2.3.1', }, }), ) @@ -909,6 +913,7 @@ class TestMonitoring: }, 'retention_time': '15d', 'retention_size': '0', + 'ip_to_bind_to': '', 'web_config': '/etc/prometheus/web.yml', }, }), diff --git a/src/python-common/ceph/deployment/service_spec.py b/src/python-common/ceph/deployment/service_spec.py index 6808567500f81..d5a59c9b12158 100644 --- a/src/python-common/ceph/deployment/service_spec.py +++ b/src/python-common/ceph/deployment/service_spec.py @@ -1885,6 +1885,7 @@ class PrometheusSpec(MonitoringSpec): preview_only: bool = False, config: Optional[Dict[str, str]] = None, networks: Optional[List[str]] = None, + only_bind_port_on_networks: bool = False, port: Optional[int] = None, retention_time: Optional[str] = None, retention_size: Optional[str] = None, @@ -1902,6 +1903,7 @@ class PrometheusSpec(MonitoringSpec): self.retention_time = retention_time.strip() if retention_time else None self.retention_size = retention_size.strip() if retention_size else None + self.only_bind_port_on_networks = only_bind_port_on_networks def validate(self) -> None: super(PrometheusSpec, self).validate()