|
| 1 | +# Copyright 2025 Canonical Ltd. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""FIXME docstring.""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from typing import Any |
| 19 | + |
| 20 | +import ops |
| 21 | + |
| 22 | +from .vendor.charms.certificate_transfer_interface.v1.certificate_transfer import ( |
| 23 | + CertificateTransferRequires, |
| 24 | +) |
| 25 | +from .vendor.charms.tempo_coordinator_k8s.v0.tracing import TracingEndpointRequirer |
| 26 | + |
| 27 | +# NOTE: the shape of certificate_transfer is `{"certificates": list[str]}` |
| 28 | +# it is trivial to read the certificates out manually |
| 29 | + |
| 30 | + |
| 31 | +class Tracing(ops.Object): |
| 32 | + """FIXME docstring.""" |
| 33 | + |
| 34 | + _certificate_transfer: CertificateTransferRequires | None |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + charm: ops.CharmBase, |
| 39 | + tracing_relation_name: str, |
| 40 | + ca_relation_name: str | None = None, |
| 41 | + ca_data: str | None = None, |
| 42 | + ): |
| 43 | + """FIXME docstring. |
| 44 | +
|
| 45 | + Include `ops[tracing]` in your dependencies. |
| 46 | +
|
| 47 | + Declare the relations that the charm supports: |
| 48 | +
|
| 49 | + ```yaml |
| 50 | + requires: |
| 51 | + charm-tracing: |
| 52 | + interface: tracing |
| 53 | + limit: 1 |
| 54 | + send-ca-cert: |
| 55 | + interface: certificate_transfer |
| 56 | + limit: 1 |
| 57 | + ``` |
| 58 | +
|
| 59 | + Initialise the tracing thing (FIXME) with the names of these relations. |
| 60 | +
|
| 61 | + ```py |
| 62 | + import ops.tracing |
| 63 | +
|
| 64 | + class SomeCharm(ops.CharmBase): |
| 65 | + def __init__(self, framework: ops.Framework): |
| 66 | + ... |
| 67 | + self._tracing = ops.tracing.Tracing( |
| 68 | + tracing_relation_name="charm-tracing", |
| 69 | + ca_relation_name="send-ca-cart", |
| 70 | + ) |
| 71 | + ``` |
| 72 | + """ |
| 73 | + super().__init__(charm, f'{tracing_relation_name}+{ca_relation_name}') |
| 74 | + self.charm = charm |
| 75 | + self.tracing_relation_name = tracing_relation_name |
| 76 | + self.ca_relation_name = ca_relation_name |
| 77 | + self.ca_data = ca_data |
| 78 | + |
| 79 | + # FIXME: Pietro recommends inspecting charm meta to validate the interface name |
| 80 | + assert self.charm.meta.relations[tracing_relation_name].interface_name == 'tracing' |
| 81 | + self._tracing = TracingEndpointRequirer( |
| 82 | + self.charm, |
| 83 | + tracing_relation_name, |
| 84 | + protocols=['otlp_http'], |
| 85 | + ) |
| 86 | + |
| 87 | + # TODO: COS requirer |
| 88 | + |
| 89 | + for event in ( |
| 90 | + # FIXME: validate this |
| 91 | + # - the start event may happen after relation-joined? |
| 92 | + # - the k8s container died and got restarted |
| 93 | + # - the vm [smth] crashed and got restarted |
| 94 | + self.charm.on.start, |
| 95 | + # In case the previous charm version has a relation but didn't save the URL |
| 96 | + self.charm.on.upgrade_charm, |
| 97 | + self._tracing.on.endpoint_changed, |
| 98 | + self._tracing.on.endpoint_removed, |
| 99 | + ): |
| 100 | + self.framework.observe(event, self._reconcile) |
| 101 | + |
| 102 | + self._certificate_transfer = None |
| 103 | + if ca_relation_name: |
| 104 | + self._certificate_transfer = CertificateTransferRequires(charm, ca_relation_name) |
| 105 | + assert ( |
| 106 | + self.charm.meta.relations[ca_relation_name].interface_name |
| 107 | + == 'certificate_transfer' |
| 108 | + ) |
| 109 | + for event in ( |
| 110 | + self._certificate_transfer.on.certificate_set_updated, |
| 111 | + self._certificate_transfer.on.certificates_removed, |
| 112 | + ): |
| 113 | + self.framework.observe(event, self._reconcile) |
| 114 | + |
| 115 | + def _reconcile(self, _event: Any): |
| 116 | + url, ca = self._get_config() |
| 117 | + self.charm.set_tracing_destination(url=url, ca=ca) |
| 118 | + |
| 119 | + def _get_config(self) -> tuple[str | None, str | None]: |
| 120 | + if not self._tracing.is_ready(): |
| 121 | + return None, None |
| 122 | + |
| 123 | + url = self._tracing.get_endpoint('otlp_http') |
| 124 | + |
| 125 | + if not url or not url.startswith(('http://', 'https://')): |
| 126 | + return None, None |
| 127 | + |
| 128 | + if url.startswith('http://'): |
| 129 | + return url, None |
| 130 | + |
| 131 | + if not self._certificate_transfer: |
| 132 | + return url, self.ca_data |
| 133 | + |
| 134 | + ca_rel = self.model.get_relation(self.ca_relation_name) if self.ca_relation_name else None |
| 135 | + ca_rel_id = ca_rel.id if ca_rel else None |
| 136 | + |
| 137 | + if ca_rel and self._certificate_transfer.is_ready(ca_rel): |
| 138 | + return url, '\n'.join( |
| 139 | + sorted(self._certificate_transfer.get_all_certificates(ca_rel_id)) |
| 140 | + ) |
| 141 | + else: |
| 142 | + return None, None |
0 commit comments