projects
/
osm
/
devops.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Jenkins refresh
[osm/devops.git]
/
installers
/
charm
/
prometheus
/
src
/
charm.py
diff --git
a/installers/charm/prometheus/src/charm.py
b/installers/charm/prometheus/src/charm.py
index
e71d949
..
af39a13
100755
(executable)
--- a/
installers/charm/prometheus/src/charm.py
+++ b/
installers/charm/prometheus/src/charm.py
@@
-22,11
+22,13
@@
# pylint: disable=E0213
# pylint: disable=E0213
+import base64
from ipaddress import ip_network
import logging
from typing import NoReturn, Optional
from urllib.parse import urlparse
from ipaddress import ip_network
import logging
from typing import NoReturn, Optional
from urllib.parse import urlparse
+import bcrypt
from oci_image import OCIImageResource
from ops.framework import EventBase
from ops.main import main
from oci_image import OCIImageResource
from ops.framework import EventBase
from ops.main import main
@@
-56,9
+58,14
@@
class ConfigModel(ModelValidator):
max_file_size: int
site_url: Optional[str]
cluster_issuer: Optional[str]
max_file_size: int
site_url: Optional[str]
cluster_issuer: Optional[str]
+ ingress_class: Optional[str]
ingress_whitelist_source_range: Optional[str]
tls_secret_name: Optional[str]
enable_web_admin_api: bool
ingress_whitelist_source_range: Optional[str]
tls_secret_name: Optional[str]
enable_web_admin_api: bool
+ image_pull_policy: str
+ security_context: bool
+ web_config_username: str
+ web_config_password: str
@validator("web_subpath")
def validate_web_subpath(cls, v):
@validator("web_subpath")
def validate_web_subpath(cls, v):
@@
-86,6
+93,18
@@
class ConfigModel(ModelValidator):
ip_network(v)
return v
ip_network(v)
return v
+ @validator("image_pull_policy")
+ def validate_image_pull_policy(cls, v):
+ values = {
+ "always": "Always",
+ "ifnotpresent": "IfNotPresent",
+ "never": "Never",
+ }
+ v = v.lower()
+ if v not in values.keys():
+ raise ValueError("value must be always, ifnotpresent or never")
+ return values[v]
+
class PrometheusCharm(CharmedOsmBase):
class PrometheusCharm(CharmedOsmBase):
@@
-109,18
+128,24
@@
class PrometheusCharm(CharmedOsmBase):
)
def _publish_prometheus_info(self, event: EventBase) -> NoReturn:
)
def _publish_prometheus_info(self, event: EventBase) -> NoReturn:
- self.prometheus.publish_info(self.app.name, PORT)
+ 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:
def _on_backup_action(self, event: EventBase) -> NoReturn:
- url = f"http://{self.model.app.name}:{PORT}/api/v
2
/admin/tsdb/snapshot"
+ url = f"http://{self.model.app.name}:{PORT}/api/v
1
/admin/tsdb/snapshot"
result = requests.post(url)
if result.status_code == 200:
event.set_results({"backup-name": result.json()["name"]})
else:
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}
, result: {result.json()}
")
+ event.fail(f"status-code: {result.status_code}")
- def _build_
files
(self, config: ConfigModel):
+ def _build_
config_file
(self, config: ConfigModel):
files_builder = FilesV3Builder()
files_builder.add_file(
"prometheus.yml",
files_builder = FilesV3Builder()
files_builder.add_file(
"prometheus.yml",
@@
-141,38
+166,69
@@
class PrometheusCharm(CharmedOsmBase):
)
return files_builder.build()
)
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
def build_pod_spec(self, image_info):
# Validate config
config = ConfigModel(**dict(self.config))
# Create Builder for the PodSpec
- pod_spec_builder = PodSpecV3Builder()
+ 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()
# 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 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
# Build Container
- container_builder = ContainerV3Builder(self.app.name, image_info)
+ 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)
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,
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,
)
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",
)
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",
"--storage.tsdb.path=/prometheus",
"--web.console.libraries=/usr/share/prometheus/console_libraries",
"--web.console.templates=/usr/share/prometheus/consoles",
@@
-183,7
+239,13
@@
class PrometheusCharm(CharmedOsmBase):
command.append("--web.enable-admin-api")
container_builder.add_command(command)
container_builder.add_volume_config(
command.append("--web.enable-admin-api")
container_builder.add_command(command)
container_builder.add_volume_config(
- "config", "/etc/prometheus", self._build_files(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
)
container = container_builder.build()
# Add container to pod spec
@@
-196,8
+258,10
@@
class PrometheusCharm(CharmedOsmBase):
str(config.max_file_size) + "m"
if config.max_file_size > 0
else config.max_file_size
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
)
ingress_resource_builder = IngressResourceV3Builder(
f"{self.app.name}-ingress", annotations
)
@@
-222,6
+286,13
@@
class PrometheusCharm(CharmedOsmBase):
pod_spec_builder.add_ingress_resource(ingress_resource)
return pod_spec_builder.build()
pod_spec_builder.add_ingress_resource(ingress_resource)
return pod_spec_builder.build()
+ def _hash_password(self, password):
+ hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
+ return hashed_password.decode()
+
+ def _base64_encode(self, phrase: str) -> str:
+ return base64.b64encode(phrase.encode("utf-8")).decode("utf-8")
+
if __name__ == "__main__":
main(PrometheusCharm)
if __name__ == "__main__":
main(PrometheusCharm)