2 # Copyright 2022 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 # Learn more at: https://juju.is/docs/sdk
27 See more: https://charmhub.io/osm
31 from typing
import Any
, Dict
33 from charms
.kafka_k8s
.v0
.kafka
import KafkaRequires
, _KafkaAvailableEvent
34 from charms
.observability_libs
.v1
.kubernetes_service_patch
import KubernetesServicePatch
35 from charms
.osm_libs
.v0
.utils
import (
39 check_container_ready
,
42 from charms
.osm_vca_integrator
.v0
.vca
import VcaDataChangedEvent
, VcaRequires
43 from lightkube
.models
.core_v1
import ServicePort
44 from ops
.charm
import ActionEvent
, CharmBase
, CharmEvents
45 from ops
.framework
import EventSource
, StoredState
46 from ops
.main
import main
47 from ops
.model
import ActiveStatus
, Container
49 from legacy_interfaces
import KeystoneClient
, MongoClient
, PrometheusClient
53 config
="mon-hostpath",
54 container_path
="/usr/lib/python3/dist-packages/osm_mon",
57 config
="common-hostpath",
58 container_path
="/usr/lib/python3/dist-packages/osm_common",
61 config
="n2vc-hostpath",
62 container_path
="/usr/lib/python3/dist-packages/n2vc",
67 logger
= logging
.getLogger(__name__
)
70 class MonEvents(CharmEvents
):
73 vca_data_changed
= EventSource(VcaDataChangedEvent
)
74 kafka_available
= EventSource(_KafkaAvailableEvent
)
77 class OsmMonCharm(CharmBase
):
78 """OSM MON Kubernetes sidecar charm."""
81 _stored
= StoredState()
82 container_name
= "mon"
85 def __init__(self
, *args
):
86 super().__init
__(*args
)
87 self
.kafka
= KafkaRequires(self
)
88 self
.mongodb_client
= MongoClient(self
, "mongodb")
89 self
.prometheus_client
= PrometheusClient(self
, "prometheus")
90 self
.keystone_client
= KeystoneClient(self
, "keystone")
91 self
.vca
= VcaRequires(self
)
92 self
._observe
_charm
_events
()
93 self
.container
: Container
= self
.unit
.get_container(self
.container_name
)
94 self
.debug_mode
= DebugMode(self
, self
._stored
, self
.container
, HOSTPATHS
)
95 self
._patch
_k
8s
_service
()
98 def external_hostname(self
) -> str:
99 """External hostname property.
102 str: the external hostname from config.
103 If not set, return the ClusterIP service name.
105 return self
.config
.get("external-hostname") or self
.app
.name
107 # ---------------------------------------------------------------------------
108 # Handlers for Charm Events
109 # ---------------------------------------------------------------------------
111 def _on_config_changed(self
, _
) -> None:
112 """Handler for the config-changed event."""
114 self
._validate
_config
()
115 self
._check
_relations
()
116 # Check if the container is ready.
117 # Eventually it will become ready after the first pebble-ready event.
118 check_container_ready(self
.container
)
119 if not self
.debug_mode
.started
:
120 self
._configure
_service
(self
.container
)
121 # Update charm status
122 self
._on
_update
_status
()
123 except CharmError
as e
:
124 logger
.debug(e
.message
)
125 self
.unit
.status
= e
.status
127 def _on_update_status(self
, _
=None) -> None:
128 """Handler for the update-status event."""
130 self
._validate
_config
()
131 self
._check
_relations
()
132 check_container_ready(self
.container
)
133 if self
.debug_mode
.started
:
135 check_service_active(self
.container
, self
.service_name
)
136 self
.unit
.status
= ActiveStatus()
137 except CharmError
as e
:
138 logger
.debug(e
.message
)
139 self
.unit
.status
= e
.status
141 def _on_required_relation_broken(self
, _
) -> None:
142 """Handler for the kafka-broken event."""
144 check_container_ready(self
.container
)
145 check_service_active(self
.container
, self
.service_name
)
146 self
.container
.stop(self
.container_name
)
149 self
._on
_update
_status
()
151 def _on_get_debug_mode_information_action(self
, event
: ActionEvent
) -> None:
152 """Handler for the get-debug-mode-information action event."""
153 if not self
.debug_mode
.started
:
155 "debug-mode has not started. Hint: juju config mon debug-mode=true"
160 "command": self
.debug_mode
.command
,
161 "password": self
.debug_mode
.password
,
163 event
.set_results(debug_info
)
165 # ---------------------------------------------------------------------------
166 # Validation and configuration and more
167 # ---------------------------------------------------------------------------
169 def _observe_charm_events(self
) -> None:
170 event_handler_mapping
= {
171 # Core lifecycle events
172 self
.on
.mon_pebble_ready
: self
._on
_config
_changed
,
173 self
.on
.config_changed
: self
._on
_config
_changed
,
174 self
.on
.update_status
: self
._on
_update
_status
,
176 self
.on
.vca_data_changed
: self
._on
_config
_changed
,
177 self
.on
.kafka_available
: self
._on
_config
_changed
,
178 self
.on
["kafka"].relation_broken
: self
._on
_required
_relation
_broken
,
180 self
.on
.get_debug_mode_information_action
: self
._on
_get
_debug
_mode
_information
_action
,
183 self
.on
[rel_name
] for rel_name
in ["mongodb", "prometheus", "keystone"]
185 event_handler_mapping
[relation
.relation_changed
] = self
._on
_config
_changed
186 event_handler_mapping
[
187 relation
.relation_broken
188 ] = self
._on
_required
_relation
_broken
190 for event
, handler
in event_handler_mapping
.items():
191 self
.framework
.observe(event
, handler
)
193 def _validate_config(self
) -> None:
194 """Validate charm configuration.
197 CharmError: if charm configuration is invalid.
199 logger
.debug("validating charm config")
201 def _check_relations(self
) -> None:
202 """Validate charm relations.
205 CharmError: if charm configuration is invalid.
207 logger
.debug("check for missing relations")
208 missing_relations
= []
210 if not self
.kafka
.host
or not self
.kafka
.port
:
211 missing_relations
.append("kafka")
212 if self
.mongodb_client
.is_missing_data_in_unit():
213 missing_relations
.append("mongodb")
214 if self
.prometheus_client
.is_missing_data_in_app():
215 missing_relations
.append("prometheus")
216 if self
.keystone_client
.is_missing_data_in_app():
217 missing_relations
.append("keystone")
219 if missing_relations
:
220 relations_str
= ", ".join(missing_relations
)
221 one_relation_missing
= len(missing_relations
) == 1
223 f
'need {relations_str} relation{"" if one_relation_missing else "s"}'
225 logger
.warning(error_msg
)
226 raise CharmError(error_msg
)
228 def _configure_service(self
, container
: Container
) -> None:
229 """Add Pebble layer with the mon service."""
230 logger
.debug(f
"configuring {self.app.name} service")
231 container
.add_layer("mon", self
._get
_layer
(), combine
=True)
234 def _get_layer(self
) -> Dict
[str, Any
]:
235 """Get layer for Pebble."""
237 # General configuration
238 "OSMMON_GLOBAL_LOGLEVEL": self
.config
["log-level"],
239 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": self
.config
[
240 "openstack-default-granularity"
242 "OSMMON_GLOBAL_REQUEST_TIMEOUT": self
.config
["global-request-timeout"],
243 "OSMMON_COLLECTOR_INTERVAL": self
.config
["collector-interval"],
244 "OSMMON_EVALUATOR_INTERVAL": self
.config
["evaluator-interval"],
245 "OSMMON_COLLECTOR_VM_INFRA_METRICS": self
.config
["vm-infra-metrics"],
246 # Kafka configuration
247 "OSMMON_MESSAGE_DRIVER": "kafka",
248 "OSMMON_MESSAGE_HOST": self
.kafka
.host
,
249 "OSMMON_MESSAGE_PORT": self
.kafka
.port
,
250 # Database configuration
251 "OSMMON_DATABASE_DRIVER": "mongo",
252 "OSMMON_DATABASE_URI": self
.mongodb_client
.connection_string
,
253 "OSMMON_DATABASE_COMMONKEY": self
.config
["database-commonkey"],
254 # Prometheus/grafana configuration
255 "OSMMON_PROMETHEUS_URL": f
"http://{self.prometheus_client.hostname}:{self.prometheus_client.port}",
256 "OSMMON_PROMETHEUS_USER": self
.prometheus_client
.user
,
257 "OSMMON_PROMETHEUS_PASSWORD": self
.prometheus_client
.password
,
258 "OSMMON_GRAFANA_URL": self
.config
["grafana-url"],
259 "OSMMON_GRAFANA_USER": self
.config
["grafana-user"],
260 "OSMMON_GRAFANA_PASSWORD": self
.config
["grafana-password"],
261 "OSMMON_KEYSTONE_ENABLED": self
.config
["keystone-enabled"],
262 "OSMMON_KEYSTONE_URL": self
.keystone_client
.host
,
263 "OSMMON_KEYSTONE_DOMAIN_NAME": self
.keystone_client
.user_domain_name
,
264 "OSMMON_KEYSTONE_SERVICE_PROJECT": self
.keystone_client
.service
,
265 "OSMMON_KEYSTONE_SERVICE_USER": self
.keystone_client
.username
,
266 "OSMMON_KEYSTONE_SERVICE_PASSWORD": self
.keystone_client
.password
,
267 "OSMMON_KEYSTONE_SERVICE_PROJECT_DOMAIN_NAME": self
.keystone_client
.project_domain_name
,
269 logger
.info(f
"{environment}")
271 environment
["OSMMON_VCA_HOST"] = self
.vca
.data
.endpoints
272 environment
["OSMMON_VCA_SECRET"] = self
.vca
.data
.secret
273 environment
["OSMMON_VCA_USER"] = self
.vca
.data
.user
274 environment
["OSMMON_VCA_CACERT"] = self
.vca
.data
.cacert
276 "summary": "mon layer",
277 "description": "pebble config layer for mon",
280 "override": "replace",
281 "summary": "mon service",
282 "command": "/bin/bash scripts/start.sh",
283 "startup": "enabled",
286 "environment": environment
,
291 def _patch_k8s_service(self
) -> None:
292 port
= ServicePort(SERVICE_PORT
, name
=f
"{self.app.name}")
293 self
.service_patcher
= KubernetesServicePatch(self
, [port
])
296 if __name__
== "__main__": # pragma: no cover