container

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2023 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package container allows to run and manage multiple containers across multiple hosts, by talking directly to the container runtime on local or remote hosts.

Index

Constants

View Source
const (
	// ConfigMountpoint is where host file-system is mounted in the -config container.
	ConfigMountpoint = "/mnt/host"
)
View Source
const (
	// StatusMissing is a value, which is set to ContainerStatus.Status field,
	// if stored container ID is not found.
	StatusMissing = "gone"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	// Config defines the properties of the container to manage.
	Config types.ContainerConfig `json:"config"`
	// Status stores container status. Setting status allows to manage existing containers
	// and e.g. removing them.
	Status *types.ContainerStatus `json:"status,omitempty"`
	// Runtime stores configuration for various container runtimes.
	Runtime RuntimeConfig `json:"runtime,omitempty"`
}

Container allows managing single container on directly reachable, configured container runtime, for example Docker using 'unix:///run/docker.sock' address.

func (*Container) New added in v0.2.0

func (c *Container) New() (Interface, error)

New creates new instance of container from Container and validates it's configuration. It also validates container runtime configuration.

func (*Container) Validate

func (c *Container) Validate() error

Validate validates container configuration.

type Containers

type Containers struct {
	// PreviousState stores previous state of the containers, which should be obtained and persisted
	// after containers modifications.
	PreviousState ContainersState `json:"previousState,omitempty"`

	// DesiredState is a user-defined desired containers configuration.
	DesiredState ContainersState `json:"desiredState,omitempty"`
}

Containers allow to orchestrate and update multiple containers spread across multiple hosts and update their configurations.

func (*Containers) CheckCurrentState

func (c *Containers) CheckCurrentState() error

CheckCurrentState iterates over containers defined in the state, checks if they exist, are running etc and writes to containers current state. This allows then to compare current state of the containers with desired state, using Containers() method, to check if there are any pending changes to cluster configuration.

Calling CheckCurrentState is required before calling Deploy(), to ensure, that Deploy() executes correct actions.

func (*Containers) Deploy

func (c *Containers) Deploy() error

Deploy creates configured containers.

CheckCurrentState() must be called before calling Deploy(), otherwise error will be returned.

func (*Containers) New

func (c *Containers) New() (ContainersInterface, error)

New validates Containers configuration and returns container object, which can be deployed.

func (*Containers) Validate

func (c *Containers) Validate() error

Validate validates Containers struct and all structs used underneath.

type ContainersInterface

type ContainersInterface interface {
	// CheckCurrentState iterates over containers defined in the state, checks if they exist, are
	// running etc and writes to containers current state. This allows then to compare current state
	// of the containers with desired state, using Containers() method, to check if there are any
	// pending changes to cluster configuration.
	//
	// Calling CheckCurrentState is required before calling Deploy(), to ensure, that Deploy() executes
	// correct actions.
	CheckCurrentState() error

	// Deploy creates configured containers.
	//
	// CheckCurrentState() must be called before calling Deploy(), otherwise error will be returned.
	Deploy() error

	// StateToYaml converts resource's containers state into YAML format and returns it to the user,
	// so it can be persisted, e.g. to the file.
	StateToYaml() ([]byte, error)

	// ToExported converts unexported containers struct into exported one, which can be then
	// serialized and persisted.
	ToExported() *Containers

	// DesiredState returns desired state of configured containers.
	//
	// Desired state differs from
	// exported or user-defined desired state, as it will have container IDs filled from the
	// previous state.
	//
	// All returned containers will also have status set to running, as this is always the desired
	// state of the container.
	//
	// Having those fields modified allows to minimize the difference when comparing previous state
	// and desired state.
	DesiredState() ContainersState
}

ContainersInterface represents capabilities of containers struct.

func FromYaml

func FromYaml(c []byte) (ContainersInterface, error)

FromYaml allows to load containers configuration and state from YAML format.

type ContainersState

type ContainersState map[string]*HostConfiguredContainer

ContainersState represents states of multiple containers.

func (ContainersState) New

New validates ContainersState struct and returns operational containerState.

type ContainersStateInterface

type ContainersStateInterface interface {
	// CheckState updates the state of all previously configured containers
	// and their configuration on the host
	CheckState() error

	// RemoveContainer removes the container by ID.
	RemoveContainer(containerName string) error

	// CreateAndStart is a helper, which creates and spawns given container.
	CreateAndStart(containerName string) error

	// Export converts unexported containersState to exported type, so it can be serialized and stored.
	Export() ContainersState
}

ContainersStateInterface represents 'constainersState' capabilities.

type Hook

type Hook func() error

Hook is an action, which may be called before or after certain container operation, like starting or creating.

type Hooks

type Hooks struct {
	// PostStart hook will be executed after container is started.
	PostStart *Hook
}

Hooks defines type of hooks HostConfiguredContainer supports.

type HostConfiguredContainer

type HostConfiguredContainer struct {
	// Container stores container configuration.
	Container Container `json:"container"`

	// Host defines how to communicate with configured container runtime.
	Host host.Host `json:"host"`

	// ConfigFiles stores a list of configuration files, which should be created
	// on the host, where the container will be created.
	ConfigFiles map[string]string `json:"configFiles,omitempty"`

	// Hooks holds all hooks, which will be triggered after certain container actions.
	//
	// Due to it's nature, it can only be set programmatically.
	Hooks *Hooks `json:"-"`
}

HostConfiguredContainer represents single container, running on remote host with it's configuration files.

func (*HostConfiguredContainer) New

New validates HostConfiguredContainer struct and return the interface implementation, which can be used for deploying the container.

func (*HostConfiguredContainer) Validate

func (m *HostConfiguredContainer) Validate() error

Validate validates HostConfiguredContainer struct. All validation rules should be placed here.

type HostConfiguredContainerInterface

type HostConfiguredContainerInterface interface {
	// ConfigurationStatus updates configuration file struct with current state on the target host.
	ConfigurationStatus() error

	// Configure copies specified configuration files on target host.
	//
	// It uses host definition to connect to container runtime, which is then used
	// to create temporary container used for copying files and also bypassing privileges requirements.
	//
	// With Kubelet runtime, 'tar' binary is required on the container to be able to write and read the configurations.
	// By default, the image which will be deployed will be used for copying the configuration as well, to avoid pulling
	// multiple images, which will save disk space and time. If it happens that this image does not have 'tar' binary,
	// user can override ConfigImage field in the configuration, to specify different image which should be
	// pulled and used for configuration management.
	Configure(paths []string) error

	// Create creates new container on target host. If container already exists,
	// error should be returned.
	Create() error

	// Status updates container status.
	Status() error

	// Start starts created container. Container must be created before it's started.
	Start() error

	// Stop stops the container.
	Stop() error

	// Delete removes the container from the host. Host volumes and configuration files
	// won't be removed.
	Delete() error
}

HostConfiguredContainerInterface defines capabilities of validated HostConfiguredContainer.

type InstanceInterface

type InstanceInterface interface {
	// Status returns container status read from the configured container runtime.
	Status() (types.ContainerStatus, error)

	// Read reads content of the given file paths in the container.
	Read(srcPath []string) ([]*types.File, error)

	// Copy copies file into the container.
	Copy(files []*types.File) error

	// Stat checks if given files exist on the container and returns map of
	// file modes. If key is missing, it means file does not exist in the container.
	Stat(paths []string) (map[string]os.FileMode, error)

	// Start starts the container.
	Start() error

	// Stop stops the container.
	Stop() error

	// Delete deletes the container.
	Delete() error
}

InstanceInterface represents operations, which can be executed on existing container.

type Interface

type Interface interface {
	// Create creates the container.
	Create() (InstanceInterface, error)

	// From status restores container instance from given status.
	FromStatus() (InstanceInterface, error)

	// UpdateStatus updates container status.
	UpdateStatus() error

	// Start starts the container.
	Start() error

	// Stop stops the container.
	Stop() error

	// Delete removes the container.
	Delete() error

	// Status returns container status.
	Status() *types.ContainerStatus

	// Config allows reading container configuration.
	Config() types.ContainerConfig

	// RuntimeConfig allows reading container runtime configuration.
	RuntimeConfig() runtime.Config

	// Runtime allows getting container runtime.
	Runtime() runtime.Runtime

	// SetRuntime allows overriding used container runtime.
	SetRuntime(runtime.Runtime)

	// SetStatus allows overriding container status.
	SetStatus(types.ContainerStatus)
}

Interface represents container capabilities, which may or may not exist.

type ResourceInstance

type ResourceInstance interface {
	ToHostConfiguredContainer() (*HostConfiguredContainer, error)
}

ResourceInstance interface represents struct, which can be converted to HostConfiguredContainer.

type RuntimeConfig

type RuntimeConfig struct {
	// Docker stores Docker runtime configuration.
	Docker *docker.Config `json:"docker,omitempty"`
}

RuntimeConfig is a collection of various runtime configurations which can be defined by user.

Directories

Path Synopsis
Package resource stores a wrapper over container.Containers, which implements types.Resource interface.
Package resource stores a wrapper over container.Containers, which implements types.Resource interface.
Package runtime provides interfaces describing container runtimes in generic way and their functionality.
Package runtime provides interfaces describing container runtimes in generic way and their functionality.
docker
Package docker implements runtime.Interface and runtime.Config interfaces by talking to Docker API.
Package docker implements runtime.Interface and runtime.Config interfaces by talking to Docker API.
Package types contains types used for managing the containers.
Package types contains types used for managing the containers.

Jump to

Keyboard shortcuts

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