From 081f469ea6358cdd9c6d4c992a7668a2199c8cdc Mon Sep 17 00:00:00 2001 From: David Garcia Date: Thu, 2 Jul 2020 18:17:56 +0200 Subject: [PATCH] Add NG-UI Charm Change-Id: Id94b6e0383994580c2b11ab66a44f7c197cb7412 Signed-off-by: David Garcia --- .gitmodules | 4 +- installers/charm/README.md | 3 +- installers/charm/ng-ui/.yamllint.yaml | 23 ++++ installers/charm/ng-ui/README.md | 14 +++ installers/charm/ng-ui/config.yaml | 42 ++++++++ installers/charm/ng-ui/files/default | 32 ++++++ installers/charm/ng-ui/hooks/start | 1 + installers/charm/ng-ui/lib/ops | 1 + installers/charm/ng-ui/metadata.yaml | 26 +++++ installers/charm/ng-ui/mod/operator | 1 + installers/charm/ng-ui/src/charm.py | 148 ++++++++++++++++++++++++++ installers/charm/ng-ui/tox.ini | 55 ++++++++++ 12 files changed, 348 insertions(+), 2 deletions(-) create mode 100644 installers/charm/ng-ui/.yamllint.yaml create mode 100644 installers/charm/ng-ui/README.md create mode 100644 installers/charm/ng-ui/config.yaml create mode 100644 installers/charm/ng-ui/files/default create mode 120000 installers/charm/ng-ui/hooks/start create mode 120000 installers/charm/ng-ui/lib/ops create mode 100644 installers/charm/ng-ui/metadata.yaml create mode 160000 installers/charm/ng-ui/mod/operator create mode 100755 installers/charm/ng-ui/src/charm.py create mode 100644 installers/charm/ng-ui/tox.ini diff --git a/.gitmodules b/.gitmodules index 16acbbf1..1fc8abd4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -11,7 +11,9 @@ # 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. - [submodule "installers/charm/pla/mod/operator"] path = installers/charm/pla/mod/operator url = https://github.com/canonical/operator +[submodule "installers/charm/ng-ui/mod/operator"] + path = installers/charm/ng-ui/mod/operator + url = https://github.com/canonical/operator diff --git a/installers/charm/README.md b/installers/charm/README.md index 0fb31dbf..a8467b01 100644 --- a/installers/charm/README.md +++ b/installers/charm/README.md @@ -43,7 +43,8 @@ The "layers" folder include one common layer for all the osm charms (osm-common) ├── nbi-k8s ├── pol-k8s ├── ro-k8s -└── ui-k8s +├── ui-k8s +└── ng-ui --> new operator framework ``` diff --git a/installers/charm/ng-ui/.yamllint.yaml b/installers/charm/ng-ui/.yamllint.yaml new file mode 100644 index 00000000..ab52c600 --- /dev/null +++ b/installers/charm/ng-ui/.yamllint.yaml @@ -0,0 +1,23 @@ +# 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. +--- +extends: default + +yaml-files: + - "*.yaml" + - "*.yml" + - ".yamllint" +ignore: | + .tox + mod diff --git a/installers/charm/ng-ui/README.md b/installers/charm/ng-ui/README.md new file mode 100644 index 00000000..b4311e58 --- /dev/null +++ b/installers/charm/ng-ui/README.md @@ -0,0 +1,14 @@ + +# NG-UI Charm \ No newline at end of file diff --git a/installers/charm/ng-ui/config.yaml b/installers/charm/ng-ui/config.yaml new file mode 100644 index 00000000..fccc7874 --- /dev/null +++ b/installers/charm/ng-ui/config.yaml @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 Arctos Labs Scandinavia AB +# +# 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. + +options: + image: + description: Docker image name + type: string + default: opensourcemano/ng-ui:8.0.0rc1 + port: + description: Port number + type: int + default: 80 + server_name: + description: Server name + type: string + default: localhost + client_max_body_size: + description: Client maximum body size + type: string + default: 15M + nbi_hostname: + description: NBI hostname + type: string + default: nbi-k8s + nbi_port: + description: NBI Port + type: int + default: 9999 diff --git a/installers/charm/ng-ui/files/default b/installers/charm/ng-ui/files/default new file mode 100644 index 00000000..00c86fc1 --- /dev/null +++ b/installers/charm/ng-ui/files/default @@ -0,0 +1,32 @@ +# 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. + + +server { + listen $port; + server_name $server_name; + root /usr/share/nginx/html; + index index.html index.htm; + client_max_body_size $client_max_body_size; + + location /osm { + proxy_pass http://$nbi_hostname:$nbi_port; + proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; + proxy_set_header Accept-Encoding ""; + } + + location / { + try_files $$uri $$uri/ /index.html; + } +} diff --git a/installers/charm/ng-ui/hooks/start b/installers/charm/ng-ui/hooks/start new file mode 120000 index 00000000..25b1f68f --- /dev/null +++ b/installers/charm/ng-ui/hooks/start @@ -0,0 +1 @@ +../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 new file mode 120000 index 00000000..d9341932 --- /dev/null +++ b/installers/charm/ng-ui/lib/ops @@ -0,0 +1 @@ +../mod/operator/ops \ No newline at end of file diff --git a/installers/charm/ng-ui/metadata.yaml b/installers/charm/ng-ui/metadata.yaml new file mode 100644 index 00000000..db13ba61 --- /dev/null +++ b/installers/charm/ng-ui/metadata.yaml @@ -0,0 +1,26 @@ +# 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. +name: ng-ui +summary: A Next Generation UI charm for Opensource MANO +description: | + New UI for OSM +series: + - kubernetes +min-juju-version: 2.7.0 +deployment: + type: stateless + service: cluster +provides: + nbi: + interface: osm-nbi diff --git a/installers/charm/ng-ui/mod/operator b/installers/charm/ng-ui/mod/operator new file mode 160000 index 00000000..a84ce877 --- /dev/null +++ b/installers/charm/ng-ui/mod/operator @@ -0,0 +1 @@ +Subproject commit a84ce8776b368a8b2bccdb173716e342db9a6b36 diff --git a/installers/charm/ng-ui/src/charm.py b/installers/charm/ng-ui/src/charm.py new file mode 100755 index 00000000..23d31613 --- /dev/null +++ b/installers/charm/ng-ui/src/charm.py @@ -0,0 +1,148 @@ +#!/usr/bin/env python3 +# 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. + +import sys +import logging + +sys.path.append("lib") + +from ops.charm import CharmBase +from ops.framework import StoredState, Object +from ops.main import main +from ops.model import ( + ActiveStatus, + MaintenanceStatus, +) + +from glob import glob +from pathlib import Path +from string import Template + +logger = logging.getLogger(__name__) + + +class NGUICharm(CharmBase): + state = StoredState() + + def __init__(self, framework, key): + super().__init__(framework, key) + self.state.set_default(spec=None) + + # Observe Charm related events + self.framework.observe(self.on.config_changed, self.on_config_changed) + self.framework.observe(self.on.start, self.on_start) + self.framework.observe(self.on.upgrade_charm, self.on_upgrade_charm) + # self.framework.observe( + # self.on.nbi_relation_joined, self.on_nbi_relation_joined + # ) + + def _apply_spec(self): + # Only apply the spec if this unit is a leader. + if not self.framework.model.unit.is_leader(): + return + new_spec = self.make_pod_spec() + if new_spec == self.state.spec: + return + self.framework.model.pod.set_spec(new_spec) + self.state.spec = new_spec + + def make_pod_spec(self): + config = self.framework.model.config + + ports = [ + {"name": "port", "containerPort": config["port"], "protocol": "TCP",}, + ] + + kubernetes = { + "readinessProbe": { + "tcpSocket": {"port": config["port"]}, + "timeoutSeconds": 5, + "periodSeconds": 5, + "initialDelaySeconds": 10, + }, + "livenessProbe": { + "tcpSocket": {"port": config["port"]}, + "timeoutSeconds": 5, + "initialDelaySeconds": 45, + }, + } + config_spec = { + "port": config["port"], + "server_name": config["server_name"], + "client_max_body_size": config["client_max_body_size"], + "nbi_hostname": config["nbi_hostname"], + "nbi_port": config["nbi_port"], + } + + files = [ + { + "name": "configuration", + "mountPath": "/etc/nginx/sites-available/", + "files": { + Path(filename) + .name: Template(Path(filename).read_text()) + .substitute(config_spec) + for filename in glob("files/*") + }, + }, + ] + logger.debug(files) + spec = { + "version": 2, + "containers": [ + { + "name": self.framework.model.app.name, + "image": "{}".format(config["image"]), + "ports": ports, + "kubernetes": kubernetes, + "files": files, + } + ], + } + + return spec + + def on_config_changed(self, event): + """Handle changes in configuration""" + unit = self.model.unit + unit.status = MaintenanceStatus("Applying new pod spec") + self._apply_spec() + unit.status = ActiveStatus("Ready") + + def on_start(self, event): + """Called when the charm is being installed""" + unit = self.model.unit + unit.status = MaintenanceStatus("Applying pod spec") + self._apply_spec() + unit.status = ActiveStatus("Ready") + + def on_upgrade_charm(self, event): + """Upgrade the charm.""" + unit = self.model.unit + unit.status = MaintenanceStatus("Upgrading charm") + self.on_start(event) + + # def on_nbi_relation_joined(self, event): + # unit = self.model.unit + # if not unit.is_leader(): + # return + # config = self.framework.model.config + # unit = MaintenanceStatus("Sending connection data") + + # unit = ActiveStatus("Ready") + + +if __name__ == "__main__": + main(NGUICharm) diff --git a/installers/charm/ng-ui/tox.ini b/installers/charm/ng-ui/tox.ini new file mode 100644 index 00000000..aafa3010 --- /dev/null +++ b/installers/charm/ng-ui/tox.ini @@ -0,0 +1,55 @@ +# 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. +[tox] +envlist = pep8 +skipsdist = True + +[testenv] +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:black] +basepython = python3 +deps = + black + yamllint +commands = + black --check --diff src + yamllint . + +[testenv:pep8] +basepython = python3 +deps=charm-tools +commands = charm-proof + +[testenv:func-noop] +basepython = python3 +commands = + true + +[testenv:func] +basepython = python3 +commands = functest-run-suite + +[testenv:func-smoke] +basepython = python3 +commands = functest-run-suite --keep-model --smoke + +[testenv:venv] +commands = {posargs} -- 2.17.1