Add NG-UI Charm 93/9293/5
authorDavid Garcia <david.garcia@canonical.com>
Thu, 2 Jul 2020 16:17:56 +0000 (18:17 +0200)
committerDavid Garcia <david.garcia@canonical.com>
Thu, 2 Jul 2020 17:05:16 +0000 (19:05 +0200)
Change-Id: Id94b6e0383994580c2b11ab66a44f7c197cb7412
Signed-off-by: David Garcia <david.garcia@canonical.com>
12 files changed:
.gitmodules
installers/charm/README.md
installers/charm/ng-ui/.yamllint.yaml [new file with mode: 0644]
installers/charm/ng-ui/README.md [new file with mode: 0644]
installers/charm/ng-ui/config.yaml [new file with mode: 0644]
installers/charm/ng-ui/files/default [new file with mode: 0644]
installers/charm/ng-ui/hooks/start [new symlink]
installers/charm/ng-ui/lib/ops [new symlink]
installers/charm/ng-ui/metadata.yaml [new file with mode: 0644]
installers/charm/ng-ui/mod/operator [new submodule]
installers/charm/ng-ui/src/charm.py [new file with mode: 0755]
installers/charm/ng-ui/tox.ini [new file with mode: 0644]

index 16acbbf..1fc8abd 100644 (file)
@@ -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
index 0fb31db..a8467b0 100644 (file)
@@ -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 (file)
index 0000000..ab52c60
--- /dev/null
@@ -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 (file)
index 0000000..b4311e5
--- /dev/null
@@ -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. -->
+# 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 (file)
index 0000000..fccc787
--- /dev/null
@@ -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 (file)
index 0000000..00c86fc
--- /dev/null
@@ -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 (symlink)
index 0000000..25b1f68
--- /dev/null
@@ -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 (symlink)
index 0000000..d934193
--- /dev/null
@@ -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 (file)
index 0000000..db13ba6
--- /dev/null
@@ -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 (submodule)
index 0000000..a84ce87
--- /dev/null
@@ -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 (executable)
index 0000000..23d3161
--- /dev/null
@@ -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 (file)
index 0000000..aafa301
--- /dev/null
@@ -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}