container

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: Apache-2.0 Imports: 16 Imported by: 3

Documentation

Overview

Package container provides an interface for interacting with Docker and potentially other container runtimes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RuntimeInto

func RuntimeInto(ctx context.Context, runtime Runtime) context.Context

RuntimeInto is used to store the container runtime client into a context.

Types

type Container

type Container struct {
	// Name is the name of the container
	Name string
	// Image is the name of the container's image
	Image string
	// Status is the status of the container
	Status string
}

Container represents a runtime container.

type ExecContainerArgs

type ExecContainerArgs struct {
	ContainerName string
	Config        *ExecContainerInput
	Command       string
	Args          []string
}

ExecContainerArgs contains the arguments passed to calls to ExecContainer.

type ExecContainerInput

type ExecContainerInput struct {
	// OutputBuffer receives the stdout of the execution.
	OutputBuffer io.Writer
	// ErrorBuffer receives the stderr of the execution.
	ErrorBuffer io.Writer
	// InputBuffer contains stdin or nil if no input.
	InputBuffer io.Reader
	// EnvironmentVars is a collection of name=values to pass as environment variables in the container.
	EnvironmentVars []string
}

ExecContainerInput contains values for running exec on a container.

type FakeRuntime

type FakeRuntime struct {
}

func (*FakeRuntime) ContainerDebugInfo

func (f *FakeRuntime) ContainerDebugInfo(ctx context.Context, containerName string, w io.Writer) error

ContainerDebugInfo gets the container metadata and logs from the runtime (docker inspect, docker logs).

func (*FakeRuntime) DeleteContainer

func (f *FakeRuntime) DeleteContainer(ctx context.Context, containerName string) error

DeleteContainer will remove a container, forcing removal if still running.

func (*FakeRuntime) DeleteContainerCalls

func (f *FakeRuntime) DeleteContainerCalls() []string

DeleteContainerCalls returns the list of containerName arguments passed to calls to DeleteContainer.

func (*FakeRuntime) ExecContainer

func (f *FakeRuntime) ExecContainer(ctx context.Context, containerName string, config *ExecContainerInput, command string, args ...string) error

ExecContainer executes a command in a running container and writes any output to the provided writer.

func (*FakeRuntime) ExecContainerCalls

func (f *FakeRuntime) ExecContainerCalls() []ExecContainerArgs

ExecContainerCalls returns the set of arguments that have been passed to the ExecContainer method. This can be used by test code to validate the expected input.

func (*FakeRuntime) GetContainerIPs

func (f *FakeRuntime) GetContainerIPs(ctx context.Context, containerName string) (string, string, error)

GetContainerIPs inspects a container to get its IPv4 and IPv6 IP addresses. Will not error if there is no IP address assigned. Calling code will need to determine whether that is an issue or not.

func (*FakeRuntime) GetHostPort

func (f *FakeRuntime) GetHostPort(ctx context.Context, containerName, portAndProtocol string) (string, error)

GetHostPort looks up the host port bound for the port and protocol (e.g. "6443/tcp").

func (*FakeRuntime) ImageExistsLocally

func (f *FakeRuntime) ImageExistsLocally(ctx context.Context, image string) (bool, error)

ImageExistsLocally returns if the specified image exists in local container image cache.

func (*FakeRuntime) KillContainer

func (f *FakeRuntime) KillContainer(ctx context.Context, containerName, signal string) error

KillContainer will kill a running container with the specified signal.

func (*FakeRuntime) KillContainerCalls

func (f *FakeRuntime) KillContainerCalls() []KillContainerArgs

KillContainerCalls returns the list of arguments passed to calls to the KillContainer method.

func (*FakeRuntime) ListContainers

func (f *FakeRuntime) ListContainers(ctx context.Context, filters FilterBuilder) ([]Container, error)

ListContainers returns a list of all containers.

func (*FakeRuntime) PullContainerImage

func (f *FakeRuntime) PullContainerImage(ctx context.Context, image string) error

PullContainerImage triggers the Docker engine to pull an image.

func (*FakeRuntime) PullContainerImageIfNotExists

func (f *FakeRuntime) PullContainerImageIfNotExists(ctx context.Context, image string) error

PullContainerImageIfNotExists triggers the Docker engine to pull an image, but only if it doesn't already exist. This is important when we're using locally built images in CI which do not exist remotely.

func (*FakeRuntime) ResetDeleteContainerCallLogs

func (f *FakeRuntime) ResetDeleteContainerCallLogs()

ResetDeleteContainerCallLogs clears all existing records of any calls to the DeleteContainer method.

func (*FakeRuntime) ResetExecContainerCallLogs

func (f *FakeRuntime) ResetExecContainerCallLogs()

ResetExecContainerCallLogs clears all existing records of any calls to the ExecContainer method.

func (*FakeRuntime) ResetKillContainerCallLogs

func (f *FakeRuntime) ResetKillContainerCallLogs()

ResetKillContainerCallLogs clears all existing records of any calls to the KillContainer method.

func (*FakeRuntime) ResetRunContainerCallLogs

