blob: 28be79aad3d204498cfedd9bbf61412dd484d804 [file] [log] [blame]
sousaedub17e76b2021-01-26 12:58:25 +01001#!/usr/bin/env python3
2# Copyright 2021 Canonical Ltd.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
16# For those usages not covered by the Apache License, Version 2.0 please
17# contact: legal@canonical.com
18#
19# To get in touch with the maintainers, please contact:
20# osm-charmers@lists.launchpad.net
21##
22
David Garcia49379ce2021-02-24 13:48:22 +010023# pylint: disable=E0213
24
David Garcia49379ce2021-02-24 13:48:22 +010025from ipaddress import ip_network
David Garciac753dc52021-03-17 15:28:47 +010026import logging
27from pathlib import Path
28from string import Template
29from typing import NoReturn, Optional
30from urllib.parse import urlparse
sousaedub17e76b2021-01-26 12:58:25 +010031
sousaedub17e76b2021-01-26 12:58:25 +010032from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010033from opslib.osm.charm import CharmedOsmBase, RelationsMissing
David Garciac753dc52021-03-17 15:28:47 +010034from opslib.osm.interfaces.prometheus import PrometheusClient
David Garcia49379ce2021-02-24 13:48:22 +010035from opslib.osm.pod import (
David Garcia49379ce2021-02-24 13:48:22 +010036 ContainerV3Builder,
David Garciac753dc52021-03-17 15:28:47 +010037 FilesV3Builder,
38 IngressResourceV3Builder,
David Garcia49379ce2021-02-24 13:48:22 +010039 PodSpecV3Builder,
40)
David Garciac753dc52021-03-17 15:28:47 +010041from opslib.osm.validator import ModelValidator, validator
David Garcia49379ce2021-02-24 13:48:22 +010042
43
sousaedub17e76b2021-01-26 12:58:25 +010044logger = logging.getLogger(__name__)
45
David Garcia49379ce2021-02-24 13:48:22 +010046PORT = 3000
sousaedub17e76b2021-01-26 12:58:25 +010047
48
David Garcia49379ce2021-02-24 13:48:22 +010049class ConfigModel(ModelValidator):
50 max_file_size: int
51 osm_dashboards: bool
52 site_url: Optional[str]
sousaedu3cc03162021-04-29 16:53:12 +020053 cluster_issuer: Optional[str]
David Garciac35943e2021-06-28 16:50:42 +020054 ingress_class: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010055 ingress_whitelist_source_range: Optional[str]
56 tls_secret_name: Optional[str]
57
58 @validator("max_file_size")
59 def validate_max_file_size(cls, v):
60 if v < 0:
61 raise ValueError("value must be equal or greater than 0")
62 return v
63
64 @validator("site_url")
65 def validate_site_url(cls, v):
66 if v:
67 parsed = urlparse(v)
68 if not parsed.scheme.startswith("http"):
69 raise ValueError("value must start with http")
70 return v
71
72 @validator("ingress_whitelist_source_range")
73 def validate_ingress_whitelist_source_range(cls, v):
74 if v:
75 ip_network(v)
76 return v
sousaedub17e76b2021-01-26 12:58:25 +010077
78
David Garcia49379ce2021-02-24 13:48:22 +010079class GrafanaCharm(CharmedOsmBase):
80 """GrafanaCharm Charm."""
sousaedub17e76b2021-01-26 12:58:25 +010081
82 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +010083 """Prometheus Charm constructor."""
84 super().__init__(*args, oci_image="image")
sousaedub17e76b2021-01-26 12:58:25 +010085
David Garcia49379ce2021-02-24 13:48:22 +010086 self.prometheus_client = PrometheusClient(self, "prometheus")
David Garciac753dc52021-03-17 15:28:47 +010087 self.framework.observe(
88 self.on["prometheus"].relation_changed, self.configure_pod
89 )
90 self.framework.observe(
91 self.on["prometheus"].relation_broken, self.configure_pod
92 )
sousaedub17e76b2021-01-26 12:58:25 +010093
David Garcia49379ce2021-02-24 13:48:22 +010094 def _build_dashboard_files(self, config: ConfigModel):
95 files_builder = FilesV3Builder()
96 files_builder.add_file(
97 "dashboard_osm.yaml",
98 Path("files/default_dashboards.yaml").read_text(),
99 )
100 if config.osm_dashboards:
101 osm_dashboards_mapping = {
sousaedu813bef82021-03-08 01:15:41 +0100102 "kafka_exporter_dashboard.json": "files/kafka_exporter_dashboard.json",
103 "mongodb_exporter_dashboard.json": "files/mongodb_exporter_dashboard.json",
104 "mysql_exporter_dashboard.json": "files/mysql_exporter_dashboard.json",
105 "nodes_exporter_dashboard.json": "files/nodes_exporter_dashboard.json",
106 "summary_dashboard.json": "files/summary_dashboard.json",
David Garcia49379ce2021-02-24 13:48:22 +0100107 }
108 for file_name, path in osm_dashboards_mapping.items():
109 files_builder.add_file(file_name, Path(path).read_text())
110 return files_builder.build()
sousaedub17e76b2021-01-26 12:58:25 +0100111
David Garcia49379ce2021-02-24 13:48:22 +0100112 def _build_datasources_files(self):
113 files_builder = FilesV3Builder()
114 files_builder.add_file(
115 "datasource_prometheus.yaml",
116 Template(Path("files/default_datasources.yaml").read_text()).substitute(
117 prometheus_host=self.prometheus_client.hostname,
118 prometheus_port=self.prometheus_client.port,
119 ),
120 )
121 return files_builder.build()
sousaedub17e76b2021-01-26 12:58:25 +0100122
David Garcia49379ce2021-02-24 13:48:22 +0100123 def _check_missing_dependencies(self):
124 missing_relations = []
sousaedub17e76b2021-01-26 12:58:25 +0100125
David Garcia49379ce2021-02-24 13:48:22 +0100126 if self.prometheus_client.is_missing_data_in_app():
127 missing_relations.append("prometheus")
sousaedub17e76b2021-01-26 12:58:25 +0100128
David Garcia49379ce2021-02-24 13:48:22 +0100129 if missing_relations:
130 raise RelationsMissing(missing_relations)
sousaedub17e76b2021-01-26 12:58:25 +0100131
David Garcia49379ce2021-02-24 13:48:22 +0100132 def build_pod_spec(self, image_info):
133 # Validate config
134 config = ConfigModel(**dict(self.config))
135 # Check relations
136 self._check_missing_dependencies()
137 # Create Builder for the PodSpec
138 pod_spec_builder = PodSpecV3Builder()
139 # Build Container
140 container_builder = ContainerV3Builder(self.app.name, image_info)
141 container_builder.add_port(name=self.app.name, port=PORT)
142 container_builder.add_http_readiness_probe(
143 "/api/health",
144 PORT,
145 initial_delay_seconds=10,
146 period_seconds=10,
147 timeout_seconds=5,
148 failure_threshold=3,
149 )
150 container_builder.add_http_liveness_probe(
151 "/api/health",
152 PORT,
153 initial_delay_seconds=60,
154 timeout_seconds=30,
155 failure_threshold=10,
156 )
157 container_builder.add_volume_config(
158 "dashboards",
159 "/etc/grafana/provisioning/dashboards/",
160 self._build_dashboard_files(config),
161 )
162 container_builder.add_volume_config(
163 "datasources",
164 "/etc/grafana/provisioning/datasources/",
165 self._build_datasources_files(),
166 )
167 container = container_builder.build()
168 # Add container to pod spec
169 pod_spec_builder.add_container(container)
170 # Add ingress resources to pod spec if site url exists
171 if config.site_url:
172 parsed = urlparse(config.site_url)
173 annotations = {
174 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
175 str(config.max_file_size) + "m"
176 if config.max_file_size > 0
177 else config.max_file_size
David Garciac35943e2021-06-28 16:50:42 +0200178 )
David Garcia49379ce2021-02-24 13:48:22 +0100179 }
David Garciac35943e2021-06-28 16:50:42 +0200180 if config.ingress_class:
181 annotations["kubernetes.io/ingress.class"] = config.ingress_class
David Garcia49379ce2021-02-24 13:48:22 +0100182 ingress_resource_builder = IngressResourceV3Builder(
183 f"{self.app.name}-ingress", annotations
sousaedub17e76b2021-01-26 12:58:25 +0100184 )
sousaedub17e76b2021-01-26 12:58:25 +0100185
David Garcia49379ce2021-02-24 13:48:22 +0100186 if config.ingress_whitelist_source_range:
187 annotations[
188 "nginx.ingress.kubernetes.io/whitelist-source-range"
189 ] = config.ingress_whitelist_source_range
sousaedub17e76b2021-01-26 12:58:25 +0100190
sousaedu3cc03162021-04-29 16:53:12 +0200191 if config.cluster_issuer:
192 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
193
David Garcia49379ce2021-02-24 13:48:22 +0100194 if parsed.scheme == "https":
195 ingress_resource_builder.add_tls(
196 [parsed.hostname], config.tls_secret_name
197 )
198 else:
199 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
200
201 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
202 ingress_resource = ingress_resource_builder.build()
203 pod_spec_builder.add_ingress_resource(ingress_resource)
204 return pod_spec_builder.build()
sousaedub17e76b2021-01-26 12:58:25 +0100205
206
207if __name__ == "__main__":
208 main(GrafanaCharm)