blob: e3e0e4240225c842a734266e83f32f50d7de665e [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 Garciac35943e2021-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
63
64 @validator("web_subpath")
65 def validate_web_subpath(cls, v):
66 if len(v) < 1:
67 raise ValueError("web-subpath must be a non-empty string")
68 return v
69
70 @validator("max_file_size")
71 def validate_max_file_size(cls, v):
72 if v < 0:
73 raise ValueError("value must be equal or greater than 0")
74 return v
75
76 @validator("site_url")
77 def validate_site_url(cls, v):
78 if v:
79 parsed = urlparse(v)
80 if not parsed.scheme.startswith("http"):
81 raise ValueError("value must start with http")
82 return v
83
84 @validator("ingress_whitelist_source_range")
85 def validate_ingress_whitelist_source_range(cls, v):
86 if v:
87 ip_network(v)
88 return v
sousaedu2459af62021-01-15 16:50:26 +000089
90
David Garcia49379ce2021-02-24 13:48:22 +010091class PrometheusCharm(CharmedOsmBase):
sousaedu2459af62021-01-15 16:50:26 +000092
sousaedu2459af62021-01-15 16:50:26 +000093 """Prometheus Charm."""
94
sousaedu2459af62021-01-15 16:50:26 +000095 def __init__(self, *args) -> NoReturn:
96 """Prometheus Charm constructor."""
David Garcia49379ce2021-02-24 13:48:22 +010097 super().__init__(*args, oci_image="image")
sousaedu2459af62021-01-15 16:50:26 +000098
99 # Registering provided relation events
David Garcia49379ce2021-02-24 13:48:22 +0100100 self.prometheus = PrometheusServer(self, "prometheus")
sousaedu2459af62021-01-15 16:50:26 +0000101 self.framework.observe(
David Garcia49379ce2021-02-24 13:48:22 +0100102 self.on.prometheus_relation_joined, # pylint: disable=E1101
103 self._publish_prometheus_info,
sousaedu2459af62021-01-15 16:50:26 +0000104 )
105
sousaedu1ec36cc2021-03-03 01:12:49 +0100106 # Registering actions
107 self.framework.observe(
108 self.on.backup_action, # pylint: disable=E1101
109 self._on_backup_action,
110 )
111
sousaedu2459af62021-01-15 16:50:26 +0000112 def _publish_prometheus_info(self, event: EventBase) -> NoReturn:
David Garcia49379ce2021-02-24 13:48:22 +0100113 self.prometheus.publish_info(self.app.name, PORT)
sousaedu2459af62021-01-15 16:50:26 +0000114
sousaedu1ec36cc2021-03-03 01:12:49 +0100115 def _on_backup_action(self, event: EventBase) -> NoReturn:
sousaedub208a172021-05-13 14:30:25 +0200116 url = f"http://{self.model.app.name}:{PORT}/api/v1/admin/tsdb/snapshot"
sousaedu1ec36cc2021-03-03 01:12:49 +0100117 result = requests.post(url)
118
119 if result.status_code == 200:
120 event.set_results({"backup-name": result.json()["name"]})
121 else:
sousaedub208a172021-05-13 14:30:25 +0200122 event.fail(f"status-code: {result.status_code}")
sousaedu1ec36cc2021-03-03 01:12:49 +0100123
David Garcia49379ce2021-02-24 13:48:22 +0100124 def _build_files(self, config: ConfigModel):
125 files_builder = FilesV3Builder()
126 files_builder.add_file(
127 "prometheus.yml",
128 (
129 "global:\n"
130 " scrape_interval: 15s\n"
131 " evaluation_interval: 15s\n"
132 "alerting:\n"
133 " alertmanagers:\n"
134 " - static_configs:\n"
135 " - targets:\n"
136 "rule_files:\n"
137 "scrape_configs:\n"
138 " - job_name: 'prometheus'\n"
139 " static_configs:\n"
140 f" - targets: [{config.default_target}]\n"
141 ),
142 )
143 return files_builder.build()
144
145 def build_pod_spec(self, image_info):
146 # Validate config
147 config = ConfigModel(**dict(self.config))
148 # Create Builder for the PodSpec
149 pod_spec_builder = PodSpecV3Builder()
sousaedu1ec36cc2021-03-03 01:12:49 +0100150
151 # Build Backup Container
152 backup_image = OCIImageResource(self, "backup-image")
153 backup_image_info = backup_image.fetch()
154 backup_container_builder = ContainerV3Builder("prom-backup", backup_image_info)
155 backup_container = backup_container_builder.build()
156 # Add backup container to pod spec
157 pod_spec_builder.add_container(backup_container)
158
David Garcia49379ce2021-02-24 13:48:22 +0100159 # Build Container
160 container_builder = ContainerV3Builder(self.app.name, image_info)
161 container_builder.add_port(name=self.app.name, port=PORT)
162 container_builder.add_http_readiness_probe(
163 "/-/ready",
164 PORT,
165 initial_delay_seconds=10,
166 timeout_seconds=30,
167 )
168 container_builder.add_http_liveness_probe(
169 "/-/healthy",
170 PORT,
171 initial_delay_seconds=30,
172 period_seconds=30,
173 )
174 command = [
175 "/bin/prometheus",
176 "--config.file=/etc/prometheus/prometheus.yml",
177 "--storage.tsdb.path=/prometheus",
178 "--web.console.libraries=/usr/share/prometheus/console_libraries",
179 "--web.console.templates=/usr/share/prometheus/consoles",
180 f"--web.route-prefix={config.web_subpath}",
181 f"--web.external-url=http://localhost:{PORT}{config.web_subpath}",
182 ]
183 if config.enable_web_admin_api:
184 command.append("--web.enable-admin-api")
185 container_builder.add_command(command)
186 container_builder.add_volume_config(
187 "config", "/etc/prometheus", self._build_files(config)
188 )
189 container = container_builder.build()
190 # Add container to pod spec
191 pod_spec_builder.add_container(container)
192 # Add ingress resources to pod spec if site url exists
193 if config.site_url:
194 parsed = urlparse(config.site_url)
195 annotations = {
196 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
197 str(config.max_file_size) + "m"
198 if config.max_file_size > 0
199 else config.max_file_size
David Garciac35943e2021-06-28 16:50:42 +0200200 )
sousaedu2459af62021-01-15 16:50:26 +0000201 }
David Garciac35943e2021-06-28 16:50:42 +0200202 if config.ingress_class:
203 annotations["kubernetes.io/ingress.class"] = config.ingress_class
David Garcia49379ce2021-02-24 13:48:22 +0100204 ingress_resource_builder = IngressResourceV3Builder(
205 f"{self.app.name}-ingress", annotations
sousaedu2459af62021-01-15 16:50:26 +0000206 )
sousaedu2459af62021-01-15 16:50:26 +0000207
David Garcia49379ce2021-02-24 13:48:22 +0100208 if config.ingress_whitelist_source_range:
209 annotations[
210 "nginx.ingress.kubernetes.io/whitelist-source-range"
211 ] = config.ingress_whitelist_source_range
sousaedu2459af62021-01-15 16:50:26 +0000212
sousaedu3cc03162021-04-29 16:53:12 +0200213 if config.cluster_issuer:
214 annotations["cert-manager.io/cluster-issuer"] = config.cluster_issuer
215
David Garcia49379ce2021-02-24 13:48:22 +0100216 if parsed.scheme == "https":
217 ingress_resource_builder.add_tls(
218 [parsed.hostname], config.tls_secret_name
219 )
220 else:
221 annotations["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
222
223 ingress_resource_builder.add_rule(parsed.hostname, self.app.name, PORT)
224 ingress_resource = ingress_resource_builder.build()
225 pod_spec_builder.add_ingress_resource(ingress_resource)
226 return pod_spec_builder.build()
sousaedu2459af62021-01-15 16:50:26 +0000227
228
229if __name__ == "__main__":
230 main(PrometheusCharm)