blob: 87776aa9dbc0f9d4ff964ec211e10975708392fa [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 Garciad68e0b42021-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]
sousaedu3ddbbd12021-08-24 19:57:24 +010057 image_pull_policy: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010058
59 @validator("max_file_size")
60 def validate_max_file_size(cls, v):
61 if v < 0:
62 raise ValueError("value must be equal or greater than 0")
63 return v
64
65 @validator("site_url")
66 def validate_site_url(cls, v):
67 if v:
68 parsed = urlparse(v)
69 if not parsed.scheme.startswith("http"):
70 raise ValueError("value must start with http")
71 return v
72
73 @validator("ingress_whitelist_source_range")
74 def validate_ingress_whitelist_source_range(cls, v):
75 if v:
76 ip_network(v)
77 return v
sousaedub17e76b2021-01-26 12:58:25 +010078
sousaedu3ddbbd12021-08-24 19:57:24 +010079 @validator("image_pull_policy")
80 def validate_image_pull_policy(cls, v):
81 values = {
82 "always": "Always",
83 "ifnotpresent": "IfNotPresent",
84 "never": "Never",
85 }
86 v = v.lower()
87 if v not in values.keys():
88 raise ValueError("value must be always, ifnotpresent or never")
89 return values[v]
90
sousaedub17e76b2021-01-26 12:58:25 +010091
David Garcia49379ce2021-02-24 13:48:22 +010092class GrafanaCharm(CharmedOsmBase):
93 """GrafanaCharm Charm."""
sousaedub17e76b2021-01-26 12:58:25 +010094
95 def __init__(self, *args) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +010096 """Prometheus Charm constructor."""
97 super().__init__(*args, oci_image="image")
sousaedub17e76b2021-01-26 12:58:25 +010098
David Garcia49379ce2021-02-24 13:48:22 +010099 self.prometheus_client = PrometheusClient(self, "prometheus")
David Garciac753dc52021-03-17 15:28:47 +0100100 self.framework.observe(
101 self.on["prometheus"].relation_changed, self.configure_pod
102 )
103 self.framework.observe(
104 self.on["prometheus"].relation_broken, self.configure_pod
105 )
sousaedub17e76b2021-01-26 12:58:25 +0100106
David Garcia49379ce2021-02-24 13:48:22 +0100107 def _build_dashboard_files(self, config: ConfigModel):
108 files_builder = FilesV3Builder()
109 files_builder.add_file(
110 "dashboard_osm.yaml",
David Garciad680be42021-08-17 11:03:55 +0200111 Path("templates/default_dashboards.yaml").read_text(),
David Garcia49379ce2021-02-24 13:48:22 +0100112 )
113 if config.osm_dashboards:
114 osm_dashboards_mapping = {
David Garciad680be42021-08-17 11:03:55 +0200115 "kafka_exporter_dashboard.json": "templates/kafka_exporter_dashboard.json",
116 "mongodb_exporter_dashboard.json": "templates/mongodb_exporter_dashboard.json",
117 "mysql_exporter_dashboard.json": "templates/mysql_exporter_dashboard.json",
118 "nodes_exporter_dashboard.json": "templates/nodes_exporter_dashboard.json",
119 "summary_dashboard.json": "templates/summary_dashboard.json",
David Garcia49379ce2021-02-24 13:48:22 +0100120 }
121 for file_name, path in osm_dashboards_mapping.items():
122 files_builder.add_file(file_name, Path(path).read_text())
123 return files_builder.build()
sousaedub17e76b2021-01-26 12:58:25 +0100124
David Garcia49379ce2021-02-24 13:48:22 +0100125 def _build_datasources_files(self):
126 files_builder = FilesV3Builder()
127 files_builder.add_file(
128 "datasource_prometheus.yaml",
David Garciad680be42021-08-17 11:03:55 +0200129 Template(Path("templates/default_datasources.yaml").read_text()).substitute(
David Garcia49379ce2021-02-24 13:48:22 +0100130 prometheus_host=self.prometheus_client.hostname,
131 prometheus_port=self.prometheus_client.port,
132 ),
133 )
134 return files_builder.build()
sousaedub17e76b2021-01-26 12:58:25 +0100135
David Garcia49379ce2021-02-24 13:48:22 +0100136 def _check_missing_dependencies(self):
137 missing_relations = []
sousaedub17e76b2021-01-26 12:58:25 +0100138
David Garcia49379ce2021-02-24 13:48:22 +0100139 if self.prometheus_client.is_missing_data_in_app():
140 missing_relations.append("prometheus")
sousaedub17e76b2021-01-26 12:58:25 +0100141
David Garcia49379ce2021-02-24 13:48:22 +0100142 if missing_relations:
143 raise RelationsMissing(missing_relations)
sousaedub17e76b2021-01-26 12:58:25 +0100144
David Garcia49379ce2021-02-24 13:48:22 +0100145 def build_pod_spec(self, image_info):
146 # Validate config
147 config = ConfigModel(**dict(self.config))
148 # Check relations
149 self._check_missing_dependencies()
150 # Create Builder for the PodSpec
151 pod_spec_builder = PodSpecV3Builder()
152 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100153 container_builder = ContainerV3Builder(
154 self.app.name, image_info, config.image_pull_policy
155 )
David Garcia49379ce2021-02-24 13:48:22 +0100156 container_builder.add_port(name=self.app.name, port=PORT)
157 container_builder.add_http_readiness_probe(
158 "/api/health",
159 PORT,
160 initial_delay_seconds=10,
161 period_seconds=10,
162 timeout_seconds=5,
163 failure_threshold=3,
164 )
165 container_builder.add_http_liveness_probe(
166 "/api/health",
167 PORT,
168 initial_delay_seconds=60,
169 timeout_seconds=30,
170 failure_threshold=10,
171 )
172 container_builder.add_volume_config(
173 "dashboards",
174 "/etc/grafana/provisioning/dashboards/",
175 self._build_dashboard_files(config),
176 )
177 container_builder.add_volume_config(
178 "datasources",
179 "/etc/grafana/provisioning/datasources/",
180 self._build_datasources_files(),
181 )
182 container = container_builder.build()
183 # Add container to pod spec
184 pod_spec_builder.add_container(container)
185 # Add ingress resources to pod spec if site url exists
186 if config.site_url:
187 parsed = urlparse(config.site_url)
188 annotations = {
189 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
190 str(config.max_file_size) + "m"
191 if config.max_file_size > 0
192 else config.max_file_size
David Garciad68e0b42021-06-28 16:50:42 +0200193 )
David Garcia49379ce2021-02-24 13:48:22 +0100194 }
David Garciad68e0b42021-06-28 16:50:42 +0200195 if config.ingress_class:
196 annotations["kubernetes.io/ingress.class"] = config.ingress_class
David Garcia49379ce2021-02-24 13:48:22 +0100197 ingress_resource_builder = IngressResourceV3Builder(
198 f"{self.app.name}-ingress", annotations
sousaedub17e76b2021-01-26 12:58:25 +0100199 )
sousaedub17e76b2021-01-26 12:58:25 +0100200
David Garcia49379ce2021-02-24 13:48:22 +0100201 if config.ingress_whitelist_source_range:
202 annotations[
203 "nginx.ingress.kubernetes.io/whitelist-source-range"
204 ] = config.ingress_whitelist_source_range
sousaedub17e76b2021-01-26 12:58:25 +0100205
sousaedu3cc03162021-04-29 16:53:12 +0200206 if config.cluster_issuer:
207 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
208
David Garcia49379ce2021-02-24 13:48:22 +0100209 if parsed.scheme == "https":
210 ingress_resource_builder.add_tls(
211 [parsed.hostname], config.tls_secret_name
212 )
213 else:
214 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
215
216 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
217 ingress_resource = ingress_resource_builder.build()
218 pod_spec_builder.add_ingress_resource(ingress_resource)
219 return pod_spec_builder.build()
sousaedub17e76b2021-01-26 12:58:25 +0100220
221
222if __name__ == "__main__":
223 main(GrafanaCharm)