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
28 from typing
import NoReturn
, Optional
31 from ops
.main
import main
32 from opslib
.osm
.charm
import CharmedOsmBase
, RelationsMissing
33 from opslib
.osm
.interfaces
.kafka
import KafkaClient
34 from opslib
.osm
.interfaces
.keystone
import KeystoneClient
35 from opslib
.osm
.interfaces
.mongo
import MongoClient
36 from opslib
.osm
.interfaces
.prometheus
import PrometheusClient
37 from opslib
.osm
.pod
import ContainerV3Builder
, FilesV3Builder
, PodSpecV3Builder
38 from opslib
.osm
.validator
import ModelValidator
, validator
41 logger
= logging
.getLogger(__name__
)
46 def _check_certificate_data(name
: str, content
: str):
47 if not name
or not content
:
48 raise ValueError("certificate name and content must be a non-empty string")
51 def _extract_certificates(certs_config
: str):
54 cert_list
= certs_config
.split(",")
55 for cert
in cert_list
:
56 name
, content
= cert
.split(":")
57 _check_certificate_data(name
, content
)
58 certificates
[name
] = content
62 def decode(content
: str):
63 return base64
.b64decode(content
.encode("utf-8")).decode("utf-8")
66 class ConfigModel(ModelValidator
):
67 keystone_enabled
: bool
72 database_commonkey
: str
73 mongodb_uri
: Optional
[str]
75 openstack_default_granularity
: int
76 global_request_timeout
: int
77 collector_interval
: int
78 evaluator_interval
: int
82 certificates
: Optional
[str]
84 @validator("log_level")
85 def validate_log_level(cls
, v
):
86 if v
not in {"INFO", "DEBUG"}:
87 raise ValueError("value must be INFO or DEBUG")
90 @validator("certificates")
91 def validate_certificates(cls
, v
):
92 # Raises an exception if it cannot extract the certificates
93 _extract_certificates(v
)
96 @validator("mongodb_uri")
97 def validate_mongodb_uri(cls
, v
):
98 if v
and not v
.startswith("mongodb://"):
99 raise ValueError("mongodb_uri is not properly formed")
103 def certificates_dict(cls
):
104 return _extract_certificates(cls
.certificates
) if cls
.certificates
else {}
107 class MonCharm(CharmedOsmBase
):
108 def __init__(self
, *args
) -> NoReturn
:
109 super().__init
__(*args
, oci_image
="image")
111 self
.kafka_client
= KafkaClient(self
, "kafka")
112 self
.framework
.observe(self
.on
["kafka"].relation_changed
, self
.configure_pod
)
113 self
.framework
.observe(self
.on
["kafka"].relation_broken
, self
.configure_pod
)
115 self
.mongodb_client
= MongoClient(self
, "mongodb")
116 self
.framework
.observe(self
.on
["mongodb"].relation_changed
, self
.configure_pod
)
117 self
.framework
.observe(self
.on
["mongodb"].relation_broken
, self
.configure_pod
)
119 self
.prometheus_client
= PrometheusClient(self
, "prometheus")
120 self
.framework
.observe(
121 self
.on
["prometheus"].relation_changed
, self
.configure_pod
123 self
.framework
.observe(
124 self
.on
["prometheus"].relation_broken
, self
.configure_pod
127 self
.keystone_client
= KeystoneClient(self
, "keystone")
128 self
.framework
.observe(self
.on
["keystone"].relation_changed
, self
.configure_pod
)
129 self
.framework
.observe(self
.on
["keystone"].relation_broken
, self
.configure_pod
)
131 def _check_missing_dependencies(self
, config
: ConfigModel
):
132 missing_relations
= []
134 if self
.kafka_client
.is_missing_data_in_unit():
135 missing_relations
.append("kafka")
136 if not config
.mongodb_uri
and self
.mongodb_client
.is_missing_data_in_unit():
137 missing_relations
.append("mongodb")
138 if self
.prometheus_client
.is_missing_data_in_app():
139 missing_relations
.append("prometheus")
140 if config
.keystone_enabled
:
141 if self
.keystone_client
.is_missing_data_in_app():
142 missing_relations
.append("keystone")
144 if missing_relations
:
145 raise RelationsMissing(missing_relations
)
147 def _build_cert_files(
151 cert_files_builder
= FilesV3Builder()
152 for name
, content
in config
.certificates_dict
.items():
153 cert_files_builder
.add_file(name
, decode(content
), mode
=0o600)
154 return cert_files_builder
.build()
156 def build_pod_spec(self
, image_info
):
158 config
= ConfigModel(**dict(self
.config
))
160 if config
.mongodb_uri
and not self
.mongodb_client
.is_missing_data_in_unit():
161 raise Exception("Mongodb data cannot be provided via config and relation")
164 self
._check
_missing
_dependencies
(config
)
166 # Create Builder for the PodSpec
167 pod_spec_builder
= PodSpecV3Builder()
170 container_builder
= ContainerV3Builder(self
.app
.name
, image_info
)
171 certs_files
= self
._build
_cert
_files
(config
)
174 container_builder
.add_volume_config("certs", "/certs", certs_files
)
176 container_builder
.add_port(name
=self
.app
.name
, port
=PORT
)
177 container_builder
.add_envs(
179 # General configuration
180 "ALLOW_ANONYMOUS_LOGIN": "yes",
181 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
.openstack_default_granularity
,
182 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config
.global_request_timeout
,
183 "OSMMON_GLOBAL_LOGLEVEL": config
.log_level
,
184 "OSMMON_COLLECTOR_INTERVAL": config
.collector_interval
,
185 "OSMMON_EVALUATOR_INTERVAL": config
.evaluator_interval
,
186 # Kafka configuration
187 "OSMMON_MESSAGE_DRIVER": "kafka",
188 "OSMMON_MESSAGE_HOST": self
.kafka_client
.host
,
189 "OSMMON_MESSAGE_PORT": self
.kafka_client
.port
,
190 # Database configuration
191 "OSMMON_DATABASE_DRIVER": "mongo",
192 "OSMMON_DATABASE_URI": config
.mongodb_uri
193 or self
.mongodb_client
.connection_string
,
194 "OSMMON_DATABASE_COMMONKEY": config
.database_commonkey
,
195 # Prometheus configuration
196 "OSMMON_PROMETHEUS_URL": f
"http://{self.prometheus_client.hostname}:{self.prometheus_client.port}",
198 "OSMMON_VCA_HOST": config
.vca_host
,
199 "OSMMON_VCA_USER": config
.vca_user
,
200 "OSMMON_VCA_SECRET": config
.vca_secret
,
201 "OSMMON_VCA_CACERT": config
.vca_cacert
,
202 "OSMMON_GRAFANA_URL": config
.grafana_url
,
203 "OSMMON_GRAFANA_USER": config
.grafana_user
,
204 "OSMMON_GRAFANA_PASSWORD": config
.grafana_password
,
207 if config
.keystone_enabled
:
208 container_builder
.add_envs(
210 "OSMMON_KEYSTONE_ENABLED": True,
211 "OSMMON_KEYSTONE_URL": self
.keystone_client
.host
,
212 "OSMMON_KEYSTONE_DOMAIN_NAME": self
.keystone_client
.user_domain_name
,
213 "OSMMON_KEYSTONE_PROJECT_DOMAIN_NAME": self
.keystone_client
.project_domain_name
,
214 "OSMMON_KEYSTONE_SERVICE_USER": self
.keystone_client
.username
,
215 "OSMMON_KEYSTONE_SERVICE_PASSWORD": self
.keystone_client
.password
,
216 "OSMMON_KEYSTONE_SERVICE_PROJECT": self
.keystone_client
.service
,
219 container
= container_builder
.build()
221 # Add container to pod spec
222 pod_spec_builder
.add_container(container
)
224 return pod_spec_builder
.build()
227 if __name__
== "__main__":