backup

package
v1.3.0-main Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrExternalDatabase = errors.New("external database detected: please back up the database separately using your database backup tools")

ErrExternalDatabase is returned when an external database is detected. External databases must be backed up separately by the user.

Functions

func CreateArchive

func CreateArchive(ctx context.Context, stagingDir string, outputDir string, metadata BackupMetadata, log logrus.FieldLogger) (archivePath string, checksumPath string, err error)

CreateArchive packages the staging directory into a timestamped tar.gz archive with SHA256 checksum. Writes metadata.json to staging directory before archiving.

Returns:

  • archivePath: path to created .tar.gz file
  • checksumPath: path to created .sha256 file
  • error: nil on success, error describing failure otherwise

Archive filename format: flightctl-backup-YYYYMMDDTHHMMSSZ.tar.gz Checksum format: <sha256-hash> <archive-filename> (compatible with sha256sum -c)

Error handling: All-or-nothing semantics. On failure, cleans up partial artifacts (archive file, checksum file). Staging directory cleanup is caller's responsibility.

func PerformBackup

func PerformBackup(ctx context.Context, deployer Deployer, outputDir string, log logrus.FieldLogger) (archivePath string, err error)

PerformBackup executes the complete backup workflow:

  1. Detect deployment type
  2. Create staging directory
  3. Backup database (if internal)
  4. Backup PKI materials
  5. Backup config (stub)
  6. Create timestamped archive with checksum
  7. Clean up staging directory

Returns path to created archive on success.

func ShellEscape

func ShellEscape(s string) string

ShellEscape escapes a string for safe use in a shell command by wrapping it in single quotes and escaping any single quotes within the string.

func ValidateDeploymentType

func ValidateDeploymentType(s string) error

ValidateDeploymentType returns an error if s is not a recognised deployment type string.

Types

type BackupMetadata

type BackupMetadata struct {
	// Timestamp is when the backup was created (UTC).
	Timestamp time.Time `json:"timestamp"`

	// Version is the FlightCtl version that created the backup.
	Version string `json:"version"`

	// DeploymentType is the deployment environment (podman or kubernetes).
	DeploymentType DeploymentType `json:"deploymentType"`

	// DatabaseIncluded indicates whether database backup is included.
	// False when external database is configured.
	DatabaseIncluded bool `json:"databaseIncluded"`
}

BackupMetadata contains metadata about a backup archive. This struct is serialized to metadata.json in the archive root.

type BackupStore

type BackupStore struct {
	// contains filtered or unexported fields
}

BackupStore provides database operations for backup functionality. This is a minimal placeholder parallel to restore.RestoreStore.

func NewBackupStore

func NewBackupStore(db *gorm.DB) *BackupStore

NewBackupStore creates a BackupStore backed by the given gorm connection.

type Deployer

type Deployer interface {
	Type() DeploymentType
	BackupDatabase(ctx context.Context, outputDir string) error
	BackupPKI(ctx context.Context, outputDir string) error
	BackupConfig(ctx context.Context, outputDir string) error
}

Deployer interface for backup operations across deployment types

type DeploymentType

type DeploymentType string

DeploymentType represents the deployment environment type

const (
	DeploymentTypePodman     DeploymentType = "podman"
	DeploymentTypeKubernetes DeploymentType = "kubernetes"
	DeploymentTypeUnknown    DeploymentType = "unknown"
)

func DetectDeployment

func DetectDeployment() (DeploymentType, error)

DetectDeployment probes the environment using default implementations. It is a convenience wrapper around (&Detector{}).Detect().

type Detector

type Detector struct {
	// PodmanChecker reports whether the Podman FlightCtl service is active.
	// Default: podmanServiceIsActive (systemctl is-active flightctl-api.service).
	PodmanChecker func() bool
	// KubeconfigChecker reports whether a kubeconfig file is reachable.
	// Default: kubeconfigFileExists ($KUBECONFIG or ~/.kube/config).
	KubeconfigChecker func() bool
}

Detector probes the local environment to determine the deployment type. Both checker fields are optional: when nil the default implementations are used. Inject custom functions in tests to control detection without requiring live services.

d := &Detector{PodmanChecker: func() bool { return true }}
dt, err := d.Detect()

func (*Detector) Detect

func (d *Detector) Detect() (DeploymentType, error)

Detect probes the environment and returns the detected deployment type. It does not create a deployer; call NewDeployerForType after detection. Mirrors the logic used by the e2e infrastructure (test/e2e/infra/factory.go autoDetect):

  • Podman: flightctl-api.service is active via systemctl (with sudo fallback)
  • Kubernetes: a kubeconfig file is reachable via $KUBECONFIG or ~/.kube/config

type KubernetesDeployer

type KubernetesDeployer struct {
	// contains filtered or unexported fields
}

KubernetesDeployer implements Deployer for Kubernetes/Helm deployments

func NewKubernetesDeployer

func NewKubernetesDeployer(log logrus.FieldLogger, opts ...KubernetesDeployerOption) *KubernetesDeployer

NewKubernetesDeployer creates a new Kubernetes deployer. Defaults: namespace "flightctl"; internalNamespace inherits namespace; helmReleaseName is left empty for auto-discovery (see resolveHelmReleaseSecret). clientset nil → in-cluster client is created on first use.

func (*KubernetesDeployer) BackupConfig

func (k *KubernetesDeployer) BackupConfig(ctx context.Context, outputDir string) error

BackupConfig backs up Helm release configuration by exporting the Helm release Secret. The Secret contains the complete release state: chart (templates, values), config (user values), and manifest. Exports to <outputDir>/config/helm-release-<name>-v<revision>.yaml.

