Merge ~mthaddon/charm-k8s-openldap/+git/charm-k8s-openldap:oci-resource into charm-k8s-openldap:master

Proposed by Tom Haddon
Status: Merged
Approved by: Tom Haddon
Approved revision: f2d6faea3de44f6bb20e46a6fee8e51d0dd94cdb
Merged at revision: 50ad4ca45e1f299015a380c83c33ad24b9921556
Proposed branch: ~mthaddon/charm-k8s-openldap/+git/charm-k8s-openldap:oci-resource
Merge into: charm-k8s-openldap:master
Diff against target: 266 lines (+31/-79)
6 files modified
.gitignore (+1/-0)
config.yaml (+4/-14)
metadata.yaml (+6/-0)
requirements.txt (+1/-0)
src/charm.py (+15/-7)
tests/unit/test_charm.py (+4/-58)
Reviewer Review Type Date Requested Status
David Lawson (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+395418@code.launchpad.net

Commit message

Switch to using the oci-image resource over static config for docker image

Description of the change

Switch to using the oci-image resource over static config for docker image

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
David Lawson (deej) wrote :

LGTM

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

Change successfully merged at revision 50ad4ca45e1f299015a380c83c33ad24b9921556

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2index 490cc43..aa250e5 100644
3--- a/.gitignore
4+++ b/.gitignore
5@@ -1,5 +1,6 @@
6 *~
7 *.charm
8+*.swp
9 .tox
10 .coverage
11 __pycache__
12diff --git a/config.yaml b/config.yaml
13index c442062..899ebfd 100644
14--- a/config.yaml
15+++ b/config.yaml
16@@ -1,20 +1,10 @@
17 # Copyright 2020 Canonical Ltd.
18 # See LICENSE file for licensing details.
19 options:
20- image_path:
21- type: string
22+ container_port:
23+ type: int
24 description: |
25- The location of the image to use.
26+ The port to use for the container, which translates to k8s 'containerPort' and 'ReadinessProbe'.
27
28 This setting is required.
29- default: 'openldapcharmers/openldap:edge'
30- image_username:
31- type: string
32- description: |
33- The username for accessing the registry specified in image_path.
34- default: ''
35- image_password:
36- type: string
37- description: |
38- The password associated with image_username for accessing the registry specified in image_path.
39- default: ''
40+ default: 389
41diff --git a/metadata.yaml b/metadata.yaml
42index 39ef633..0cf8dc9 100644
43--- a/metadata.yaml
44+++ b/metadata.yaml
45@@ -13,3 +13,9 @@ requires:
46 db:
47 interface: pgsql
48 limit: 1
49+resources:
50+ openldap-image:
51+ type: oci-image
52+ description: docker image for OpenLDAP
53+ auto-fetch: true
54+ upstream-source: 'openldapcharmers/openldap:2.4.50'
55diff --git a/requirements.txt b/requirements.txt
56index 12aa474..1326f30 100644
57--- a/requirements.txt
58+++ b/requirements.txt
59@@ -1,3 +1,4 @@
60 charmhelpers
61 ops
62 ops-lib-pgsql
63+https://github.com/juju-solutions/resource-oci-image/archive/master.zip
64diff --git a/src/charm.py b/src/charm.py
65index f8d5489..cab7f6c 100755
66--- a/src/charm.py
67+++ b/src/charm.py
68@@ -5,6 +5,7 @@
69 import logging
70
71 from charmhelpers.core import host
72+from oci_image import OCIImageResource, OCIImageResourceError
73 import ops.lib
74 from ops.charm import (
75 CharmBase,
76@@ -18,6 +19,7 @@ from ops.framework import (
77 )
78 from ops.model import (
79 ActiveStatus,
80+ BlockedStatus,
81 MaintenanceStatus,
82 WaitingStatus,
83 )
84@@ -50,6 +52,8 @@ class OpenLDAPK8sCharm(CharmBase):
85 def __init__(self, *args):
86 super().__init__(*args)
87
88+ self.image = OCIImageResource(self, 'openldap-image')
89+
90 self.leader_data = LeadershipSettings()
91
92 self.framework.observe(self.on.start, self._configure_pod)
93@@ -98,12 +102,16 @@ class OpenLDAPK8sCharm(CharmBase):
94
95 def _make_pod_spec(self):
96 """Return a pod spec with some core configuration."""
97+ # get image details using OCI image helper library
98+ try:
99+ image_details = self.image.fetch()
100+ logging.info("using imageDetails: {}")
101+ except OCIImageResourceError:
102+ logging.exception('An error occurred while fetching the image info')
103+ self.unit.status = BlockedStatus('Error fetching image information')
104+ return {}
105+
106 config = self.model.config
107- image_details = {
108- 'imagePath': config['image_path'],
109- }
110- if config['image_username']:
111- image_details.update({'username': config['image_username'], 'password': config['image_password']})
112 pod_config = self._make_pod_config()
113
114 return {
115@@ -112,10 +120,10 @@ class OpenLDAPK8sCharm(CharmBase):
116 {
117 'name': self.app.name,
118 'imageDetails': image_details,
119- 'ports': [{'containerPort': 389, 'protocol': 'TCP'}],
120+ 'ports': [{'containerPort': config['container_port'], 'protocol': 'TCP'}],
121 'envConfig': pod_config,
122 'kubernetes': {
123- 'readinessProbe': {'tcpSocket': {'port': 389}},
124+ 'readinessProbe': {'tcpSocket': {'port': config['container_port']}},
125 },
126 }
127 ],
128diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
129index 00aba40..5d8df25 100644
130--- a/tests/unit/test_charm.py
131+++ b/tests/unit/test_charm.py
132@@ -14,30 +14,6 @@ from ops.model import (
133
134 from unittest.mock import MagicMock
135
136-CONFIG_ALL = {
137- 'image_path': 'example.com/openldap:latest',
138- 'image_username': 'image_user',
139- 'image_password': 'image_pass',
140-}
141-
142-CONFIG_IMAGE_NO_CREDS = {
143- 'image_path': 'example.com/openldap:latest',
144- 'image_username': '',
145- 'image_password': '',
146-}
147-
148-CONFIG_IMAGE_NO_IMAGE = {
149- 'image_path': '',
150- 'image_username': '',
151- 'image_password': '',
152-}
153-
154-CONFIG_IMAGE_NO_PASSWORD = {
155- 'image_path': 'example.com/openldap:latest',
156- 'image_username': 'production',
157- 'image_password': '',
158-}
159-
160 DB_URI = {
161 'dbname': 'openldap',
162 'user': 'ldap_user',
163@@ -52,10 +28,10 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
164 self.harness = testing.Harness(OpenLDAPK8sCharm)
165 self.harness.begin()
166 self.harness.disable_hooks()
167+ self.harness.add_oci_resource('openldap-image')
168
169 def test_make_pod_config(self):
170 """Make basic, correct pod config."""
171- self.harness.update_config(CONFIG_IMAGE_NO_CREDS)
172 self.harness.charm._state.postgres = DB_URI
173 expected = {
174 'POSTGRES_NAME': 'openldap',
175@@ -71,32 +47,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
176
177 def test_make_pod_spec(self):
178 """Basic, correct pod spec."""
179- self.harness.update_config(CONFIG_ALL)
180- self.harness.charm._state.postgres = DB_URI
181- with patch.object(self.harness.charm, "get_admin_password") as get_admin_password:
182- get_admin_password.return_value = 'badmin_password'
183- expected = {
184- 'version': 3,
185- 'containers': [
186- {
187- 'name': 'openldap',
188- 'imageDetails': {
189- 'imagePath': 'example.com/openldap:latest',
190- 'username': 'image_user',
191- 'password': 'image_pass',
192- },
193- 'ports': [{'containerPort': 389, 'protocol': 'TCP'}],
194- 'envConfig': self.harness.charm._make_pod_config(),
195- 'kubernetes': {
196- 'readinessProbe': {'tcpSocket': {'port': 389}},
197- },
198- }
199- ],
200- }
201- self.assertEqual(self.harness.charm._make_pod_spec(), expected)
202-
203- def test_make_pod_spec_no_image_creds(self):
204- self.harness.update_config(CONFIG_IMAGE_NO_CREDS)
205 self.harness.charm._state.postgres = DB_URI
206 with patch.object(self.harness.charm, "get_admin_password") as get_admin_password:
207 get_admin_password.return_value = 'badmin_password'
208@@ -106,7 +56,9 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
209 {
210 'name': 'openldap',
211 'imageDetails': {
212- 'imagePath': 'example.com/openldap:latest',
213+ 'imagePath': 'registrypath',
214+ 'username': 'username',
215+ 'password': 'password',
216 },
217 'ports': [{'containerPort': 389, 'protocol': 'TCP'}],
218 'envConfig': self.harness.charm._make_pod_config(),
219@@ -122,7 +74,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
220 """Check that we block correctly without a Postgres relation."""
221 mock_event = MagicMock()
222
223- self.harness.update_config(CONFIG_ALL)
224 expected = WaitingStatus('Waiting for database relation')
225 self.harness.charm._configure_pod(mock_event)
226 self.assertEqual(self.harness.charm.unit.status, expected)
227@@ -131,7 +82,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
228 """Test pod config as a non-leader."""
229 mock_event = MagicMock()
230
231- self.harness.update_config(CONFIG_ALL)
232 self.harness.charm._state.postgres = DB_URI
233 expected = ActiveStatus()
234 self.harness.charm._configure_pod(mock_event)
235@@ -141,7 +91,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
236 """Test pod configuration with everything working appropriately."""
237 mock_event = MagicMock()
238
239- self.harness.update_config(CONFIG_ALL)
240 self.harness.charm._state.postgres = DB_URI
241 self.harness.set_leader(True)
242 expected = ActiveStatus()
243@@ -153,7 +102,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
244 def test_on_database_relation_joined(self):
245 mock_event = MagicMock()
246
247- self.harness.update_config(CONFIG_ALL)
248 self.harness.set_leader(True)
249 expected = "openldap"
250 self.harness.charm._on_database_relation_joined(mock_event)
251@@ -162,7 +110,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
252 def test_on_master_changed(self):
253 mock_event = MagicMock()
254
255- self.harness.update_config(CONFIG_ALL)
256 self.harness.set_leader(True)
257 master = namedtuple('master', ['dbname', 'user', 'password', 'host', 'port'])
258 master.dbname = "openldap"
259@@ -181,7 +128,6 @@ class TestOpenLDAPK8sCharmHooksDisabled(unittest.TestCase):
260 def test_on_get_admin_password_action(self):
261 mock_event = MagicMock()
262
263- self.harness.update_config(CONFIG_ALL)
264 with patch.object(self.harness.charm, "get_admin_password") as get_admin_password:
265 get_admin_password.return_value = 'badmin_password'
266 self.harness.charm._on_get_admin_password_action(mock_event)

Subscribers

People subscribed via source and target branches

to all changes: