From 1ec36ccbb8c823266918d5b9e24259dad717e139 Mon Sep 17 00:00:00 2001 From: sousaedu Date: Wed, 3 Mar 2021 01:12:49 +0100 Subject: [PATCH] Adding backup action to prometheus charm Change-Id: Icfc97e308c7d49eb1e7c5c18b6f21648822cba87 Signed-off-by: sousaedu --- installers/charm/prometheus/actions.yaml | 23 +++++++++++++++++ installers/charm/prometheus/config.yaml | 2 +- installers/charm/prometheus/metadata.yaml | 4 +++ installers/charm/prometheus/requirements.txt | 6 ++++- installers/charm/prometheus/src/charm.py | 26 ++++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 installers/charm/prometheus/actions.yaml diff --git a/installers/charm/prometheus/actions.yaml b/installers/charm/prometheus/actions.yaml new file mode 100644 index 00000000..e41f3df0 --- /dev/null +++ b/installers/charm/prometheus/actions.yaml @@ -0,0 +1,23 @@ +# 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 +## + +backup: + description: "Do a mongodb backup" diff --git a/installers/charm/prometheus/config.yaml b/installers/charm/prometheus/config.yaml index d953de33..9f35e511 100644 --- a/installers/charm/prometheus/config.yaml +++ b/installers/charm/prometheus/config.yaml @@ -55,4 +55,4 @@ options: enable_web_admin_api: type: boolean description: Boolean to enable the web admin api - default: False + default: false diff --git a/installers/charm/prometheus/metadata.yaml b/installers/charm/prometheus/metadata.yaml index f021418c..0e09c3b5 100644 --- a/installers/charm/prometheus/metadata.yaml +++ b/installers/charm/prometheus/metadata.yaml @@ -34,6 +34,10 @@ deployment: type: stateful service: cluster resources: + backup-image: + type: oci-image + description: Container image to run backup actions + upstream-source: "ed1000/prometheus-backup:latest" image: type: oci-image description: OSM docker image for Prometheus diff --git a/installers/charm/prometheus/requirements.txt b/installers/charm/prometheus/requirements.txt index 1a8928c7..e1150c5d 100644 --- a/installers/charm/prometheus/requirements.txt +++ b/installers/charm/prometheus/requirements.txt @@ -19,4 +19,8 @@ # osm-charmers@lists.launchpad.net ## -git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master \ No newline at end of file +git+https://github.com/charmed-osm/ops-lib-charmed-osm/@master +git+https://github.com/juju-solutions/resource-oci-image/@c5778285d332edf3d9a538f9d0c06154b7ec1b0b#egg=oci-image +ops +requests +urllib3>1.25.9 diff --git a/installers/charm/prometheus/src/charm.py b/installers/charm/prometheus/src/charm.py index 47533ddf..5cd163da 100755 --- a/installers/charm/prometheus/src/charm.py +++ b/installers/charm/prometheus/src/charm.py @@ -27,6 +27,7 @@ import logging from typing import NoReturn, Optional from urllib.parse import urlparse +from oci_image import OCIImageResource from ops.framework import EventBase from ops.main import main from opslib.osm.charm import CharmedOsmBase @@ -41,6 +42,7 @@ from opslib.osm.validator import ( ModelValidator, validator, ) +import requests logger = logging.getLogger(__name__) @@ -99,9 +101,24 @@ class PrometheusCharm(CharmedOsmBase): self._publish_prometheus_info, ) + # Registering actions + self.framework.observe( + self.on.backup_action, # pylint: disable=E1101 + self._on_backup_action, + ) + def _publish_prometheus_info(self, event: EventBase) -> NoReturn: self.prometheus.publish_info(self.app.name, PORT) + def _on_backup_action(self, event: EventBase) -> NoReturn: + url = f"http://{self.model.app.name}:{PORT}/api/v2/admin/tsdb/snapshot" + result = requests.post(url) + + if result.status_code == 200: + event.set_results({"backup-name": result.json()["name"]}) + else: + event.fail(f"status-code: {result.status_code}, result: {result.json()}") + def _build_files(self, config: ConfigModel): files_builder = FilesV3Builder() files_builder.add_file( @@ -128,6 +145,15 @@ class PrometheusCharm(CharmedOsmBase): config = ConfigModel(**dict(self.config)) # Create Builder for the PodSpec pod_spec_builder = PodSpecV3Builder() + + # Build Backup Container + backup_image = OCIImageResource(self, "backup-image") + backup_image_info = backup_image.fetch() + backup_container_builder = ContainerV3Builder("prom-backup", backup_image_info) + backup_container = backup_container_builder.build() + # Add backup container to pod spec + pod_spec_builder.add_container(backup_container) + # Build Container container_builder = ContainerV3Builder(self.app.name, image_info) container_builder.add_port(name=self.app.name, port=PORT) -- 2.17.1