Why backup the Helm Secret (not ConfigMaps): - ConfigMaps are generated artifacts from chart + user values - Helm Secret is the source of truth for the release - Backing up ConfigMaps directly would be overwritten on next helm upgrade

Restore process: 1. Apply the Secret: kubectl apply -f helm-release-<name>-v<revision>.yaml 2. Run helm upgrade: helm upgrade <release> <chart> -n <namespace>

  • Helm sees the existing release (from the restored Secret)
  • Re-evaluates values and regenerates ConfigMaps automatically
  • ConfigMaps are recreated with correct configuration

3. ConfigMaps backup is not needed - they are regenerated by Helm

Returns error if no Helm release Secrets are found or if multiple are found without an explicit release name.

func (*KubernetesDeployer) BackupDatabase

func (k *KubernetesDeployer) BackupDatabase(ctx context.Context, outputDir string) error

BackupDatabase backs up the PostgreSQL database using pg_dump via Kubernetes client-go. Credentials are read from the flightctl-db-app-secret Kubernetes Secret in internalNamespace. If that Secret does not exist, the database is assumed to be external and ErrExternalDatabase is returned without creating a backup.

func (*KubernetesDeployer) BackupPKI

func (k *KubernetesDeployer) BackupPKI(ctx context.Context, outputDir string) error

BackupPKI backs up PKI materials by exporting all PKI/TLS Secrets to YAML files. Writes one YAML file per Secret to <outputDir>/pki/<secretName>.yaml. All YAML files are created with pkiFileMode permissions (contain sensitive data).

func (*KubernetesDeployer) Type

Type returns the deployment type

type KubernetesDeployerOption

type KubernetesDeployerOption func(*KubernetesDeployer)

KubernetesDeployerOption configures a KubernetesDeployer.

func WithClientset

WithClientset injects a Kubernetes clientset (for testing).

func WithHelmReleaseName

func WithHelmReleaseName(name string) KubernetesDeployerOption

WithHelmReleaseName sets the Helm release name for values extraction.

func WithInternalNamespace

func WithInternalNamespace(ns string) KubernetesDeployerOption

WithInternalNamespace sets the Kubernetes namespace for the DB pod.

func WithNamespace

func WithNamespace(ns string) KubernetesDeployerOption

WithNamespace sets the Kubernetes namespace for PKI Secrets (Release namespace).

func WithRestConfig

func WithRestConfig(cfg *rest.Config) KubernetesDeployerOption

WithRestConfig injects a REST config (for testing).

type PodmanDeployer

type PodmanDeployer struct {
	// contains filtered or unexported fields
}

PodmanDeployer implements Deployer for Podman/quadlet deployments

func NewPodmanDeployer

func NewPodmanDeployer(log logrus.FieldLogger, opts ...PodmanDeployerOption) *PodmanDeployer

NewPodmanDeployer creates a new Podman deployer. Defaults: pkiPath "/etc/flightctl/pki", serviceConfigPath "/etc/flightctl/service-config.yaml", dbContainerName "flightctl-db", containerCLI "podman", kvContainerName "flightctl-kv".

func (*PodmanDeployer) BackupConfig

func (p *PodmanDeployer) BackupConfig(ctx context.Context, outputDir string) error

BackupConfig backs up service configuration files for Podman deployments. Copies /etc/flightctl/service-config.yaml to <outputDir>/config/service-config.yaml. Exports PAM Issuer volume to <outputDir>/volumes/pam-issuer-etc.tar. Returns error if service-config.yaml is missing (required file). Logs warning if PAM Issuer volume export fails (optional component).

func (*PodmanDeployer) BackupDatabase

func (p *PodmanDeployer) BackupDatabase(ctx context.Context, outputDir string) error

BackupDatabase backs up the PostgreSQL database by executing pg_dump inside the database container. Credentials are loaded from the service configuration file at serviceConfigPath. For internal databases, executes pg_dump via podman exec and writes dump to <outputDir>/db/dump.sql. For external databases, returns ErrExternalDatabase without creating a backup.

func (*PodmanDeployer) BackupPKI

func (p *PodmanDeployer) BackupPKI(ctx context.Context, outputDir string) error

BackupPKI backs up PKI materials by copying /etc/flightctl/pki/ directory tree. Preserves file permissions (typically 0600 for private keys, 0644 for certificates). Writes output to <outputDir>/pki/. The operation respects context cancellation during the directory walk.

func (*PodmanDeployer) Type

func (p *PodmanDeployer) Type() DeploymentType

Type returns the deployment type

type PodmanDeployerOption

type PodmanDeployerOption func(*PodmanDeployer)

PodmanDeployerOption configures a PodmanDeployer.

func WithContainerCLI

func WithContainerCLI(cli string) PodmanDeployerOption

WithContainerCLI sets the container CLI command (e.g. "podman" or "docker").

func WithDBContainerName

func WithDBContainerName(name string) PodmanDeployerOption

WithDBContainerName sets the database container name for pg_dump exec.

func WithDBName

func WithDBName(name string) PodmanDeployerOption

WithDBName overrides the database name used for pg_dump. When empty, cfg.Database.Name from the service config is used.

func WithKVContainerName

func WithKVContainerName(name string) PodmanDeployerOption

WithKVContainerName sets the KV container name (reserved for future use).

func WithPKIPath

func WithPKIPath(path string) PodmanDeployerOption

WithPKIPath sets the PKI source directory path.

func WithServiceConfigPath

func WithServiceConfigPath(path string) PodmanDeployerOption

WithServiceConfigPath sets the service configuration file path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL