sorter

package
v0.0.0-...-5840427 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package sorter provides sorting functionality for Watchtower containers. It implements dependency-based topological sorting and creation time ordering.

Key components:

  • SortByDependencies: Sorts containers in place by links, detecting circular references.
  • SortByCreated: Sorts containers in place by creation time with fallback to current time.
  • Sorter: Common interface for all sorting implementations.

Usage example:

err := sorter.SortByDependencies(containers)
if err != nil {
    logrus.WithError(err).Error("Dependency sort failed")
}

err = sorter.SortByCreated(containers)
if err != nil {
    logrus.WithError(err).Error("Time sort failed")
}

The package uses logrus for logging sort operations and errors.

Index

Constants

This section is empty.

Variables

View Source
var ErrCircularReference = errors.New("circular reference detected")

ErrCircularReference indicates a circular dependency between containers.

View Source
var ErrIdentifierCollision = errors.New("identifier collision detected")

ErrIdentifierCollision indicates an identifier collision between containers.

Functions

func SortByCreated

func SortByCreated(containers []types.Container) error

SortByCreated sorts containers in place by creation time.

Parameters:

  • containers: Slice to sort in place.

Returns:

  • error: propagated from TimeSorter.Sort.

func SortByDependencies

func SortByDependencies(containers []types.Container) error

SortByDependencies sorts containers in place by dependencies.

Parameters:

  • containers: Slice to sort in place.

Returns:

  • error: Non-nil if circular reference detected, nil on success.

Types

type CircularReferenceError

type CircularReferenceError struct {
	ContainerName string
	CyclePath     []string
}

CircularReferenceError represents a circular dependency error with the container name and cycle path.

func (CircularReferenceError) Error

func (e CircularReferenceError) Error() string

Error implements the error interface.

func (CircularReferenceError) Unwrap

func (e CircularReferenceError) Unwrap() error

Unwrap returns the underlying error for errors.Is compatibility.

type DependencySorter

type DependencySorter struct{}

DependencySorter handles topological sorting by dependencies.

func (DependencySorter) Sort

func (ds DependencySorter) Sort(containers []types.Container) error

Sort sorts containers in place by dependencies, placing Watchtower containers last.

This function implements a two-phase sorting strategy to ensure proper update order:

  1. Separate Watchtower containers from regular containers, as Watchtower instances should always be updated last to avoid disrupting the update process itself.
  2. Perform topological sorting on non-Watchtower containers using Kahn's algorithm to respect dependency relationships (containers that depend on others must be updated after their dependencies).

The sorting ensures that: - Dependent containers are updated after their dependencies - Watchtower containers are processed last to maintain monitoring capability - Circular dependencies are detected and reported as errors

Time Complexity: O(V + E) where V is containers and E is dependency links Space Complexity: O(V + E) for graph structures

Parameters:

  • containers: Slice to sort in place. Modified directly.

Returns:

  • error: Non-nil if circular reference detected, nil on success. Error includes the container name and cycle path for debugging.

type IdentifierCollisionError

type IdentifierCollisionError struct {
	DuplicateIdentifier string
	AffectedContainers  []types.Container
}

IdentifierCollisionError represents an error when multiple containers have the same normalized identifier.

func (IdentifierCollisionError) Error

func (e IdentifierCollisionError) Error() string

Error implements the error interface.

func (IdentifierCollisionError) Unwrap

func (e IdentifierCollisionError) Unwrap() error

Unwrap returns the underlying error for errors.Is compatibility.

type Sorter

type Sorter interface {
	Sort(containers []types.Container) error
}

Sorter provides a common interface for sorting containers.

type TimeSorter

type TimeSorter struct{}

TimeSorter sorts containers by creation time.

func (TimeSorter) Sort

func (ts TimeSorter) Sort(containers []types.Container) error

Sort sorts containers in place by creation time, using far future time as fallback for invalid dates.

Parameters:

  • containers: Slice to sort in place.

Returns:

  • error: Always nil (no errors possible).

Directories

Path Synopsis
Package mocks provides mock implementations for container interfaces used in testing.
Package mocks provides mock implementations for container interfaces used in testing.

Jump to

Keyboard shortcuts

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