- def __init__(self, *args) -> NoReturn:
- """Grafana Charm constructor."""
- super().__init__(*args)
-
- # Internal state initialization
- self.state.set_default(pod_spec=None)
-
- self.port = GRAFANA_PORT
- self.image = OCIImageResource(self, "image")
-
- # Registering regular events
- self.framework.observe(self.on.start, self.configure_pod)
- self.framework.observe(self.on.config_changed, self.configure_pod)
-
- # Registering required relation events
- self.framework.observe(self.on.prometheus_relation_changed, self.configure_pod)
-
- # Registering required relation broken events
- self.framework.observe(self.on.prometheus_relation_broken, self.configure_pod)
-
- @property
- def relations_requirements(self):
- return [RelationDefinition("prometheus", ["host", "port"], Unit)]
-
- def get_relation_state(self):
- relation_state = {}
- for relation_requirements in self.relations_requirements:
- data = get_relation_data(self, relation_requirements)
- relation_state = {**relation_state, **data}
- check_missing_relation_data(relation_state, self.relations_requirements)
- return relation_state
-
- def configure_pod(self, _=None) -> NoReturn:
- """Assemble the pod spec and apply it, if possible.
-
- Args:
- event (EventBase): Hook or Relation event that started the
- function.
- """
- if not self.unit.is_leader():
- self.unit.status = ActiveStatus("ready")
- return
-
- relation_state = None
- try:
- relation_state = self.get_relation_state()
- except RelationsMissing as exc:
- logger.exception("Relation missing error")
- self.unit.status = BlockedStatus(exc.message)
- return
-
- self.unit.status = MaintenanceStatus("Assembling pod spec")
-
- # Fetch image information
- try:
- self.unit.status = MaintenanceStatus("Fetching image information")
- image_info = self.image.fetch()
- except OCIImageResourceError:
- self.unit.status = BlockedStatus("Error fetching image information")
- return
-
- try:
- pod_spec = make_pod_spec(
- image_info,
- self.model.config,
- relation_state,
- self.model.app.name,
- self.port,
+ def _build_dashboard_files(self, config: ConfigModel):
+ files_builder = FilesV3Builder()
+ files_builder.add_file(
+ "dashboard_osm.yaml",
+ Path("templates/default_dashboards.yaml").read_text(),
+ )
+ if config.osm_dashboards:
+ osm_dashboards_mapping = {
+ "kafka_exporter_dashboard.json": "templates/kafka_exporter_dashboard.json",
+ "mongodb_exporter_dashboard.json": "templates/mongodb_exporter_dashboard.json",
+ "mysql_exporter_dashboard.json": "templates/mysql_exporter_dashboard.json",
+ "nodes_exporter_dashboard.json": "templates/nodes_exporter_dashboard.json",
+ "summary_dashboard.json": "templates/summary_dashboard.json",
+ }
+ for file_name, path in osm_dashboards_mapping.items():
+ files_builder.add_file(file_name, Path(path).read_text())
+ return files_builder.build()
+
+ def _build_datasources_files(self):
+ files_builder = FilesV3Builder()
+ files_builder.add_file(
+ "datasource_prometheus.yaml",
+ Template(Path("templates/default_datasources.yaml").read_text()).substitute(
+ prometheus_host=self.prometheus_client.hostname,
+ prometheus_port=self.prometheus_client.port,
+ ),
+ )
+ return files_builder.build()
+
+ def _check_missing_dependencies(self):
+ missing_relations = []
+
+ if self.prometheus_client.is_missing_data_in_app():
+ missing_relations.append("prometheus")
+
+ if missing_relations:
+ raise RelationsMissing(missing_relations)
+
+ def build_pod_spec(self, image_info):
+ # Validate config
+ config = ConfigModel(**dict(self.config))
+ # Check relations
+ self._check_missing_dependencies()
+ # Create Builder for the PodSpec
+ pod_spec_builder = PodSpecV3Builder()
+ # Build Container
+ container_builder = ContainerV3Builder(
+ self.app.name, image_info, config.image_pull_policy
+ )
+ container_builder.add_port(name=self.app.name, port=PORT)
+ container_builder.add_http_readiness_probe(
+ "/api/health",
+ PORT,
+ initial_delay_seconds=10,
+ period_seconds=10,
+ timeout_seconds=5,
+ failure_threshold=3,
+ )
+ container_builder.add_http_liveness_probe(
+ "/api/health",
+ PORT,
+ initial_delay_seconds=60,
+ timeout_seconds=30,
+ failure_threshold=10,
+ )
+ container_builder.add_volume_config(
+ "dashboards",
+ "/etc/grafana/provisioning/dashboards/",
+ self._build_dashboard_files(config),
+ )
+ container_builder.add_volume_config(
+ "datasources",
+ "/etc/grafana/provisioning/datasources/",
+ self._build_datasources_files(),
+ )
+ container = container_builder.build()
+ # Add container to pod spec
+ pod_spec_builder.add_container(container)
+ # Add ingress resources to pod spec if site url exists
+ if config.site_url:
+ parsed = urlparse(config.site_url)
+ annotations = {
+ "nginx.ingress.kubernetes.io/proxy-body-size": "{}".format(
+ str(config.max_file_size) + "m"
+ if config.max_file_size > 0
+ else config.max_file_size
+ )
+ }
+ if config.ingress_class:
+ annotations["kubernetes.io/ingress.class"] = config.ingress_class
+ ingress_resource_builder = IngressResourceV3Builder(
+ f"{self.app.name}-ingress", annotations