blob: ec6eaabed06de53fbe4618e58f8f1e146085b13c [file] [log] [blame]
sousaedub025f302020-11-16 14:40:14 +00001#!/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
sousaedub025f302020-11-16 14:40:14 +000026import logging
27from pathlib import Path
sousaedu10721602021-05-18 17:28:17 +020028from typing import NoReturn, Optional
sousaedu6332d382021-02-25 23:24:47 +010029from urllib.parse import urlparse
sousaedub025f302020-11-16 14:40:14 +000030
David Garcia4a0db7c2022-02-21 11:48:11 +010031from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
sousaedub025f302020-11-16 14:40:14 +000032from ops.main import main
sousaedu10721602021-05-18 17:28:17 +020033from opslib.osm.charm import CharmedOsmBase, RelationsMissing
34from opslib.osm.interfaces.grafana import GrafanaDashboardTarget
sousaedu10721602021-05-18 17:28:17 +020035from 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
sousaedub025f302020-11-16 14:40:14 +000042
sousaedub025f302020-11-16 14:40:14 +000043
44logger = logging.getLogger(__name__)
45
sousaedu10721602021-05-18 17:28:17 +020046PORT = 9308
sousaedub025f302020-11-16 14:40:14 +000047
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]
sousaedu0dc25b32021-08-30 16:33:33 +010055 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010056 security_context: bool
David Garciaa2ebf4e2022-03-11 16:58:35 +010057 kafka_endpoint: Optional[str]
sousaedu10721602021-05-18 17:28:17 +020058
59 @validator("site_url")
60 def validate_site_url(cls, v):
61 if v:
62 parsed = urlparse(v)
63 if not parsed.scheme.startswith("http"):
64 raise ValueError("value must start with http")
65 return v
66
67 @validator("ingress_whitelist_source_range")
68 def validate_ingress_whitelist_source_range(cls, v):
69 if v:
70 ip_network(v)
71 return v
sousaedub025f302020-11-16 14:40:14 +000072
sousaedu3ddbbd12021-08-24 19:57:24 +010073 @validator("image_pull_policy")
74 def validate_image_pull_policy(cls, v):
75 values = {
76 "always": "Always",
77 "ifnotpresent": "IfNotPresent",
78 "never": "Never",
79 }
80 v = v.lower()
81 if v not in values.keys():
82 raise ValueError("value must be always, ifnotpresent or never")
83 return values[v]
84
David Garciaa2ebf4e2022-03-11 16:58:35 +010085 @validator("kafka_endpoint")
86 def validate_kafka_endpoint(cls, v):
87 if v and len(v.split(":")) != 2:
88 raise ValueError("value must be in the format <host>:<port>")
89 return v
90
91
92class KafkaEndpoint:
93 def __init__(self, host: str, port: str) -> None:
94 self.host = host
95 self.port = port
96
sousaedub025f302020-11-16 14:40:14 +000097
sousaedu10721602021-05-18 17:28:17 +020098class KafkaExporterCharm(CharmedOsmBase):
David Garcia4a0db7c2022-02-21 11:48:11 +010099
100 on = KafkaEvents()
101
sousaedub025f302020-11-16 14:40:14 +0000102 def __init__(self, *args) -> NoReturn:
sousaedu10721602021-05-18 17:28:17 +0200103 super().__init__(*args, oci_image="image")
sousaedub025f302020-11-16 14:40:14 +0000104
sousaedu10721602021-05-18 17:28:17 +0200105 # Provision Kafka relation to exchange information
David Garcia4a0db7c2022-02-21 11:48:11 +0100106 self.kafka = KafkaRequires(self)
107 self.framework.observe(self.on.kafka_available, self.configure_pod)
108 self.framework.observe(self.on.kafka_broken, self.configure_pod)
sousaedub025f302020-11-16 14:40:14 +0000109
sousaedu10721602021-05-18 17:28:17 +0200110 # Register relation to provide a Scraping Target
111 self.scrape_target = PrometheusScrapeTarget(self, "prometheus-scrape")
sousaedub025f302020-11-16 14:40:14 +0000112 self.framework.observe(
sousaedu10721602021-05-18 17:28:17 +0200113 self.on["prometheus-scrape"].relation_joined, self._publish_scrape_info
sousaedub025f302020-11-16 14:40:14 +0000114 )
115
sousaedu10721602021-05-18 17:28:17 +0200116 # Register relation to provide a Dasboard Target
117 self.dashboard_target = GrafanaDashboardTarget(self, "grafana-dashboard")
118 self.framework.observe(
119 self.on["grafana-dashboard"].relation_joined, self._publish_dashboard_info
120 )
121
122 def _publish_scrape_info(self, event) -> NoReturn:
123 """Publishes scraping information for Prometheus.
sousaedub025f302020-11-16 14:40:14 +0000124
125 Args:
sousaedu10721602021-05-18 17:28:17 +0200126 event (EventBase): Prometheus relation event.
sousaedub025f302020-11-16 14:40:14 +0000127 """
sousaedu10721602021-05-18 17:28:17 +0200128 if self.unit.is_leader():
129 hostname = (
130 urlparse(self.model.config["site_url"]).hostname
131 if self.model.config["site_url"]
132 else self.model.app.name
sousaedub025f302020-11-16 14:40:14 +0000133 )
sousaedu10721602021-05-18 17:28:17 +0200134 port = str(PORT)
135 if self.model.config.get("site_url", "").startswith("https://"):
136 port = "443"
137 elif self.model.config.get("site_url", "").startswith("http://"):
138 port = "80"
sousaedub025f302020-11-16 14:40:14 +0000139
sousaedu10721602021-05-18 17:28:17 +0200140 self.scrape_target.publish_info(
141 hostname=hostname,
142 port=port,
143 metrics_path="/metrics",
144 scrape_interval="30s",
145 scrape_timeout="15s",
146 )
sousaedub025f302020-11-16 14:40:14 +0000147
sousaedu10721602021-05-18 17:28:17 +0200148 def _publish_dashboard_info(self, event) -> NoReturn:
149 """Publish dashboards for Grafana.
150
151 Args:
152 event (EventBase): Grafana relation event.
153 """
154 if self.unit.is_leader():
155 self.dashboard_target.publish_info(
156 name="osm-kafka",
David Garciad680be42021-08-17 11:03:55 +0200157 dashboard=Path("templates/kafka_exporter_dashboard.json").read_text(),
sousaedu10721602021-05-18 17:28:17 +0200158 )
159
David Garciaa2ebf4e2022-03-11 16:58:35 +0100160 def _is_kafka_endpoint_set(self, config: ConfigModel) -> bool:
161 """Check if Kafka endpoint is set."""
162 return config.kafka_endpoint or self._is_kafka_relation_set()
sousaedu10721602021-05-18 17:28:17 +0200163
David Garciaa2ebf4e2022-03-11 16:58:35 +0100164 def _is_kafka_relation_set(self) -> bool:
165 """Check if the Kafka relation is set or not."""
166 return self.kafka.host and self.kafka.port
sousaedu10721602021-05-18 17:28:17 +0200167
David Garciaa2ebf4e2022-03-11 16:58:35 +0100168 @property
169 def kafka_endpoint(self) -> KafkaEndpoint:
170 config = ConfigModel(**dict(self.config))
171 if config.kafka_endpoint:
172 host, port = config.kafka_endpoint.split(":")
173 else:
174 host = self.kafka.host
175 port = self.kafka.port
176 return KafkaEndpoint(host, port)
sousaedu10721602021-05-18 17:28:17 +0200177
178 def build_pod_spec(self, image_info):
179 """Build the PodSpec to be used.
180
181 Args:
182 image_info (str): container image information.
183
184 Returns:
185 Dict: PodSpec information.
186 """
187 # Validate config
188 config = ConfigModel(**dict(self.config))
189
190 # Check relations
David Garciaa2ebf4e2022-03-11 16:58:35 +0100191 if not self._is_kafka_endpoint_set(config):
192 raise RelationsMissing(["kafka"])
sousaedu10721602021-05-18 17:28:17 +0200193
194 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100195 pod_spec_builder = PodSpecV3Builder(
196 enable_security_context=config.security_context
197 )
sousaedu10721602021-05-18 17:28:17 +0200198
199 # Build container
sousaedu3ddbbd12021-08-24 19:57:24 +0100200 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100201 self.app.name,
202 image_info,
203 config.image_pull_policy,
204 run_as_non_root=config.security_context,
sousaedu3ddbbd12021-08-24 19:57:24 +0100205 )
David Garciaa2ebf4e2022-03-11 16:58:35 +0100206 container_builder.add_port(name="exporter", port=PORT)
sousaedu10721602021-05-18 17:28:17 +0200207 container_builder.add_http_readiness_probe(
208 path="/api/health",
209 port=PORT,
210 initial_delay_seconds=10,
211 period_seconds=10,
212 timeout_seconds=5,
213 success_threshold=1,
214 failure_threshold=3,
215 )
216 container_builder.add_http_liveness_probe(
217 path="/api/health",
218 port=PORT,
219 initial_delay_seconds=60,
220 timeout_seconds=30,
221 failure_threshold=10,
222 )
223 container_builder.add_command(
224 [
225 "kafka_exporter",
David Garciaa2ebf4e2022-03-11 16:58:35 +0100226 f"--kafka.server={self.kafka_endpoint.host}:{self.kafka_endpoint.port}",
sousaedu10721602021-05-18 17:28:17 +0200227 ]
228 )
229 container = container_builder.build()
230
231 # Add container to PodSpec
232 pod_spec_builder.add_container(container)
233
234 # Add ingress resources to PodSpec if site url exists
235 if config.site_url:
236 parsed = urlparse(config.site_url)
David Garciad68e0b42021-06-28 16:50:42 +0200237 annotations = {}
238 if config.ingress_class:
239 annotations["kubernetes.io/ingress.class"] = config.ingress_class
sousaedu10721602021-05-18 17:28:17 +0200240 ingress_resource_builder = IngressResourceV3Builder(
241 f"{self.app.name}-ingress", annotations
242 )
243
244 if config.ingress_whitelist_source_range:
245 annotations[
246 "nginx.ingress.kubernetes.io/whitelist-source-range"
247 ] = config.ingress_whitelist_source_range
248
249 if config.cluster_issuer:
250 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
251
252 if parsed.scheme == "https":
253 ingress_resource_builder.add_tls(
254 [parsed.hostname], config.tls_secret_name
255 )
256 else:
257 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
258
259 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
260 ingress_resource = ingress_resource_builder.build()
261 pod_spec_builder.add_ingress_resource(ingress_resource)
262
sousaedu10721602021-05-18 17:28:17 +0200263 return pod_spec_builder.build()
sousaedub025f302020-11-16 14:40:14 +0000264
265
266if __name__ == "__main__":
sousaedu6332d382021-02-25 23:24:47 +0100267 main(KafkaExporterCharm)