| 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 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 25 | import logging |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 26 | from typing import Optional, NoReturn |
| 27 | from ipaddress import ip_network |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 28 | |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 29 | from ops.main import main |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 30 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 31 | from opslib.osm.charm import CharmedOsmBase, RelationsMissing |
| 32 | |
| 33 | from opslib.osm.pod import ( |
| 34 | IngressResourceV3Builder, |
| 35 | FilesV3Builder, |
| 36 | ContainerV3Builder, |
| 37 | PodSpecV3Builder, |
| 38 | ) |
| 39 | |
| 40 | |
| 41 | from opslib.osm.validator import ( |
| 42 | ModelValidator, |
| 43 | validator, |
| 44 | ) |
| 45 | |
| 46 | from opslib.osm.interfaces.prometheus import PrometheusClient |
| 47 | |
| 48 | from urllib.parse import urlparse |
| 49 | from string import Template |
| 50 | from pathlib import Path |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 51 | |
| 52 | logger = logging.getLogger(__name__) |
| 53 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 54 | PORT = 3000 |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 55 | |
| 56 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 57 | class ConfigModel(ModelValidator): |
| 58 | max_file_size: int |
| 59 | osm_dashboards: bool |
| 60 | site_url: Optional[str] |
| 61 | ingress_whitelist_source_range: Optional[str] |
| 62 | tls_secret_name: Optional[str] |
| 63 | |
| 64 | @validator("max_file_size") |
| 65 | def validate_max_file_size(cls, v): |
| 66 | if v < 0: |
| 67 | raise ValueError("value must be equal or greater than 0") |
| 68 | return v |
| 69 | |
| 70 | @validator("site_url") |
| 71 | def validate_site_url(cls, v): |
| 72 | if v: |
| 73 | parsed = urlparse(v) |
| 74 | if not parsed.scheme.startswith("http"): |
| 75 | raise ValueError("value must start with http") |
| 76 | return v |
| 77 | |
| 78 | @validator("ingress_whitelist_source_range") |
| 79 | def validate_ingress_whitelist_source_range(cls, v): |
| 80 | if v: |
| 81 | ip_network(v) |
| 82 | return v |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 83 | |
| 84 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 85 | class GrafanaCharm(CharmedOsmBase): |
| 86 | """GrafanaCharm Charm.""" |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 87 | |
| 88 | def __init__(self, *args) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 89 | """Prometheus Charm constructor.""" |
| 90 | super().__init__(*args, oci_image="image") |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 91 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 92 | self.prometheus_client = PrometheusClient(self, "prometheus") |
| 93 | self.framework.observe(self.on["prometheus"].relation_changed, self.configure_pod) |
| 94 | self.framework.observe(self.on["prometheus"].relation_broken, self.configure_pod) |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 95 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 96 | def _build_dashboard_files(self, config: ConfigModel): |
| 97 | files_builder = FilesV3Builder() |
| 98 | files_builder.add_file( |
| 99 | "dashboard_osm.yaml", |
| 100 | Path("files/default_dashboards.yaml").read_text(), |
| 101 | ) |
| 102 | if config.osm_dashboards: |
| 103 | osm_dashboards_mapping = { |
| 104 | "kafka_exporter_dashboard.yaml": "files/kafka_exporter_dashboard.yaml", |
| 105 | "mongodb_exporter_dashboard.yaml": "files/mongodb_exporter_dashboard.yaml", |
| 106 | "mysql_exporter_dashboard.yaml": "files/mysql_exporter_dashboard.yaml", |
| 107 | "nodes_exporter_dashboard.yaml": "files/nodes_exporter_dashboard.yaml", |
| 108 | "summary_dashboard.yaml": "files/summary_dashboard.yaml", |
| 109 | } |
| 110 | for file_name, path in osm_dashboards_mapping.items(): |
| 111 | files_builder.add_file(file_name, Path(path).read_text()) |
| 112 | return files_builder.build() |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 113 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 114 | def _build_datasources_files(self): |
| 115 | files_builder = FilesV3Builder() |
| 116 | files_builder.add_file( |
| 117 | "datasource_prometheus.yaml", |
| 118 | Template(Path("files/default_datasources.yaml").read_text()).substitute( |
| 119 | prometheus_host=self.prometheus_client.hostname, |
| 120 | prometheus_port=self.prometheus_client.port, |
| 121 | ), |
| 122 | ) |
| 123 | return files_builder.build() |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 124 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 125 | def _check_missing_dependencies(self): |
| 126 | missing_relations = [] |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 127 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 128 | if self.prometheus_client.is_missing_data_in_app(): |
| 129 | missing_relations.append("prometheus") |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 130 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 131 | if missing_relations: |
| 132 | raise RelationsMissing(missing_relations) |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 133 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 134 | def build_pod_spec(self, image_info): |
| 135 | # Validate config |
| 136 | config = ConfigModel(**dict(self.config)) |
| 137 | # Check relations |
| 138 | self._check_missing_dependencies() |
| 139 | # Create Builder for the PodSpec |
| 140 | pod_spec_builder = PodSpecV3Builder() |
| 141 | # Build Container |
| 142 | container_builder = ContainerV3Builder(self.app.name, image_info) |
| 143 | container_builder.add_port(name=self.app.name, port=PORT) |
| 144 | container_builder.add_http_readiness_probe( |
| 145 | "/api/health", |
| 146 | PORT, |
| 147 | initial_delay_seconds=10, |
| 148 | period_seconds=10, |
| 149 | timeout_seconds=5, |
| 150 | failure_threshold=3, |
| 151 | ) |
| 152 | container_builder.add_http_liveness_probe( |
| 153 | "/api/health", |
| 154 | PORT, |
| 155 | initial_delay_seconds=60, |
| 156 | timeout_seconds=30, |
| 157 | failure_threshold=10, |
| 158 | ) |
| 159 | container_builder.add_volume_config( |
| 160 | "dashboards", |
| 161 | "/etc/grafana/provisioning/dashboards/", |
| 162 | self._build_dashboard_files(config), |
| 163 | ) |
| 164 | container_builder.add_volume_config( |
| 165 | "datasources", |
| 166 | "/etc/grafana/provisioning/datasources/", |
| 167 | self._build_datasources_files(), |
| 168 | ) |
| 169 | container = container_builder.build() |
| 170 | # Add container to pod spec |
| 171 | pod_spec_builder.add_container(container) |
| 172 | # Add ingress resources to pod spec if site url exists |
| 173 | if config.site_url: |
| 174 | parsed = urlparse(config.site_url) |
| 175 | annotations = { |
| 176 | "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format( |
| 177 | str(config.max_file_size) + "m" |
| 178 | if config.max_file_size > 0 |
| 179 | else config.max_file_size |
| 180 | ), |
| 181 | } |
| 182 | ingress_resource_builder = IngressResourceV3Builder( |
| 183 | f"{self.app.name}-ingress", annotations |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 184 | ) |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 185 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 186 | if config.ingress_whitelist_source_range: |
| 187 | annotations[ |
| 188 | "nginx.ingress.kubernetes.io/whitelist-source-range" |
| 189 | ] = config.ingress_whitelist_source_range |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 190 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame^] | 191 | if parsed.scheme == "https": |
| 192 | ingress_resource_builder.add_tls( |
| 193 | [parsed.hostname], config.tls_secret_name |
| 194 | ) |
| 195 | else: |
| 196 | annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false" |
| 197 | |
| 198 | ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT) |
| 199 | ingress_resource = ingress_resource_builder.build() |
| 200 | pod_spec_builder.add_ingress_resource(ingress_resource) |
| 201 | return pod_spec_builder.build() |
| sousaedu | b17e76b | 2021-01-26 12:58:25 +0100 | [diff] [blame] | 202 | |
| 203 | |
| 204 | if __name__ == "__main__": |
| 205 | main(GrafanaCharm) |