Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 35 additions & 94 deletions charms/garm-configurator/lib/garm_configurator_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
The ``garm-configurator`` charm owns the relation contract and publishes one provider
configuration per application with ``ops.Relation.save``. The models live with the charm so
consumers can share the same definitions and use ``ops.Relation.load`` instead of hand-writing
databag keys. ``ProviderConfig`` models the complete published payload (scaleset fields, runner
options, and provider credentials), so the configurator writes it with a single
``ops.Relation.save`` call.
databag keys. The application databag holds two nested values: ``provider`` (the provider and
GitHub App credentials) and ``scaleset`` (the scaleset and runner fields).
``ConfiguratorApplicationData`` is the write model that nests both, so the configurator writes
the complete payload with a single ``ops.Relation.save`` call.

The payload lives on the application databag: the configurator's leader unit writes it once for
the whole application. The ``GarmConfiguratorRequirer`` aggregates every joined
Expand All @@ -21,7 +22,6 @@
"""

import dataclasses
import json
import logging
import typing

Expand All @@ -35,14 +35,13 @@

@pydantic.dataclasses.dataclass
class ProviderConfig:
"""Complete relation payload published by one ``garm-configurator`` application.
"""Provider and GitHub App credentials published by one ``garm-configurator`` application.

This is the write model for the relation: the configurator fills the scaleset and runner
fields plus the provider and GitHub App credentials and publishes it with one
``ops.Relation.save`` to the application databag. The field names are the relation databag
keys, so they are the wire contract between the configurator and GARM. The auth URL is
required: an application publishes the rest of the provider payload alongside it, so a
databag without it is incomplete and treated as invalid rather than as an empty provider.
This is the nested ``provider`` value of the application databag. The field names are the
keys inside that value, so they are the wire contract between the configurator and GARM.
The auth URL is required: an application publishes the rest of the provider payload
alongside it, so a databag without it is incomplete and treated as invalid rather than as an
empty provider.

Attributes:
openstack_auth_url: Keystone authentication URL.
Expand All @@ -57,27 +56,6 @@ class ProviderConfig:
github_app_id: GitHub App ID.
github_installation_id: GitHub App installation ID.
github_private_key_secret_uri: Juju secret URI holding the GitHub App private key.
name: The name of the scaleset.
provider_name: The GARM provider the scaleset runs on.
image_id: Image name or ID used for runners.
image_visibility: Glance visibility used for an image-name lookup.
security_group: Security group attached to runner instances.
flavor: Runner flavor.
os_arch: Runner CPU architecture.
min_idle_runner: Minimum number of idle runners.
max_runner: Maximum number of runners.
labels: Comma-separated runner labels.
runner_group: Runner group for org registration.
enable_shell: Whether GARM's remote shell is enabled, as a boolean string.
pre_install_scripts: JSON object of pre-install script name to script body.
org: Organization the runners register to.
repo: Repository the runners register to.
dockerhub_mirror: Docker registry mirror URL.
runner_http_proxy: Upstream HTTP proxy for runners.
aproxy_exclude_addresses: Comma-separated IPv4 addresses, CIDRs, or ranges to bypass.
aproxy_redirect_ports: Comma-separated ports or port ranges to forward.
otel_collector_endpoint: OTEL exporter endpoint.
pre_job_script: Bash script appended to the runner pre-job hook.
"""

openstack_auth_url: str
Expand All @@ -92,36 +70,15 @@ class ProviderConfig:
github_app_id: int | None = None
github_installation_id: int | None = None
github_private_key_secret_uri: str | None = None
name: str = ""
provider_name: str = ""
image_id: str = ""
image_visibility: str = ""
security_group: str = ""
flavor: str = ""
os_arch: str = ""
min_idle_runner: str = ""
max_runner: str = ""
labels: str = ""
runner_group: str = ""
enable_shell: str = ""
pre_install_scripts: str = ""
org: str = ""
repo: str = ""
dockerhub_mirror: str = ""
runner_http_proxy: str = ""
aproxy_exclude_addresses: str = ""
aproxy_redirect_ports: str = ""
otel_collector_endpoint: str = ""
pre_job_script: str = ""


