blob: 61589e20ba2632f608a31fa82f890bb46e00e043 [file] [log] [blame]
sousaedu2459af62021-01-15 16:50:26 +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
David Garcia49379ce2021-02-24 13:48:22 +010023# pylint: disable=E0213
24
David Garcia49379ce2021-02-24 13:48:22 +010025from ipaddress import ip_network
David Garciac753dc52021-03-17 15:28:47 +010026import logging
27from typing import NoReturn, Optional
28from urllib.parse import urlparse
sousaedu2459af62021-01-15 16:50:26 +000029
sousaedu1ec36cc2021-03-03 01:12:49 +010030from oci_image import OCIImageResource
David Garcia49379ce2021-02-24 13:48:22 +010031from ops.framework import EventBase
sousaedu2459af62021-01-15 16:50:26 +000032from ops.main import main
David Garcia49379ce2021-02-24 13:48:22 +010033from opslib.osm.charm import CharmedOsmBase
David Garciac753dc52021-03-17 15:28:47 +010034from opslib.osm.interfaces.prometheus import PrometheusServer
David Garcia49379ce2021-02-24 13:48:22 +010035from opslib.osm.pod import (
David Garcia49379ce2021-02-24 13:48:22 +010036 ContainerV3Builder,
David Garciac753dc52021-03-17 15:28:47 +010037 FilesV3Builder,
38 IngressResourceV3Builder,
David Garcia49379ce2021-02-24 13:48:22 +010039 PodSpecV3Builder,
40)
David Garcia49379ce2021-02-24 13:48:22 +010041from opslib.osm.validator import (
42 ModelValidator,
43 validator,
44)
sousaedu1ec36cc2021-03-03 01:12:49 +010045import requests
David Garcia49379ce2021-02-24 13:48:22 +010046
sousaedu2459af62021-01-15 16:50:26 +000047
48logger = logging.getLogger(__name__)
49
David Garcia49379ce2021-02-24 13:48:22 +010050PORT = 9090
sousaedu2459af62021-01-15 16:50:26 +000051
52
David Garcia49379ce2021-02-24 13:48:22 +010053class ConfigModel(ModelValidator):
54 web_subpath: str
55 default_target: str
56 max_file_size: int
57 site_url: Optional[str]
sousaedu3cc03162021-04-29 16:53:12 +020058 cluster_issuer: Optional[str]
David Garciad68e0b42021-06-28 16:50:42 +020059 ingress_class: Optional[str]
David Garcia49379ce2021-02-24 13:48:22 +010060 ingress_whitelist_source_range: Optional[str]
61 tls_secret_name: Optional[str]
62 enable_web_admin_api: bool
sousaedu0dc25b32021-08-30 16:33:33 +010063 image_pull_policy: str
sousaedu540d9372021-09-29 01:53:30 +010064 security_context: bool
David Garcia49379ce2021-02-24 13:48:22 +010065
66 @validator("web_subpath")
67 def validate_web_subpath(cls, v):
68 if len(v) < 1:
69 raise ValueError("web-subpath must be a non-empty string")
70 return v
71
72 @validator("max_file_size")
73 def validate_max_file_size(cls, v):
74 if v < 0:
75 raise ValueError("value must be equal or greater than 0")
76 return v
77
78 @validator("site_url")
79 def validate_site_url(cls, v):
80 if v:
81 parsed = urlparse(v)
82 if not parsed.scheme.startswith("http"):
83 raise ValueError("value must start with http")
84 return v
85
86 @validator("ingress_whitelist_source_range")
87 def validate_ingress_whitelist_source_range(cls, v):
88 if v:
89 ip_network(v)
90 return v
sousaedu2459af62021-01-15 16:50:26 +000091
sousaedu3ddbbd12021-08-24 19:57:24 +010092 @validator("image_pull_policy")
93 def validate_image_pull_policy(cls, v):
94 values = {
95 "always": "Always",
96 "ifnotpresent": "IfNotPresent",
97 "never": "Never",
98 }
99 v = v.lower()
100 if v not in values.keys():
101 raise ValueError("value must be always, ifnotpresent or never")
102 return values[v]
103
sousaedu2459af62021-01-15 16:50:26 +0000104
David Garcia49379ce2021-02-24 13:48:22 +0100105class PrometheusCharm(CharmedOsmBase):
sousaedu2459af62021-01-15 16:50:26 +0000106
sousaedu2459af62021-01-15 16:50:26 +0000107 """Prometheus Charm."""
108
sousaedu2459af62021-01-15 16:50:26 +0000109 def __init__(self, *args) -> NoReturn:
110 """Prometheus Charm constructor."""
David Garcia49379ce2021-02-24 13:48:22 +0100111 super().__init__(*args, oci_image="image")
sousaedu2459af62021-01-15 16:50:26 +0000112
113 # Registering provided relation events
David Garcia49379ce2021-02-24 13:48:22 +0100114 self.prometheus = PrometheusServer(self, "prometheus")
sousaedu2459af62021-01-15 16:50:26 +0000115 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +0100116 self.on.prometheus_relation_joined, # pylint: disable=E1101
117 self._publish_prometheus_info,
sousaedu2459af62021-01-15 16:50:26 +0000118 )
119
sousaedu1ec36cc2021-03-03 01:12:49 +0100120 # Registering actions
121 self.framework.observe(
122 self.on.backup_action, # pylint: disable=E1101
123 self._on_backup_action,
124 )
125
sousaedu2459af62021-01-15 16:50:26 +0000126 def _publish_prometheus_info(self, event: EventBase) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +0100127 self.prometheus.publish_info(self.app.name, PORT)
sousaedu2459af62021-01-15 16:50:26 +0000128
sousaedu1ec36cc2021-03-03 01:12:49 +0100129 def _on_backup_action(self, event: EventBase) -> NoReturn:
sousaedub208a172021-05-13 14:30:25 +0200130 url = f"http://{self.model.app.name}:{PORT}/api/v1/admin/tsdb/snapshot"
sousaedu1ec36cc2021-03-03 01:12:49 +0100131 result = requests.post(url)
132
133 if result.status_code == 200:
134 event.set_results({"backup-name": result.json()["name"]})
135 else:
sousaedub208a172021-05-13 14:30:25 +0200136 event.fail(f"status-code: {result.status_code}")
sousaedu1ec36cc2021-03-03 01:12:49 +0100137
David Garcia49379ce2021-02-24 13:48:22 +0100138 def _build_files(self, config: ConfigModel):
139 files_builder = FilesV3Builder()
140 files_builder.add_file(
141 "prometheus.yml",
142 (
143 "global:\n"
144 " scrape_interval: 15s\n"
145 " evaluation_interval: 15s\n"
146 "alerting:\n"
147 " alertmanagers:\n"
148 " - static_configs:\n"
149 " - targets:\n"
150 "rule_files:\n"
151 "scrape_configs:\n"
152 " - job_name: 'prometheus'\n"
153 " static_configs:\n"
154 f" - targets: [{config.default_target}]\n"
155 ),
156 )
157 return files_builder.build()
158
159 def build_pod_spec(self, image_info):
160 # Validate config
161 config = ConfigModel(**dict(self.config))
162 # Create Builder for the PodSpec
sousaedu540d9372021-09-29 01:53:30 +0100163 pod_spec_builder = PodSpecV3Builder(
164 enable_security_context=config.security_context
165 )
sousaedu1ec36cc2021-03-03 01:12:49 +0100166
167 # Build Backup Container
168 backup_image = OCIImageResource(self, "backup-image")
169 backup_image_info = backup_image.fetch()
170 backup_container_builder = ContainerV3Builder("prom-backup", backup_image_info)
171 backup_container = backup_container_builder.build()
172 # Add backup container to pod spec
173 pod_spec_builder.add_container(backup_container)
174
David Garcia49379ce2021-02-24 13:48:22 +0100175 # Build Container
sousaedu3ddbbd12021-08-24 19:57:24 +0100176 container_builder = ContainerV3Builder(
sousaedu540d9372021-09-29 01:53:30 +0100177 self.app.name,
178 image_info,
179 config.image_pull_policy,
180 run_as_non_root=config.security_context,
sousaedu3ddbbd12021-08-24 19:57:24 +0100181 )
David Garcia49379ce2021-02-24 13:48:22 +0100182 container_builder.add_port(name=self.app.name, port=PORT)
183 container_builder.add_http_readiness_probe(
184 "/-/ready",
185 PORT,
186 initial_delay_seconds=10,
187 timeout_seconds=30,
188 )
189 container_builder.add_http_liveness_probe(
190 "/-/healthy",
191 PORT,
192 initial_delay_seconds=30,
193 period_seconds=30,
194 )
195 command = [
196 "/bin/prometheus",
197 "--config.file=/etc/prometheus/prometheus.yml",
198 "--storage.tsdb.path=/prometheus",
199 "--web.console.libraries=/usr/share/prometheus/console_libraries",
200 "--web.console.templates=/usr/share/prometheus/consoles",
201 f"--web.route-prefix={config.web_subpath}",
202 f"--web.external-url=http://localhost:{PORT}{config.web_subpath}",
203 ]
204 if config.enable_web_admin_api:
205 command.append("--web.enable-admin-api")
206 container_builder.add_command(command)
207 container_builder.add_volume_config(
208 "config", "/etc/prometheus", self._build_files(config)
209 )
210 container = container_builder.build()
211 # Add container to pod spec
212 pod_spec_builder.add_container(container)
213 # Add ingress resources to pod spec if site url exists
214 if config.site_url:
215 parsed = urlparse(config.site_url)
216 annotations = {
217 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
218 str(config.max_file_size) + "m"
219 if config.max_file_size > 0
220 else config.max_file_size
David Garciad68e0b42021-06-28 16:50:42 +0200221 )
sousaedu2459af62021-01-15 16:50:26 +0000222 }
David Garciad68e0b42021-06-28 16:50:42 +0200223 if config.ingress_class:
224 annotations["kubernetes.io/ingress.class"] = config.ingress_class
David Garcia49379ce2021-02-24 13:48:22 +0100225 ingress_resource_builder = IngressResourceV3Builder(
226 f"{self.app.name}-ingress", annotations
sousaedu2459af62021-01-15 16:50:26 +0000227 )
sousaedu2459af62021-01-15 16:50:26 +0000228
David Garcia49379ce2021-02-24 13:48:22 +0100229 if config.ingress_whitelist_source_range:
230 annotations[
231 "nginx.ingress.kubernetes.io/whitelist-source-range"
232 ] = config.ingress_whitelist_source_range
sousaedu2459af62021-01-15 16:50:26 +0000233
sousaedu3cc03162021-04-29 16:53:12 +0200234 if config.cluster_issuer:
235 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
236
David Garcia49379ce2021-02-24 13:48:22 +0100237 if parsed.scheme == "https":
238 ingress_resource_builder.add_tls(
239 [parsed.hostname], config.tls_secret_name
240 )
241 else:
242 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
243
244 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
245 ingress_resource = ingress_resource_builder.build()
246 pod_spec_builder.add_ingress_resource(ingress_resource)
247 return pod_spec_builder.build()
sousaedu2459af62021-01-15 16:50:26 +0000248
249
250if __name__ == "__main__":
251 main(PrometheusCharm)