blob: adbb519733ad9d6eca7eefcf73b129ae6053fbfb [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]
David Garciad68e0b42021-06-28 16:50:42 +020052 ingress_class: Optional[str]
sousaedu10721602021-05-18 17:28:17 +020053 ingress_whitelist_source_range: Optional[str]
54 tls_secret_name: Optional[str]
55 mysql_uri: Optional[str]
sousaedu3ddbbd12021-08-24 19:57:24 +010056 image_pull_policy: Optional[str]
sousaedu10721602021-05-18 17:28:17 +020057
58 @validator("site_url")
59 def validate_site_url(cls, v):
60 if v:
61 parsed = urlparse(v)
62 if not parsed.scheme.startswith("http"):
63 raise ValueError("value must start with http")
64 return v
65
66 @validator("ingress_whitelist_source_range")
67 def validate_ingress_whitelist_source_range(cls, v):
68 if v:
69 ip_network(v)
70 return v
71
72 @validator("mysql_uri")
73 def validate_mysql_uri(cls, v):
74 if v and not v.startswith("mysql://"):
75 raise ValueError("mysql_uri is not properly formed")
76 return v
sousaedu90d10f52021-02-08 02:14:48 +010077
sousaedu3ddbbd12021-08-24 19:57:24 +010078 @validator("image_pull_policy")
79 def validate_image_pull_policy(cls, v):
80 values = {
81 "always": "Always",
82 "ifnotpresent": "IfNotPresent",
83 "never": "Never",
84 }
85 v = v.lower()
86 if v not in values.keys():
87 raise ValueError("value must be always, ifnotpresent or never")
88 return values[v]
89
sousaedu90d10f52021-02-08 02:14:48 +010090
sousaedu10721602021-05-18 17:28:17 +020091class MysqlExporterCharm(CharmedOsmBase):
sousaedu90d10f52021-02-08 02:14:48 +010092 def __init__(self, *args) -> NoReturn:
sousaedu10721602021-05-18 17:28:17 +020093 super().__init__(*args, oci_image="image")
sousaedu90d10f52021-02-08 02:14:48 +010094
sousaedu10721602021-05-18 17:28:17 +020095 # Provision Kafka relation to exchange information
96 self.mysql_client = MysqlClient(self, "mysql")
97 self.framework.observe(self.on["mysql"].relation_changed, self.configure_pod)
98 self.framework.observe(self.on["mysql"].relation_broken, self.configure_pod)
sousaedu90d10f52021-02-08 02:14:48 +010099
sousaedu10721602021-05-18 17:28:17 +0200100 # Register relation to provide a Scraping Target
101 self.scrape_target = PrometheusScrapeTarget(self, "prometheus-scrape")
sousaedu90d10f52021-02-08 02:14:48 +0100102 self.framework.observe(
sousaedu10721602021-05-18 17:28:17 +0200103 self.on["prometheus-scrape"].relation_joined, self._publish_scrape_info
sousaedu90d10f52021-02-08 02:14:48 +0100104 )
105
sousaedu10721602021-05-18 17:28:17 +0200106 # Register relation to provide a Dasboard Target
107 self.dashboard_target = GrafanaDashboardTarget(self, "grafana-dashboard")
108 self.framework.observe(
109 self.on["grafana-dashboard"].relation_joined, self._publish_dashboard_info
110 )
111
112 def _publish_scrape_info(self, event) -> NoReturn:
113 """Publishes scraping information for Prometheus.
sousaedu90d10f52021-02-08 02:14:48 +0100114
115 Args:
sousaedu10721602021-05-18 17:28:17 +0200116 event (EventBase): Prometheus relation event.
sousaedu90d10f52021-02-08 02:14:48 +0100117 """
sousaedu10721602021-05-18 17:28:17 +0200118 if self.unit.is_leader():
119 hostname = (
120 urlparse(self.model.config["site_url"]).hostname
121 if self.model.config["site_url"]
122 else self.model.app.name
sousaedu90d10f52021-02-08 02:14:48 +0100123 )
sousaedu10721602021-05-18 17:28:17 +0200124 port = str(PORT)
125 if self.model.config.get("site_url", "").startswith("https://"):
126 port = "443"
127 elif self.model.config.get("site_url", "").startswith("http://"):
128 port = "80"
sousaedu90d10f52021-02-08 02:14:48 +0100129
sousaedu10721602021-05-18 17:28:17 +0200130 self.scrape_target.publish_info(
131 hostname=hostname,
132 port=port,
133 metrics_path="/metrics",
134 scrape_interval="30s",
135 scrape_timeout="15s",
136 )
sousaedu90d10f52021-02-08 02:14:48 +0100137
sousaedu10721602021-05-18 17:28:17 +0200138 def _publish_dashboard_info(self, event) -> NoReturn:
139 """Publish dashboards for Grafana.
sousaedu90d10f52021-02-08 02:14:48 +0100140
141 Args:
sousaedu10721602021-05-18 17:28:17 +0200142 event (EventBase): Grafana relation event.
sousaedu90d10f52021-02-08 02:14:48 +0100143 """
sousaedu10721602021-05-18 17:28:17 +0200144 if self.unit.is_leader():
145 self.dashboard_target.publish_info(
146 name="osm-mysql",
147 dashboard=Path("files/mysql_exporter_dashboard.json").read_text(),
sousaedu90d10f52021-02-08 02:14:48 +0100148 )
sousaedu90d10f52021-02-08 02:14:48 +0100149
sousaedu10721602021-05-18 17:28:17 +0200150 def _check_missing_dependencies(self, config: ConfigModel):
151 """Check if there is any relation missing.
sousaedu90d10f52021-02-08 02:14:48 +0100152
sousaedu10721602021-05-18 17:28:17 +0200153 Args:
154 config (ConfigModel): object with configuration information.
155
156 Raises:
157 RelationsMissing: if kafka is missing.
158 """
159 missing_relations = []
160
161 if not config.mysql_uri and self.mysql_client.is_missing_data_in_unit():
162 missing_relations.append("mysql")
163
164 if missing_relations:
165 raise RelationsMissing(missing_relations)
166
167 def build_pod_spec(self, image_info):
168 """Build the PodSpec to be used.
169
170 Args:
171 image_info (str): container image information.
172
173 Returns:
174 Dict: PodSpec information.
175 """
176 # Validate config
177 config = ConfigModel(**dict(self.config))
178
179 if config.mysql_uri and not self.mysql_client.is_missing_data_in_unit():
180 raise Exception("Mysql data cannot be provided via config and relation")
181
182 # Check relations
183 self._check_missing_dependencies(config)
184
185 # Create Builder for the PodSpec
186 pod_spec_builder = PodSpecV3Builder()
187
188 # Build container
sousaedu3ddbbd12021-08-24 19:57:24 +0100189 container_builder = ContainerV3Builder(
190 self.app.name, image_info, config.image_pull_policy
191 )
sousaedu10721602021-05-18 17:28:17 +0200192 container_builder.add_port(name=self.app.name, port=PORT)
193 container_builder.add_http_readiness_probe(
194 path="/api/health",
195 port=PORT,
196 initial_delay_seconds=10,
197 period_seconds=10,
198 timeout_seconds=5,
199 success_threshold=1,
200 failure_threshold=3,
201 )
202 container_builder.add_http_liveness_probe(
203 path="/api/health",
204 port=PORT,
205 initial_delay_seconds=60,
206 timeout_seconds=30,
207 failure_threshold=10,
208 )
209
210 data_source = (
211 config.mysql_uri.replace("mysql://", "").split("/")[0]
212 if config.mysql_uri
213 else f"root:{self.mysql_client.root_password}@{self.mysql_client.host}:{self.mysql_client.port}"
214 )
215
216 container_builder.add_envs(
217 {
218 "DATA_SOURCE_NAME": data_source,
219 }
220 )
221 container = container_builder.build()
222
223 # Add container to PodSpec
224 pod_spec_builder.add_container(container)
225
226 # Add ingress resources to PodSpec if site url exists
227 if config.site_url:
228 parsed = urlparse(config.site_url)
David Garciad68e0b42021-06-28 16:50:42 +0200229 annotations = {}
230 if config.ingress_class:
231 annotations["kubernetes.io/ingress.class"] = config.ingress_class
sousaedu10721602021-05-18 17:28:17 +0200232 ingress_resource_builder = IngressResourceV3Builder(
233 f"{self.app.name}-ingress", annotations
234 )
235
236 if config.ingress_whitelist_source_range:
237 annotations[
238 "nginx.ingress.kubernetes.io/whitelist-source-range"
239 ] = config.ingress_whitelist_source_range
240
241 if config.cluster_issuer:
242 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
243
244 if parsed.scheme == "https":
245 ingress_resource_builder.add_tls(
246 [parsed.hostname], config.tls_secret_name
247 )
248 else:
249 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
250
251 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
252 ingress_resource = ingress_resource_builder.build()
253 pod_spec_builder.add_ingress_resource(ingress_resource)
254
255 logger.debug(pod_spec_builder.build())
256
257 return pod_spec_builder.build()
sousaedu90d10f52021-02-08 02:14:48 +0100258
259
260if __name__ == "__main__":
sousaedu10721602021-05-18 17:28:17 +0200261 main(MysqlExporterCharm)