2 # Copyright 2021 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 # pylint: disable=E0213
25 from ipaddress
import ip_network
27 from pathlib
import Path
29 from string
import Template
30 from typing
import NoReturn
, Optional
31 from urllib
.parse
import urlparse
33 from ops
.main
import main
34 from opslib
.osm
.charm
import CharmedOsmBase
, RelationsMissing
35 from opslib
.osm
.interfaces
.grafana
import GrafanaCluster
36 from opslib
.osm
.interfaces
.mysql
import MysqlClient
37 from opslib
.osm
.interfaces
.prometheus
import PrometheusClient
38 from opslib
.osm
.pod
import (
41 IngressResourceV3Builder
,
45 from opslib
.osm
.validator
import ModelValidator
, validator
48 logger
= logging
.getLogger(__name__
)
51 class ConfigModel(ModelValidator
):
57 site_url
: Optional
[str]
58 cluster_issuer
: Optional
[str]
59 ingress_class
: Optional
[str]
60 ingress_whitelist_source_range
: Optional
[str]
61 tls_secret_name
: Optional
[str]
62 image_pull_policy
: str
63 security_context
: bool
65 @validator("log_level")
66 def validate_log_level(cls
, v
):
67 allowed_values
= ("debug", "info", "warn", "error", "critical")
68 if v
not in allowed_values
:
71 f
'incorrect value. Allowed values are "{separator.join(allowed_values)}"'
75 @validator("max_file_size")
76 def validate_max_file_size(cls
, v
):
78 raise ValueError("value must be equal or greater than 0")
81 @validator("site_url")
82 def validate_site_url(cls
, v
):
85 if not parsed
.scheme
.startswith("http"):
86 raise ValueError("value must start with http")
89 @validator("ingress_whitelist_source_range")
90 def validate_ingress_whitelist_source_range(cls
, v
):
95 @validator("image_pull_policy")
96 def validate_image_pull_policy(cls
, v
):
99 "ifnotpresent": "IfNotPresent",
103 if v
not in values
.keys():
104 raise ValueError("value must be always, ifnotpresent or never")
108 class GrafanaCharm(CharmedOsmBase
):
109 """GrafanaCharm Charm."""
111 def __init__(self
, *args
) -> NoReturn
:
112 """Prometheus Charm constructor."""
113 super().__init
__(*args
, oci_image
="image", mysql_uri
=True)
114 # Initialize relation objects
115 self
.prometheus_client
= PrometheusClient(self
, "prometheus")
116 self
.grafana_cluster
= GrafanaCluster(self
, "cluster")
117 self
.mysql_client
= MysqlClient(self
, "db")
119 event_observer_mapping
= {
120 self
.on
["prometheus"].relation_changed
: self
.configure_pod
,
121 self
.on
["prometheus"].relation_broken
: self
.configure_pod
,
122 self
.on
["db"].relation_changed
: self
.configure_pod
,
123 self
.on
["db"].relation_broken
: self
.configure_pod
,
125 for event
, observer
in event_observer_mapping
.items():
126 self
.framework
.observe(event
, observer
)
128 def _build_dashboard_files(self
, config
: ConfigModel
):
129 files_builder
= FilesV3Builder()
130 files_builder
.add_file(
131 "dashboard_osm.yaml",
132 Path("templates/default_dashboards.yaml").read_text(),
134 if config
.osm_dashboards
:
135 osm_dashboards_mapping
= {
136 "kafka_exporter_dashboard.json": "templates/kafka_exporter_dashboard.json",
137 "mongodb_exporter_dashboard.json": "templates/mongodb_exporter_dashboard.json",
138 "mysql_exporter_dashboard.json": "templates/mysql_exporter_dashboard.json",
139 "nodes_exporter_dashboard.json": "templates/nodes_exporter_dashboard.json",
140 "summary_dashboard.json": "templates/summary_dashboard.json",
142 for file_name
, path
in osm_dashboards_mapping
.items():
143 files_builder
.add_file(file_name
, Path(path
).read_text())
144 return files_builder
.build()
146 def _build_datasources_files(self
):
147 files_builder
= FilesV3Builder()
148 prometheus_user
= self
.prometheus_client
.user
149 prometheus_password
= self
.prometheus_client
.password
150 enable_basic_auth
= all([prometheus_user
, prometheus_password
])
151 files_builder
.add_file(
152 "datasource_prometheus.yaml",
153 Template(Path("templates/default_datasources.yaml").read_text()).substitute(
154 prometheus_host
=self
.prometheus_client
.hostname
,
155 prometheus_port
=self
.prometheus_client
.port
,
156 enable_basic_auth
=enable_basic_auth
,
157 user
=f
"user: {prometheus_user}" if enable_basic_auth
else "",
158 password
=f
"password: {prometheus_password}"
163 return files_builder
.build()
165 def _check_missing_dependencies(self
, config
: ConfigModel
, external_db
: bool):
166 missing_relations
= []
168 if self
.prometheus_client
.is_missing_data_in_app():
169 missing_relations
.append("prometheus")
171 if not external_db
and self
.mysql_client
.is_missing_data_in_unit():
172 missing_relations
.append("db")
174 if missing_relations
:
175 raise RelationsMissing(missing_relations
)
177 def build_pod_spec(self
, image_info
, **kwargs
):
179 config
= ConfigModel(**dict(self
.config
))
180 mysql_config
= kwargs
["mysql_config"]
181 if mysql_config
.mysql_uri
and not self
.mysql_client
.is_missing_data_in_unit():
182 raise Exception("Mysql data cannot be provided via config and relation")
185 external_db
= True if mysql_config
.mysql_uri
else False
186 self
._check
_missing
_dependencies
(config
, external_db
)
188 # Get initial password
189 admin_initial_password
= self
.grafana_cluster
.admin_initial_password
190 if not admin_initial_password
:
191 admin_initial_password
= _generate_random_password()
192 self
.grafana_cluster
.set_initial_password(admin_initial_password
)
194 # Create Builder for the PodSpec
195 pod_spec_builder
= PodSpecV3Builder(
196 enable_security_context
=config
.security_context
199 # Add secrets to the pod
200 grafana_secret_name
= f
"{self.app.name}-admin-secret"
201 pod_spec_builder
.add_secret(
204 "admin-password": admin_initial_password
,
205 "mysql-url": mysql_config
.mysql_uri
or self
.mysql_client
.get_uri(),
210 container_builder
= ContainerV3Builder(
213 config
.image_pull_policy
,
214 run_as_non_root
=config
.security_context
,
216 container_builder
.add_port(name
=self
.app
.name
, port
=config
.port
)
217 container_builder
.add_http_readiness_probe(
220 initial_delay_seconds
=10,
225 container_builder
.add_http_liveness_probe(
228 initial_delay_seconds
=60,
230 failure_threshold
=10,
232 container_builder
.add_volume_config(
234 "/etc/grafana/provisioning/dashboards/",
235 self
._build
_dashboard
_files
(config
),
237 container_builder
.add_volume_config(
239 "/etc/grafana/provisioning/datasources/",
240 self
._build
_datasources
_files
(),
243 container_builder
.add_envs(
245 "GF_SERVER_HTTP_PORT": config
.port
,
246 "GF_LOG_LEVEL": config
.log_level
,
247 "GF_SECURITY_ADMIN_USER": config
.admin_user
,
250 container_builder
.add_secret_envs(
251 secret_name
=grafana_secret_name
,
253 "GF_SECURITY_ADMIN_PASSWORD": "admin-password",
254 "GF_DATABASE_URL": "mysql-url",
257 container
= container_builder
.build()
258 pod_spec_builder
.add_container(container
)
260 # Add Pod restart policy
261 restart_policy
= PodRestartPolicy()
262 restart_policy
.add_secrets(secret_names
=(grafana_secret_name
,))
263 pod_spec_builder
.set_restart_policy(restart_policy
)
265 # Add ingress resources to pod spec if site url exists
267 parsed
= urlparse(config
.site_url
)
269 "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
270 str(config
.max_file_size
) + "m"
271 if config
.max_file_size
> 0
272 else config
.max_file_size
275 if config
.ingress_class
:
276 annotations
["kubernetes.io/ingress.class"] = config
.ingress_class
277 ingress_resource_builder
= IngressResourceV3Builder(
278 f
"{self.app.name}-ingress", annotations
281 if config
.ingress_whitelist_source_range
:
283 "nginx.ingress.kubernetes.io/whitelist-source-range"
284 ] = config
.ingress_whitelist_source_range
286 if config
.cluster_issuer
:
287 annotations
["cert-manager.io/cluster-issuer"] = config
.cluster_issuer
289 if parsed
.scheme
== "https":
290 ingress_resource_builder
.add_tls(
291 [parsed
.hostname
], config
.tls_secret_name
294 annotations
["nginx.ingress.kubernetes.io/ssl-redirect"] = "false"
296 ingress_resource_builder
.add_rule(
297 parsed
.hostname
, self
.app
.name
, config
.port
299 ingress_resource
= ingress_resource_builder
.build()
300 pod_spec_builder
.add_ingress_resource(ingress_resource
)
301 return pod_spec_builder
.build()
304 def _generate_random_password():
305 return secrets
.token_hex(16)
308 if __name__
== "__main__":