blob: 149940a6e01ed9e50b588995da132f23a4873bca [file] [log] [blame]
sousaedu903379c2021-02-08 13:34:21 +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
sousaedu903379c2021-02-08 13:34:21 +010026import logging
27from pathlib import Path
sousaedu10721602021-05-18 17:28:17 +020028from typing import NoReturn, Optional
sousaedu3884e232021-02-25 21:32:25 +010029from urllib.parse import urlparse
sousaedu903379c2021-02-08 13:34:21 +010030
sousaedu903379c2021-02-08 13:34:21 +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.mongo import MongoClient
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
sousaedu903379c2021-02-08 13:34:21 +010042
sousaedu903379c2021-02-08 13:34:21 +010043
44logger = logging.getLogger(__name__)
45
sousaedu10721602021-05-18 17:28:17 +020046PORT = 9216
sousaedu903379c2021-02-08 13:34:21 +010047
48
sousaedu10721602021-05-18 17:28:17 +020049class ConfigModel(ModelValidator):
50 site_url: Optional[str]
51 cluster_issuer: Optional[str]
David Garciac35943e2021-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 mongodb_uri: Optional[str]
56
57 @validator("site_url")
58 def validate_site_url(cls, v):
59 if v:
60 parsed = urlparse(v)
61 if not parsed.scheme.startswith("http"):
62 raise ValueError("value must start with http")
63 return v
64
65 @validator("ingress_whitelist_source_range")
66 def validate_ingress_whitelist_source_range(cls, v):
67 if v:
68 ip_network(v)
69 return v
70
71 @validator("mongodb_uri")
72 def validate_mongodb_uri(cls, v):
73 if v and not v.startswith("mongodb://"):
74 raise ValueError("mongodb_uri is not properly formed")
75 return v
sousaedu903379c2021-02-08 13:34:21 +010076
77
sousaedu10721602021-05-18 17:28:17 +020078class MongodbExporterCharm(CharmedOsmBase):
sousaedu903379c2021-02-08 13:34:21 +010079 def __init__(self, *args) -> NoReturn:
sousaedu10721602021-05-18 17:28:17 +020080 super().__init__(*args, oci_image="image")
sousaedu903379c2021-02-08 13:34:21 +010081
sousaedu10721602021-05-18 17:28:17 +020082 # Provision Kafka relation to exchange information
83 self.mongodb_client = MongoClient(self, "mongodb")
84 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
85 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaedu903379c2021-02-08 13:34:21 +010086
sousaedu10721602021-05-18 17:28:17 +020087 # Register relation to provide a Scraping Target
88 self.scrape_target = PrometheusScrapeTarget(self, "prometheus-scrape")
sousaedu903379c2021-02-08 13:34:21 +010089 self.framework.observe(
sousaedu10721602021-05-18 17:28:17 +020090 self.on["prometheus-scrape"].relation_joined, self._publish_scrape_info
sousaedu903379c2021-02-08 13:34:21 +010091 )
92
sousaedu10721602021-05-18 17:28:17 +020093 # Register relation to provide a Dasboard Target
94 self.dashboard_target = GrafanaDashboardTarget(self, "grafana-dashboard")
95 self.framework.observe(
96 self.on["grafana-dashboard"].relation_joined, self._publish_dashboard_info
97 )
98
99 def _publish_scrape_info(self, event) -> NoReturn:
100 """Publishes scraping information for Prometheus.
sousaedu903379c2021-02-08 13:34:21 +0100101
102 Args:
sousaedu10721602021-05-18 17:28:17 +0200103 event (EventBase): Prometheus relation event.
sousaedu903379c2021-02-08 13:34:21 +0100104 """
sousaedu10721602021-05-18 17:28:17 +0200105 if self.unit.is_leader():
106 hostname = (
107 urlparse(self.model.config["site_url"]).hostname
108 if self.model.config["site_url"]
109 else self.model.app.name
sousaedu903379c2021-02-08 13:34:21 +0100110 )
sousaedu10721602021-05-18 17:28:17 +0200111 port = str(PORT)
112 if self.model.config.get("site_url", "").startswith("https://"):
113 port = "443"
114 elif self.model.config.get("site_url", "").startswith("http://"):
115 port = "80"
sousaedu903379c2021-02-08 13:34:21 +0100116
sousaedu10721602021-05-18 17:28:17 +0200117 self.scrape_target.publish_info(
118 hostname=hostname,
119 port=port,
120 metrics_path="/metrics",
121 scrape_interval="30s",
122 scrape_timeout="15s",
123 )
sousaedu903379c2021-02-08 13:34:21 +0100124
sousaedu10721602021-05-18 17:28:17 +0200125 def _publish_dashboard_info(self, event) -> NoReturn:
126 """Publish dashboards for Grafana.
127
128 Args:
129 event (EventBase): Grafana relation event.
130 """
131 if self.unit.is_leader():
132 self.dashboard_target.publish_info(
133 name="osm-mongodb",
134 dashboard=Path("files/mongodb_exporter_dashboard.json").read_text(),
135 )
136
137 def _check_missing_dependencies(self, config: ConfigModel):
138 """Check if there is any relation missing.
139
140 Args:
141 config (ConfigModel): object with configuration information.
142
143 Raises:
144 RelationsMissing: if kafka is missing.
145 """
146 missing_relations = []
147
148 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
149 missing_relations.append("mongodb")
150
151 if missing_relations:
152 raise RelationsMissing(missing_relations)
153
154 def build_pod_spec(self, image_info):
155 """Build the PodSpec to be used.
156
157 Args:
158 image_info (str): container image information.
159
160 Returns:
161 Dict: PodSpec information.
162 """
163 # Validate config
164 config = ConfigModel(**dict(self.config))
165
166 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
167 raise Exception("Mongodb data cannot be provided via config and relation")
168
169 # Check relations
170 self._check_missing_dependencies(config)
171
172 # Create Builder for the PodSpec
173 pod_spec_builder = PodSpecV3Builder()
174
175 # Build container
176 container_builder = ContainerV3Builder(self.app.name, image_info)
177 container_builder.add_port(name=self.app.name, port=PORT)
178 container_builder.add_http_readiness_probe(
179 path="/api/health",
180 port=PORT,
181 initial_delay_seconds=10,
182 period_seconds=10,
183 timeout_seconds=5,
184 success_threshold=1,
185 failure_threshold=3,
186 )
187 container_builder.add_http_liveness_probe(
188 path="/api/health",
189 port=PORT,
190 initial_delay_seconds=60,
191 timeout_seconds=30,
192 failure_threshold=10,
193 )
194
195 unparsed = (
196 config.mongodb_uri
197 if config.mongodb_uri
198 else self.mongodb_client.connection_string
199 )
200 parsed = urlparse(unparsed)
201 mongodb_uri = f"mongodb://{parsed.netloc.split(',')[0]}{parsed.path}"
202 if parsed.query:
203 mongodb_uri += f"?{parsed.query}"
204
205 container_builder.add_envs(
206 {
207 "MONGODB_URI": mongodb_uri,
208 }
209 )
210 container = container_builder.build()
211
212 # Add container to PodSpec
213 pod_spec_builder.add_container(container)
214
215 # Add ingress resources to PodSpec if site url exists
216 if config.site_url:
217 parsed = urlparse(config.site_url)
218 annotations = {}
David Garciac35943e2021-06-28 16:50:42 +0200219 if config.ingress_class:
220 annotations["kubernetes.io/ingress.class"] = config.ingress_class
sousaedu10721602021-05-18 17:28:17 +0200221 ingress_resource_builder = IngressResourceV3Builder(
222 f"{self.app.name}-ingress", annotations
223 )
224
225 if config.ingress_whitelist_source_range:
226 annotations[
227 "nginx.ingress.kubernetes.io/whitelist-source-range"
228 ] = config.ingress_whitelist_source_range
229
230 if config.cluster_issuer:
231 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
232
233 if parsed.scheme == "https":
234 ingress_resource_builder.add_tls(
235 [parsed.hostname], config.tls_secret_name
236 )
237 else:
238 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
239
240 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
241 ingress_resource = ingress_resource_builder.build()
242 pod_spec_builder.add_ingress_resource(ingress_resource)
243
244 logger.debug(pod_spec_builder.build())
245
246 return pod_spec_builder.build()
sousaedu903379c2021-02-08 13:34:21 +0100247
248
249if __name__ == "__main__":
sousaedu3884e232021-02-25 21:32:25 +0100250 main(MongodbExporterCharm)