@dataclasses.dataclass(frozen=True)
class ScalesetConfig:
"""Scaleset and runner fields published by one ``garm-configurator`` application.

The requirer derives this view from the ``ProviderConfig`` it loads from the application
databag, so consumers apply their own parsing and validation without re-reading the
databag. The fields keep the relation's string wire form; an empty string means "unset".
This is the nested ``scaleset`` value of the application databag. The requirer exposes it
as-is, so consumers apply their own parsing and validation without re-reading the databag.
The fields keep the relation's string wire form; an empty string means "unset".

These fields are published on the application databag by the configurator's leader unit, so
every unit of a deployment sees one consistent payload.
Expand Down Expand Up @@ -173,6 +130,23 @@ class ScalesetConfig:
pre_job_script: str = ""


@pydantic.dataclasses.dataclass
class ConfiguratorApplicationData:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the name to ProviderApplicatData

"""Complete application databag payload published by one ``garm-configurator`` application.

This is the write model for the relation: the configurator fills both nested values and
publishes them with one ``ops.Relation.save``, so the application databag holds a
``provider`` object and a ``scaleset`` object instead of a flat set of keys.

Attributes:
provider: The provider and GitHub App credentials.
scaleset: The scaleset and runner fields.
"""

provider: ProviderConfig
scaleset: ScalesetConfig


@dataclasses.dataclass(frozen=True)
class ConfiguratorRelation:
"""Relation data published by one ``garm-configurator`` application.
Expand All @@ -194,10 +168,10 @@ def provider_name(self) -> str:
"""Return the GARM provider name published by the configurator.

Returns:
The provider name the configurator wrote to its application databag, matching
The provider name the configurator wrote to the ``scaleset`` value, matching
GARM's provider registry and the name its scalesets reference.
"""
return self.provider.provider_name
return self.scaleset.provider_name


