+ config = ConfigModel(**dict(self.config))
+ self.prometheus.publish_info(
+ self.app.name,
+ PORT,
+ user=config.web_config_username,
+ password=config.web_config_password,
+ )
+
+ def _on_backup_action(self, event: EventBase) -> NoReturn:
+ url = f"http://{self.model.app.name}:{PORT}/api/v1/admin/tsdb/snapshot"
+ result = requests.post(url)
+
+ if result.status_code == 200:
+ event.set_results({"backup-name": result.json()["name"]})
+ else:
+ event.fail(f"status-code: {result.status_code}")
+
+ def _build_config_file(self, config: ConfigModel):
+ files_builder = FilesV3Builder()
+ files_builder.add_file(
+ "prometheus.yml",
+ (
+ "global:\n"
+ " scrape_interval: 15s\n"
+ " evaluation_interval: 15s\n"
+ "alerting:\n"
+ " alertmanagers:\n"
+ " - static_configs:\n"
+ " - targets:\n"
+ "rule_files:\n"
+ "scrape_configs:\n"
+ " - job_name: 'prometheus'\n"
+ " static_configs:\n"
+ f" - targets: [{config.default_target}]\n"
+ ),
+ )
+ return files_builder.build()
+
+ def _build_webconfig_file(self):
+ files_builder = FilesV3Builder()
+ files_builder.add_file("web.yml", "web-config-file", secret=True)
+ return files_builder.build()
+
+ def build_pod_spec(self, image_info):
+ # Validate config
+ config = ConfigModel(**dict(self.config))
+ # Create Builder for the PodSpec
+ pod_spec_builder = PodSpecV3Builder(
+ enable_security_context=config.security_context
+ )
+
+ # Build Backup Container
+ backup_image = OCIImageResource(self, "backup-image")
+ backup_image_info = backup_image.fetch()
+ backup_container_builder = ContainerV3Builder("prom-backup", backup_image_info)
+ backup_container = backup_container_builder.build()
+
+ # Add backup container to pod spec
+ pod_spec_builder.add_container(backup_container)
+
+ # Add pod secrets
+ prometheus_secret_name = f"{self.app.name}-secret"
+ pod_spec_builder.add_secret(
+ prometheus_secret_name,
+ {
+ "web-config-file": (
+ "basic_auth_users:\n"
+ f" {config.web_config_username}: {self._hash_password(config.web_config_password)}\n"
+ )
+ },
+ )
+
+ # Build Container
+ container_builder = ContainerV3Builder(
+ self.app.name,
+ image_info,
+ config.image_pull_policy,
+ run_as_non_root=config.security_context,
+ )
+ container_builder.add_port(name=self.app.name, port=PORT)
+ token = self._base64_encode(
+ f"{config.web_config_username}:{config.web_config_password}"
+ )
+ container_builder.add_http_readiness_probe(
+ "/-/ready",
+ PORT,
+ initial_delay_seconds=10,
+ timeout_seconds=30,
+ http_headers=[("Authorization", f"Basic {token}")],
+ )
+ container_builder.add_http_liveness_probe(
+ "/-/healthy",
+ PORT,
+ initial_delay_seconds=30,
+ period_seconds=30,
+ http_headers=[("Authorization", f"Basic {token}")],
+ )
+ command = [
+ "/bin/prometheus",
+ "--config.file=/etc/prometheus/prometheus.yml",
+ "--web.config.file=/etc/prometheus/web-config/web.yml",
+ "--storage.tsdb.path=/prometheus",
+ "--web.console.libraries=/usr/share/prometheus/console_libraries",
+ "--web.console.templates=/usr/share/prometheus/consoles",
+ f"--web.route-prefix={config.web_subpath}",
+ f"--web.external-url=http://localhost:{PORT}{config.web_subpath}",
+ ]
+ if config.enable_web_admin_api:
+ command.append("--web.enable-admin-api")
+ container_builder.add_command(command)
+ container_builder.add_volume_config(
+ "config", "/etc/prometheus", self._build_config_file(config)
+ )
+ container_builder.add_volume_config(
+ "web-config",
+ "/etc/prometheus/web-config",
+ self._build_webconfig_file(),
+ secret_name=prometheus_secret_name,
+ )
+ 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
+ )