From 68faf8d30c3e08ca7dc1974281ade474f6f815b7 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Tue, 1 Sep 2020 10:12:16 +0200 Subject: [PATCH] Fix juju status in OSM Charms Change-Id: Iab33813f81c394f2444fb9407b4fab4b70929c90 Signed-off-by: David Garcia --- installers/charm/bundles/osm-ha/bundle.yaml | 12 ++++---- installers/charm/lcm-k8s/reactive/lcm.py | 20 ++++++++++-- installers/charm/mon-k8s/reactive/mon_k8s.py | 9 ++++++ installers/charm/nbi-k8s/reactive/nbi.py | 21 +++++++++++++ installers/charm/ng-ui/.gitignore | 16 ++++++++++ installers/charm/ng-ui/hooks/start | 1 - installers/charm/ng-ui/lib/ops | 1 - installers/charm/ng-ui/requirements.txt | 14 +++++++++ installers/charm/ng-ui/src/charm.py | 13 ++++---- installers/charm/ng-ui/tox.ini | 12 +++++++- installers/charm/pla/.gitignore | 16 ++++++++++ installers/charm/pla/hooks/install | 1 - installers/charm/pla/hooks/start | 1 - installers/charm/pla/hooks/upgrade-charm | 1 - installers/charm/pla/lib/ops | 1 - installers/charm/pla/requirements.txt | 14 +++++++++ installers/charm/pla/src/charm.py | 22 +++++++------- installers/charm/pla/tox.ini | 13 +++++++- installers/charm/pol-k8s/reactive/pol_k8s.py | 8 +++++ installers/charm/ro-k8s/reactive/ro.py | 32 ++++++++++++-------- installers/charm/ui-k8s/reactive/ui.py | 8 +++++ 21 files changed, 190 insertions(+), 46 deletions(-) create mode 100644 installers/charm/ng-ui/.gitignore delete mode 120000 installers/charm/ng-ui/hooks/start delete mode 120000 installers/charm/ng-ui/lib/ops create mode 100644 installers/charm/ng-ui/requirements.txt create mode 100644 installers/charm/pla/.gitignore delete mode 120000 installers/charm/pla/hooks/install delete mode 120000 installers/charm/pla/hooks/start delete mode 120000 installers/charm/pla/hooks/upgrade-charm delete mode 120000 installers/charm/pla/lib/ops create mode 100644 installers/charm/pla/requirements.txt diff --git a/installers/charm/bundles/osm-ha/bundle.yaml b/installers/charm/bundles/osm-ha/bundle.yaml index 8928028b..f60082e0 100644 --- a/installers/charm/bundles/osm-ha/bundle.yaml +++ b/installers/charm/bundles/osm-ha/bundle.yaml @@ -82,7 +82,7 @@ applications: gui-x: 0 gui-y: 50 nbi-k8s: - charm: "cs:~charmed-osm/nbi-k8s" + charm: "%(prefix)s/nbi-k8s%(suffix)s" channel: "%(channel)s" scale: 3 series: kubernetes @@ -96,7 +96,7 @@ applications: gui-x: 0 gui-y: -200 ro-k8s: - charm: "cs:~charmed-osm/ro-k8s" + charm: "%(prefix)s/ro-k8s%(suffix)s" channel: "%(channel)s" scale: 3 series: kubernetes @@ -110,7 +110,7 @@ applications: gui-x: -250 gui-y: 300 ui-k8s: - charm: "cs:~charmed-osm/ui-k8s" + charm: "%(prefix)s/ui-k8s%(suffix)s" channel: "%(channel)s" scale: 3 series: kubernetes @@ -133,7 +133,7 @@ applications: gui-x: 500 gui-y: 100 lcm-k8s: - charm: "cs:~charmed-osm/lcm-k8s" + charm: "%(prefix)s/lcm-k8s%(suffix)s" channel: "%(channel)s" scale: 3 series: kubernetes @@ -152,7 +152,7 @@ applications: gui-x: -250 gui-y: 50 mon-k8s: - charm: "cs:~charmed-osm/mon-k8s" + charm: "%(prefix)s/mon-k8s%(suffix)s" channel: "%(channel)s" scale: 3 series: kubernetes @@ -174,7 +174,7 @@ applications: gui-x: 250 gui-y: 50 pol-k8s: - charm: "cs:~charmed-osm/pol-k8s" + charm: "%(prefix)s/pol-k8s%(suffix)s" channel: "%(channel)s" scale: 3 series: kubernetes diff --git a/installers/charm/lcm-k8s/reactive/lcm.py b/installers/charm/lcm-k8s/reactive/lcm.py index 3ada8cb2..313ed63a 100644 --- a/installers/charm/lcm-k8s/reactive/lcm.py +++ b/installers/charm/lcm-k8s/reactive/lcm.py @@ -22,6 +22,10 @@ from charmhelpers.core.hookenv import ( ) from charms import layer import yaml +import logging + +logger = logging.getLogger(__name__) + @hook("upgrade-charm") @when("leadership.is_leader") @@ -37,18 +41,21 @@ def restart(): @when_not("kafka.ready") @when_not("lcm-k8s.configured") +@when("leadership.is_leader") def waiting_for_kafka(): layer.status.waiting("Waiting for kafka to be ready") @when_not("mongo.ready") @when_not("lcm-k8s.configured") +@when("leadership.is_leader") def waiting_for_mongo(): layer.status.waiting("Waiting for mongo to be ready") @when_not("ro.ready") @when_not("lcm-k8s.configured") +@when("leadership.is_leader") def waiting_for_ro(): layer.status.waiting("Waiting for ro to be ready") @@ -96,7 +103,14 @@ def configure(): layer.status.blocked("k8s spec failed to deploy: {}".format(e)) +@when("kafka.ready", "mongo.ready", "ro.ready") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("lcm-k8s.configured") +@when("leadership.is_leader") def set_lcm_active(): layer.status.active("ready") @@ -130,8 +144,8 @@ def make_pod_spec(ro_host, ro_port, kafka_host, kafka_port, mongo_uri): "mongo_uri": mongo_uri, } data.update(cfg) - - spec = yaml.safe_dump(pod_spec_template % data) + logs = pod_spec_template % data + spec = yaml.safe_load(pod_spec_template % data) if "vca_apiproxy" in cfg and cfg["vca_apiproxy"] != "": spec["containers"][0]["config"]["OSMLCM_VCA_APIPROXY"] = cfg["vca_apiproxy"] - return spec \ No newline at end of file + return spec diff --git a/installers/charm/mon-k8s/reactive/mon_k8s.py b/installers/charm/mon-k8s/reactive/mon_k8s.py index 9eae0b19..79780e03 100644 --- a/installers/charm/mon-k8s/reactive/mon_k8s.py +++ b/installers/charm/mon-k8s/reactive/mon_k8s.py @@ -33,18 +33,21 @@ def restart(): @when_not("kafka.ready") @when_not("mon-k8s.configured") +@when("leadership.is_leader") def waiting_for_kafka(): layer.status.waiting("Waiting for kafka to be ready") @when_not("mongo.ready") @when_not("mon-k8s.configured") +@when("leadership.is_leader") def waiting_for_mongo(): layer.status.waiting("Waiting for mongo to be ready") @when_not("endpoint.prometheus.available") @when_not("mon-k8s.configured") +@when("leadership.is_leader") def waiting_for_prometheus(): layer.status.waiting("Waiting for prometheus to be ready") @@ -81,6 +84,12 @@ def configure(): layer.status.blocked("k8s spec failed to deploy: {}".format(e)) +@when("kafka.ready", "mongo.ready", "endpoint.prometheus.available") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("mon-k8s.configured") def set_mon_active(): layer.status.active("ready") diff --git a/installers/charm/nbi-k8s/reactive/nbi.py b/installers/charm/nbi-k8s/reactive/nbi.py index edcdd4cc..855d8006 100644 --- a/installers/charm/nbi-k8s/reactive/nbi.py +++ b/installers/charm/nbi-k8s/reactive/nbi.py @@ -46,23 +46,38 @@ def clear_keystone_ready(): @when_not("kafka.ready") @when_not("nbi-k8s.configured") +@when("leadership.is_leader") def waiting_for_kafka(): layer.status.waiting("Waiting for kafka to be ready") @when_not("mongo.ready") @when_not("nbi-k8s.configured") +@when("leadership.is_leader") def waiting_for_mongo(): layer.status.waiting("Waiting for mongo to be ready") @when_not("endpoint.prometheus.available") @when_not("nbi-k8s.configured") +@when("leadership.is_leader") def waiting_for_prometheus(): layer.status.waiting("Waiting for prometheus to be ready") @when_not("keystone.ready") +@when("leadership.is_leader") +@when_not("nbi-k8s.configured") +def waiting_for_keystone(): + auth_backend = config().get("auth-backend") + if auth_backend == "keystone": + layer.status.waiting("Waiting for Keystone to be ready") + else: + set_flag("keystone.ready") + + +@when_not("keystone.ready") +@when("leadership.is_leader") @when_not("nbi-k8s.configured") def waiting_for_keystone(): auth_backend = config().get("auth-backend") @@ -162,6 +177,12 @@ def configure(): log("Error: {}".format(traceback.format_exc())) +@when("kafka.ready", "mongo.ready", "endpoint.prometheus.available") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("nbi-k8s.configured") def set_nbi_active(): layer.status.active("ready") diff --git a/installers/charm/ng-ui/.gitignore b/installers/charm/ng-ui/.gitignore new file mode 100644 index 00000000..dfaa805f --- /dev/null +++ b/installers/charm/ng-ui/.gitignore @@ -0,0 +1,16 @@ +# Copyright 2020 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +.vscode +build +ng-ui.charm \ No newline at end of file diff --git a/installers/charm/ng-ui/hooks/start b/installers/charm/ng-ui/hooks/start deleted file mode 120000 index 25b1f68f..00000000 --- a/installers/charm/ng-ui/hooks/start +++ /dev/null @@ -1 +0,0 @@ -../src/charm.py \ No newline at end of file diff --git a/installers/charm/ng-ui/lib/ops b/installers/charm/ng-ui/lib/ops deleted file mode 120000 index d9341932..00000000 --- a/installers/charm/ng-ui/lib/ops +++ /dev/null @@ -1 +0,0 @@ -../mod/operator/ops \ No newline at end of file diff --git a/installers/charm/ng-ui/requirements.txt b/installers/charm/ng-ui/requirements.txt new file mode 100644 index 00000000..10ecdcd5 --- /dev/null +++ b/installers/charm/ng-ui/requirements.txt @@ -0,0 +1,14 @@ +# Copyright 2020 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +ops diff --git a/installers/charm/ng-ui/src/charm.py b/installers/charm/ng-ui/src/charm.py index ce48927a..8e21bcd2 100755 --- a/installers/charm/ng-ui/src/charm.py +++ b/installers/charm/ng-ui/src/charm.py @@ -157,7 +157,7 @@ class NGUICharm(CharmBase): }, } ) - logger.debug(files) + spec = { "version": 2, "containers": [ @@ -188,11 +188,12 @@ class NGUICharm(CharmBase): self.on_start(event) def on_nbi_relation_changed(self, event): - unit = self.model.unit - if not unit.is_leader(): - return - self.state.nbi_host = event.relation.data[event.unit].get("host") - self.state.nbi_port = event.relation.data[event.unit].get("port") + nbi_host = event.relation.data[event.unit].get("host") + nbi_port = event.relation.data[event.unit].get("port") + if nbi_host and self.state.nbi_host != nbi_host: + self.state.nbi_host = nbi_host + if nbi_port and self.state.nbi_port != nbi_port: + self.state.nbi_port = nbi_port self._apply_spec() diff --git a/installers/charm/ng-ui/tox.ini b/installers/charm/ng-ui/tox.ini index aafa3010..1c22bd55 100644 --- a/installers/charm/ng-ui/tox.ini +++ b/installers/charm/ng-ui/tox.ini @@ -20,10 +20,20 @@ setenv = VIRTUAL_ENV={envdir} PYTHONHASHSEED=0 whitelist_externals = juju passenv = HOME TERM CS_API_* OS_* AMULET_* -deps = -r{toxinidir}/test-requirements.txt install_command = pip install {opts} {packages} +[testenv:build] +basepython = python3 +passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY +whitelist_externals = charmcraft + rm + unzip +commands = + rm -rf release + charmcraft build + unzip ng-ui.charm -d release + [testenv:black] basepython = python3 deps = diff --git a/installers/charm/pla/.gitignore b/installers/charm/pla/.gitignore new file mode 100644 index 00000000..db6d41ae --- /dev/null +++ b/installers/charm/pla/.gitignore @@ -0,0 +1,16 @@ +# Copyright 2020 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +.vscode +build +pla.charm \ No newline at end of file diff --git a/installers/charm/pla/hooks/install b/installers/charm/pla/hooks/install deleted file mode 120000 index 25b1f68f..00000000 --- a/installers/charm/pla/hooks/install +++ /dev/null @@ -1 +0,0 @@ -../src/charm.py \ No newline at end of file diff --git a/installers/charm/pla/hooks/start b/installers/charm/pla/hooks/start deleted file mode 120000 index 25b1f68f..00000000 --- a/installers/charm/pla/hooks/start +++ /dev/null @@ -1 +0,0 @@ -../src/charm.py \ No newline at end of file diff --git a/installers/charm/pla/hooks/upgrade-charm b/installers/charm/pla/hooks/upgrade-charm deleted file mode 120000 index 25b1f68f..00000000 --- a/installers/charm/pla/hooks/upgrade-charm +++ /dev/null @@ -1 +0,0 @@ -../src/charm.py \ No newline at end of file diff --git a/installers/charm/pla/lib/ops b/installers/charm/pla/lib/ops deleted file mode 120000 index d9341932..00000000 --- a/installers/charm/pla/lib/ops +++ /dev/null @@ -1 +0,0 @@ -../mod/operator/ops \ No newline at end of file diff --git a/installers/charm/pla/requirements.txt b/installers/charm/pla/requirements.txt new file mode 100644 index 00000000..10ecdcd5 --- /dev/null +++ b/installers/charm/pla/requirements.txt @@ -0,0 +1,14 @@ +# Copyright 2020 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +ops diff --git a/installers/charm/pla/src/charm.py b/installers/charm/pla/src/charm.py index 1fc6386f..e137394e 100755 --- a/installers/charm/pla/src/charm.py +++ b/installers/charm/pla/src/charm.py @@ -84,7 +84,7 @@ class PLACharm(CharmBase): config = self.framework.model.config ports = [ - {"name": "port", "containerPort": config["port"], "protocol": "TCP", }, + {"name": "port", "containerPort": config["port"], "protocol": "TCP",}, ] config_spec = { @@ -123,23 +123,23 @@ class PLACharm(CharmBase): """Upgrade the charm.""" unit = self.model.unit unit.status = MaintenanceStatus("Upgrading charm") - self.on_start(event) + self._apply_spec() def on_kafka_relation_changed(self, event): - unit = self.model.unit - if not unit.is_leader(): - return - self.state.kafka_host = event.relation.data[event.unit].get("host") - self.state.kafka_port = event.relation.data[event.unit].get("port") + kafka_host = event.relation.data[event.unit].get("host") + kafka_port = event.relation.data[event.unit].get("port") + if kafka_host and self.state.kafka_host != kafka_host: + self.state.kafka_host = kafka_host + if kafka_port and self.state.kafka_port != kafka_port: + self.state.kafka_port = kafka_port self._apply_spec() def on_mongo_relation_changed(self, event): - unit = self.model.unit - if not unit.is_leader(): - return - self.state.mongodb_uri = event.relation.data[event.unit].get( + mongodb_uri = event.relation.data[event.unit].get( "connection_string" ) + if mongodb_uri and self.state.mongodb_uri != mongodb_uri: + self.state.mongodb_uri = mongodb_uri self._apply_spec() diff --git a/installers/charm/pla/tox.ini b/installers/charm/pla/tox.ini index d2a8c531..678a5ec5 100644 --- a/installers/charm/pla/tox.ini +++ b/installers/charm/pla/tox.ini @@ -21,10 +21,21 @@ setenv = VIRTUAL_ENV={envdir} PYTHONHASHSEED=0 whitelist_externals = juju passenv = HOME TERM CS_API_* OS_* AMULET_* -deps = -r{toxinidir}/test-requirements.txt install_command = pip install {opts} {packages} + +[testenv:build] +basepython = python3 +passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY +whitelist_externals = charmcraft + rm + unzip +commands = + rm -rf release + charmcraft build + unzip pla.charm -d release + [testenv:black] basepython = python3 deps = diff --git a/installers/charm/pol-k8s/reactive/pol_k8s.py b/installers/charm/pol-k8s/reactive/pol_k8s.py index c31a3231..5629b4ba 100644 --- a/installers/charm/pol-k8s/reactive/pol_k8s.py +++ b/installers/charm/pol-k8s/reactive/pol_k8s.py @@ -32,12 +32,14 @@ def restart(): @when_not("kafka.ready") +@when("leadership.is_leader") @when_not("pol-k8s.configured") def waiting_for_kafka(): layer.status.waiting("Waiting for kafka to be ready") @when_not("mongo.ready") +@when("leadership.is_leader") @when_not("pol-k8s.configured") def waiting_for_mongo(): layer.status.waiting("Waiting for mongo to be ready") @@ -69,6 +71,12 @@ def configure(): layer.status.blocked("k8s spec failed to deploy: {}".format(e)) +@when("kafka.ready", "mongo.ready") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("pol-k8s.configured") def set_pol_active(): layer.status.active("ready") diff --git a/installers/charm/ro-k8s/reactive/ro.py b/installers/charm/ro-k8s/reactive/ro.py index afd7f459..8ad6f98c 100644 --- a/installers/charm/ro-k8s/reactive/ro.py +++ b/installers/charm/ro-k8s/reactive/ro.py @@ -34,6 +34,7 @@ def restart(): @when_not("mysql.available") @when_not("ro-k8s.configured") +@when("leadership.is_leader") def waiting_for_mysql(): layer.status.waiting("Waiting for mysql to be ready") @@ -46,22 +47,29 @@ def configure(): try: mysql = endpoint_from_flag("mysql.available") - spec = make_pod_spec( - mysql.host(), - mysql.port(), - mysql.user(), - mysql.password(), - mysql.root_password(), - ) - - log("set pod spec:\n{}".format(spec)) - pod_spec_set(spec) - layer.status.active("creating container") - set_flag("ro-k8s.configured") + if mysql.host() is not None: + spec = make_pod_spec( + mysql.host(), + mysql.port(), + mysql.user(), + mysql.password(), + mysql.root_password(), + ) + + log("set pod spec:\n{}".format(spec)) + pod_spec_set(spec) + layer.status.active("creating container") + set_flag("ro-k8s.configured") except Exception as e: layer.status.blocked("k8s spec failed to deploy: {}".format(e)) +@when("mysql.available") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("ro-k8s.configured") def set_ro_active(): layer.status.active("ready") diff --git a/installers/charm/ui-k8s/reactive/ui.py b/installers/charm/ui-k8s/reactive/ui.py index 46e57094..5ca81433 100644 --- a/installers/charm/ui-k8s/reactive/ui.py +++ b/installers/charm/ui-k8s/reactive/ui.py @@ -38,12 +38,14 @@ def restart(): @when_not("mysql.available") @when_not("ui-k8s.configured") +@when("leadership.is_leader") def waiting_for_mysql(): layer.status.waiting("Waiting for mysql to be available") @when_not("nbi.ready") @when_not("ui-k8s.configured") +@when("leadership.is_leader") def waiting_for_nbi(): layer.status.waiting("Waiting for nbi to be available") @@ -74,6 +76,12 @@ def configure(): layer.status.blocked("k8s spec failed to deploy: {}".format(e)) +@when("mysql.available", "nbi.ready") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + @when("ui-k8s.configured") def set_ui_active(): layer.status.active("ready") -- 2.17.1