Merge ~barryprice/charm-k8s-discourse/+git/charm-k8s-discourse:v2.8.0.beta7-20.04 into charm-k8s-discourse:v2.8.0.beta7-20.04

Proposed by Barry Price
Status: Merged
Approved by: Barry Price
Approved revision: 902dfd48f34c356618ef9da905fb2ca9ec4a119f
Merge reported by: Barry Price
Merged at revision: 902dfd48f34c356618ef9da905fb2ca9ec4a119f
Proposed branch: ~barryprice/charm-k8s-discourse/+git/charm-k8s-discourse:v2.8.0.beta7-20.04
Merge into: charm-k8s-discourse:v2.8.0.beta7-20.04
Diff against target: 155 lines (+34/-18)
2 files modified
lib/charms/redis_k8s/v0/redis.py (+33/-14)
tox.ini (+1/-4)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
🤖 prod-jenkaas-is (community) continuous-integration Needs Fixing
Canonical IS Reviewers Pending
Review via email: mp+411007@code.launchpad.net

Commit message

Run charmcraft fetch-lib to update our redis_k8s library, and update tox.ini to use the correct PYTHONPATH

To post a comment you must log in.
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
🤖 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 :
review: Needs Fixing (continuous-integration)
Revision history for this message
Tom Haddon (mthaddon) wrote :

A comment inline about the path being controllable via tox.

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
Barry Price (barryprice) wrote :

Hmm I think I'm missing something then, tox.ini already has:

[testenv]
basepython = python3
setenv =
  PYTHONPATH = {toxinidir}/build/lib:{toxinidir}/build/venv

Revision history for this message
🤖 prod-jenkaas-is (prod-jenkaas-is) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Tom Haddon (mthaddon) wrote :

I think there's been a change in charmcraft which means the "build" directory is no longer used as before (it's now used in a way similar to snapcraft).

As a result I think you'll want something like this https://pastebin.ubuntu.com/p/kkx5d6Pgf2/ - essentially just adding `{toxinidir}/lib` to the testenv path. Fwiw, Jon Seager's teams will be standardising on a tox.ini similar to this https://github.com/jnsgruk/charm-kubernetes-dashboard/blob/main/tox.ini. I'm not saying we should change to this exactly, but it shows `{toxinidir}/lib` as part of testenv.

Revision history for this message
Barry Price (barryprice) wrote :

Thanks, that works locally - have pushed another revision.

Removing the existing directories in PYTHONPATH doesn't seem to cause any problems either, so if those are historical I think they can go too.

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 :
review: Needs Fixing (continuous-integration)
Revision history for this message
Tom Haddon (mthaddon) wrote :

LGTM, thx

review: Approve
Revision history for this message
Barry Price (barryprice) wrote :

Mergebot can't land this because of LP:1949763 and LP:1949764.

Marking it WIP until those are addressed, then we can use this to test the fixes.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/lib/charms/redis_k8s/v0/redis.py b/lib/charms/redis_k8s/v0/redis.py
index 70787b6..5360ce0 100644
--- a/lib/charms/redis_k8s/v0/redis.py
+++ b/lib/charms/redis_k8s/v0/redis.py
@@ -6,15 +6,29 @@ redis interface.
6Import `RedisRequires` in your charm by adding the following to `src/charm.py`:6Import `RedisRequires` in your charm by adding the following to `src/charm.py`:
7```7```
8from charms.redis_k8s.v0.redis import RedisRequires8from charms.redis_k8s.v0.redis import RedisRequires
99```
10# In your charm's `__init__` method.10And then in your charm's `__init__` method:
11```
12# Make sure you set redis_relation in StoredState. Assuming you refer to this
13# as `self._stored`:
14self._stored.set_default(redis_relation={})
11self.redis = RedisRequires(self, self._stored)15self.redis = RedisRequires(self, self._stored)
12```16```
13And then wherever you need to reference the relation data:17And then wherever you need to reference the relation data it will be available
18via StoredState:
14```19```
20redis_hosts = []
15for redis_unit in self._stored.redis_relation:21for redis_unit in self._stored.redis_relation:
16 redis_host = self._stored.redis_relation[redis_unit]["hostname"]22 redis_hosts.append({
17 redis_port = self._stored.redis_relation[redis_unit]["port"]23 "redis-host": self._stored.redis_relation[redis_unit]["hostname"],
24 "redis-port": self._stored.redis_relation[redis_unit]["port"],
25 })
26```
27You will also need to add the following to `metadata.yaml`:
28```
29requires:
30 redis:
31 interface: redis
18```32```
19"""33"""
20import logging34import logging
@@ -22,30 +36,31 @@ import logging
22from ops.charm import CharmEvents36from ops.charm import CharmEvents
23from ops.framework import EventBase, EventSource, Object37from ops.framework import EventBase, EventSource, Object
2438
25# The unique Charmhub library identifier, never change it39# The unique Charmhub library identifier, never change it.
26LIBID = "fe18a608cec5465fa5153e419abcad7b"40LIBID = "fe18a608cec5465fa5153e419abcad7b"
2741
28# Increment this major API version when introducing breaking changes42# Increment this major API version when introducing breaking changes.
29LIBAPI = 043LIBAPI = 0
3044
31# Increment this PATCH version before using `charmcraft publish-lib` or reset45# Increment this PATCH version before using `charmcraft publish-lib` or reset
32# to 0 if you are raising the major API version46# to 0 if you are raising the major API version.
33LIBPATCH = 147LIBPATCH = 2
3448
35logger = logging.getLogger(__name__)49logger = logging.getLogger(__name__)
3650
3751
38class RedisRelationUpdatedEvent(EventBase):52class RedisRelationUpdatedEvent(EventBase):
39 pass53 """An event for the redis relation having been updated."""
4054
4155
42class RedisRelationCharmEvents(CharmEvents):56class RedisRelationCharmEvents(CharmEvents):
57 """A class to carry custom charm events so requires can react to relation changes."""
43 redis_relation_updated = EventSource(RedisRelationUpdatedEvent)58 redis_relation_updated = EventSource(RedisRelationUpdatedEvent)
4459
4560
46class RedisRequires(Object):61class RedisRequires(Object):
47 def __init__(self, charm, _stored):62 def __init__(self, charm, _stored):
48 # Define a constructor that takes the charm and it's StoredState63 """A class implementing the redis requires relation."""
49 super().__init__(charm, "redis")64 super().__init__(charm, "redis")
50 self.framework.observe(charm.on.redis_relation_changed, self._on_relation_changed)65 self.framework.observe(charm.on.redis_relation_changed, self._on_relation_changed)
51 self.framework.observe(charm.on.redis_relation_broken, self._on_relation_broken)66 self.framework.observe(charm.on.redis_relation_broken, self._on_relation_broken)
@@ -53,19 +68,20 @@ class RedisRequires(Object):
53 self.charm = charm68 self.charm = charm
5469
55 def _on_relation_changed(self, event):70 def _on_relation_changed(self, event):
71 """Handle the relation changed event."""
56 if not event.unit:72 if not event.unit:
57 return73 return
5874
59 hostname = event.relation.data[event.unit].get("hostname")75 hostname = event.relation.data[event.unit].get("hostname")
60 port = event.relation.data[event.unit].get("port")76 port = event.relation.data[event.unit].get("port")
61 # Store some data from the relation in local state
62 self._stored.redis_relation[event.relation.id] = {"hostname": hostname, "port": port}77 self._stored.redis_relation[event.relation.id] = {"hostname": hostname, "port": port}
6378
64 # Trigger an event that our charm can react to.79 # Trigger an event that our charm can react to.
65 self.charm.on.redis_relation_updated.emit()80 self.charm.on.redis_relation_updated.emit()
6681
67 def _on_relation_broken(self, event):82 def _on_relation_broken(self, event):
68 # Remove the unit data from local state83 """Handle the relation broken event."""
84 # Remove the unit data from local state.
69 self._stored.redis_relation.pop(event.relation.id, None)85 self._stored.redis_relation.pop(event.relation.id, None)
7086
71 # Trigger an event that our charm can react to.87 # Trigger an event that our charm can react to.
@@ -74,23 +90,26 @@ class RedisRequires(Object):
7490
75class RedisProvides(Object):91class RedisProvides(Object):
76 def __init__(self, charm, port):92 def __init__(self, charm, port):
93 """A class implementing the redis provides relation."""
77 super().__init__(charm, "redis")94 super().__init__(charm, "redis")
78 self.framework.observe(charm.on.redis_relation_changed, self._on_relation_changed)95 self.framework.observe(charm.on.redis_relation_changed, self._on_relation_changed)
79 self._port = port96 self._port = port
8097
81 def _on_relation_changed(self, event):98 def _on_relation_changed(self, event):
99 """Handle the relation changed event."""
82 if not self.model.unit.is_leader():100 if not self.model.unit.is_leader():
83 logger.debug("Relation changes ignored by non-leader")101 logger.debug("Relation changes ignored by non-leader")
84 return102 return
85103
86 event.relation.data[self.model.unit]['hostname'] = str(self._bind_address(event))104 event.relation.data[self.model.unit]['hostname'] = str(self._bind_address(event))
87 event.relation.data[self.model.unit]['port'] = str(self._port)105 event.relation.data[self.model.unit]['port'] = str(self._port)
88 # The reactive Redis charm exposes also 'password'. When tackling106 # The reactive Redis charm also exposes 'password'. When tackling
89 # https://github.com/canonical/redis-operator/issues/7 add 'password'107 # https://github.com/canonical/redis-operator/issues/7 add 'password'
90 # field so that it matches the exposed interface information from it.108 # field so that it matches the exposed interface information from it.
91 # event.relation.data[self.unit]['password'] = ''109 # event.relation.data[self.unit]['password'] = ''
92110
93 def _bind_address(self, event):111 def _bind_address(self, event):
112 """Convenience function for getting the unit address."""
94 relation = self.model.get_relation(event.relation.name, event.relation.id)113 relation = self.model.get_relation(event.relation.name, event.relation.id)
95 if address := self.model.get_binding(relation).network.bind_address:114 if address := self.model.get_binding(relation).network.bind_address:
96 return address115 return address
diff --git a/tox.ini b/tox.ini
index d48b030..857a545 100644
--- a/tox.ini
+++ b/tox.ini
@@ -5,8 +5,6 @@ skip_missing_interpreters = True
55
6[testenv]6[testenv]
7basepython = python37basepython = python3
8setenv =
9 PYTHONPATH = {toxinidir}/build/lib:{toxinidir}/build/venv
108
11[testenv:unit]9[testenv:unit]
12commands =10commands =
@@ -15,7 +13,7 @@ commands =
15deps = -r{toxinidir}/tests/unit/requirements.txt13deps = -r{toxinidir}/tests/unit/requirements.txt
16 -r{toxinidir}/requirements.txt14 -r{toxinidir}/requirements.txt
17setenv =15setenv =
18 PYTHONPATH={toxinidir}/src:{toxinidir}/build/lib:{toxinidir}/build/venv16 PYTHONPATH={toxinidir}/src:{toxinidir}/lib
19 TZ=UTC17 TZ=UTC
2018
21[testenv:black]19[testenv:black]
@@ -32,4 +30,3 @@ exclude = .git, __pycache__, .tox,
32ignore = E23130ignore = E231
33max-line-length = 12031max-line-length = 120
34max-complexity = 1032max-complexity = 10
35

Subscribers

People subscribed via source and target branches