Skip to content

Commit

Permalink
Update charm libraries (#76)
Browse files Browse the repository at this point in the history
* chore: update charm libraries

* Fix unit tests

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Arturo Seijas <[email protected]>
  • Loading branch information
github-actions[bot] and arturo-seijas authored May 24, 2024
1 parent dd68a59 commit af33046
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
14 changes: 10 additions & 4 deletions lib/charms/loki_k8s/v0/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ def _alert_rules_error(self, event):

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

PYDEPS = ["cosl"]

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -2119,9 +2121,13 @@ def _download_and_push_promtail_to_workload(self, promtail_info: dict) -> None:
# If no Juju proxy variable was set, we set proxies to None to let the ProxyHandler get
# the proxy env variables from the environment
proxies = {
"https_proxy": os.environ.get("JUJU_CHARM_HTTPS_PROXY", ""),
"http_proxy": os.environ.get("JUJU_CHARM_HTTP_PROXY", ""),
"no_proxy": os.environ.get("JUJU_CHARM_NO_PROXY", ""),
# The ProxyHandler uses only the protocol names as keys
# https://docs.python.org/3/library/urllib.request.html#urllib.request.ProxyHandler
"https": os.environ.get("JUJU_CHARM_HTTPS_PROXY", ""),
"http": os.environ.get("JUJU_CHARM_HTTP_PROXY", ""),
# The ProxyHandler uses `no` for the no_proxy key
# https://github.com/python/cpython/blob/3.12/Lib/urllib/request.py#L2553
"no": os.environ.get("JUJU_CHARM_NO_PROXY", ""),
}
proxies = {k: v for k, v in proxies.items() if v != ""} or None

Expand Down
35 changes: 18 additions & 17 deletions lib/charms/nginx_ingress_integrator/v0/nginx_route.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Canonical Ltd.
# Copyright 2024 Canonical Ltd.
# Licensed under the Apache2.0. See LICENSE file in charm source for details.
"""Library for the nginx-route relation.
Expand Down Expand Up @@ -86,7 +86,7 @@

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

__all__ = ["require_nginx_route", "provide_nginx_route"]

Expand All @@ -100,13 +100,6 @@ class _NginxRouteAvailableEvent(ops.framework.EventBase):
"""


class _NginxRouteProxyAvailableEvent(ops.framework.EventBase):
"""NginxRouteProxyAvailableEvent custom event.
This event indicates the NginxRouteProxy provider is available.
"""


class _NginxRouteBrokenEvent(ops.charm.RelationBrokenEvent):
"""NginxRouteBrokenEvent custom event.
Expand All @@ -122,12 +115,11 @@ class _NginxRouteCharmEvents(ops.charm.CharmEvents):
nginx_route_broken: Event to indicate that Nginx route relation is broken.
"""

nginx_route_proxy_available = ops.framework.EventSource(_NginxRouteProxyAvailableEvent)
nginx_route_available = ops.framework.EventSource(_NginxRouteAvailableEvent)
nginx_route_broken = ops.framework.EventSource(_NginxRouteBrokenEvent)


class _NginxRouteRequirer(ops.framework.Object):
class NginxRouteRequirer(ops.framework.Object):
"""This class defines the functionality for the 'requires' side of the 'nginx-route' relation.
Hook events observed:
Expand Down Expand Up @@ -156,7 +148,7 @@ def __init__(
self._config_reconciliation,
)
# Set default values.
self._config: typing.Dict[str, typing.Union[str, int, bool]] = {
self.config: typing.Dict[str, typing.Union[str, int, bool]] = {
"service-namespace": self._charm.model.name,
**config,
}
Expand All @@ -171,11 +163,11 @@ def _config_reconciliation(self, _event: typing.Any = None) -> None:
delete_keys = {
relation_field
for relation_field in relation_app_data
if relation_field not in self._config
if relation_field not in self.config
}
for delete_key in delete_keys:
del relation_app_data[delete_key]
relation_app_data.update({k: str(v) for k, v in self._config.items()})
relation_app_data.update({k: str(v) for k, v in self.config.items()})


# C901 is ignored since the method has too many ifs but wouldn't be
Expand All @@ -189,6 +181,7 @@ def require_nginx_route( # pylint: disable=too-many-locals,too-many-branches,to
service_port: int,
additional_hostnames: typing.Optional[str] = None,
backend_protocol: typing.Optional[str] = None,
enable_access_log: typing.Optional[bool] = None,
limit_rps: typing.Optional[int] = None,
limit_whitelist: typing.Optional[str] = None,
max_body_size: typing.Optional[int] = None,
Expand All @@ -202,7 +195,7 @@ def require_nginx_route( # pylint: disable=too-many-locals,too-many-branches,to
session_cookie_max_age: typing.Optional[int] = None,
tls_secret_name: typing.Optional[str] = None,
nginx_route_relation_name: str = "nginx-route",
) -> None:
) -> NginxRouteRequirer:
"""Set up nginx-route relation handlers on the requirer side.
This function must be invoked in the charm class constructor.
Expand All @@ -219,6 +212,8 @@ def require_nginx_route( # pylint: disable=too-many-locals,too-many-branches,to
additional-hostnames option via relation, optional.
backend_protocol: configure Nginx ingress integrator
backend-protocol option via relation, optional.
enable_access_log: configure Nginx ingress
nginx.ingress.kubernetes.io/enable-access-log option.
limit_rps: configure Nginx ingress integrator limit-rps
option via relation, optional.
limit_whitelist: configure Nginx ingress integrator
Expand Down Expand Up @@ -247,6 +242,9 @@ def require_nginx_route( # pylint: disable=too-many-locals,too-many-branches,to
nginx_route_relation_name: Specifies the relation name of
the relation handled by this requirer class. The relation
must have the nginx-route interface.
Returns:
the NginxRouteRequirer.
"""
config: typing.Dict[str, typing.Union[str, int, bool]] = {}
if service_hostname is not None:
Expand All @@ -259,6 +257,8 @@ def require_nginx_route( # pylint: disable=too-many-locals,too-many-branches,to
config["additional-hostnames"] = additional_hostnames
if backend_protocol is not None:
config["backend-protocol"] = backend_protocol
if enable_access_log is not None:
config["enable-access-log"] = "true" if enable_access_log else "false"
if limit_rps is not None:
config["limit-rps"] = limit_rps
if limit_whitelist is not None:
Expand All @@ -284,7 +284,7 @@ def require_nginx_route( # pylint: disable=too-many-locals,too-many-branches,to
if tls_secret_name is not None:
config["tls-secret-name"] = tls_secret_name

_NginxRouteRequirer(
return NginxRouteRequirer(
charm=charm, config=config, nginx_route_relation_name=nginx_route_relation_name
)

Expand Down Expand Up @@ -412,7 +412,8 @@ def provide_nginx_route(
RuntimeError: If provide_nginx_route was invoked twice with
the same nginx-route relation name
"""
if __provider_references.get(charm, {}).get(nginx_route_relation_name) is not None:
ref_dict: typing.Dict[str, typing.Any] = __provider_references.get(charm, {})
if ref_dict.get(nginx_route_relation_name) is not None:
raise RuntimeError(
"provide_nginx_route was invoked twice with the same nginx-route relation name"
)
Expand Down
21 changes: 10 additions & 11 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def __init__(self, *args):
- `scrape_timeout`
- `proxy_url`
- `relabel_configs`
- `metrics_relabel_configs`
- `metric_relabel_configs`
- `sample_limit`
- `label_limit`
- `label_name_length_limit`
Expand Down Expand Up @@ -362,7 +362,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 = 44
LIBPATCH = 47

PYDEPS = ["cosl"]

Expand All @@ -377,7 +377,7 @@ def _on_scrape_targets_changed(self, event):
"scrape_timeout",
"proxy_url",
"relabel_configs",
"metrics_relabel_configs",
"metric_relabel_configs",
"sample_limit",
"label_limit",
"label_name_length_limit",
Expand Down Expand Up @@ -521,8 +521,8 @@ def expand_wildcard_targets_into_individual_jobs(
# for such a target. Therefore labeling with Juju topology, excluding the
# unit name.
non_wildcard_static_config["labels"] = {
**non_wildcard_static_config.get("labels", {}),
**topology.label_matcher_dict,
**non_wildcard_static_config.get("labels", {}),
}

non_wildcard_static_configs.append(non_wildcard_static_config)
Expand All @@ -547,9 +547,9 @@ def expand_wildcard_targets_into_individual_jobs(
if topology:
# Add topology labels
modified_static_config["labels"] = {
**modified_static_config.get("labels", {}),
**topology.label_matcher_dict,
**{"juju_unit": unit_name},
**modified_static_config.get("labels", {}),
}

# Instance relabeling for topology should be last in order.
Expand Down Expand Up @@ -1537,12 +1537,11 @@ def set_scrape_job_spec(self, _=None):
relation.data[self._charm.app]["scrape_metadata"] = json.dumps(self._scrape_metadata)
relation.data[self._charm.app]["scrape_jobs"] = json.dumps(self._scrape_jobs)

if alert_rules_as_dict:
# Update relation data with the string representation of the rule file.
# Juju topology is already included in the "scrape_metadata" field above.
# The consumer side of the relation uses this information to name the rules file
# that is written to the filesystem.
relation.data[self._charm.app]["alert_rules"] = json.dumps(alert_rules_as_dict)
# Update relation data with the string representation of the rule file.
# Juju topology is already included in the "scrape_metadata" field above.
# The consumer side of the relation uses this information to name the rules file
# that is written to the filesystem.
relation.data[self._charm.app]["alert_rules"] = json.dumps(alert_rules_as_dict)

def _set_unit_ip(self, _=None):
"""Set unit host address.
Expand Down
2 changes: 1 addition & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self, *args):
on_nginx_route_broken=self._on_config_changed,
nginx_route_relation_name="nginx-proxy",
)
self.framework.observe(self.on.nginx_route_proxy_available, self._on_config_changed)
self.framework.observe(self.on.nginx_route_available, self._on_config_changed)

def _on_content_cache_pebble_ready(self, event) -> None:
"""Handle content_cache_pebble_ready event and configure workload container.
Expand Down

0 comments on commit af33046

Please sign in to comment.