Merge ~mthaddon/charm-k8s-ingress/+git/charm-k8s-ingress:master into charm-k8s-ingress:master

Proposed by Tom Haddon
Status: Merged
Approved by: Tom Haddon
Approved revision: 56a29e5557c9dbce4fe0c3c0a90a75bfa7da1799
Merged at revision: 9b917e496f37699c2b1e025320113ae31ec5f61a
Proposed branch: ~mthaddon/charm-k8s-ingress/+git/charm-k8s-ingress:master
Merge into: charm-k8s-ingress:master
Diff against target: 48 lines (+18/-1)
2 files modified
src/charm.py (+3/-1)
tests/unit/test_charm.py (+15/-0)
Reviewer Review Type Date Requested Status
Jon Seager Approve
🤖 prod-jenkaas-is (community) continuous-integration Approve
Review via email: mp+400169@code.launchpad.net

Commit message

Only define k8s ingress and k8s service if we're the leader, to avoid collision in case of scale out

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-is (prod-jenkaas-is) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-is (prod-jenkaas-is) wrote :

PASSED: Continuous integration, rev:56a29e5557c9dbce4fe0c3c0a90a75bfa7da1799
https://jenkins.canonical.com/is/job/lp-charm-k8s-ingress-ci/4/
Executed test runs:
    SUCCESS: https://jenkins.canonical.com/is/job/lp-charm-test/34/
    None: https://jenkins.canonical.com/is/job/lp-update-mp/49141/

Click here to trigger a rebuild:
https://jenkins.canonical.com/is/job/lp-charm-k8s-ingress-ci/4//rebuild

review: Approve (continuous-integration)
Revision history for this message
Jon Seager (jnsgruk) wrote :

Good call :)

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 9b917e496f37699c2b1e025320113ae31ec5f61a

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/charm.py b/src/charm.py
2index 3bd55ff..5e48538 100755
3--- a/src/charm.py
4+++ b/src/charm.py
5@@ -208,7 +208,9 @@ class CharmK8SIngressCharm(CharmBase):
6 def _on_config_changed(self, _):
7 """Handle the config changed event."""
8 msg = ""
9- if self.config["service-name"]:
10+ # We only want to do anything here if we're the leader to avoid
11+ # collision if we've scaled out this application.
12+ if self.unit.is_leader() and self.config["service-name"]:
13 self._define_service()
14 self._define_ingress()
15 # It's not recommended to do this via ActiveStatus, but we don't
16diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
17index 2bca57e..b861cb0 100644
18--- a/tests/unit/test_charm.py
19+++ b/tests/unit/test_charm.py
20@@ -23,6 +23,8 @@ class TestCharm(unittest.TestCase):
21 @mock.patch('charm.CharmK8SIngressCharm._define_service')
22 def test_config_changed(self, _define_service, _define_ingress, _report_service_ips):
23 """Test our config changed handler."""
24+ # First of all test, with leader set to True.
25+ self.harness.set_leader(True)
26 _report_service_ips.return_value = ["10.0.1.12"]
27 # Confirm our _define_ingress and _define_service methods haven't been called.
28 self.assertEqual(_define_ingress.call_count, 0)
29@@ -38,6 +40,19 @@ class TestCharm(unittest.TestCase):
30 self.assertEqual(_define_service.call_count, 1)
31 # Confirm status is as expected.
32 self.assertEqual(self.harness.charm.unit.status, ActiveStatus('Ingress with service IP(s): 10.0.1.12'))
33+ # And now test with leader is False.
34+ _define_ingress.reset_mock()
35+ _define_service.reset_mock()
36+ self.harness.set_leader(False)
37+ self.harness.update_config({"service-name": ""})
38+ self.assertEqual(_define_ingress.call_count, 0)
39+ self.assertEqual(_define_service.call_count, 0)
40+ # Leader False, but service-name defined should still do nothing.
41+ self.harness.update_config({"service-name": "gunicorn"})
42+ self.assertEqual(_define_ingress.call_count, 0)
43+ self.assertEqual(_define_service.call_count, 0)
44+ # Confirm status is as expected.
45+ self.assertEqual(self.harness.charm.unit.status, ActiveStatus())
46
47 def test_get_k8s_ingress(self):
48 """Test getting our definition of a k8s ingress."""

Subscribers

People subscribed via source and target branches

to all changes: