Merge ~sudeephb/charm-telegraf:bug/2008436 into charm-telegraf:master

Proposed by Sudeep Bhandari
Status: Merged
Approved by: Eric Chen
Approved revision: 50146bdc3ecf70bf1f069ffa740a513e9da3676b
Merged at revision: 58d7c9cc1bf6d181515b26b7c907b7748e855507
Proposed branch: ~sudeephb/charm-telegraf:bug/2008436
Merge into: charm-telegraf:master
Diff against target: 110 lines (+45/-6)
3 files modified
src/files/telegraf_exec_metrics.py (+0/-1)
src/reactive/telegraf.py (+4/-4)
src/tests/unit/test_telegraf.py (+41/-1)
Reviewer Review Type Date Requested Status
Martin Kalcok (community) Approve
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Robert Gildein Approve
Eric Chen Approve
Tianqi Xiao (community) Needs Fixing
BootStack Reviewers Pending
Peter Jose De Sousa Pending
Review via email: mp+438095@code.launchpad.net

Commit message

Handle prometheus relation when tls not available

Fixes bug: #2008436

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Martin Kalcok (martin-kalcok) wrote :

I have one inline comment and I think it'd be best to add unit tests as well. At least:

  * one that would test that the situation from LP#2008436 is resolved
  * one that would test that change in certificates relation does not try to reconfigure prometheus relation if it does not exist.

review: Needs Fixing
Revision history for this message
Eric Chen (eric-chen) wrote :

Please check my inline question.

review: Needs Information
Revision history for this message
Sudeep Bhandari (sudeephb) :
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Eric Chen (eric-chen) wrote :

inline message again

review: Needs Information
Revision history for this message
Robert Gildein (rgildein) :
Revision history for this message
Tianqi Xiao (txiao) wrote :

Thanks for the PR. I have 1 comment in-line

review: Needs Fixing
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Eric Chen (eric-chen) :
review: Approve
Revision history for this message
Tianqi Xiao (txiao) wrote :

Overall LGTM. Just one suggestion inline

Revision history for this message
Robert Gildein (rgildein) wrote :

LGTM

review: Approve
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Martin Kalcok (martin-kalcok) :
review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 58d7c9cc1bf6d181515b26b7c907b7748e855507

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/files/telegraf_exec_metrics.py b/src/files/telegraf_exec_metrics.py
2index ec71bb9..a073239 100755
3--- a/src/files/telegraf_exec_metrics.py
4+++ b/src/files/telegraf_exec_metrics.py
5@@ -258,7 +258,6 @@ class ZoneinfoFileMetric(FileMetric):
6
7
8 class CmdMetric(FileMetric):
9-
10 cmd = []
11
12 def get_cmd_output(self, cmd, is_json=False):
13diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py
14index 898c8e9..b335f62 100644
15--- a/src/reactive/telegraf.py
16+++ b/src/reactive/telegraf.py
17@@ -1702,7 +1702,6 @@ def generate_prometheus_output_config(
18 options_ip_range = options.pop("ip_range", [])
19 ip_range = options_ip_range + prometheus_ip_range
20 if listen is not None:
21-
22 return {
23 "listen": listen,
24 "ip_range": ip_range,
25@@ -1807,13 +1806,15 @@ def render_prometheus_client_config(port, address, ip_range):
26 "certificates.available",
27 "tls_client.certs.changed",
28 "tls_client.server.certs.changed",
29+ "endpoint.prometheus-client.changed",
30 )
31-@when("endpoint.prometheus-client.changed")
32-def configure_prometheus_client_with_relation(prometheus):
33+@when("prometheus-client.available")
34+def configure_prometheus_client_with_relation(prometheus=None):
35 hookenv.log(
36 "Configuring prometheus_client output plugin, with prometheus-client relation",
37 level=hookenv.DEBUG,
38 )
39+ prometheus = prometheus or endpoint_from_flag("prometheus-client.available")
40 port, address = get_prometheus_bindaddress()
41 hookenv.log("get_prometheus_bindaddress: {}:{}".format(address, str(port)))
42 # We'll iterate through the prometheus-client relation counterparts,
43@@ -2217,7 +2218,6 @@ def configure_tls():
44 @when("endpoint.certificates.departed")
45 @when_not("certificates.available")
46 def cleanup_tls():
47-
48 if TLS_CERT.exists() and TLS_KEY.exists():
49 TLS_CERT.unlink()
50 TLS_KEY.unlink()
51diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py
52index 37a98ca..5f59a91 100644
53--- a/src/tests/unit/test_telegraf.py
54+++ b/src/tests/unit/test_telegraf.py
55@@ -202,7 +202,6 @@ def check_sysstat_config(original, expected):
56
57 @pytest.mark.parametrize("install_method", ["deb", "snap"])
58 def test_write_telegraf_file(mocker, install_method):
59-
60 if install_method == "deb":
61 user = telegraf.DEB_OWNER
62 group = telegraf.DEB_GROUP
63@@ -1512,6 +1511,47 @@ def test_config_changed_apt(mocker, monkeypatch, config):
64 service_restart.assert_called_once_with(telegraf.DEB_SERVICE)
65
66
67+@pytest.mark.parametrize(
68+ "flags, call_configure_prometheus_client_with_relation",
69+ [
70+ (["prometheus-client.available", "tls_client.server.certs.changed"], True),
71+ (["prometheus-client.available", "endpoint.prometheus-client.changed"], True),
72+ (["tls_client.certs.changed"], False),
73+ ],
74+)
75+def test_prometheus_reconfiguration_when_certificate_changed(
76+ flags, call_configure_prometheus_client_with_relation, mocker, monkeypatch
77+):
78+ """Verify that the function configure_prometheus_client_with_relation is invoked when expected.""" # noqa E501
79+ mocker.patch("reactive.telegraf.host.service_restart")
80+ monkeypatch.setattr(telegraf.host, "service_running", lambda n: True)
81+ prometheus_interface = mocker.Mock()
82+ prometheus_interface.configure = mocker.Mock()
83+ endpoint_from_flag = mocker.patch("reactive.telegraf.endpoint_from_flag")
84+ endpoint_from_flag.return_value = prometheus_interface
85+ get_prometheus_bindaddress = mocker.patch(
86+ "reactive.telegraf.get_prometheus_bindaddress"
87+ )
88+ get_prometheus_bindaddress.return_value = ("1.2.3.4", "9090")
89+ mocker.patch("reactive.telegraf.check_prometheus_port")
90+ relation_ids = ["prometheus-client:0"]
91+ related_units = ["prometheus/0"]
92+ monkeypatch.setattr(telegraf.hookenv, "relation_ids", lambda r: relation_ids)
93+ monkeypatch.setattr(telegraf.hookenv, "related_units", lambda r: related_units)
94+ mocker.patch.object(
95+ telegraf.hookenv, "network_get", return_value={"ingress_addresses": ["1.2.3.4"]}
96+ )
97+ get_prometheus_ip_range = mocker.patch("reactive.telegraf.get_prometheus_ip_range")
98+ get_prometheus_ip_range.return_value = []
99+ for flag in flags:
100+ set_flag(flag)
101+ bus.dispatch()
102+ if call_configure_prometheus_client_with_relation:
103+ prometheus_interface.configure.assert_called_once()
104+ else:
105+ prometheus_interface.configure.assert_not_called()
106+
107+
108 def test_config_changed_extra_options(mocker, monkeypatch, config):
109 service_restart = mocker.patch("reactive.telegraf.host.service_restart")
110 monkeypatch.setattr(telegraf.host, "service_running", lambda n: True)

Subscribers

People subscribed via source and target branches

to all changes: