restore

package
v1.2.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: Apache-2.0 Imports: 39 Imported by: 0

Documentation

Overview

Package restore is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractArchive added in v1.2.0

func ExtractArchive(ctx context.Context, archivePath string) (string, error)

ExtractArchive validates that archivePath exists and is a readable regular file, then extracts the tar.gz to a new temporary directory.

Returns the path to the extracted directory on success. Caller is responsible for cleanup via os.RemoveAll.

On error, returns ("", err) — the empty string is guaranteed so that the caller's cleanup guard (if extractDir != "") is always safe.

func PrepareDevices

func PrepareDevices(ctx context.Context, rs *RestoreStore, kv kvstore.KVStore, log logrus.FieldLogger) (devicesUpdated int64, err error)

PrepareDevices performs post-restoration device preparation: clear KV store, update device and enrollment request annotations, add awaiting-reconnection keys, and create a system restored event in the store.

func ReadMetadata added in v1.2.0

func ReadMetadata(extractDir string) (*backup.BackupMetadata, error)

ReadMetadata reads and unmarshals metadata.json from the root of an extracted archive directory. Returns a non-nil pointer on success. Returns nil and an error if the file is missing or contains invalid JSON.

func Restore added in v1.2.0

func Restore(
	ctx context.Context,
	archivePath string,
	deployer Deployer,
	log *logrus.Logger,
) (retErr error)

Restore executes the full restore workflow:

  1. Verifies SHA256 checksum of the archive (verifyChecksum)
  2. Extracts the archive to a temporary directory (ExtractArchive)
  3. Reads and validates metadata.json (ReadMetadata)
  4. Validates the archive's deployment type against deployer.Type() (ValidateDeploymentType)
  5. Stops FlightCtl services (deployer.StopServices)
  6. Imports the database (deployer.RestoreDatabase)
  7. Restores PKI materials (deployer.RestorePKI)
  8. Restores service configuration (deployer.RestoreConfig)
  9. Retrieves service credentials via deployer.GetConfig
  10. Exposes DB and KV via deployer.ExposeService (no-op for Podman, port-forward for Kubernetes)
  11. Runs post-restoration device preparation (PrepareDevices)
  12. Starts FlightCtl services (deployer.StartServices) — deferred, always runs even on failure

The deployer encapsulates all deployment-specific operations and credential extraction. The temporary extraction directory is always cleaned up before Restore returns.

func ValidateDeploymentType added in v1.2.0

func ValidateDeploymentType(metadata *backup.BackupMetadata, currentType backup.DeploymentType) error

ValidateDeploymentType checks that the archive's recorded deployment type matches currentType. Returns a descriptive error on mismatch naming both types so the operator knows exactly what diverged.

Types

type Deployer added in v1.2.0

type Deployer interface {
	// Type returns the deployment type this deployer targets.
	Type() backup.DeploymentType
	// StopServices stops the FlightCtl services that interact with the database
	// and other restored state. Must be called before any restore steps.
	StopServices(ctx context.Context) error
	// StartServices starts the FlightCtl services previously stopped by StopServices.
	// Must be called after all restore steps complete, whether they succeed or fail.
	StartServices(ctx context.Context) error
	// RestoreDatabase imports the database dump from the extracted archive.
	// If db/dump.sql is absent in extractDir the database is external — the
	// function logs restore instructions and returns nil without error.
	// StopServices must be called before RestoreDatabase.
	RestoreDatabase(ctx context.Context, extractDir string) error
	// ExposeService returns the host and port to use for connecting to the named
	// service ("flightctl-db" or "flightctl-kv"). For Kubernetes it starts a
	// kubectl port-forward and returns 127.0.0.1:<localPort>; cleanup() must be
	// deferred by the caller. For Podman it returns the original host/port from
	// the service config with a no-op cleanup (services are accessible on the same host).
	ExposeService(ctx context.Context, serviceName string) (host string, port int, cleanup func(), err error)
	// GetConfig extracts service configuration (DB and KV credentials) from the
	// running infrastructure. For Kubernetes, reads from cluster Secrets; for
	// Podman, reads the service config file on the host.
	GetConfig(ctx context.Context) (*config.Config, error)
	// RestorePKI restores PKI materials from the extracted archive directory.
	// For Podman, it copies <extractDir>/pki/ to the configured PKI destination
	// (default: /etc/flightctl/pki/), preserving file permissions.
	// For Kubernetes, it applies each <extractDir>/pki/*.yaml file as a Secret via the Go client (create or update).
	// Returns an error if the pki/ subdirectory is absent from the archive.
	// StopServices must be called before RestorePKI.
	RestorePKI(ctx context.Context, extractDir string) error
	// RestoreConfig restores service configuration from the extracted archive directory.
	// For Podman: copies <extractDir>/config/service-config.yaml to the configured service
	// config path and imports the PAM Issuer volume from <extractDir>/volumes/pam-issuer-etc.tar
	// (optional — logs a warning if absent or if the import fails).
	// For Kubernetes: decodes the backed-up Helm release Secret, applies it to the cluster,
	// reconstructs the chart and user values from the release data, and runs helm upgrade.
	// StopServices must be called before RestoreConfig.
	RestoreConfig(ctx context.Context, extractDir string) error
}

Deployer performs deployment-type-specific restore operations. The caller (restore.Restore) is responsible for orchestrating StopServices and StartServices around all individual restore steps so that services remain stopped for the full duration of the restore, regardless of how many steps are involved.

type KubernetesRestoreDeployer added in v1.2.0

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

KubernetesRestoreDeployer implements Deployer for Kubernetes/Helm deployments.

func NewKubernetesRestoreDeployer added in v1.2.0

func NewKubernetesRestoreDeployer(
	log logrus.FieldLogger,
	opts ...KubernetesRestoreOption,
) *KubernetesRestoreDeployer

NewKubernetesRestoreDeployer creates a KubernetesRestoreDeployer. Defaults: namespace "flightctl"; internalNamespace inherits namespace; clientset nil → in-cluster client is created on first use; restCfg nil → in-cluster config is resolved on first use.

func (*KubernetesRestoreDeployer) ExposeService added in v1.2.0

func (k *KubernetesRestoreDeployer) ExposeService(ctx context.Context, serviceName string) (string, int, func(), error)

ExposeService starts a kubectl port-forward to the named service and returns the localhost address and a free local port. cleanup() must be deferred. Supported service names: "flightctl-db", "flightctl-kv". Well-known default ports (5432/6379) are used as the port-forward target. The function blocks until the tunnel accepts TCP connections (up to portForwardReadyTimeout).

func (*KubernetesRestoreDeployer) GetConfig added in v1.2.0

GetConfig reads DB and KV credentials from Kubernetes Secrets and returns a populated config.Config. The result is cached after the first successful call.

func (*KubernetesRestoreDeployer) RestoreConfig added in v1.2.0

func (k *KubernetesRestoreDeployer) RestoreConfig(ctx context.Context, extractDir string) error

getFreePort asks the OS for a free local TCP port by binding to :0. RestoreConfig applies the backed-up Helm release Secret to the cluster, then reconstructs the chart and user values from the release data and runs helm upgrade.

func (*KubernetesRestoreDeployer) RestoreDatabase added in v1.2.0

func (k *KubernetesRestoreDeployer) RestoreDatabase(ctx context.Context, extractDir string) error

RestoreDatabase imports db/dump.sql from extractDir into the Kubernetes DB pod using the temp-DB-then-rename strategy.

The dump is restored into a fresh temporary database using kubectl exec into the flightctl-db deployment as the postgres OS user (UNIX socket auth, no credentials required). Once the restore succeeds the live database is atomically swapped via ALTER DATABASE RENAME and then dropped (unless keepOldDB is true, in which case it is preserved for operator recovery).

func (*KubernetesRestoreDeployer) RestorePKI added in v1.2.0

func (k *KubernetesRestoreDeployer) RestorePKI(ctx context.Context, extractDir string) error

RestorePKI reads each *.yaml file from the pki/ subdirectory of the extracted archive and creates or updates the corresponding Kubernetes Secret via the Go client. Returns an error if pki/ is absent or contains no YAML files.

func (*KubernetesRestoreDeployer) StartServices added in v1.2.0

func (k *KubernetesRestoreDeployer) StartServices(ctx context.Context) error

StartServices scales FlightCtl deployments back to the replica counts recorded by StopServices.

func (*KubernetesRestoreDeployer) StopServices added in v1.2.0

func (k *KubernetesRestoreDeployer) StopServices(ctx context.Context) error

StopServices scales FlightCtl deployments to zero replicas, recording the original counts so StartServices can restore them, then waits until each deployment has no running pods before returning.

func (*KubernetesRestoreDeployer) Type added in v1.2.0

type KubernetesRestoreOption added in v1.2.0

type KubernetesRestoreOption func(*KubernetesRestoreDeployer)

KubernetesRestoreOption configures a KubernetesRestoreDeployer.

func WithHelmUpgradeFunc added in v1.2.0

func WithHelmUpgradeFunc(fn func(ctx context.Context, releaseName, chartDir, namespace, valuesFile string) error) KubernetesRestoreOption

WithHelmUpgradeFunc injects a custom helm upgrade function (for testing). The default runs the system helm CLI.

func WithKeepOldDB added in v1.2.0

func WithKeepOldDB(keep bool) KubernetesRestoreOption

WithKeepOldDB controls whether the pre-restore database is dropped after a successful swap (false, default) or preserved under <dbname>_old_<timestamp> (true) for operator recovery.

func WithRestoreClientset added in v1.2.0

func WithRestoreClientset(cs kubernetes.Interface) KubernetesRestoreOption

WithRestoreClientset injects a Kubernetes clientset (for testing).

func WithRestoreInternalNamespace added in v1.2.0

func WithRestoreInternalNamespace(ns string) KubernetesRestoreOption

WithRestoreInternalNamespace sets the internal Kubernetes namespace (worker, periodic, db).

func WithRestoreNamespace added in v1.2.0

func WithRestoreNamespace(ns string) KubernetesRestoreOption

WithRestoreNamespace sets the external Kubernetes namespace (api, ui).

func WithRestoreRestConfig added in v1.2.0

func WithRestoreRestConfig(cfg *rest.Config) KubernetesRestoreOption

WithRestoreRestConfig injects a REST config (for testing).

type MockDeployer added in v1.2.0

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

MockDeployer is a mock of Deployer interface.

func NewMockDeployer added in v1.2.0

func NewMockDeployer(ctrl *gomock.Controller) *MockDeployer

NewMockDeployer creates a new mock instance.

func (*MockDeployer) EXPECT added in v1.2.0

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockDeployer) ExposeService added in v1.2.0

func (m *MockDeployer) ExposeService(ctx context.Context, serviceName string) (string, int, func(), error)

ExposeService mocks base method.

func (*MockDeployer) GetConfig added in v1.2.0

func (m *MockDeployer) GetConfig(ctx context.Context) (*config.Config, error)

GetConfig mocks base method.

func (*MockDeployer) RestoreConfig added in v1.2.0

func (m *MockDeployer) RestoreConfig(ctx context.Context, extractDir string) error

RestoreConfig mocks base method.

func (*MockDeployer) RestoreDatabase added in v1.2.0

func (m *MockDeployer) RestoreDatabase(ctx context.Context, extractDir string) error

RestoreDatabase mocks base method.

func (*MockDeployer) RestorePKI added in v1.2.0

func (m *MockDeployer) RestorePKI(ctx context.Context, extractDir string) error

RestorePKI mocks base method.

func (*MockDeployer) StartServices added in v1.2.0

func (m *MockDeployer) StartServices(ctx context.Context) error

StartServices mocks base method.

func (*MockDeployer) StopServices added in v1.2.0

func (m *MockDeployer) StopServices(ctx context.Context) error

StopServices mocks base method.

func (*MockDeployer) Type added in v1.2.0

Type mocks base method.

type MockDeployerMockRecorder added in v1.2.0

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

MockDeployerMockRecorder is the mock recorder for MockDeployer.

func (*MockDeployerMockRecorder) ExposeService added in v1.2.0

func (mr *MockDeployerMockRecorder) ExposeService(ctx, serviceName any) *gomock.Call

ExposeService indicates an expected call of ExposeService.

func (*MockDeployerMockRecorder) GetConfig added in v1.2.0

func (mr *MockDeployerMockRecorder) GetConfig(ctx any) *gomock.Call

GetConfig indicates an expected call of GetConfig.

func (*MockDeployerMockRecorder) RestoreConfig added in v1.2.0

func (mr *MockDeployerMockRecorder) RestoreConfig(ctx, extractDir any) *gomock.Call

RestoreConfig indicates an expected call of RestoreConfig.

func (*MockDeployerMockRecorder) RestoreDatabase added in v1.2.0

func (mr *MockDeployerMockRecorder) RestoreDatabase(ctx, extractDir any) *gomock.Call

RestoreDatabase indicates an expected call of RestoreDatabase.

func (*MockDeployerMockRecorder) RestorePKI added in v1.2.0

func (mr *MockDeployerMockRecorder) RestorePKI(ctx, extractDir any) *gomock.Call

RestorePKI indicates an expected call of RestorePKI.

func (*MockDeployerMockRecorder) StartServices added in v1.2.0

func (mr *MockDeployerMockRecorder) StartServices(ctx any) *gomock.Call

StartServices indicates an expected call of StartServices.

func (*MockDeployerMockRecorder) StopServices added in v1.2.0

func (mr *MockDeployerMockRecorder) StopServices(ctx any) *gomock.Call

StopServices indicates an expected call of StopServices.

func (*MockDeployerMockRecorder) Type added in v1.2.0

func (mr *MockDeployerMockRecorder) Type() *gomock.Call

Type indicates an expected call of Type.

type MockServiceHandler added in v1.2.0

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

MockServiceHandler is a mock of ServiceHandler interface.

func NewMockServiceHandler added in v1.2.0

func NewMockServiceHandler(ctrl *gomock.Controller) *MockServiceHandler

NewMockServiceHandler creates a new mock instance.

func (*MockServiceHandler) EXPECT added in v1.2.0

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockServiceHandler) Start added in v1.2.0

func (m *MockServiceHandler) Start(ctx context.Context) error

Start mocks base method.

func (*MockServiceHandler) Stop added in v1.2.0

func (m *MockServiceHandler) Stop(ctx context.Context) error

Stop mocks base method.

type MockServiceHandlerMockRecorder added in v1.2.0

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

MockServiceHandlerMockRecorder is the mock recorder for MockServiceHandler.

func (*MockServiceHandlerMockRecorder) Start added in v1.2.0

Start indicates an expected call of Start.

func (*MockServiceHandlerMockRecorder) Stop added in v1.2.0

Stop indicates an expected call of Stop.

type PodmanRestoreDeployer added in v1.2.0

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

PodmanRestoreDeployer implements Deployer for Podman/quadlet deployments.

func NewPodmanRestoreDeployer added in v1.2.0

func NewPodmanRestoreDeployer(
	log logrus.FieldLogger,
	opts ...PodmanRestoreOption,
) *PodmanRestoreDeployer

NewPodmanRestoreDeployer creates a PodmanRestoreDeployer. Defaults: containerName "flightctl-db", containerCLI "podman", kvContainerName "flightctl-kv", serviceHandler NewSystemctlServiceHandler(nil) with the default service list, serviceConfigPath "/etc/flightctl/service-config.yaml".

func (*PodmanRestoreDeployer) ExposeService added in v1.2.0

func (p *PodmanRestoreDeployer) ExposeService(ctx context.Context, serviceName string) (string, int, func(), error)

ExposeService returns the original host and port from the service config for the named service. For flightctl-kv, when a published port is available on kvContainerName, that is used instead.

func (*PodmanRestoreDeployer) GetConfig added in v1.2.0

func (p *PodmanRestoreDeployer) GetConfig(ctx context.Context) (*config.Config, error)

GetConfig loads DB and KV credentials from the service configuration file. The result is cached after the first successful load.

func (*PodmanRestoreDeployer) RestoreConfig added in v1.2.0

func (p *PodmanRestoreDeployer) RestoreConfig(ctx context.Context, extractDir string) error

RestoreConfig copies service-config.yaml from the archive to the configured service config path and imports the PAM Issuer volume (optional). Returns an error only if service-config.yaml is absent from the archive; PAM Issuer volume failures are logged as warnings since the component is optional.

func (*PodmanRestoreDeployer) RestoreDatabase added in v1.2.0

func (p *PodmanRestoreDeployer) RestoreDatabase(ctx context.Context, extractDir string) error

RestoreDatabase imports db/dump.sql from extractDir into the Podman DB container using the temp-DB-then-rename strategy.

The dump is restored into a fresh temporary database as the postgres OS user (UNIX socket auth, no credentials needed). Once the restore succeeds the live database is atomically swapped via ALTER DATABASE RENAME and then dropped (unless keepOldDB is true, in which case it is preserved for operator recovery).

func (*PodmanRestoreDeployer) RestorePKI added in v1.2.0

func (p *PodmanRestoreDeployer) RestorePKI(ctx context.Context, extractDir string) error

RestorePKI copies the pki/ directory from the extracted archive into the configured PKI destination, preserving each file's mode. Returns an error if pki/ is absent.

func (*PodmanRestoreDeployer) StartServices added in v1.2.0

func (p *PodmanRestoreDeployer) StartServices(ctx context.Context) error

StartServices delegates to the ServiceHandler.

func (*PodmanRestoreDeployer) StopServices added in v1.2.0

func (p *PodmanRestoreDeployer) StopServices(ctx context.Context) error

StopServices delegates to the ServiceHandler.

func (*PodmanRestoreDeployer) Type added in v1.2.0

type PodmanRestoreOption added in v1.2.0

type PodmanRestoreOption func(*PodmanRestoreDeployer)

PodmanRestoreOption configures a PodmanRestoreDeployer.

func WithContainerCLI added in v1.2.0

func WithContainerCLI(cli string) PodmanRestoreOption

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

func WithContainerName added in v1.2.0

func WithContainerName(name string) PodmanRestoreOption

WithContainerName sets the database container name. Deprecated: use WithDBContainerName.

func WithDBContainerName added in v1.2.0

func WithDBContainerName(name string) PodmanRestoreOption

WithDBContainerName sets the database container name.

func WithDBName added in v1.2.0

func WithDBName(name string) PodmanRestoreOption

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

func WithKVContainerName added in v1.2.0

func WithKVContainerName(name string) PodmanRestoreOption

WithKVContainerName sets the KV container name used to resolve published ports in ExposeService for flightctl-kv.

func WithPAMIssuerVolumeName added in v1.2.0

func WithPAMIssuerVolumeName(name string) PodmanRestoreOption

WithPAMIssuerVolumeName sets the Podman volume name for PAM Issuer volume restore. Defaults to "flightctl-pam-issuer-etc".

func WithPKIDestPath added in v1.2.0

func WithPKIDestPath(path string) PodmanRestoreOption

WithPKIDestPath sets the destination directory for PKI file restoration. Defaults to /etc/flightctl/pki.

func WithPodmanKeepOldDB added in v1.2.0

func WithPodmanKeepOldDB(keep bool) PodmanRestoreOption

WithPodmanKeepOldDB controls whether the pre-restore database is dropped after a successful swap (false, default) or preserved under <dbname>_old_<timestamp> (true).

func WithServiceConfigPath added in v1.2.0

func WithServiceConfigPath(path string) PodmanRestoreOption

WithServiceConfigPath sets the path to the service configuration file used to extract DB and KV credentials. Defaults to /etc/flightctl/service-config.yaml.

func WithServiceHandler added in v1.2.0

func WithServiceHandler(h ServiceHandler) PodmanRestoreOption

WithServiceHandler sets the service lifecycle handler.

type RestoreStore

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

RestoreStore provides the minimal set of database operations required by the post-restoration preparation logic. It wraps a *gorm.DB directly so the restore package does not depend on internal/store.Store.

func NewRestoreStore

func NewRestoreStore(db *gorm.DB) *RestoreStore

NewRestoreStore creates a RestoreStore backed by the given gorm connection.

func (*RestoreStore) CreateEvent

func (s *RestoreStore) CreateEvent(ctx context.Context, orgId uuid.UUID, resource *domain.Event) error

CreateEvent persists a domain event.

func (*RestoreStore) GetAllDeviceNames

func (s *RestoreStore) GetAllDeviceNames(ctx context.Context, orgId uuid.UUID) ([]string, error)

GetAllDeviceNames returns all device names for a given organization.

func (*RestoreStore) ListOrganizations

func (s *RestoreStore) ListOrganizations(ctx context.Context) ([]*orgmodel.Organization, error)

ListOrganizations returns all organizations (no filtering).

func (*RestoreStore) PrepareDevicesAfterRestore

func (s *RestoreStore) PrepareDevicesAfterRestore(ctx context.Context) (int64, error)

PrepareDevicesAfterRestore sets the awaitingReconnect annotation on all eligible devices, clears their lastSeen timestamps, and sets a status summary.

func (*RestoreStore) PrepareEnrollmentRequestsAfterRestore

func (s *RestoreStore) PrepareEnrollmentRequestsAfterRestore(ctx context.Context) (int64, error)

PrepareEnrollmentRequestsAfterRestore sets the awaitingReconnect annotation on all non-approved enrollment requests.

type ServiceHandler added in v1.2.0

type ServiceHandler interface {
	Stop(ctx context.Context) error
	Start(ctx context.Context) error
}

ServiceHandler manages the lifecycle of FlightCtl services on a Podman host.

type SystemctlServiceHandler added in v1.2.0

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

SystemctlServiceHandler implements ServiceHandler using systemctl.

func NewSystemctlServiceHandler added in v1.2.0

func NewSystemctlServiceHandler(serviceNames []string) *SystemctlServiceHandler

NewSystemctlServiceHandler creates a SystemctlServiceHandler. serviceNames nil → default FlightCtl service names.

func (*SystemctlServiceHandler) Start added in v1.2.0

func (*SystemctlServiceHandler) Stop added in v1.2.0

Jump to

Keyboard shortcuts

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