# A scaleset name identifies the scaleset; a provider can legitimately serve several scalesets,
Expand Down Expand Up @@ -300,9 +274,7 @@ def get_data(self) -> ConfiguratorData:
)
continue
try:
provider = relation.load(
ProviderConfig, application, decoder=_decode_relation_value
)
application_data = relation.load(ConfiguratorApplicationData, application)
except ValueError:
logger.warning(
"Ignoring incomplete garm-configurator relation data: relation_id=%d app=%s",
Expand All @@ -314,13 +286,8 @@ def get_data(self) -> ConfiguratorData:
configurator_relation = ConfiguratorRelation(
relation_id=relation.id,
application_name=application.name,
provider=provider,
scaleset=ScalesetConfig(
**{
field.name: getattr(provider, field.name)
for field in dataclasses.fields(ScalesetConfig)
}
),
provider=application_data.provider,
scaleset=application_data.scaleset,
)
aggregated.append(configurator_relation)
identity = tuple(
Expand Down Expand Up @@ -349,29 +316,3 @@ def get_data(self) -> ConfiguratorData:
relations=tuple(aggregated),
invalid_relation_ids=frozenset(invalid_relation_ids),
)


def _decode_relation_value(value: str) -> typing.Any:
"""Decode one relation databag value, tolerating data from an older configurator.

``ops.Relation.save`` JSON-encodes every value, but a configurator upgraded from a charm
that still wrote raw strings only republishes on its next reconcile. Decoding with a
fallback lets the consumer read both forms without treating the relation as invalid.

Only JSON strings and null are returned as decoded values; numeric JSON stays in its raw
string form so string fields (the scaleset fields keep their string wire form) are not
rejected and integer fields still receive a value pydantic can coerce.

Args:
value: The raw string stored in the relation databag.

Returns:
The decoded string or None, or the raw string for any other value.
"""
try:
decoded = json.loads(value)
except (json.JSONDecodeError, TypeError):
return value
if decoded is None or isinstance(decoded, str):
return decoded
return value
55 changes: 30 additions & 25 deletions charms/garm-configurator/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import typing

import ops
from garm_configurator_relation import ConfiguratorApplicationData
from garm_configurator_relation import ProviderConfig as RelationProviderConfig
from garm_configurator_relation import ScalesetConfig as RelationScalesetConfig

from charm_state import (
GARM_RELATION_NAME,
Expand Down Expand Up @@ -139,12 +141,13 @@ def _ensure_relation_secret(
def _configure_garm_relation(self, state: CharmState) -> None:
"""Publish the complete configurator payload to the garm-configurator relation.

Builds one ``RelationProviderConfig`` holding the scaleset fields, runner options,
image reference, and OpenStack/GitHub App credentials, then writes it to the
application databag with a single ``ops.Relation.save`` call. ``Relation.save``
persists every model field, so an unset optional field is published as an empty string
rather than omitted; consumers treat the empty string as "unset". The ``image_id`` field
carries either an OpenStack image name or ID for compatibility.
Builds a ``ConfiguratorApplicationData`` nesting the provider and GitHub App credentials
under ``provider`` and the scaleset fields, runner options, and image reference under
``scaleset``, then writes it to the application databag with a single
``ops.Relation.save`` call. ``Relation.save`` persists every model field, so an unset
optional field is published as an empty string rather than omitted; consumers treat the
empty string as "unset". The ``image_id`` field carries either an OpenStack image name or
ID for compatibility.

Also provisions Juju secrets for the OpenStack password and GitHub App private key and
writes their secret URIs alongside the credential fields. Secret provisioning is gated
Expand Down Expand Up @@ -175,7 +178,7 @@ def _configure_garm_relation(self, state: CharmState) -> None:

pre_install = state.scaleset_config.pre_install_scripts
rc = state.runner_config
basic_data: dict[str, str] = {
scaleset_data: dict[str, str] = {
"name": state.scaleset_config.name,
"provider_name": state.provider_config.provider_name,
"image_id": state.image or "",
Expand All @@ -199,28 +202,30 @@ def _configure_garm_relation(self, state: CharmState) -> None:
"pre_job_script": rc.pre_job_script or "",
}
if state.scaleset_config.org:
basic_data["org"] = state.scaleset_config.org
scaleset_data["org"] = state.scaleset_config.org
if state.scaleset_config.repo:
basic_data["repo"] = state.scaleset_config.repo
scaleset_data["repo"] = state.scaleset_config.repo

garm_relation.save(
RelationProviderConfig(
**basic_data,
openstack_auth_url=state.provider_config.auth_url,
openstack_username=state.provider_config.username,
openstack_password_secret_uri=(
str(password_secret.id) if password_secret is not None else None
),
openstack_project_name=state.provider_config.project_name,
openstack_user_domain_name=state.provider_config.user_domain_name,
openstack_project_domain_name=state.provider_config.project_domain_name,
openstack_region_name=state.provider_config.region_name,
openstack_network=state.provider_config.network,
github_app_id=state.github_app_config.app_id,
github_installation_id=state.github_app_config.installation_id,
github_private_key_secret_uri=(
str(github_key_secret.id) if github_key_secret is not None else None
ConfiguratorApplicationData(
provider=RelationProviderConfig(
openstack_auth_url=state.provider_config.auth_url,
openstack_username=state.provider_config.username,
openstack_password_secret_uri=(
str(password_secret.id) if password_secret is not None else None
),
openstack_project_name=state.provider_config.project_name,
openstack_user_domain_name=state.provider_config.user_domain_name,
openstack_project_domain_name=state.provider_config.project_domain_name,
openstack_region_name=state.provider_config.region_name,
openstack_network=state.provider_config.network,
github_app_id=state.github_app_config.app_id,
github_installation_id=state.github_app_config.installation_id,
github_private_key_secret_uri=(
str(github_key_secret.id) if github_key_secret is not None else None
),
),
scaleset=RelationScalesetConfig(**scaleset_data),
),
self.app,
)
Expand Down
32 changes: 23 additions & 9 deletions charms/garm-configurator/tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@ def _make_garm_configurator_relation():
return Relation(endpoint="garm-configurator")


def _relation_section(relation, section: str) -> dict:
"""Return a nested application databag value decoded from its JSON form."""
return json.loads(relation.local_app_data[section])


def _relation_value(relation, key: str):
"""Return an application databag value decoded from the JSON form ``Relation.save`` writes."""
value = relation.local_app_data.get(key)
return "" if value is None else json.loads(value)
"""Return a value from the nested ``provider`` or ``scaleset`` application databag value."""
for section in ("provider", "scaleset"):
data = _relation_section(relation, section)
if key in data:
return data[key]
raise KeyError(key)


def _valid_config(secret: Secret, private_key_secret: Secret) -> dict:
Expand Down Expand Up @@ -118,7 +126,7 @@ def test_configured_image_is_published_without_image_relation():

garm_out = out.get_relation(garm_relation.id)
assert out.unit_status == ops.ActiveStatus("Ready")
assert "image" not in garm_out.local_app_data
assert "image" not in _relation_section(garm_out, "scaleset")
assert _relation_value(garm_out, "image_id") == "runner-noble-amd64"
assert _relation_value(garm_out, "openstack_auth_url") == (
"https://keystone.example.com:5000/v3"
Expand Down Expand Up @@ -250,7 +258,7 @@ def test_clearing_configured_image_without_relation_withdraws_only_image():
assert _relation_value(garm_out, "openstack_auth_url") == (
"https://keystone.example.com:5000/v3"
)
assert "openstack_password_secret_uri" in garm_out.local_app_data
assert "openstack_password_secret_uri" in _relation_section(garm_out, "provider")
assert _relation_value(garm_out, "github_private_key_secret_uri")
relation_id = garm_relation.id
relation_secrets = {
Expand Down Expand Up @@ -741,6 +749,12 @@ def test_reconcile_writes_full_config_to_garm_relation():
out = ctx.run(ctx.on.config_changed(), state)
garm_out = out.get_relation(garm_relation.id)

assert set(garm_out.local_app_data) == {"provider", "scaleset"}
provider_data = _relation_section(garm_out, "provider")
scaleset_data = _relation_section(garm_out, "scaleset")
assert "name" not in provider_data
assert "openstack_auth_url" not in scaleset_data

# Provider config
assert _relation_value(garm_out, "openstack_auth_url") == (
"https://keystone.example.com:5000/v3"
Expand All @@ -757,7 +771,7 @@ def test_reconcile_writes_full_config_to_garm_relation():
# GitHub config
assert _relation_value(garm_out, "github_app_id") == 99999
assert _relation_value(garm_out, "github_installation_id") == 67890
assert "github_private_key" not in garm_out.local_app_data
assert "github_private_key" not in provider_data
assert _relation_value(garm_out, "github_private_key_secret_uri")

# Scaleset config
Expand Down Expand Up @@ -834,7 +848,7 @@ def test_reconcile_writes_optional_scaleset_fields_to_garm_relation():
Image UUID is present, garm relation is joined.
act: config-changed fires.
assert: org, runner_group, and pre_install_scripts are present in the
garm relation data. repo is published as an empty string.
garm relation data. repo is absent.
"""
ctx = Context(GarmConfiguratorCharm)
secret = _make_secret()
Expand Down Expand Up @@ -1010,11 +1024,11 @@ def test_runner_config_fields_written_to_garm_configurator_relation():
assert _relation_value(rel_out, "pre_job_script") == "echo start"


def test_runner_config_fields_empty_when_unset():
def test_runner_config_fields_absent_when_unset():
"""
arrange: Valid config with no runner config options set.
act: Run config-changed with a garm-configurator relation present.
assert: The six runner config keys are published as empty strings.
assert: The six runner config keys are absent from the databag.
"""
ctx = Context(GarmConfiguratorCharm)
secret = _make_secret()
Expand Down
Loading
Loading