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
32 from typing
import Any
, Dict
34 from charms
.kafka_k8s
.v0
.kafka
import KafkaEvents
, KafkaRequires
35 from charms
.observability_libs
.v1
.kubernetes_service_patch
import KubernetesServicePatch
36 from charms
.osm_libs
.v0
.utils
import (
40 check_container_ready
,
43 from charms
.osm_ro
.v0
.ro
import RoProvides
44 from lightkube
.models
.core_v1
import ServicePort
45 from ops
.charm
import ActionEvent
, CharmBase
, RelationJoinedEvent
46 from ops
.framework
import StoredState
47 from ops
.main
import main
48 from ops
.model
import ActiveStatus
, Container
50 from legacy_interfaces
import MongoClient
55 container_path
="/usr/lib/python3/dist-packages/osm_ro",
58 config
="common-hostpath",
59 container_path
="/usr/lib/python3/dist-packages/osm_common",
63 USER
= GROUP
= "appuser"
65 logger
= logging
.getLogger(__name__
)
68 def decode(content
: str):
69 """Base64 decoding of a string."""
70 return base64
.b64decode(content
.encode("utf-8")).decode("utf-8")
73 class OsmRoCharm(CharmBase
):
74 """OSM RO Kubernetes sidecar charm."""
78 _stored
= StoredState()
80 def __init__(self
, *args
):
81 super().__init
__(*args
)
82 self
._stored
.set_default(certificates
=set())
83 self
.kafka
= KafkaRequires(self
)
84 self
.mongodb_client
= MongoClient(self
, "mongodb")
85 self
._observe
_charm
_events
()
86 self
._patch
_k
8s
_service
()
87 self
.ro
= RoProvides(self
)
88 self
.container
: Container
= self
.unit
.get_container("ro")
89 self
.debug_mode
= DebugMode(self
, self
._stored
, self
.container
, HOSTPATHS
)
91 # ---------------------------------------------------------------------------
92 # Handlers for Charm Events
93 # ---------------------------------------------------------------------------
95 def _on_config_changed(self
, _
) -> None:
96 """Handler for the config-changed event."""
98 self
._validate
_config
()
99 self
._check
_relations
()
100 # Check if the container is ready.
101 # Eventually it will become ready after the first pebble-ready event.
102 check_container_ready(self
.container
)
104 self
._configure
_certificates
()
105 if not self
.debug_mode
.started
:
106 self
._configure
_service
()
107 self
._update
_ro
_relation
()
109 # Update charm status
110 self
._on
_update
_status
()
111 except CharmError
as e
:
112 logger
.debug(e
.message
)
113 self
.unit
.status
= e
.status
115 def _on_update_status(self
, _
=None) -> None:
116 """Handler for the update-status event."""
118 self
._validate
_config
()
119 self
._check
_relations
()
120 check_container_ready(self
.container
)
121 if self
.debug_mode
.started
:
123 check_service_active(self
.container
, self
.service_name
)
124 self
.unit
.status
= ActiveStatus()
125 except CharmError
as e
:
126 logger
.debug(e
.message
)
127 self
.unit
.status
= e
.status
129 def _on_required_relation_broken(self
, _
) -> None:
130 """Handler for the kafka-broken event."""
132 check_container_ready(self
.container
)
133 check_service_active(self
.container
, "ro")
134 self
.container
.stop("ro")
138 self
._on
_update
_status
()
140 def _update_ro_relation(self
, event
: RelationJoinedEvent
= None) -> None:
141 """Handler for the ro-relation-joined event."""
143 if self
.unit
.is_leader():
144 check_container_ready(self
.container
)
145 check_service_active(self
.container
, "ro")
146 self
.ro
.set_host_info(
147 self
.app
.name
, SERVICE_PORT
, event
.relation
if event
else None
149 except CharmError
as e
:
150 self
.unit
.status
= e
.status
152 def _on_get_debug_mode_information_action(self
, event
: ActionEvent
) -> None:
153 """Handler for the get-debug-mode-information action event."""
154 if not self
.debug_mode
.started
:
156 f
"debug-mode has not started. Hint: juju config {self.app.name} debug-mode=true"
160 debug_info
= {"command": self
.debug_mode
.command
, "password": self
.debug_mode
.password
}
161 event
.set_results(debug_info
)
163 # ---------------------------------------------------------------------------
164 # Validation and configuration and more
165 # ---------------------------------------------------------------------------
167 def _patch_k8s_service(self
) -> None:
168 port
= ServicePort(SERVICE_PORT
, name
=f
"{self.app.name}")
169 self
.service_patcher
= KubernetesServicePatch(self
, [port
])
171 def _observe_charm_events(self
) -> None:
172 event_handler_mapping
= {
173 # Core lifecycle events
174 self
.on
.ro_pebble_ready
: self
._on
_config
_changed
,
175 self
.on
.config_changed
: self
._on
_config
_changed
,
176 self
.on
.update_status
: self
._on
_update
_status
,
178 self
.on
.kafka_available
: self
._on
_config
_changed
,
179 self
.on
["kafka"].relation_broken
: self
._on
_required
_relation
_broken
,
180 self
.on
["mongodb"].relation_changed
: self
._on
_config
_changed
,
181 self
.on
["mongodb"].relation_broken
: self
._on
_required
_relation
_broken
,
182 self
.on
.ro_relation_joined
: self
._update
_ro
_relation
,
184 self
.on
.get_debug_mode_information_action
: self
._on
_get
_debug
_mode
_information
_action
,
187 for event
, handler
in event_handler_mapping
.items():
188 self
.framework
.observe(event
, handler
)
190 def _validate_config(self
) -> None:
191 """Validate charm configuration.
194 CharmError: if charm configuration is invalid.
196 logger
.debug("validating charm config")
197 if self
.config
["log-level"].upper() not in [
205 raise CharmError("invalid value for log-level option")
207 refresh_period
= self
.config
.get("period_refresh_active")
208 if refresh_period
and refresh_period
< 60 and refresh_period
!= -1:
210 "Refresh Period is too tight, insert >= 60 seconds or disable using -1"
213 def _check_relations(self
) -> None:
214 """Validate charm relations.
217 CharmError: if charm configuration is invalid.
219 logger
.debug("check for missing relations")
220 missing_relations
= []
222 if not self
.kafka
.host
or not self
.kafka
.port
:
223 missing_relations
.append("kafka")
224 if self
.mongodb_client
.is_missing_data_in_unit():
225 missing_relations
.append("mongodb")
227 if missing_relations
:
228 relations_str
= ", ".join(missing_relations
)
229 one_relation_missing
= len(missing_relations
) == 1
230 error_msg
= f
'need {relations_str} relation{"" if one_relation_missing else "s"}'
231 logger
.warning(error_msg
)
232 raise CharmError(error_msg
)
234 def _configure_certificates(self
) -> None:
235 """Push certificates to the RO container."""
236 if not (certificate_config
:= self
.config
.get("certificates")):
239 certificates_list
= certificate_config
.split(",")
240 updated_certificates
= set()
242 for certificate
in certificates_list
:
243 if ":" not in certificate
:
245 name
, content
= certificate
.split(":")
246 content
= decode(content
)
255 updated_certificates
.add(name
)
256 self
._stored
.certificates
.add(name
)
257 logger
.info(f
"certificate {name} pushed successfully")
259 stored_certificates
= {c
for c
in self
._stored
.certificates
}
260 for certificate_to_remove
in stored_certificates
.difference(updated_certificates
):
261 self
.container
.remove_path(f
"/certs/{certificate_to_remove}")
262 self
._stored
.certificates
.remove(certificate_to_remove
)
263 logger
.info(f
"certificate {certificate_to_remove} removed successfully")
265 def _configure_service(self
) -> None:
266 """Add Pebble layer with the ro service."""
267 logger
.debug(f
"configuring {self.app.name} service")
268 self
.container
.add_layer("ro", self
._get
_layer
(), combine
=True)
269 self
.container
.replan()
271 def _get_layer(self
) -> Dict
[str, Any
]:
272 """Get layer for Pebble."""
274 "summary": "ro layer",
275 "description": "pebble config layer for ro",
278 "override": "replace",
279 "summary": "ro service",
280 "command": "python3 -u -m osm_ng_ro.ro_main",
281 "startup": "enabled",
285 # General configuration
286 "OSMRO_LOG_LEVEL": self
.config
["log-level"].upper(),
287 # Kafka configuration
288 "OSMRO_MESSAGE_HOST": self
.kafka
.host
,
289 "OSMRO_MESSAGE_PORT": self
.kafka
.port
,
290 "OSMRO_MESSAGE_DRIVER": "kafka",
291 # Database configuration
292 "OSMRO_DATABASE_DRIVER": "mongo",
293 "OSMRO_DATABASE_URI": self
.mongodb_client
.connection_string
,
294 "OSMRO_DATABASE_COMMONKEY": self
.config
["database-commonkey"],
295 # Storage configuration
296 "OSMRO_STORAGE_DRIVER": "mongo",
297 "OSMRO_STORAGE_PATH": "/app/storage",
298 "OSMRO_STORAGE_COLLECTION": "files",
299 "OSMRO_STORAGE_URI": self
.mongodb_client
.connection_string
,
300 "OSMRO_PERIOD_REFRESH_ACTIVE": self
.config
.get("period_refresh_active")
308 if __name__
== "__main__": # pragma: no cover