Skip to content

Commit

Permalink
update charm libraries (#108)
Browse files Browse the repository at this point in the history
* update charm libraries

* update karma_dashboard library

* ignore unused ignores
  • Loading branch information
lucabello authored Dec 9, 2022
1 parent 7d52879 commit f084203
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 60 deletions.
4 changes: 2 additions & 2 deletions lib/charms/catalogue_k8s/v0/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

LIBID = "fa28b361293b46668bcd1f209ada6983"
LIBAPI = 0
LIBPATCH = 2
LIBPATCH = 3

DEFAULT_RELATION_NAME = "catalogue"

Expand All @@ -37,7 +37,7 @@ def __init__(
self,
charm,
relation_name: str = DEFAULT_RELATION_NAME,
item: CatalogueItem = None,
item: Optional[CatalogueItem] = None,
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
):
super().__init__(charm, relation_name)
Expand Down
75 changes: 57 additions & 18 deletions lib/charms/grafana_k8s/v0/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 17
LIBPATCH = 19

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -622,23 +622,53 @@ def _replace_template_fields( # noqa: C901
#
# COS only knows about Prometheus and Loki.
for panel in panels:
if "datasource" not in panel or not panel.get("datasource", ""):
if "datasource" not in panel or not panel.get("datasource"):
continue
if not existing_templates:
if "loki" in panel.get("datasource"):
panel["datasource"] = "${lokids}"
datasource = panel.get("datasource")
if type(datasource) == str:
if "loki" in datasource:
panel["datasource"] = "${lokids}"
else:
panel["datasource"] = "${prometheusds}"
elif type(datasource) == dict:
# In dashboards exported by Grafana 9, datasource type is dict
dstype = datasource.get("type", "")
if dstype == "loki":
panel["datasource"]["uid"] = "${lokids}"
elif dstype == "prometheus":
panel["datasource"]["uid"] = "${prometheusds}"
else:
logger.debug("Unrecognized datasource type '%s'; skipping", dstype)
continue
else:
panel["datasource"] = "${prometheusds}"
logger.error("Unknown datasource format: skipping")
continue
else:
if panel["datasource"].lower() in replacements.values():
# Already a known template variable
if type(panel["datasource"]) == str:
if panel["datasource"].lower() in replacements.values():
# Already a known template variable
continue
# Strip out variable characters and maybe braces
ds = re.sub(r"(\$|\{|\})", "", panel["datasource"])
replacement = replacements.get(datasources[ds], "")
if replacement:
used_replacements.append(ds)
panel["datasource"] = replacement or panel["datasource"]
elif type(panel["datasource"]) == dict:
dstype = panel["datasource"].get("type", "")
if panel["datasource"].get("uid", "").lower() in replacements.values():
# Already a known template variable
continue
# Strip out variable characters and maybe braces
ds = re.sub(r"(\$|\{|\})", "", panel["datasource"].get("uid", ""))
replacement = replacements.get(datasources[ds], "")
if replacement:
used_replacements.append(ds)
panel["datasource"]["uid"] = replacement
else:
logger.error("Unknown datasource format: skipping")
continue
# Strip out variable characters and maybe braces
ds = re.sub(r"(\$|\{|\})", "", panel["datasource"])
replacement = replacements.get(datasources[ds], "")
if replacement:
used_replacements.append(ds)
panel["datasource"] = replacement or panel["datasource"]

# Put our substitutions back
dict_content["panels"] = panels
Expand Down Expand Up @@ -758,13 +788,22 @@ def _modify_panel(panel: dict, topology: dict, transformer: "CosTool") -> dict:
# If there's no expression, we don't need to do anything
if "expr" not in target.keys():
continue
expr = target["expr"]

if "datasource" not in panel.keys():
continue
elif panel["datasource"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]]
expr = target["expr"]
else:
if type(panel["datasource"]) == str:
if panel["datasource"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]]
elif type(panel["datasource"]) == dict:
if panel["datasource"]["uid"] not in known_datasources:
continue
querytype = known_datasources[panel["datasource"]["uid"]]
else:
logger.error("Unknown datasource format: skipping")
continue

