k8s_backup_target

K8s Backup Target library.

This library implements the Requirer and Provider roles for the k8s_backup_target relation interface. It is used by client charms to declare backup specifications, and by backup charms or backup integrator charms to consume them and forward to backup operators.

The k8s_backup_target interface allows a charm (the provider) to provide a declarative description of what Kubernetes resources should be included in a backup. These specifications are sent to the backup charm or backup integrator charm (the requirer), which merges them with schedule configuration and forwards to the backup operator.

This interface follows a least-privilege model: client charms do not manipulate cluster resources themselves. Instead, they define what should be backed up and leave execution to the backup operator.

Provider Example

from charmlibs.interfaces.k8s_backup_target import (
    K8sBackupTargetProvider,
    K8sBackupTargetSpec,
)

class SomeCharm(CharmBase):
    def __init__(self, *args):
        # ...
        self.backup = K8sBackupTargetProvider(
            self,
            relation_name="backup",
            spec=K8sBackupTargetSpec(
                include_namespaces=["my-namespace"],
                include_resources=["persistentvolumeclaims", "services", "deployments"],
                ttl=str(self.config["ttl"]),
            ),
            # Optional: refresh the data on custom events
            refresh_event=[self.on.config_changed],
        )

Requirer Example

from charmlibs.interfaces.k8s_backup_target import (
    K8sBackupTargetRequirer,
)

class BackupIntegratorCharm(CharmBase):
    def __init__(self, *args):
        # ...
        self.backup_requirer = K8sBackupTargetRequirer(self, relation_name="k8s-backup-target")

    def _on_backup_action(self, event):
        spec = self.backup_requirer.get_backup_spec(
            app_name=event.params["app"],
            endpoint=event.params["endpoint"],
            model=event.params["model"],
        )
        ...
class K8sBackupTargetProvider(
charm: CharmBase,
relation_name: str,
spec: K8sBackupTargetSpec,
refresh_event: BoundEvent | list[BoundEvent] | None = None,
)

Bases: Object

Provider class for the backup target configuration relation.

class K8sBackupTargetRequirer(charm: CharmBase, relation_name: str)

Bases: object

Requirer class for the backup target configuration relation.

property is_ready: bool

Check if the relation has valid backup target data.

Returns:

True if at least one relation has parseable backup_targets data.

get_backup_spec(
app_name: str,
endpoint: str,
model: str,
) K8sBackupTargetSpec | None

Get a K8sBackupTargetSpec for a given (app, endpoint, model).

Parameters:
  • app_name – The name of the application for which the backup is configured.

  • endpoint – The name of the relation (from metadata.yaml).

  • model – The model name of the application.

Returns:

The backup specification if available, otherwise None.

class K8sBackupTargetSpec(
*,
include_namespaces: list[str] | None = None,
include_resources: list[str] | None = None,
exclude_namespaces: list[str] | None = None,
exclude_resources: list[str] | None = None,
include_cluster_resources: bool | None = None,
label_selector: dict[str, str] | None = None,
ttl: str | None = None,
)

Bases: BaseModel

Backup target configuration specifying what Kubernetes resources to back up.

Parameters:
  • include_namespaces – Namespaces to include in the backup (None means all).

  • include_resources – Resource kinds to include in the backup (None means all).

  • exclude_namespaces – Namespaces to exclude from the backup.

  • exclude_resources – Resource kinds to exclude from the backup.

  • label_selector – Label selector for filtering resources.

  • include_cluster_resources – Whether to include cluster-scoped resources in the backup. Defaults to None (auto detect based on resources).

  • ttl – TTL for the backup, if applicable. Example: “24h”, “10m10s”, etc.

include_namespaces: list[str] | None
include_resources: list[str] | None
exclude_namespaces: list[str] | None
exclude_resources: list[str] | None
include_cluster_resources: bool | None
label_selector: dict[str, str] | None
ttl: str | None
__post_init__()

Validate the specification.

__pydantic_extra_info__ = None

A wrapper around the __pydantic_extra__ annotation, if explicitly annotated on a model.

This is a private attribute, not meant to be used outside Pydantic.