planner

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetServiceNames

func GetServiceNames(services []ServiceInfo) []string

GetServiceNames extracts service names from a list of ServiceInfo.

func NeedsApply

func NeedsApply(services []ServiceInfo) bool

NeedsApply determines if any services in the list require application/reconciliation.

func RenderResourcePlan added in v0.1.1

func RenderResourcePlan(rp *ResourcePlan) string

RenderResourcePlan renders a ResourcePlan with consistent formatting

Types

type Action added in v0.1.1

type Action string

Action represents a standardized action that can be taken on a resource

const (
	ActionCreate    Action = "create"
	ActionUpdate    Action = "update"
	ActionDelete    Action = "delete"
	ActionReconcile Action = "reconcile"
	ActionNoop      Action = "no-op"
)

type DockerClient

type DockerClient interface {
	// Volume operations
	ListVolumes(ctx context.Context) ([]string, error)
	CreateVolume(ctx context.Context, name string, labels map[string]string) error
	RemoveVolume(ctx context.Context, name string) error

	// Volume file operations
	ReadFileFromVolume(ctx context.Context, volumeName, targetPath, relFile string) (string, error)
	WriteFileToVolume(ctx context.Context, volumeName, targetPath, relFile, content string) error
	ExtractTarToVolume(ctx context.Context, volumeName, targetPath string, tarReader io.Reader) error
	RemovePathsFromVolume(ctx context.Context, volumeName, targetPath string, relPaths []string) error

	// Network operations
	ListNetworks(ctx context.Context) ([]string, error)
	CreateNetwork(ctx context.Context, name string, labels map[string]string) error
	RemoveNetwork(ctx context.Context, name string) error

	// Container operations
	ListComposeContainersAll(ctx context.Context) ([]dockercli.PsBrief, error)
	RestartContainer(ctx context.Context, name string) error
	RemoveContainer(ctx context.Context, name string, force bool) error
	UpdateContainerLabels(ctx context.Context, containerName string, labels map[string]string) error
	InspectContainerLabels(ctx context.Context, containerName string, keys []string) (map[string]string, error)

	// Compose operations
	ComposeConfigFull(ctx context.Context, root string, files []string, profiles []string, envFiles []string, inline []string) (dockercli.ComposeConfigDoc, error)
	ComposeConfigServices(ctx context.Context, root string, files []string, profiles []string, envFiles []string, inline []string) ([]string, error)
	ComposeConfigHash(ctx context.Context, root string, files []string, profiles []string, envFiles []string, project, serviceName, identifier string, inline []string) (string, error)
	ComposePs(ctx context.Context, root string, files []string, profiles []string, envFiles []string, project string, inline []string) ([]dockercli.ComposePsItem, error)
	ComposeUp(ctx context.Context, root string, files []string, profiles []string, envFiles []string, project string, inline []string) (string, error)
}

DockerClient defines the interface for Docker operations needed by the planner. This allows for easy mocking in tests and potential future support for other container runtimes.

type FilesetManager

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

FilesetManager handles synchronization of filesets into Docker volumes.

func NewFilesetManager

func NewFilesetManager(planner *Planner) *FilesetManager

NewFilesetManager creates a new fileset manager.

func (*FilesetManager) SyncFilesets

func (fm *FilesetManager) SyncFilesets(ctx context.Context, cfg manifest.Config, existingVolumes map[string]struct{}) (map[string]struct{}, error)

SyncFilesets synchronizes all filesets into their target volumes and returns services that need restart.

type Plan

type Plan struct {
	Resources *ResourcePlan
}

Plan represents a structured plan with resources organized by type

func (*Plan) String

func (pln *Plan) String() string

type Planner

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

Planner creates a plan comparing desired and current docker state.

func New

func New() *Planner

func NewWithDocker

func NewWithDocker(client DockerClient) *Planner

func (*Planner) Apply

func (p *Planner) Apply(ctx context.Context, cfg manifest.Config) error

Apply creates missing top-level resources with labels and performs compose up, labeling containers with identifier.

func (*Planner) BuildPlan

func (p *Planner) BuildPlan(ctx context.Context, cfg manifest.Config) (*Plan, error)

BuildPlan produces a structured plan with resources organized by type.

func (*Planner) Prune

func (p *Planner) Prune(ctx context.Context, cfg manifest.Config) error

Prune removes unmanaged resources labeled with the identifier. It deletes volumes, networks, and containers that are labeled but not present in cfg.

func (*Planner) WithParallel

func (p *Planner) WithParallel(enabled bool) *Planner

WithParallel enables or disables parallel processing for plan building.

func (*Planner) WithPrinter

func (p *Planner) WithPrinter(pr ui.Printer) *Planner

WithPrinter sets the output printer for user-facing messages during apply/prune.

func (*Planner) WithProgress

func (p *Planner) WithProgress(pb *ui.Progress) *Planner

WithProgress sets a progress bar to report stepwise progress during apply.

type ProgressEstimator

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

ProgressEstimator handles estimation of total work items for progress tracking.

func NewProgressEstimator

func NewProgressEstimator(planner *Planner) *ProgressEstimator

NewProgressEstimator creates a new progress estimator.

func (*ProgressEstimator) EstimateAndStartProgress

func (pe *ProgressEstimator) EstimateAndStartProgress(ctx context.Context, cfg manifest.Config, identifier string) error

EstimateAndStartProgress calculates the total number of work items and starts the progress tracker.

type Resource added in v0.1.1

type Resource struct {
	Type       ResourceType
	Name       string        // e.g., "traefik_config" for volumes, "linkwarden/postgres" for services
	Action     Action        // The action to be taken
	Details    string        // Optional details about the action
	Parent     string        // For nested resources (e.g., fileset name for files)
	ChangeType ui.ChangeType // Maps to UI change type for rendering
}

Resource represents a single infrastructure resource with its planned action

func NewNestedResource added in v0.1.1

func NewNestedResource(resType ResourceType, name string, parent string, action Action, details string) Resource

NewNestedResource creates a resource that belongs to a parent (e.g., service in app, file in fileset)

func NewResource added in v0.1.1

func NewResource(resType ResourceType, name string, action Action, details string) Resource

NewResource creates a new resource with the appropriate change type

func (Resource) FormatAction added in v0.1.1

func (r Resource) FormatAction() string

FormatAction returns a human-readable action string

type ResourceManager

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

ResourceManager handles creation of top-level resources like volumes and networks.

func NewResourceManager

func NewResourceManager(planner *Planner) *ResourceManager

NewResourceManager creates a new resource manager.

func (*ResourceManager) EnsureNetworksExist

func (rm *ResourceManager) EnsureNetworksExist(ctx context.Context, cfg manifest.Config, labels map[string]string) error

EnsureNetworksExist creates any missing networks defined in the manifest.

func (*ResourceManager) EnsureVolumesExist

func (rm *ResourceManager) EnsureVolumesExist(ctx context.Context, cfg manifest.Config, labels map[string]string) (map[string]struct{}, error)

EnsureVolumesExist creates any missing volumes derived from filesets and explicit volume definitions.

type ResourcePlan added in v0.1.1

type ResourcePlan struct {
	Volumes      []Resource
	Networks     []Resource
	Applications map[string][]Resource // Application name -> services
	Filesets     map[string][]Resource // Fileset name -> file changes
	Containers   []Resource            // Orphaned containers to remove
}

ResourcePlan represents a structured plan with resources organized by type

func (*ResourcePlan) AllResources added in v0.1.1

func (rp *ResourcePlan) AllResources() []Resource

AllResources returns all resources from the plan as a flat list (for testing)

func (*ResourcePlan) CountActions added in v0.1.1

func (rp *ResourcePlan) CountActions() (create, update, delete int)

CountActions counts the number of each action type in the plan

type ResourceType added in v0.1.1

type ResourceType string

ResourceType represents the type of infrastructure resource

const (
	ResourceVolume    ResourceType = "volume"
	ResourceNetwork   ResourceType = "network"
	ResourceService   ResourceType = "service"
	ResourceContainer ResourceType = "container"
	ResourceFileset   ResourceType = "fileset"
	ResourceFile      ResourceType = "file" // Individual file in a fileset
)

type RestartManager

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

RestartManager handles restarting services after fileset updates.

func NewRestartManager

func NewRestartManager(planner *Planner) *RestartManager

NewRestartManager creates a new restart manager.

func (*RestartManager) RestartPendingServices

func (rm *RestartManager) RestartPendingServices(ctx context.Context, restartPending map[string]struct{}) error

RestartPendingServices restarts all services queued for restart after fileset updates.

type ServiceInfo

type ServiceInfo struct {
	Name        string
	AppName     string
	State       ServiceState
	DesiredHash string
	RunningHash string
	Container   *dockercli.ComposePsItem // nil if not running
}

ServiceInfo contains information about a service's desired and actual state.

type ServiceState

type ServiceState int

ServiceState represents the current state of a service relative to its desired configuration.

const (
	// ServiceMissing indicates the service is not running
	ServiceMissing ServiceState = iota
	// ServiceRunning indicates the service is running and up-to-date
	ServiceRunning
	// ServiceDrifted indicates the service is running but configuration has drifted
	ServiceDrifted
	// ServiceIdentifierMismatch indicates the service is running but has wrong identifier label
	ServiceIdentifierMismatch
)

type ServiceStateDetector

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

ServiceStateDetector handles detection of service state changes.

func NewServiceStateDetector

func NewServiceStateDetector(docker DockerClient) *ServiceStateDetector

NewServiceStateDetector creates a new service state detector.

func (*ServiceStateDetector) BuildInlineEnv

func (d *ServiceStateDetector) BuildInlineEnv(ctx context.Context, app manifest.Application, sopsConfig *manifest.SopsConfig) []string

BuildInlineEnv constructs the inline environment variables for an application, including SOPS secrets.

func (*ServiceStateDetector) DetectAllServicesState

func (d *ServiceStateDetector) DetectAllServicesState(ctx context.Context, appName string, app manifest.Application, identifier string, sopsConfig *manifest.SopsConfig) ([]ServiceInfo, error)

DetectAllServicesState analyzes the state of all services in an application.

func (*ServiceStateDetector) DetectServiceState

func (d *ServiceStateDetector) DetectServiceState(ctx context.Context, serviceName, appName string, app manifest.Application, identifier string, inline []string, running map[string]dockercli.ComposePsItem) (ServiceInfo, error)

DetectServiceState determines the state of a single service.

func (*ServiceStateDetector) GetPlannedServices

func (d *ServiceStateDetector) GetPlannedServices(ctx context.Context, app manifest.Application, inline []string) ([]string, error)

GetPlannedServices returns the list of services defined in the application's compose files.

func (*ServiceStateDetector) GetRunningServices

func (d *ServiceStateDetector) GetRunningServices(ctx context.Context, app manifest.Application, inline []string) (map[string]dockercli.ComposePsItem, error)

GetRunningServices returns a map of currently running services for the application.

func (*ServiceStateDetector) WithParallel

func (d *ServiceStateDetector) WithParallel(enabled bool) *ServiceStateDetector

WithParallel enables or disables parallel processing for service state detection.

Jump to

Keyboard shortcuts

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