# Capture all values inside `[]` into a list which we'll iterate over later to
# put them back in-order. Then apply the regex again and replace everything with
Expand Down Expand Up @@ -1155,7 +1194,7 @@ def _content_to_dashboard_object(self, content: str, inject_dropdowns: bool = Tr
return {
"charm": self._charm.meta.name,
"content": content,
"juju_topology": self._juju_topology,
"juju_topology": self._juju_topology if inject_dropdowns else {},
"inject_dropdowns": inject_dropdowns,
}

Expand Down
16 changes: 8 additions & 8 deletions lib/charms/karma_k8s/v0/karma_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 3
LIBPATCH = 4

# Set to match metadata.yaml
INTERFACE_NAME = "karma_dashboard"
Expand Down Expand Up @@ -328,7 +328,7 @@ def __init__(self, charm, relation_name: str = "dashboard"):
# It is needed here because the target URL may be set by the consumer before any
# "karma-dashboard" relation is joined, in which case there are no relation unit data bags
# available for storing the target URL.
self._stored.set_default(config={})
self._stored.set_default(config=dict())

events = self.charm.on[self.name]
self.framework.observe(events.relation_joined, self._on_relation_joined)
Expand All @@ -344,12 +344,12 @@ def config_valid(self) -> bool:
True if the currently stored configuration for an alertmanager target is valid; False
otherwise.
"""
return KarmaAlertmanagerConfig.is_valid(self._stored.config)
return KarmaAlertmanagerConfig.is_valid(self._stored.config) # type: ignore

@property
def target(self) -> Optional[str]:
"""str: Alertmanager URL to be used by Karma."""
return self._stored.config.get("uri", None)
return self._stored.config.get("uri", None) # type: ignore

@target.setter
def target(self, url: str) -> None:
Expand All @@ -370,12 +370,12 @@ def target(self, url: str) -> None:
logger.warning("Invalid config: {%s, %s}", name, url)
return

self._stored.config.update(config)
self._stored.config.update(config) # type: ignore

# target changed - must update all relation data
self._update_relation_data()

def _update_relation_data(self, event: RelationJoinedEvent = None):
def _update_relation_data(self, event: Optional[RelationJoinedEvent] = None):
"""Helper function for updating relation data bags.
This function can be used in two different ways:
Expand All @@ -391,7 +391,7 @@ def _update_relation_data(self, event: RelationJoinedEvent = None):
# a single consumer charm's unit may be related to multiple karma dashboards
if self.name in self.charm.model.relations:
for relation in self.charm.model.relations[self.name]:
relation.data[self.charm.unit].update(self._stored.config)
relation.data[self.charm.unit].update(self._stored.config) # type: ignore
else:
# update relation data only for the newly joined relation
event.relation.data[self.charm.unit].update(self._stored.config)
event.relation.data[self.charm.unit].update(self._stored.config) # type: ignore
11 changes: 7 additions & 4 deletions lib/charms/observability_libs/v0/juju_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
LIBID = "bced1658f20f49d28b88f61f83c2d232"

LIBAPI = 0
LIBPATCH = 3
LIBPATCH = 4


class InvalidUUIDError(Exception):
Expand All @@ -94,8 +94,8 @@ def __init__(
model: str,
model_uuid: str,
application: str,
unit: str = None,
charm_name: str = None,
unit: Optional[str] = None,
charm_name: Optional[str] = None,
):
"""Build a JujuTopology object.
Expand Down Expand Up @@ -181,7 +181,10 @@ def from_dict(cls, data: dict):
)

def as_dict(
self, *, remapped_keys: Dict[str, str] = None, excluded_keys: List[str] = None
self,
*,
remapped_keys: Optional[Dict[str, str]] = None,
excluded_keys: Optional[List[str]] = None,
) -> OrderedDict:
"""Format the topology information into an ordered dict.
Expand Down
6 changes: 3 additions & 3 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _on_scrape_targets_changed(self, event):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 25
LIBPATCH = 26

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -453,7 +453,7 @@ def prefix_job_names(scrape_configs: List[dict], prefix: str) -> List[dict]:
def expand_wildcard_targets_into_individual_jobs(
scrape_jobs: List[dict],
hosts: Dict[str, Tuple[str, str]],
topology: JujuTopology = None,
topology: Optional[JujuTopology] = None,
) -> List[dict]:
"""Extract wildcard hosts from the given scrape_configs list into separate jobs.
Expand Down Expand Up @@ -1384,7 +1384,7 @@ def __init__(
alert_rules_path: str = DEFAULT_ALERT_RULES_RELATIVE_PATH,
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
external_url: str = "",
lookaside_jobs_callable: Callable = None,
lookaside_jobs_callable: Optional[Callable] = None,
):
"""Construct a metrics provider for a Prometheus charm.
Expand Down
Loading

0 comments on commit f084203

Please sign in to comment.