func (f *FakeRuntime) ResetRunContainerCallLogs()

ResetRunContainerCallLogs clears all existing records of calls to the RunContainer method.

func (*FakeRuntime) RunContainer

func (f *FakeRuntime) RunContainer(ctx context.Context, runConfig *RunContainerInput, output io.Writer) error

RunContainer will run a docker container with the given settings and arguments, returning any errors.

func (*FakeRuntime) RunContainerCalls

func (f *FakeRuntime) RunContainerCalls() []RunContainerArgs

RunContainerCalls returns a list of arguments passed in to all calls to RunContainer.

func (*FakeRuntime) SaveContainerImage

func (f *FakeRuntime) SaveContainerImage(ctx context.Context, image, dest string) error

SaveContainerImage saves a Docker image to the file specified by dest.

type FilterBuilder

type FilterBuilder map[string]map[string][]string

FilterBuilder is a helper for building up filter strings of "key=value" or "key=name=value".

func (FilterBuilder) AddKeyNameValue

func (f FilterBuilder) AddKeyNameValue(key, name, value string)

AddKeyNameValue adds a filter with a name=value (--filter "label=io.x-k8s.kind.cluster=quick-start-n95t5z").

func (FilterBuilder) AddKeyValue

func (f FilterBuilder) AddKeyValue(key, value string)

AddKeyValue adds a filter with a single name (--filter "label=io.x-k8s.kind.cluster").

type KillContainerArgs

type KillContainerArgs struct {
	Container string
	Signal    string
}

KillContainerArgs contains the arguments passed to calls to Kill.

type Mount

type Mount struct {
	// Source is the source host path to mount.
	Source string
	// Target is the path to mount in the container.
	Target string
	// ReadOnly specifies if the mount should be mounted read only.
	ReadOnly bool
}

Mount contains mount details.

type PortMapping

type PortMapping struct {
	// ContainerPort is the port in the container to map to.
	ContainerPort int32
	// HostPort is the port to expose on the host.
	HostPort int32
	// ListenAddress is the address to bind to.
	ListenAddress string
	// Protocol is the protocol (tcp, udp, etc.) to use.
	Protocol string
}

PortMapping contains port mapping information for the container.

type RunContainerArgs

type RunContainerArgs struct {
	RunConfig *RunContainerInput
	Output    io.Writer
}

RunContainerArgs contains the arguments passed to calls to RunContainer.

type RunContainerInput

type RunContainerInput struct {
	// Image is the name of the image to run.
	Image string
	// Name is the name to set for the container.
	Name string
	// Network is the name of the network to connect to.
	Network string
	// User is the user name to run as.
	User string
	// Group is the user group to run as.
	Group string
	// Volumes is a collection of any volumes (docker's "-v" arg) to mount in the container.
	Volumes map[string]string
	// Tmpfs is the temporary filesystem mounts to add.
	Tmpfs map[string]string
	// Mount contains mount information for the container.
	Mounts []Mount
	// EnvironmentVars is a collection of name/values to pass as environment variables in the container.
	EnvironmentVars map[string]string
	// CommandArgs is the command and any additional arguments to execute in the container.
	CommandArgs []string
	// Entrypoint defines the entry point to use.
	Entrypoint []string
	// Labels to apply to the container.
	Labels map[string]string
	// PortMappings contains host<>container ports to map.
	PortMappings []PortMapping
	// IPFamily is the IP version to use.
	IPFamily clusterv1.ClusterIPFamily
}

RunContainerInput holds the configuration settings for running a container.

type Runtime

type Runtime interface {
	SaveContainerImage(ctx context.Context, image, dest string) error
	PullContainerImageIfNotExists(ctx context.Context, image string) error
	PullContainerImage(ctx context.Context, image string) error
	ImageExistsLocally(ctx context.Context, image string) (bool, error)
	GetHostPort(ctx context.Context, containerName, portAndProtocol string) (string, error)
	GetContainerIPs(ctx context.Context, containerName string) (string, string, error)
	ExecContainer(ctx context.Context, containerName string, config *ExecContainerInput, command string, args ...string) error
	RunContainer(ctx context.Context, runConfig *RunContainerInput, output io.Writer) error
	ListContainers(ctx context.Context, filters FilterBuilder) ([]Container, error)
	ContainerDebugInfo(ctx context.Context, containerName string, w io.Writer) error
	DeleteContainer(ctx context.Context, containerName string) error
	KillContainer(ctx context.Context, containerName, signal string) error
}

Runtime defines the interface for interacting with a container runtime.

func NewDockerClient

func NewDockerClient() (Runtime, error)

NewDockerClient gets a client for interacting with a Docker container runtime.

func NewFakeClient

func NewFakeClient() (Runtime, error)

NewFakeClient gets a client for testing.

func RuntimeFrom

func RuntimeFrom(ctx context.Context) (Runtime, error)

RuntimeFrom is used to extract the container runtime client from a context. If there is no runtime present, it will return nil.

Jump to

Keyboard shortcuts

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