Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Update charm libraries (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Github Actions <[email protected]>
  • Loading branch information
observability-noctua-bot and Github Actions authored Sep 20, 2023
1 parent d25e148 commit fd1d8c4
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/charms/traefik_k8s/v1/ingress_per_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _on_ingress_revoked(self, event: IngressPerUnitRevokedForUnitEvent):

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

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -114,6 +114,7 @@ def _on_ingress_revoked(self, event: IngressPerUnitRevokedForUnitEvent):
"mode": {"type": "string"},
"strip-prefix": {"type": "string"},
"redirect-https": {"type": "string"},
"scheme": {"type": "string"},
},
"required": ["model", "name", "host", "port"],
}
Expand Down Expand Up @@ -154,6 +155,7 @@ def _on_ingress_revoked(self, event: IngressPerUnitRevokedForUnitEvent):
"mode": Optional[Literal["tcp", "http"]],
"strip-prefix": Optional[bool],
"redirect-https": Optional[bool],
"scheme": Optional[Literal["http", "https"]],
},
total=False,
)
Expand Down Expand Up @@ -184,8 +186,8 @@ def _validate_data(data, schema):
if not DO_VALIDATION:
return
try:
jsonschema.validate(instance=data, schema=schema)
except jsonschema.ValidationError as e:
jsonschema.validate(instance=data, schema=schema) # pyright: ignore[reportUnboundVariable]
except jsonschema.ValidationError as e: # pyright: ignore[reportUnboundVariable]
raise DataValidationError(data, schema) from e


Expand Down Expand Up @@ -485,7 +487,16 @@ def _get_requirer_unit_data(self, relation: Relation, remote_unit: Unit) -> Requ

databag = relation.data[remote_unit]
remote_data: Dict[str, Union[int, str]] = {}
for k in ("port", "host", "model", "name", "mode", "strip-prefix", "redirect-https"):
for k in (
"port",
"host",
"model",
"name",
"mode",
"strip-prefix",
"redirect-https",
"scheme",
):
v = databag.get(k)
if v is not None:
remote_data[k] = v
Expand Down Expand Up @@ -663,6 +674,9 @@ def __init__(
listen_to: Literal["only-this-unit", "all-units", "both"] = "only-this-unit",
strip_prefix: bool = False,
redirect_https: bool = False,
# FIXME: now that `provide_ingress_requirements` takes a scheme, this arg can be changed to
# str type in v2.
scheme: typing.Callable[[], str] = lambda: "http",
):
"""Constructor for IngressPerUnitRequirer.
Expand Down Expand Up @@ -692,6 +706,7 @@ def __init__(
will be notified *twice* of changes to this unit's ingress!).
strip_prefix: remove prefixes from the URL path.
redirect_https: redirect incoming requests to HTTPS
scheme: callable returning the scheme to use when constructing the ingress url.
"""
super().__init__(charm, relation_name)
self._stored.set_default(current_urls=None) # type: ignore
Expand All @@ -703,6 +718,7 @@ def __init__(
self._mode = mode
self._strip_prefix = strip_prefix
self._redirect_https = redirect_https
self._get_scheme = scheme

self.listen_to = listen_to

Expand Down Expand Up @@ -771,25 +787,37 @@ def is_ready(self) -> bool:
return False
return bool(self.url)

def provide_ingress_requirements(self, *, host: Optional[str] = None, port: int):
def provide_ingress_requirements(
self, *, scheme: Optional[str] = None, host: Optional[str] = None, port: int
):
"""Publishes the data that Traefik needs to provide ingress.
Args:
scheme: Scheme to be used; if unspecified, use the one used by __init__.
host: Hostname to be used by the ingress provider to address the
requirer unit; if unspecified, FQDN will be used instead
port: the port of the service (required)
"""
assert self.relation, "no relation"
# This public method may be used at various points of the charm lifecycle, possibly when
# the ingress relation is not yet there.
# Abort if there is no relation (instead of requiring the caller to guard against it).
if not self.relation:
return

if not host:
host = socket.getfqdn()

if not scheme:
# If scheme was not provided, use the one given to the constructor.
scheme = self._get_scheme()

data = {
"model": self.model.name,
"name": self.unit.name,
"host": host,
"port": str(port),
"mode": self._mode,
"scheme": scheme,
}

if self._strip_prefix:
Expand Down

0 comments on commit fd1d8c4

Please sign in to comment.