From e4c6aeb7d0f548675ec88afde016b30920c97698 Mon Sep 17 00:00:00 2001 From: sousaedu Date: Fri, 15 Jan 2021 18:41:15 +0000 Subject: [PATCH] Adding Grafana charm Change-Id: I07f1861c5b42acc8459daa6351a17c72dc2ca2f3 Signed-off-by: sousaedu --- installers/charm/grafana-k8s/.gitignore | 24 ++++ installers/charm/grafana-k8s/.yamllint.yaml | 34 ++++++ installers/charm/grafana-k8s/README.md | 23 ++++ installers/charm/grafana-k8s/config.yaml | 34 ++++++ installers/charm/grafana-k8s/icon.svg | 85 ++++++++++++++ installers/charm/grafana-k8s/layer.yaml | 29 +++++ installers/charm/grafana-k8s/metadata.yaml | 34 ++++++ .../charm/grafana-k8s/reactive/grafana.py | 104 ++++++++++++++++++ .../grafana-k8s/reactive/spec_template.yaml | 71 ++++++++++++ .../charm/grafana-k8s/test-requirements.txt | 22 ++++ .../grafana-k8s/tests/basic_deployment.py | 60 ++++++++++ .../grafana-k8s/tests/bundles/grafana-ha.yaml | 38 +++++++ .../grafana-k8s/tests/bundles/grafana.yaml | 38 +++++++ installers/charm/grafana-k8s/tests/tests.yaml | 28 +++++ installers/charm/grafana-k8s/tox.ini | 82 ++++++++++++++ 15 files changed, 706 insertions(+) create mode 100644 installers/charm/grafana-k8s/.gitignore create mode 100644 installers/charm/grafana-k8s/.yamllint.yaml create mode 100644 installers/charm/grafana-k8s/README.md create mode 100644 installers/charm/grafana-k8s/config.yaml create mode 100644 installers/charm/grafana-k8s/icon.svg create mode 100644 installers/charm/grafana-k8s/layer.yaml create mode 100644 installers/charm/grafana-k8s/metadata.yaml create mode 100644 installers/charm/grafana-k8s/reactive/grafana.py create mode 100644 installers/charm/grafana-k8s/reactive/spec_template.yaml create mode 100644 installers/charm/grafana-k8s/test-requirements.txt create mode 100644 installers/charm/grafana-k8s/tests/basic_deployment.py create mode 100644 installers/charm/grafana-k8s/tests/bundles/grafana-ha.yaml create mode 100644 installers/charm/grafana-k8s/tests/bundles/grafana.yaml create mode 100644 installers/charm/grafana-k8s/tests/tests.yaml create mode 100644 installers/charm/grafana-k8s/tox.ini diff --git a/installers/charm/grafana-k8s/.gitignore b/installers/charm/grafana-k8s/.gitignore new file mode 100644 index 00000000..712eb963 --- /dev/null +++ b/installers/charm/grafana-k8s/.gitignore @@ -0,0 +1,24 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +release/ +__pycache__ +.tox diff --git a/installers/charm/grafana-k8s/.yamllint.yaml b/installers/charm/grafana-k8s/.yamllint.yaml new file mode 100644 index 00000000..21b95b5b --- /dev/null +++ b/installers/charm/grafana-k8s/.yamllint.yaml @@ -0,0 +1,34 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +--- + +extends: default +rules: + line-length: disable +yaml-files: + - '*.yaml' + - '*.yml' + - '.yamllint' +ignore: | + reactive/ + .tox + release/ diff --git a/installers/charm/grafana-k8s/README.md b/installers/charm/grafana-k8s/README.md new file mode 100644 index 00000000..ad66a557 --- /dev/null +++ b/installers/charm/grafana-k8s/README.md @@ -0,0 +1,23 @@ + + +# Overview + +Grafana for Juju CAAS diff --git a/installers/charm/grafana-k8s/config.yaml b/installers/charm/grafana-k8s/config.yaml new file mode 100644 index 00000000..2f606c10 --- /dev/null +++ b/installers/charm/grafana-k8s/config.yaml @@ -0,0 +1,34 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +options: + advertised-hostname: + description: Grafana Hostname + type: string + default: "grafana-k8s" + advertised-port: + description: Grafana Port + type: int + default: 3000 + image: + type: string + description: OCI image + default: rocks.canonical.com:443/grafana/grafana:latest diff --git a/installers/charm/grafana-k8s/icon.svg b/installers/charm/grafana-k8s/icon.svg new file mode 100644 index 00000000..093de146 --- /dev/null +++ b/installers/charm/grafana-k8s/icon.svg @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/installers/charm/grafana-k8s/layer.yaml b/installers/charm/grafana-k8s/layer.yaml new file mode 100644 index 00000000..0c9220d7 --- /dev/null +++ b/installers/charm/grafana-k8s/layer.yaml @@ -0,0 +1,29 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +includes: + - "layer:caas-base" + - 'layer:status' + - 'layer:leadership' + - "layer:osm-common" + - "interface:prometheus" + +repo: https://code.launchpad.net/osm-k8s-bundle diff --git a/installers/charm/grafana-k8s/metadata.yaml b/installers/charm/grafana-k8s/metadata.yaml new file mode 100644 index 00000000..1f5dbe77 --- /dev/null +++ b/installers/charm/grafana-k8s/metadata.yaml @@ -0,0 +1,34 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +name: "grafana-k8s" +summary: "Grafana charm for Kubernetes" +maintainers: + - "SolutionsQA " +description: | + A CAAS charm to deploy grafana +tags: + - "application" +series: + - "kubernetes" +requires: + prometheus: + interface: prometheus diff --git a/installers/charm/grafana-k8s/reactive/grafana.py b/installers/charm/grafana-k8s/reactive/grafana.py new file mode 100644 index 00000000..923aca6d --- /dev/null +++ b/installers/charm/grafana-k8s/reactive/grafana.py @@ -0,0 +1,104 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +from charms.layer.caas_base import pod_spec_set +from charms.reactive import endpoint_from_flag +from charms.reactive import when, when_not, hook +from charms.reactive.flags import set_flag, clear_flag +from charmhelpers.core.hookenv import ( + log, + metadata, + config, +) +from charms import layer +from charmhelpers.core import hookenv +import traceback + + +@hook("upgrade-charm") +@when("leadership.is_leader") +def upgrade(): + clear_flag("grafana-k8s.configured") + + +@when("config.changed") +@when("leadership.is_leader") +def restart(): + clear_flag("grafana-k8s.configured") + + +@when_not("endpoint.prometheus.available") +@when("leadership.is_leader") +def waiting_for_prometheus_interface(): + layer.status.waiting("Waiting for prometheus interface") + + +@when("endpoint.prometheus.available") +@when_not("grafana-k8s.configured") +@when("leadership.is_leader") +def configure(): + layer.status.maintenance("Configuring grafana container") + try: + prometheus = endpoint_from_flag("endpoint.prometheus.available") + prometheus_url = prometheus.targets()[0]["targets"][0] + + if prometheus_url: + spec = make_pod_spec(prometheus_url) + log("set pod spec:\n{}".format(spec)) + pod_spec_set(spec) + set_flag("grafana-k8s.configured") + layer.status.active("ready") + + except Exception as e: + layer.status.blocked("k8s spec failed to deploy: {}".format(e)) + log(traceback.format_exc(), level=hookenv.ERROR) + + +@when("grafana-k8s.configured") +def set_grafana_active(): + layer.status.active("ready") + + +@when("endpoint.prometheus.available") +@when_not("leadership.is_leader") +def non_leaders_active(): + layer.status.active("ready") + + +def make_pod_spec(prometheus_url): + """Make pod specification for Kubernetes + + Returns: + pod_spec: Pod specification for Kubernetes + """ + with open("reactive/spec_template.yaml") as spec_file: + pod_spec_template = spec_file.read() + + md = metadata() + cfg = config() + + data = { + "name": md.get("name"), + "docker_image": cfg.get("image"), + "prometheus_url": prometheus_url, + } + data.update(cfg) + return pod_spec_template % data diff --git a/installers/charm/grafana-k8s/reactive/spec_template.yaml b/installers/charm/grafana-k8s/reactive/spec_template.yaml new file mode 100644 index 00000000..10165848 --- /dev/null +++ b/installers/charm/grafana-k8s/reactive/spec_template.yaml @@ -0,0 +1,71 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +version: 2 +containers: + - name: %(name)s + image: %(docker_image)s + ports: + - containerPort: %(advertised-port)s + protocol: TCP + files: + - name: dashboards + mountPath: /etc/grafana/provisioning/dashboards/ + files: + dashboards-osm.yml: | + apiVersion: 1 + providers: + - name: 'osm' + orgId: 1 + folder: '' + type: file + options: + path: /etc/grafana/provisioning/dashboards/ + - name: datasources + mountPath: /etc/grafana/provisioning/datasources/ + files: + datasource-prometheus.yml: | + datasources: + - access: proxy + editable: true + is_default: true + name: osm_prometheus + org_id: 1 + type: prometheus + url: http://%(prometheus_url)s + version: 1 + kubernetes: + readinessProbe: + httpGet: + path: /api/health + port: %(advertised-port)s + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + livenessProbe: + httpGet: + path: /api/health + port: %(advertised-port)s + initialDelaySeconds: 60 + timeoutSeconds: 30 + failureThreshold: 10 \ No newline at end of file diff --git a/installers/charm/grafana-k8s/test-requirements.txt b/installers/charm/grafana-k8s/test-requirements.txt new file mode 100644 index 00000000..b302c2e7 --- /dev/null +++ b/installers/charm/grafana-k8s/test-requirements.txt @@ -0,0 +1,22 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +git+https://github.com/davigar15/zaza.git#egg=zaza diff --git a/installers/charm/grafana-k8s/tests/basic_deployment.py b/installers/charm/grafana-k8s/tests/basic_deployment.py new file mode 100644 index 00000000..4cb760ab --- /dev/null +++ b/installers/charm/grafana-k8s/tests/basic_deployment.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +import unittest +import zaza.model as model +import requests as http +import time + + +def get_grafana_uri(): + ip = model.get_status().applications["grafana-k8s"]["public-address"] + port = 3000 + return "http://{}:{}".format(ip, port) + + +class BasicDeployment(unittest.TestCase): + def setUp(self): + ready = False + num_retries = 0 + while not ready and num_retries < 5: + if ( + model.get_status().applications["grafana-k8s"]["status"]["status"] + == "active" + ): + ready = True + else: + num_retries += 1 + time.sleep(5) + + def test_get_grafana_uri(self): + get_grafana_uri() + + def test_grafana_get_home(self): + grafana_uri = get_grafana_uri() + body = http.get("{}/api/dashboards/home".format(grafana_uri)) + self.assertEqual(body.status_code, 401) # TODO: Get API Token + + def test_grafana_get_tags(self): + grafana_uri = get_grafana_uri() + body = http.get("{}/api/dashboards/tags".format(grafana_uri)) + self.assertEqual(body.status_code, 401) # TODO: Get API Token diff --git a/installers/charm/grafana-k8s/tests/bundles/grafana-ha.yaml b/installers/charm/grafana-k8s/tests/bundles/grafana-ha.yaml new file mode 100644 index 00000000..bce8ad62 --- /dev/null +++ b/installers/charm/grafana-k8s/tests/bundles/grafana-ha.yaml @@ -0,0 +1,38 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +bundle: kubernetes +applications: + prometheus-k8s: + charm: 'cs:~charmed-osm/prometheus-k8s' + channel: 'edge' + scale: 2 + options: + default-target: "mon-k8s:8000" + series: kubernetes + storage: + database: 50M + grafana-k8s: + charm: '../../release' + scale: 2 +relations: + - - 'grafana-k8s:prometheus' + - 'prometheus-k8s:prometheus' diff --git a/installers/charm/grafana-k8s/tests/bundles/grafana.yaml b/installers/charm/grafana-k8s/tests/bundles/grafana.yaml new file mode 100644 index 00000000..d144d149 --- /dev/null +++ b/installers/charm/grafana-k8s/tests/bundles/grafana.yaml @@ -0,0 +1,38 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +bundle: kubernetes +applications: + prometheus-k8s: + charm: 'cs:~charmed-osm/prometheus-k8s' + channel: 'edge' + scale: 1 + options: + default-target: "mon-k8s:8000" + series: kubernetes + storage: + database: 50M + grafana-k8s: + charm: '../../release' + scale: 1 +relations: + - - 'grafana-k8s:prometheus' + - 'prometheus-k8s:prometheus' diff --git a/installers/charm/grafana-k8s/tests/tests.yaml b/installers/charm/grafana-k8s/tests/tests.yaml new file mode 100644 index 00000000..281044a1 --- /dev/null +++ b/installers/charm/grafana-k8s/tests/tests.yaml @@ -0,0 +1,28 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +gate_bundles: + - grafana + - grafana-ha +smoke_bundles: + - grafana +tests: + - tests.basic_deployment.BasicDeployment diff --git a/installers/charm/grafana-k8s/tox.ini b/installers/charm/grafana-k8s/tox.ini new file mode 100644 index 00000000..63875b34 --- /dev/null +++ b/installers/charm/grafana-k8s/tox.ini @@ -0,0 +1,82 @@ +# Copyright 2021 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. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact: legal@canonical.com +# +# To get in touch with the maintainers, please contact: +# osm-charmers@lists.launchpad.net +## + +[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:build] +basepython = python3 +passenv=HTTP_PROXY HTTPS_PROXY NO_PROXY +setenv = CHARM_LAYERS_DIR = /tmp +whitelist_externals = git + charm + rm + mv +commands = + rm -rf /tmp/canonical-osm /tmp/osm-common + rm -rf release/ + git clone https://git.launchpad.net/charm-osm-common /tmp/osm-common + charm build . --build-dir /tmp + mv /tmp/grafana-k8s/ release/ + +[testenv:black] +basepython = python3 +deps = + black + yamllint + flake8 +commands = + black --check --diff . + yamllint . + flake8 reactive/ --max-line-length=88 + flake8 tests/ --max-line-length=88 + +[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.25.1