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

Proposed by Tom Haddon
Status: Merged
Approved by: Jon Seager
Approved revision: fc9fdd373478a03b409319fdae158a7502d762dc
Merged at revision: d3294df8d2b74b8fdf4a4cd332b599e65441b007
Proposed branch: ~mthaddon/charm-k8s-ingress/+git/charm-k8s-ingress:default-namespace
Merge into: charm-k8s-ingress:master
Diff against target: 132 lines (+26/-14)
3 files modified
config.yaml (+1/-1)
src/charm.py (+18/-13)
tests/unit/test_charm.py (+7/-0)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-is (community) continuous-integration Approve
ingress-charmers Pending
Review via email: mp+400183@code.launchpad.net

Commit message

Use current juju model as default namespace

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:fc9fdd373478a03b409319fdae158a7502d762dc
https://jenkins.canonical.com/is/job/lp-charm-k8s-ingress-ci/7/
Executed test runs:
    SUCCESS: https://jenkins.canonical.com/is/job/lp-charm-test/37/
    None: https://jenkins.canonical.com/is/job/lp-update-mp/49407/

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

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

Change successfully merged at revision d3294df8d2b74b8fdf4a4cd332b599e65441b007

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index e9d5df8..c023767 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -15,7 +15,7 @@ options:
6 type: string
7 service-namespace:
8 default: ""
9- description: The namespace of the service to create an ingress for.
10+ description: The namespace of the service to create an ingress for. Will default to the namespace this charm is deployed into.
11 type: string
12 service-port:
13 default: 0
14diff --git a/src/charm.py b/src/charm.py
15index 7639bdb..a6a666d 100755
16--- a/src/charm.py
17+++ b/src/charm.py
18@@ -60,6 +60,11 @@ class CharmK8SIngressCharm(CharmBase):
19 return "{}-ingress".format(self.config["service-name"])
20
21 @property
22+ def _namespace(self):
23+ """Return the namespace to operate on."""
24+ return self.config["service-namespace"] or self.model.name
25+
26+ @property
27 def _service_name(self):
28 """Return a service name for the use creating a k8s service."""
29 # Avoid collision with service name created by Juju.
30@@ -146,7 +151,7 @@ class CharmK8SIngressCharm(CharmBase):
31 """Report on service IP(s)."""
32 self.k8s_auth()
33 api = _core_v1_api()
34- services = api.list_namespaced_service(namespace=self.config["service-namespace"])
35+ services = api.list_namespaced_service(namespace=self._namespace)
36 return [x.spec.cluster_ip for x in services.items if x.metadata.name == self._service_name]
37
38 def _define_service(self):
39@@ -154,36 +159,36 @@ class CharmK8SIngressCharm(CharmBase):
40 self.k8s_auth()
41 api = _core_v1_api()
42 body = self._get_k8s_service()
43- services = api.list_namespaced_service(namespace=self.config["service-namespace"])
44+ services = api.list_namespaced_service(namespace=self._namespace)
45 if self._service_name in [x.metadata.name for x in services.items]:
46 # Currently failing with port[1].name required but we're only
47 # defining one port above...
48 # api.patch_namespaced_service(
49 # name=service_name,
50- # namespace=self.config["service-namespace"],
51+ # namespace=self._namespace,
52 # body=body,
53 # )
54 api.delete_namespaced_service(
55 name=self._service_name,
56- namespace=self.config["service-namespace"],
57+ namespace=self._namespace,
58 )
59 api.create_namespaced_service(
60- namespace=self.config["service-namespace"],
61+ namespace=self._namespace,
62 body=body,
63 )
64 logger.info(
65 "Service updated in namespace %s with name %s",
66- self.config["service-namespace"],
67+ self._namespace,
68 self.config["service-name"],
69 )
70 else:
71 api.create_namespaced_service(
72- namespace=self.config["service-namespace"],
73+ namespace=self._namespace,
74 body=body,
75 )
76 logger.info(
77 "Service created in namespace %s with name %s",
78- self.config["service-namespace"],
79+ self._namespace,
80 self.config["service-name"],
81 )
82
83@@ -192,26 +197,26 @@ class CharmK8SIngressCharm(CharmBase):
84 self.k8s_auth()
85 api = _networking_v1_beta1_api()
86 body = self._get_k8s_ingress()
87- ingresses = api.list_namespaced_ingress(namespace=self.config["service-namespace"])
88+ ingresses = api.list_namespaced_ingress(namespace=self._namespace)
89 if self._ingress_name in [x.metadata.name for x in ingresses.items]:
90 api.patch_namespaced_ingress(
91 name=self._ingress_name,
92- namespace=self.config["service-namespace"],
93+ namespace=self._namespace,
94 body=body,
95 )
96 logger.info(
97 "Ingress updated in namespace %s with name %s",
98- self.config["service-namespace"],
99+ self._namespace,
100 self.config["service-name"],
101 )
102 else:
103 api.create_namespaced_ingress(
104- namespace=self.config["service-namespace"],
105+ namespace=self._namespace,
106 body=body,
107 )
108 logger.info(
109 "Ingress created in namespace %s with name %s",
110- self.config["service-namespace"],
111+ self._namespace,
112 self.config["service-name"],
113 )
114
115diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
116index 2d335c7..22ca656 100644
117--- a/tests/unit/test_charm.py
118+++ b/tests/unit/test_charm.py
119@@ -54,6 +54,13 @@ class TestCharm(unittest.TestCase):
120 # Confirm status is as expected.
121 self.assertEqual(self.harness.charm.unit.status, ActiveStatus())
122
123+ def test_namespace(self):
124+ """Test for the namespace property."""
125+ self.assertEqual(self.harness.charm.config["service-namespace"], "")
126+ self.assertEqual(self.harness.charm._namespace, self.harness.charm.model.name)
127+ self.harness.update_config({"service-namespace": "mymodelname"})
128+ self.assertEqual(self.harness.charm._namespace, "mymodelname")
129+
130 def test_get_k8s_ingress(self):
131 """Test getting our definition of a k8s ingress."""
132 self.harness.disable_hooks()

Subscribers

People subscribed via source and target branches

to all changes: