engine

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package engine resolves which endpoint layerx should talk to for a given container engine (Docker, Podman).

Container engines have their own notion of "which daemon am I currently talking to": Docker has contexts (`docker context use my-remote`), Podman has connections (`podman system connection default staging`). A tool that speaks the Docker Engine REST API but ignores those files ends up talking to a different daemon than the user's own CLI would — either failing to connect or, worse, silently inspecting the wrong image on the wrong host.

The Resolver interface here is the single place layerx asks "for engine E, what's the active endpoint on this machine, and how did we pick it?". Adding support for a future engine means implementing one more Resolver; nothing in cmd/ needs to change beyond wiring the new engine name in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DockerResolver

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

DockerResolver reports the endpoint the Docker CLI would talk to, given the current process environment and ~/.docker/ layout.

Resolution order, mirroring `docker` itself:

  1. DOCKER_HOST — the explicit override every Docker script already uses
  2. DOCKER_CONTEXT — env-selected context (overrides config.json "currentContext")
  3. currentContext in ~/.docker/config.json
  4. no active endpoint (Endpoint{}, nil)

The zero value is a Docker resolver backed by os.Getenv and the real filesystem; the env/files fields are only overridden in tests.

func NewDockerResolver

func NewDockerResolver() *DockerResolver

NewDockerResolver returns a resolver using the real process environment and filesystem. Test constructors go through newDockerResolverWithDeps.

func (*DockerResolver) Name

func (r *DockerResolver) Name() string

func (*DockerResolver) Resolve

func (r *DockerResolver) Resolve() (Endpoint, error)

type Endpoint

type Endpoint struct {
	Host   string
	Source string
}

Endpoint is a resolved daemon target for one engine.

Host is the URL the moby client understands (unix://, npipe://, tcp://, ssh://). Source describes where the value came from ("env:DOCKER_HOST", "docker-context:my-remote", "podman-connection:staging"); it is intended for human-readable diagnostic output, not for programmatic dispatch.

The zero value means "no active endpoint configured" — a legitimate outcome, distinct from an error. Callers should treat it as a signal to fall back to the historical socket-probing path so users who have never touched contexts or connections see no behaviour change.

func (Endpoint) IsZero

func (e Endpoint) IsZero() bool

type ErrConfigMalformed

type ErrConfigMalformed struct {
	Engine string
	Path   string
	Cause  error
}

ErrConfigMalformed is returned when a config file exists but cannot be parsed. Distinct from "no config present" (Endpoint{}, nil) so callers can surface a specific message instead of silently falling back to the historical socket probe on a user's broken JSON/TOML.

func (*ErrConfigMalformed) Error

func (e *ErrConfigMalformed) Error() string

func (*ErrConfigMalformed) Unwrap

func (e *ErrConfigMalformed) Unwrap() error

type ErrConnectionNotFound

type ErrConnectionNotFound struct {
	Engine    string
	Requested string
	Available []string
	// Origin describes where the requested name came from ("env" or
	// "active default in config"), so the message can distinguish a bad
	// env var from a stale config default.
	Origin string
}

ErrConnectionNotFound is returned when the user has explicitly named a context/connection (via DOCKER_CONTEXT / CONTAINER_CONNECTION or as the active default) that is not present in the on-disk config. Distinct from ErrConfigMalformed and from the zero Endpoint: the config is fine, the name just doesn't resolve. Available lists the names that DO exist, so the error can surface them without the caller re-reading the file.

func (*ErrConnectionNotFound) Error

func (e *ErrConnectionNotFound) Error() string

type PodmanResolver

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

PodmanResolver reports the endpoint the Podman CLI would talk to, given the current process environment and ~/.config/containers/ layout.

Resolution order, mirroring `podman` itself:

  1. CONTAINER_HOST — Podman's explicit override
  2. DOCKER_HOST — accepted for back-compat with layerx pre-context users who scripted DOCKER_HOST=$(podman system connection list ...) already
  3. CONTAINER_CONNECTION — env-selected connection name
  4. Default connection in ~/.config/containers/podman-connections.json (Podman 4.4+; the current source of truth)
  5. active_service in ~/.config/containers/containers.conf (older Podman)
  6. no active endpoint (Endpoint{}, nil)

SSH-only connection URIs are returned verbatim. layerx's moby/moby/client consumer either dials them via the SDK's ssh:// helper or errors out at connect time — either way the user sees a real message pointing at the exact URI, not a silent connection to the local socket.

func NewPodmanResolver

func NewPodmanResolver() *PodmanResolver

func (*PodmanResolver) Name

func (r *PodmanResolver) Name() string

func (*PodmanResolver) Resolve

func (r *PodmanResolver) Resolve() (Endpoint, error)

type Resolver

type Resolver interface {
	// Name is the short engine identifier ("docker", "podman") used in log
	// lines and error messages.
	Name() string

	// Resolve returns the active endpoint or the zero Endpoint when no
	// context/connection is configured for this engine.
	//
	// Implementations MUST honour engine-native environment overrides
	// (DOCKER_HOST / DOCKER_CONTEXT for Docker, CONTAINER_HOST /
	// CONTAINER_CONNECTION for Podman) so scripting and CI workflows that
	// already export those variables keep working unchanged.
	//
	// A malformed config file returns ErrConfigMalformed with enough
	// context to point the user at the offending file. Missing config
	// files are not errors — they return the zero Endpoint.
	Resolve() (Endpoint, error)
}

Resolver reports the currently active endpoint for one engine on this machine. Implementations must not touch the network; they read only local config files and environment variables.

Jump to

Keyboard shortcuts

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