| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 1 | #!/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 Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 23 | # pylint: disable=E0213 |
| 24 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 25 | from ipaddress import ip_network |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 26 | import logging |
| 27 | from pathlib import Path |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 28 | import secrets |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 29 | from string import Template |
| 30 | from typing import NoReturn, Optional |
| 31 | from urllib.parse import urlparse |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 32 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 33 | from ops.main import main |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 34 | from opslib.osm.charm import CharmedOsmBase, RelationsMissing |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 35 | from opslib.osm.interfaces.grafana import GrafanaCluster |
| 36 | from opslib.osm.interfaces.mysql import MysqlClient |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 37 | from opslib.osm.interfaces.prometheus import PrometheusClient |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 38 | from opslib.osm.pod import ( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 39 | ContainerV3Builder, |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 40 | FilesV3Builder, |
| 41 | IngressResourceV3Builder, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 42 | PodSpecV3Builder, |
| 43 | ) |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 44 | from opslib.osm.validator import ModelValidator, validator |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 45 | |
| 46 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 47 | logger = logging.getLogger(__name__) |
| 48 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 49 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 50 | class ConfigModel(ModelValidator): |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 51 | log_level: str |
| 52 | port: int |
| 53 | admin_user: str |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 54 | max_file_size: int |
| 55 | osm_dashboards: bool |
| 56 | site_url: Optional[str] |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 57 | cluster_issuer: Optional[str] |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 58 | ingress_class: Optional[str] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 59 | ingress_whitelist_source_range: Optional[str] |
| 60 | tls_secret_name: Optional[str] |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 61 | image_pull_policy: Optional[str] |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 62 | |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 63 | @validator("log_level") |
| 64 | def validate_log_level(cls, v): |
| 65 | allowed_values = ("debug", "info", "warn", "error", "critical") |
| 66 | if v not in allowed_values: |
| 67 | separator = '", "' |
| 68 | raise ValueError( |
| 69 | f'incorrect value. Allowed values are "{separator.join(allowed_values)}"' |
| 70 | ) |
| 71 | return v |
| 72 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 73 | @validator("max_file_size") |
| 74 | def validate_max_file_size(cls, v): |
| 75 | if v < 0: |
| 76 | raise ValueError("value must be equal or greater than 0") |
| 77 | return v |
| 78 | |
| 79 | @validator("site_url") |
| 80 | def validate_site_url(cls, v): |
| 81 | if v: |
| 82 | parsed = urlparse(v) |
| 83 | if not parsed.scheme.startswith("http"): |
| 84 | raise ValueError("value must start with http") |
| 85 | return v |
| 86 | |
| 87 | @validator("ingress_whitelist_source_range") |
| 88 | def validate_ingress_whitelist_source_range(cls, v): |
| 89 | if v: |
| 90 | ip_network(v) |
| 91 | return v |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 92 | |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 93 | @validator("image_pull_policy") |
| 94 | def validate_image_pull_policy(cls, v): |
| 95 | values = { |
| 96 | "always": "Always", |
| 97 | "ifnotpresent": "IfNotPresent", |
| 98 | "never": "Never", |
| 99 | } |
| 100 | v = v.lower() |
| 101 | if v not in values.keys(): |
| 102 | raise ValueError("value must be always, ifnotpresent or never") |
| 103 | return values[v] |
| 104 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 105 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 106 | class GrafanaCharm(CharmedOsmBase): |
| 107 | """GrafanaCharm Charm.""" |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 108 | |
| 109 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 110 | """Prometheus Charm constructor.""" |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 111 | super().__init__(*args, oci_image="image", mysql_uri=True) |
| 112 | # Initialize relation objects |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 113 | self.prometheus_client = PrometheusClient(self, "prometheus") |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 114 | self.grafana_cluster = GrafanaCluster(self, "cluster") |
| 115 | self.mysql_client = MysqlClient(self, "db") |
| 116 | # Observe events |
| 117 | event_observer_mapping = { |
| 118 | self.on["prometheus"].relation_changed: self.configure_pod, |
| 119 | self.on["prometheus"].relation_broken: self.configure_pod, |
| 120 | self.on["db"].relation_changed: self.configure_pod, |
| 121 | self.on["db"].relation_broken: self.configure_pod, |
| 122 | } |
| 123 | for event, observer in event_observer_mapping.items(): |
| 124 | self.framework.observe(event, observer) |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 125 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 126 | def _build_dashboard_files(self, config: ConfigModel): |
| 127 | files_builder = FilesV3Builder() |
| 128 | files_builder.add_file( |
| 129 | "dashboard_osm.yaml", |
| David Garcia | d680be4 | 2021-08-17 11:03:55 +0200 | [diff] [blame] | 130 | Path("templates/default_dashboards.yaml").read_text(), |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 131 | ) |
| 132 | if config.osm_dashboards: |
| 133 | osm_dashboards_mapping = { |
| David Garcia | d680be4 | 2021-08-17 11:03:55 +0200 | [diff] [blame] | 134 | "kafka_exporter_dashboard.json": "templates/kafka_exporter_dashboard.json", |
| 135 | "mongodb_exporter_dashboard.json": "templates/mongodb_exporter_dashboard.json", |
| 136 | "mysql_exporter_dashboard.json": "templates/mysql_exporter_dashboard.json", |
| 137 | "nodes_exporter_dashboard.json": "templates/nodes_exporter_dashboard.json", |
| 138 | "summary_dashboard.json": "templates/summary_dashboard.json", |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 139 | } |
| 140 | for file_name, path in osm_dashboards_mapping.items(): |
| 141 | files_builder.add_file(file_name, Path(path).read_text()) |
| 142 | return files_builder.build() |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 143 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 144 | def _build_datasources_files(self): |
| 145 | files_builder = FilesV3Builder() |
| 146 | files_builder.add_file( |
| 147 | "datasource_prometheus.yaml", |
| David Garcia | d680be4 | 2021-08-17 11:03:55 +0200 | [diff] [blame] | 148 | Template(Path("templates/default_datasources.yaml").read_text()).substitute( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 149 | prometheus_host=self.prometheus_client.hostname, |
| 150 | prometheus_port=self.prometheus_client.port, |
| 151 | ), |
| 152 | ) |
| 153 | return files_builder.build() |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 154 | |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 155 | def _check_missing_dependencies(self, config: ConfigModel, external_db: bool): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 156 | missing_relations = [] |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 157 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 158 | if self.prometheus_client.is_missing_data_in_app(): |
| 159 | missing_relations.append("prometheus") |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 160 | |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 161 | if not external_db and self.mysql_client.is_missing_data_in_unit(): |
| 162 | missing_relations.append("db") |
| 163 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 164 | if missing_relations: |
| 165 | raise RelationsMissing(missing_relations) |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 166 | |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 167 | def build_pod_spec(self, image_info, **kwargs): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 168 | # Validate config |
| 169 | config = ConfigModel(**dict(self.config)) |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 170 | mysql_config = kwargs["mysql_config"] |
| 171 | if mysql_config.mysql_uri and not self.mysql_client.is_missing_data_in_unit(): |
| 172 | raise Exception("Mysql data cannot be provided via config and relation") |
| 173 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 174 | # Check relations |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 175 | external_db = True if mysql_config.mysql_uri else False |
| 176 | self._check_missing_dependencies(config, external_db) |
| 177 | |
| 178 | # Get initial password |
| 179 | admin_initial_password = self.grafana_cluster.admin_initial_password |
| 180 | if not admin_initial_password: |
| 181 | admin_initial_password = _generate_random_password() |
| 182 | self.grafana_cluster.set_initial_password(admin_initial_password) |
| 183 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 184 | # Create Builder for the PodSpec |
| 185 | pod_spec_builder = PodSpecV3Builder() |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 186 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 187 | # Build Container |
| sousaedu | 3ddbbd1 | 2021-08-24 19:57:24 +0100 | [diff] [blame] | 188 | container_builder = ContainerV3Builder( |
| 189 | self.app.name, image_info, config.image_pull_policy |
| 190 | ) |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 191 | container_builder.add_port(name=self.app.name, port=config.port) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 192 | container_builder.add_http_readiness_probe( |
| 193 | "/api/health", |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 194 | config.port, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 195 | initial_delay_seconds=10, |
| 196 | period_seconds=10, |
| 197 | timeout_seconds=5, |
| 198 | failure_threshold=3, |
| 199 | ) |
| 200 | container_builder.add_http_liveness_probe( |
| 201 | "/api/health", |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 202 | config.port, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 203 | initial_delay_seconds=60, |
| 204 | timeout_seconds=30, |
| 205 | failure_threshold=10, |
| 206 | ) |
| 207 | container_builder.add_volume_config( |
| 208 | "dashboards", |
| 209 | "/etc/grafana/provisioning/dashboards/", |
| 210 | self._build_dashboard_files(config), |
| 211 | ) |
| 212 | container_builder.add_volume_config( |
| 213 | "datasources", |
| 214 | "/etc/grafana/provisioning/datasources/", |
| 215 | self._build_datasources_files(), |
| 216 | ) |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 217 | |
| 218 | container_builder.add_envs( |
| 219 | { |
| 220 | "GF_SERVER_HTTP_PORT": config.port, |
| 221 | "GF_LOG_LEVEL": config.log_level, |
| 222 | "GF_SECURITY_ADMIN_USER": config.admin_user, |
| 223 | "GF_SECURITY_ADMIN_PASSWORD": { |
| 224 | "secret": {"name": "grafana-admin-secret", "key": "admin-password"} |
| 225 | }, |
| 226 | "GF_DATABASE_URL": { |
| 227 | "secret": {"name": "grafana-admin-secret", "key": "mysql-url"} |
| 228 | }, |
| 229 | }, |
| 230 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 231 | container = container_builder.build() |
| 232 | # Add container to pod spec |
| 233 | pod_spec_builder.add_container(container) |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 234 | pod_spec_builder.add_secret( |
| 235 | "grafana-admin-secret", |
| 236 | { |
| 237 | "admin-password": admin_initial_password, |
| 238 | "mysql-url": mysql_config.mysql_uri or self.mysql_client.get_uri(), |
| 239 | }, |
| 240 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 241 | # Add ingress resources to pod spec if site url exists |
| 242 | if config.site_url: |
| 243 | parsed = urlparse(config.site_url) |
| 244 | annotations = { |
| 245 | "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format( |
| 246 | str(config.max_file_size) + "m" |
| 247 | if config.max_file_size > 0 |
| 248 | else config.max_file_size |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 249 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 250 | } |
| David Garcia | d68e0b4 | 2021-06-28 16:50:42 +0200 | [diff] [blame] | 251 | if config.ingress_class: |
| 252 | annotations["kubernetes.io/ingress.class"] = config.ingress_class |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 253 | ingress_resource_builder = IngressResourceV3Builder( |
| 254 | f"{self.app.name}-ingress", annotations |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 255 | ) |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 256 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 257 | if config.ingress_whitelist_source_range: |
| 258 | annotations[ |
| 259 | "nginx.ingress.kubernetes.io/whitelist-source-range" |
| 260 | ] = config.ingress_whitelist_source_range |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 261 | |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 262 | if config.cluster_issuer: |
| 263 | annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer |
| 264 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 265 | if parsed.scheme == "https": |
| 266 | ingress_resource_builder.add_tls( |
| 267 | [parsed.hostname], config.tls_secret_name |
| 268 | ) |
| 269 | else: |
| 270 | annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false" |
| 271 | |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 272 | ingress_resource_builder.add_rule( |
| 273 | parsed.hostname, self.app.name, config.port |
| 274 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 275 | ingress_resource = ingress_resource_builder.build() |
| 276 | pod_spec_builder.add_ingress_resource(ingress_resource) |
| 277 | return pod_spec_builder.build() |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 278 | |
| 279 | |
| sousaedu | 28dfe7e | 2021-06-30 15:03:28 +0100 | [diff] [blame^] | 280 | def _generate_random_password(): |
| 281 | return secrets.token_hex(16) |
| 282 | |
| 283 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 284 | if __name__ == "__main__": |
| 285 | main(GrafanaCharm) |