| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [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 typing import NoReturn, Optional |
| 28 | from urllib.parse import urlparse |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 29 | |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 30 | from oci_image import OCIImageResource |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 31 | from ops.framework import EventBase |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 32 | from ops.main import main |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 33 | from opslib.osm.charm import CharmedOsmBase |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 34 | from opslib.osm.interfaces.prometheus import PrometheusServer |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 35 | from opslib.osm.pod import ( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 36 | ContainerV3Builder, |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 37 | FilesV3Builder, |
| 38 | IngressResourceV3Builder, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 39 | PodSpecV3Builder, |
| 40 | ) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 41 | from opslib.osm.validator import ( |
| 42 | ModelValidator, |
| 43 | validator, |
| 44 | ) |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 45 | import requests |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 46 | |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 47 | |
| 48 | logger = logging.getLogger(__name__) |
| 49 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 50 | PORT = 9090 |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 51 | |
| 52 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 53 | class ConfigModel(ModelValidator): |
| 54 | web_subpath: str |
| 55 | default_target: str |
| 56 | max_file_size: int |
| 57 | site_url: Optional[str] |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 58 | cluster_issuer: 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] |
| 61 | enable_web_admin_api: bool |
| 62 | |
| 63 | @validator("web_subpath") |
| 64 | def validate_web_subpath(cls, v): |
| 65 | if len(v) < 1: |
| 66 | raise ValueError("web-subpath must be a non-empty string") |
| 67 | return v |
| 68 | |
| 69 | @validator("max_file_size") |
| 70 | def validate_max_file_size(cls, v): |
| 71 | if v < 0: |
| 72 | raise ValueError("value must be equal or greater than 0") |
| 73 | return v |
| 74 | |
| 75 | @validator("site_url") |
| 76 | def validate_site_url(cls, v): |
| 77 | if v: |
| 78 | parsed = urlparse(v) |
| 79 | if not parsed.scheme.startswith("http"): |
| 80 | raise ValueError("value must start with http") |
| 81 | return v |
| 82 | |
| 83 | @validator("ingress_whitelist_source_range") |
| 84 | def validate_ingress_whitelist_source_range(cls, v): |
| 85 | if v: |
| 86 | ip_network(v) |
| 87 | return v |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 88 | |
| 89 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 90 | class PrometheusCharm(CharmedOsmBase): |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 91 | |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 92 | """Prometheus Charm.""" |
| 93 | |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 94 | def __init__(self, *args) -> NoReturn: |
| 95 | """Prometheus Charm constructor.""" |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 96 | super().__init__(*args, oci_image="image") |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 97 | |
| 98 | # Registering provided relation events |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 99 | self.prometheus = PrometheusServer(self, "prometheus") |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 100 | self.framework.observe( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 101 | self.on.prometheus_relation_joined, # pylint: disable=E1101 |
| 102 | self._publish_prometheus_info, |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 103 | ) |
| 104 | |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 105 | # Registering actions |
| 106 | self.framework.observe( |
| 107 | self.on.backup_action, # pylint: disable=E1101 |
| 108 | self._on_backup_action, |
| 109 | ) |
| 110 | |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 111 | def _publish_prometheus_info(self, event: EventBase) -> NoReturn: |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 112 | self.prometheus.publish_info(self.app.name, PORT) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 113 | |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 114 | def _on_backup_action(self, event: EventBase) -> NoReturn: |
| sousaedu | b208a17 | 2021-05-13 14:30:25 +0200 | [diff] [blame] | 115 | url = f"http://{self.model.app.name}:{PORT}/api/v1/admin/tsdb/snapshot" |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 116 | result = requests.post(url) |
| 117 | |
| 118 | if result.status_code == 200: |
| 119 | event.set_results({"backup-name": result.json()["name"]}) |
| 120 | else: |
| sousaedu | b208a17 | 2021-05-13 14:30:25 +0200 | [diff] [blame] | 121 | event.fail(f"status-code: {result.status_code}") |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 122 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 123 | def _build_files(self, config: ConfigModel): |
| 124 | files_builder = FilesV3Builder() |
| 125 | files_builder.add_file( |
| 126 | "prometheus.yml", |
| 127 | ( |
| 128 | "global:\n" |
| 129 | " scrape_interval: 15s\n" |
| 130 | " evaluation_interval: 15s\n" |
| 131 | "alerting:\n" |
| 132 | " alertmanagers:\n" |
| 133 | " - static_configs:\n" |
| 134 | " - targets:\n" |
| 135 | "rule_files:\n" |
| 136 | "scrape_configs:\n" |
| 137 | " - job_name: 'prometheus'\n" |
| 138 | " static_configs:\n" |
| 139 | f" - targets: [{config.default_target}]\n" |
| 140 | ), |
| 141 | ) |
| 142 | return files_builder.build() |
| 143 | |
| 144 | def build_pod_spec(self, image_info): |
| 145 | # Validate config |
| 146 | config = ConfigModel(**dict(self.config)) |
| 147 | # Create Builder for the PodSpec |
| 148 | pod_spec_builder = PodSpecV3Builder() |
| sousaedu | 1ec36cc | 2021-03-03 01:12:49 +0100 | [diff] [blame] | 149 | |
| 150 | # Build Backup Container |
| 151 | backup_image = OCIImageResource(self, "backup-image") |
| 152 | backup_image_info = backup_image.fetch() |
| 153 | backup_container_builder = ContainerV3Builder("prom-backup", backup_image_info) |
| 154 | backup_container = backup_container_builder.build() |
| 155 | # Add backup container to pod spec |
| 156 | pod_spec_builder.add_container(backup_container) |
| 157 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 158 | # Build Container |
| 159 | container_builder = ContainerV3Builder(self.app.name, image_info) |
| 160 | container_builder.add_port(name=self.app.name, port=PORT) |
| 161 | container_builder.add_http_readiness_probe( |
| 162 | "/-/ready", |
| 163 | PORT, |
| 164 | initial_delay_seconds=10, |
| 165 | timeout_seconds=30, |
| 166 | ) |
| 167 | container_builder.add_http_liveness_probe( |
| 168 | "/-/healthy", |
| 169 | PORT, |
| 170 | initial_delay_seconds=30, |
| 171 | period_seconds=30, |
| 172 | ) |
| 173 | command = [ |
| 174 | "/bin/prometheus", |
| 175 | "--config.file=/etc/prometheus/prometheus.yml", |
| 176 | "--storage.tsdb.path=/prometheus", |
| 177 | "--web.console.libraries=/usr/share/prometheus/console_libraries", |
| 178 | "--web.console.templates=/usr/share/prometheus/consoles", |
| 179 | f"--web.route-prefix={config.web_subpath}", |
| 180 | f"--web.external-url=http://localhost:{PORT}{config.web_subpath}", |
| 181 | ] |
| 182 | if config.enable_web_admin_api: |
| 183 | command.append("--web.enable-admin-api") |
| 184 | container_builder.add_command(command) |
| 185 | container_builder.add_volume_config( |
| 186 | "config", "/etc/prometheus", self._build_files(config) |
| 187 | ) |
| 188 | container = container_builder.build() |
| 189 | # Add container to pod spec |
| 190 | pod_spec_builder.add_container(container) |
| 191 | # Add ingress resources to pod spec if site url exists |
| 192 | if config.site_url: |
| 193 | parsed = urlparse(config.site_url) |
| 194 | annotations = { |
| 195 | "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format( |
| 196 | str(config.max_file_size) + "m" |
| 197 | if config.max_file_size > 0 |
| 198 | else config.max_file_size |
| 199 | ), |
| David Garcia | da31d6e | 2021-05-27 16:06:20 +0200 | [diff] [blame] | 200 | "kubernetes.io/ingress.class": "public", |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 201 | } |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 202 | ingress_resource_builder = IngressResourceV3Builder( |
| 203 | f"{self.app.name}-ingress", annotations |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 204 | ) |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 205 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 206 | if config.ingress_whitelist_source_range: |
| 207 | annotations[ |
| 208 | "nginx.ingress.kubernetes.io/whitelist-source-range" |
| 209 | ] = config.ingress_whitelist_source_range |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 210 | |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 211 | if config.cluster_issuer: |
| 212 | annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer |
| 213 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 214 | if parsed.scheme == "https": |
| 215 | ingress_resource_builder.add_tls( |
| 216 | [parsed.hostname], config.tls_secret_name |
| 217 | ) |
| 218 | else: |
| 219 | annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false" |
| 220 | |
| 221 | ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT) |
| 222 | ingress_resource = ingress_resource_builder.build() |
| 223 | pod_spec_builder.add_ingress_resource(ingress_resource) |
| 224 | return pod_spec_builder.build() |
| sousaedu | 2459af6 | 2021-01-15 16:50:26 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | if __name__ == "__main__": |
| 228 | main(PrometheusCharm) |