blob: 2ae7d8371f94674fd1c27bc4d1280f7c86b1c968 [file] [log] [blame]
sousaedu90d10f52021-02-08 02:14:48 +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
sousaedu10721602021-05-18 17:28:17 +020023# pylint: disable=E0213
24
25from ipaddress import ip_network
sousaedu90d10f52021-02-08 02:14:48 +010026import logging
27from pathlib import Path
sousaedu10721602021-05-18 17:28:17 +020028from typing import NoReturn, Optional
sousaedu90d10f52021-02-08 02:14:48 +010029from urllib.parse import urlparse
30
sousaedu90d10f52021-02-08 02:14:48 +010031from ops.main import main
sousaedu10721602021-05-18 17:28:17 +020032from opslib.osm.charm import CharmedOsmBase, RelationsMissing
33from opslib.osm.interfaces.grafana import GrafanaDashboardTarget
34from opslib.osm.interfaces.mysql import MysqlClient
35from opslib.osm.interfaces.prometheus import PrometheusScrapeTarget
36from opslib.osm.pod import (
37 ContainerV3Builder,
38 IngressResourceV3Builder,
39 PodSpecV3Builder,
40)
41from opslib.osm.validator import ModelValidator, validator
sousaedu90d10f52021-02-08 02:14:48 +010042
sousaedu90d10f52021-02-08 02:14:48 +010043
44logger = logging.getLogger(__name__)
45
sousaedu10721602021-05-18 17:28:17 +020046PORT = 9104
sousaedu90d10f52021-02-08 02:14:48 +010047
48
sousaedu10721602021-05-18 17:28:17 +020049class ConfigModel(ModelValidator):
50 site_url: Optional[str]
51 cluster_issuer: Optional[str]
52 ingress_whitelist_source_range: Optional[str]
53 tls_secret_name: Optional[str]
54 mysql_uri: Optional[str]
55
56 @validator("site_url")
57 def validate_site_url(cls, v):
58 if v:
59 parsed = urlparse(v)
60 if not parsed.scheme.startswith("http"):
61 raise ValueError("value must start with http")
62 return v
63
64 @validator("ingress_whitelist_source_range")
65 def validate_ingress_whitelist_source_range(cls, v):
66 if v:
67 ip_network(v)
68 return v
69
70 @validator("mysql_uri")
71 def validate_mysql_uri(cls, v):
72 if v and not v.startswith("mysql://"):
73 raise ValueError("mysql_uri is not properly formed")
74 return v
sousaedu90d10f52021-02-08 02:14:48 +010075
76
sousaedu10721602021-05-18 17:28:17 +020077class MysqlExporterCharm(CharmedOsmBase):
sousaedu90d10f52021-02-08 02:14:48 +010078 def __init__(self, *args) -> NoReturn:
sousaedu10721602021-05-18 17:28:17 +020079 super().__init__(*args, oci_image="image")
sousaedu90d10f52021-02-08 02:14:48 +010080
sousaedu10721602021-05-18 17:28:17 +020081 # Provision Kafka relation to exchange information
82 self.mysql_client = MysqlClient(self, "mysql")
83 self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod)
84 self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod)
sousaedu90d10f52021-02-08 02:14:48 +010085
sousaedu10721602021-05-18 17:28:17 +020086 # Register relation to provide a Scraping Target
87 self.scrape_target = PrometheusScrapeTarget(self, "prometheus-scrape")
sousaedu90d10f52021-02-08 02:14:48 +010088 self.framework.observe(
sousaedu10721602021-05-18 17:28:17 +020089 self.on["prometheus-scrape"].relation_joined, self._publish_scrape_info
sousaedu90d10f52021-02-08 02:14:48 +010090 )
91
sousaedu10721602021-05-18 17:28:17 +020092 # Register relation to provide a Dasboard Target
93 self.dashboard_target = GrafanaDashboardTarget(self, "grafana-dashboard")
94 self.framework.observe(
95 self.on["grafana-dashboard"].relation_joined, self._publish_dashboard_info
96 )
97
98 def _publish_scrape_info(self, event) -> NoReturn:
99 """Publishes scraping information for Prometheus.
sousaedu90d10f52021-02-08 02:14:48 +0100100
101 Args:
sousaedu10721602021-05-18 17:28:17 +0200102 event (EventBase): Prometheus relation event.
sousaedu90d10f52021-02-08 02:14:48 +0100103 """
sousaedu10721602021-05-18 17:28:17 +0200104 if self.unit.is_leader():
105 hostname = (
106 urlparse(self.model.config["site_url"]).hostname
107 if self.model.config["site_url"]
108 else self.model.app.name
sousaedu90d10f52021-02-08 02:14:48 +0100109 )
sousaedu10721602021-05-18 17:28:17 +0200110 port = str(PORT)
111 if self.model.config.get("site_url", "").startswith("https://"):
112 port = "443"
113 elif self.model.config.get("site_url", "").startswith("http://"):
114 port = "80"
sousaedu90d10f52021-02-08 02:14:48 +0100115
sousaedu10721602021-05-18 17:28:17 +0200116 self.scrape_target.publish_info(
117 hostname=hostname,
118 port=port,
119 metrics_path="/metrics",
120 scrape_interval="30s",
121 scrape_timeout="15s",
122 )
sousaedu90d10f52021-02-08 02:14:48 +0100123
sousaedu10721602021-05-18 17:28:17 +0200124 def _publish_dashboard_info(self, event) -> NoReturn:
125 """Publish dashboards for Grafana.
sousaedu90d10f52021-02-08 02:14:48 +0100126
127 Args:
sousaedu10721602021-05-18 17:28:17 +0200128 event (EventBase): Grafana relation event.
sousaedu90d10f52021-02-08 02:14:48 +0100129 """
sousaedu10721602021-05-18 17:28:17 +0200130 if self.unit.is_leader():
131 self.dashboard_target.publish_info(
132 name="osm-mysql",
133 dashboard=Path("files/mysql_exporter_dashboard.json").read_text(),
sousaedu90d10f52021-02-08 02:14:48 +0100134 )
sousaedu90d10f52021-02-08 02:14:48 +0100135
sousaedu10721602021-05-18 17:28:17 +0200136 def _check_missing_dependencies(self, config: ConfigModel):
137 """Check if there is any relation missing.
sousaedu90d10f52021-02-08 02:14:48 +0100138
sousaedu10721602021-05-18 17:28:17 +0200139 Args:
140 config (ConfigModel): object with configuration information.
141
142 Raises:
143 RelationsMissing: if kafka is missing.
144 """
145 missing_relations = []
146
147 if not config.mysql_uri and self.mysql_client.is_missing_data_in_unit():
148 missing_relations.append("mysql")
149
150 if missing_relations:
151 raise RelationsMissing(missing_relations)
152
153 def build_pod_spec(self, image_info):
154 """Build the PodSpec to be used.
155
156 Args:
157 image_info (str): container image information.
158
159 Returns:
160 Dict: PodSpec information.
161 """
162 # Validate config
163 config = ConfigModel(**dict(self.config))
164
165 if config.mysql_uri and not self.mysql_client.is_missing_data_in_unit():
166 raise Exception("Mysql data cannot be provided via config and relation")
167
168 # Check relations
169 self._check_missing_dependencies(config)
170
171 # Create Builder for the PodSpec
172 pod_spec_builder = PodSpecV3Builder()
173
174 # Build container
175 container_builder = ContainerV3Builder(self.app.name, image_info)
176 container_builder.add_port(name=self.app.name, port=PORT)
177 container_builder.add_http_readiness_probe(
178 path="/api/health",
179 port=PORT,
180 initial_delay_seconds=10,
181 period_seconds=10,
182 timeout_seconds=5,
183 success_threshold=1,
184 failure_threshold=3,
185 )
186 container_builder.add_http_liveness_probe(
187 path="/api/health",
188 port=PORT,
189 initial_delay_seconds=60,
190 timeout_seconds=30,
191 failure_threshold=10,
192 )
193
194 data_source = (
195 config.mysql_uri.replace("mysql://", "").split("/")[0]
196 if config.mysql_uri
197 else f"root:{self.mysql_client.root_password}@{self.mysql_client.host}:{self.mysql_client.port}"
198 )
199
200 container_builder.add_envs(
201 {
202 "DATA_SOURCE_NAME": data_source,
203 }
204 )
205 container = container_builder.build()
206
207 # Add container to PodSpec
208 pod_spec_builder.add_container(container)
209
210 # Add ingress resources to PodSpec if site url exists
211 if config.site_url:
212 parsed = urlparse(config.site_url)
213 annotations = {}
214 ingress_resource_builder = IngressResourceV3Builder(
215 f"{self.app.name}-ingress", annotations
216 )
217
218 if config.ingress_whitelist_source_range:
219 annotations[
220 "nginx.ingress.kubernetes.io/whitelist-source-range"
221 ] = config.ingress_whitelist_source_range
222
223 if config.cluster_issuer:
224 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
225
226 if parsed.scheme == "https":
227 ingress_resource_builder.add_tls(
228 [parsed.hostname], config.tls_secret_name
229 )
230 else:
231 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
232
233 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
234 ingress_resource = ingress_resource_builder.build()
235 pod_spec_builder.add_ingress_resource(ingress_resource)
236
237 logger.debug(pod_spec_builder.build())
238
239 return pod_spec_builder.build()
sousaedu90d10f52021-02-08 02:14:48 +0100240
241
242if __name__ == "__main__":
sousaedu10721602021-05-18 17:28:17 +0200243 main(MysqlExporterCharm)