blob: 500a1e3d9753a84aa631791abedc0bbad23c7ad3 [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,
David Garcia141d9352021-09-08 17:48:40 +020039 PodRestartPolicy,
sousaedu10721602021-05-18 17:28:17 +020040 PodSpecV3Builder,
41)
42from opslib.osm.validator import ModelValidator, validator
sousaedu903379c2021-02-08 13:34:21 +010043
sousaedu903379c2021-02-08 13:34:21 +010044
45logger = logging.getLogger(__name__)
46
sousaedu10721602021-05-18 17:28:17 +020047PORT = 9216
sousaedu903379c2021-02-08 13:34:21 +010048
49
sousaedu10721602021-05-18 17:28:17 +020050class ConfigModel(ModelValidator):
51 site_url: Optional[str]
52 cluster_issuer: Optional[str]
David Garciad68e0b42021-06-28 16:50:42 +020053 ingress_class: Optional[str]
sousaedu10721602021-05-18 17:28:17 +020054 ingress_whitelist_source_range: Optional[str]
55 tls_secret_name: Optional[str]
56 mongodb_uri: Optional[str]
sousaedu0dc25b32021-08-30 16:33:33 +010057 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010058 security_context: bool
sousaedu10721602021-05-18 17:28:17 +020059
60 @validator("site_url")
61 def validate_site_url(cls, v):
62 if v:
63 parsed = urlparse(v)
64 if not parsed.scheme.startswith("http"):
65 raise ValueError("value must start with http")
66 return v
67
68 @validator("ingress_whitelist_source_range")
69 def validate_ingress_whitelist_source_range(cls, v):
70 if v:
71 ip_network(v)
72 return v
73
74 @validator("mongodb_uri")
75 def validate_mongodb_uri(cls, v):
76 if v and not v.startswith("mongodb://"):
77 raise ValueError("mongodb_uri is not properly formed")
78 return v
sousaedu903379c2021-02-08 13:34:21 +010079
sousaedu3ddbbd12021-08-24 19:57:24 +010080 @validator("image_pull_policy")
81 def validate_image_pull_policy(cls, v):
82 values = {
83 "always": "Always",
84 "ifnotpresent": "IfNotPresent",
85 "never": "Never",
86 }
87 v = v.lower()
88 if v not in values.keys():
89 raise ValueError("value must be always, ifnotpresent or never")
90 return values[v]
91
sousaedu903379c2021-02-08 13:34:21 +010092
sousaedu10721602021-05-18 17:28:17 +020093class MongodbExporterCharm(CharmedOsmBase):
sousaedu903379c2021-02-08 13:34:21 +010094 def __init__(self, *args) -> NoReturn:
sousaedu10721602021-05-18 17:28:17 +020095 super().__init__(*args, oci_image="image")
sousaedu903379c2021-02-08 13:34:21 +010096
sousaedu10721602021-05-18 17:28:17 +020097 # Provision Kafka relation to exchange information
98 self.mongodb_client = MongoClient(self, "mongodb")
99 self.framework.observe(self.on["mongodb"].relation_changed, self.configure_pod)
100 self.framework.observe(self.on["mongodb"].relation_broken, self.configure_pod)
sousaedu903379c2021-02-08 13:34:21 +0100101
sousaedu10721602021-05-18 17:28:17 +0200102 # Register relation to provide a Scraping Target
103 self.scrape_target = PrometheusScrapeTarget(self, "prometheus-scrape")
sousaedu903379c2021-02-08 13:34:21 +0100104 self.framework.observe(
sousaedu10721602021-05-18 17:28:17 +0200105 self.on["prometheus-scrape"].relation_joined, self._publish_scrape_info
sousaedu903379c2021-02-08 13:34:21 +0100106 )
107
sousaedu10721602021-05-18 17:28:17 +0200108 # Register relation to provide a Dasboard Target
109 self.dashboard_target = GrafanaDashboardTarget(self, "grafana-dashboard")
110 self.framework.observe(
111 self.on["grafana-dashboard"].relation_joined, self._publish_dashboard_info
112 )
113
114 def _publish_scrape_info(self, event) -> NoReturn:
115 """Publishes scraping information for Prometheus.
sousaedu903379c2021-02-08 13:34:21 +0100116
117 Args:
sousaedu10721602021-05-18 17:28:17 +0200118 event (EventBase): Prometheus relation event.
sousaedu903379c2021-02-08 13:34:21 +0100119 """
sousaedu10721602021-05-18 17:28:17 +0200120 if self.unit.is_leader():
121 hostname = (
122 urlparse(self.model.config["site_url"]).hostname
123 if self.model.config["site_url"]
124 else self.model.app.name
sousaedu903379c2021-02-08 13:34:21 +0100125 )
sousaedu10721602021-05-18 17:28:17 +0200126 port = str(PORT)
127 if self.model.config.get("site_url", "").startswith("https://"):
128 port = "443"
129 elif self.model.config.get("site_url", "").startswith("http://"):
130 port = "80"
sousaedu903379c2021-02-08 13:34:21 +0100131
sousaedu10721602021-05-18 17:28:17 +0200132 self.scrape_target.publish_info(
133 hostname=hostname,
134 port=port,
135 metrics_path="/metrics",
136 scrape_interval="30s",
137 scrape_timeout="15s",
138 )
sousaedu903379c2021-02-08 13:34:21 +0100139
sousaedu10721602021-05-18 17:28:17 +0200140 def _publish_dashboard_info(self, event) -> NoReturn:
141 """Publish dashboards for Grafana.
142
143 Args:
144 event (EventBase): Grafana relation event.
145 """
146 if self.unit.is_leader():
147 self.dashboard_target.publish_info(
148 name="osm-mongodb",
David Garciad680be42021-08-17 11:03:55 +0200149 dashboard=Path("templates/mongodb_exporter_dashboard.json").read_text(),
sousaedu10721602021-05-18 17:28:17 +0200150 )
151
152 def _check_missing_dependencies(self, config: ConfigModel):
153 """Check if there is any relation missing.
154
155 Args:
156 config (ConfigModel): object with configuration information.
157
158 Raises:
159 RelationsMissing: if kafka is missing.
160 """
161 missing_relations = []
162
163 if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
164 missing_relations.append("mongodb")
165
166 if missing_relations:
167 raise RelationsMissing(missing_relations)
168
169 def build_pod_spec(self, image_info):
170 """Build the PodSpec to be used.
171
172 Args:
173 image_info (str): container image information.
174
175 Returns:
176 Dict: PodSpec information.
177 """
178 # Validate config
179 config = ConfigModel(**dict(self.config))
180
181 if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
182 raise Exception("Mongodb data cannot be provided via config and relation")
183
184 # Check relations
185 self._check_missing_dependencies(config)
186
David Garcia141d9352021-09-08 17:48:40 +0200187 unparsed = (
188 config.mongodb_uri
189 if config.mongodb_uri
190 else self.mongodb_client.connection_string
191 )
192 parsed = urlparse(unparsed)
193 mongodb_uri = f"mongodb://{parsed.netloc.split(',')[0]}{parsed.path}"
194 if parsed.query:
195 mongodb_uri += f"?{parsed.query}"
196
sousaedu10721602021-05-18 17:28:17 +0200197 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100198 pod_spec_builder = PodSpecV3Builder(
199 enable_security_context=config.security_context
200 )
sousaedu10721602021-05-18 17:28:17 +0200201
David Garcia141d9352021-09-08 17:48:40 +0200202 # Add secrets to the pod
203 mongodb_secret_name = f"{self.app.name}-mongodb-secret"
204 pod_spec_builder.add_secret(mongodb_secret_name, {"uri": mongodb_uri})
205
sousaedu10721602021-05-18 17:28:17 +0200206 # Build container
sousaedu3ddbbd12021-08-24 19:57:24 +0100207 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100208 self.app.name,
209 image_info,
210 config.image_pull_policy,
211 run_as_non_root=config.security_context,
sousaedu3ddbbd12021-08-24 19:57:24 +0100212 )
sousaedu10721602021-05-18 17:28:17 +0200213 container_builder.add_port(name=self.app.name, port=PORT)
214 container_builder.add_http_readiness_probe(
215 path="/api/health",
216 port=PORT,
217 initial_delay_seconds=10,
218 period_seconds=10,
219 timeout_seconds=5,
220 success_threshold=1,
221 failure_threshold=3,
222 )
223 container_builder.add_http_liveness_probe(
224 path="/api/health",
225 port=PORT,
226 initial_delay_seconds=60,
227 timeout_seconds=30,
228 failure_threshold=10,
229 )
230
David Garcia141d9352021-09-08 17:48:40 +0200231 container_builder.add_secret_envs(mongodb_secret_name, {"MONGODB_URI": "uri"})
sousaedu10721602021-05-18 17:28:17 +0200232 container = container_builder.build()
233
234 # Add container to PodSpec
235 pod_spec_builder.add_container(container)
236
David Garcia141d9352021-09-08 17:48:40 +0200237 # Add Pod restart policy
238 restart_policy = PodRestartPolicy()
239 restart_policy.add_secrets(secret_names=(mongodb_secret_name,))
240 pod_spec_builder.set_restart_policy(restart_policy)
241
sousaedu10721602021-05-18 17:28:17 +0200242 # Add ingress resources to PodSpec if site url exists
243 if config.site_url:
244 parsed = urlparse(config.site_url)
David Garciad68e0b42021-06-28 16:50:42 +0200245 annotations = {}
246 if config.ingress_class:
247 annotations["kubernetes.io/ingress.class"] = config.ingress_class
sousaedu10721602021-05-18 17:28:17 +0200248 ingress_resource_builder = IngressResourceV3Builder(
249 f"{self.app.name}-ingress", annotations
250 )
251
252 if config.ingress_whitelist_source_range:
253 annotations[
254 "nginx.ingress.kubernetes.io/whitelist-source-range"
255 ] = config.ingress_whitelist_source_range
256
257 if config.cluster_issuer:
258 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
259
260 if parsed.scheme == "https":
261 ingress_resource_builder.add_tls(
262 [parsed.hostname], config.tls_secret_name
263 )
264 else:
265 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
266
267 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
268 ingress_resource = ingress_resource_builder.build()
269 pod_spec_builder.add_ingress_resource(ingress_resource)
270
sousaedu10721602021-05-18 17:28:17 +0200271 return pod_spec_builder.build()
sousaedu903379c2021-02-08 13:34:21 +0100272
273
274if __name__ == "__main__":
sousaedu3884e232021-02-25 21:32:25 +0100275 main(MongodbExporterCharm)