ctrd

package
v0.0.0-...-7ef7dd6 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2018 License: Apache-2.0 Imports: 51 Imported by: 0

README

ctrd

ctrd is a package to interact with containerd using containerd's exposed API. Pouch will vendor package containerd in repo containerd/containerd and alibaba/containerd.

difference between containerds

First, we should said alibaba/containerd is definitely forked from upstream project containerd/containerd.

containerd/containerd definitely has the code base of the other one. Then what is the usage of alibaba/containerd. We say it is BACK PORTING.

Every time releasing Pouch to launch a new version, we vendor containerd/containerd. Since upstream containerd/containerd keeps its own way to evolve, when a serious bug is fixed in the upstream and Pouch needs this for backporting only, it is not proper to vendor the latest container/containerd which contains too many changes. As a result, we come up with a way to backport severe bugfix in alibaba/containerd. Then when backporting, we vendor alibaba/containerd, otherwise we still take advantages of upstream containerd/containerd.

To check if the package we use is containerd/containerd or alibaba/containerd, we can refer to vendor.json.

Documentation

Index

Constants

View Source
const (
	// ContainersCreateEventTopic for container create
	ContainersCreateEventTopic = "/containers/create"
	// ContainersDeleteEventTopic for container delete
	ContainersDeleteEventTopic = "/containers/delete"

	// TaskCreateEventTopic for task create
	TaskCreateEventTopic = runtime.TaskCreateEventTopic
	// TaskDeleteEventTopic for task delete
	TaskDeleteEventTopic = runtime.TaskDeleteEventTopic
	// TaskExitEventTopic for task exit
	TaskExitEventTopic = runtime.TaskExitEventTopic
	// TaskOOMEventTopic for task oom
	TaskOOMEventTopic = runtime.TaskOOMEventTopic
)

Variables

View Source
var ErrGetCtrdClient = errors.New("failed to get a containerd grpc client")

ErrGetCtrdClient is an error returned when failed to get a containerd grpc client from clients pool.

Functions

func NewDefaultSpec

func NewDefaultSpec(ctx context.Context, id string) (*specs.Spec, error)

NewDefaultSpec new a template spec with default.

func SetImageProxy

func SetImageProxy(p string)

SetImageProxy sets value of image http proxy

Types

type APIClient

type APIClient interface {
	ContainerAPIClient
	ImageAPIClient
	SnapshotAPIClient

	Version(ctx context.Context) (containerd.Version, error)
	Cleanup() error
}

APIClient defines common methods of containerd api client

func NewClient

func NewClient(homeDir string, opts ...ClientOpt) (APIClient, error)

NewClient connect to containerd.

type Client

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

Client is the client side the daemon holds to communicate with containerd.

func (*Client) Cleanup

func (c *Client) Cleanup() error

Cleanup handle containerd instance exits.

func (*Client) Close

func (c *Client) Close() error

Close closes the client.

func (*Client) ContainerPID

func (c *Client) ContainerPID(ctx context.Context, id string) (int, error)

ContainerPID returns the container's init process id.

func (*Client) ContainerPIDs

func (c *Client) ContainerPIDs(ctx context.Context, id string) ([]int, error)

ContainerPIDs returns the all processes's ids inside the container.

func (*Client) ContainerStats

func (c *Client) ContainerStats(ctx context.Context, id string) (*containerdtypes.Metric, error)

ContainerStats returns stats of the container.

func (*Client) CreateCheckpoint

func (c *Client) CreateCheckpoint(ctx context.Context, id string, checkpointDir string, exit bool) error

CreateCheckpoint create a checkpoint from a running container

func (*Client) CreateContainer

func (c *Client) CreateContainer(ctx context.Context, container *Container, checkpointDir string) error

CreateContainer create container and start process.

func (*Client) CreateImageReference

func (c *Client) CreateImageReference(ctx context.Context, img ctrdmetaimages.Image) (ctrdmetaimages.Image, error)

CreateImageReference creates the image in the meta data in the containerd.

func (*Client) CreateSnapshot

func (c *Client) CreateSnapshot(ctx context.Context, id, ref string) error

CreateSnapshot creates a active snapshot with image's name and id.

func (*Client) DestroyContainer

func (c *Client) DestroyContainer(ctx context.Context, id string, timeout int64) (*Message, error)

DestroyContainer kill container and delete it.

func (*Client) Events

Events subscribe containerd events through an event subscribe client.

func (*Client) ExecContainer

func (c *Client) ExecContainer(ctx context.Context, process *Process) error

ExecContainer executes a process in container.

func (*Client) Get

func (c *Client) Get(ctx context.Context) (*WrapperClient, error)

Get will reture an available containerd grpc client, Or occurred an error

func (*Client) GetImage

func (c *Client) GetImage(ctx context.Context, ref string) (containerd.Image, error)

GetImage returns the containerd's Image.

func (*Client) GetMounts

func (c *Client) GetMounts(ctx context.Context, id string) ([]mount.Mount, error)

GetMounts returns the mounts for the active snapshot transaction identified by key.

func (*Client) GetSnapshot

func (c *Client) GetSnapshot(ctx context.Context, id string) (snapshots.Info, error)

GetSnapshot returns the snapshot's info by id.

func (*Client) GetSnapshotUsage

func (c *Client) GetSnapshotUsage(ctx context.Context, id string) (snapshots.Usage, error)

GetSnapshotUsage returns the resource usage of an active or committed snapshot excluding the usage of parent snapshots.

func (*Client) ImportImage

func (c *Client) ImportImage(ctx context.Context, importer ctrdmetaimages.Importer, reader io.Reader) ([]containerd.Image, error)

ImportImage creates a set of images by tarstream.

NOTE: One tar may have several manifests.

func (*Client) ListImages

func (c *Client) ListImages(ctx context.Context, filter ...string) ([]containerd.Image, error)

ListImages lists all images.

func (*Client) PauseContainer

func (c *Client) PauseContainer(ctx context.Context, id string) error

PauseContainer pauses container.

func (*Client) ProbeContainer

func (c *Client) ProbeContainer(ctx context.Context, id string, timeout time.Duration) *Message

ProbeContainer probe the container's status, if timeout <= 0, will block to receive message.

func (*Client) PullImage

func (c *Client) PullImage(ctx context.Context, ref string, authConfig *types.AuthConfig, stream *jsonstream.JSONStream) (containerd.Image, error)

PullImage downloads an image from the remote repository.

func (*Client) RecoverContainer

func (c *Client) RecoverContainer(ctx context.Context, id string, io *containerio.IO) error

RecoverContainer reload the container from metadata and watch it, if program be restarted.

func (*Client) RemoveImage

func (c *Client) RemoveImage(ctx context.Context, ref string) error

RemoveImage deletes an image.

func (*Client) RemoveSnapshot

func (c *Client) RemoveSnapshot(ctx context.Context, id string) error

RemoveSnapshot removes the snapshot by id.

func (*Client) ResizeContainer

func (c *Client) ResizeContainer(ctx context.Context, id string, opts types.ResizeOptions) error

ResizeContainer changes the size of the TTY of the init process running in the container to the given height and width.

func (*Client) ResizeExec

func (c *Client) ResizeExec(ctx context.Context, id string, execid string, opts types.ResizeOptions) error

ResizeExec changes the size of the TTY of the exec process running in the container to the given height and width.

func (*Client) SaveImage

func (c *Client) SaveImage(ctx context.Context, exporter ctrdmetaimages.Exporter, ref string) (io.ReadCloser, error)

SaveImage saves image to tarstream

func (*Client) SetEventsHooks

func (c *Client) SetEventsHooks(hooks ...func(context.Context, string, string, map[string]string) error)

SetEventsHooks specified the methods to handle the containerd events.

func (*Client) SetExecExitHooks

func (c *Client) SetExecExitHooks(hooks ...func(string, *Message) error)

SetExecExitHooks specified the handlers of exec process exit.

func (*Client) SetExitHooks

func (c *Client) SetExitHooks(hooks ...func(string, *Message) error)

SetExitHooks specified the handlers of container exit.

func (*Client) UnpauseContainer

func (c *Client) UnpauseContainer(ctx context.Context, id string) error

UnpauseContainer unpauses container.

func (*Client) UpdateResources

func (c *Client) UpdateResources(ctx context.Context, id string, resources types.Resources) error

UpdateResources updates the configurations of a container.

func (*Client) Version

func (c *Client) Version(ctx context.Context) (containerd.Version, error)

Version returns the version of containerd.

func (*Client) WaitContainer

func (c *Client) WaitContainer(ctx context.Context, id string) (types.ContainerWaitOKBody, error)

WaitContainer waits until container's status is stopped.

func (*Client) WalkSnapshot

func (c *Client) WalkSnapshot(ctx context.Context, fn func(context.Context, snapshots.Info) error) error

WalkSnapshot walk all snapshots. For each snapshot, the function will be called.

type ClientOpt

type ClientOpt func(c *clientOpts) error

ClientOpt allows caller to set options for containerd client.

func WithContainerdBinary

func WithContainerdBinary(containerdBinary string) ClientOpt

WithContainerdBinary specifies the containerd binary path.

func WithDebugLog

func WithDebugLog(debugLog bool) ClientOpt

WithDebugLog set debugLog flag for containerd client. debugLog decides containerd log level.

func WithDefaultNamespace

func WithDefaultNamespace(ns string) ClientOpt

WithDefaultNamespace sets the default namespace on the client

Any operation that does not have a namespace set on the context will be provided the default namespace

func WithGrpcClientPoolCapacity

func WithGrpcClientPoolCapacity(grpcClientPoolCapacity int) ClientOpt

WithGrpcClientPoolCapacity sets containerd clients pool capacity.

func WithHomeDir

func WithHomeDir(homeDir string) ClientOpt

WithHomeDir set home dir for containerd.

func WithMaxStreamsClient

func WithMaxStreamsClient(maxStreamsClient int) ClientOpt

WithMaxStreamsClient sets one containerd grpc client can hold max streams client.

func WithOOMScoreAdjust

func WithOOMScoreAdjust(oomScore int) ClientOpt

WithOOMScoreAdjust sets oom-score for containerd instance.

func WithRPCAddr

func WithRPCAddr(rpcAddr string) ClientOpt

WithRPCAddr set containerd listen address.

func WithStartDaemon

func WithStartDaemon(startDaemon bool) ClientOpt

WithStartDaemon set startDaemon flag for containerd client. startDaemon is a flag to decide whether start a new containerd instance when create a containerd client.

type Container

type Container struct {
	ID      string
	Image   string
	Runtime string
	Labels  map[string]string
	IO      *containerio.IO
	Spec    *specs.Spec

	// BaseFS is rootfs used by containerd container
	BaseFS string

	// RootFSProvided is a flag to point the container is created by specifying rootfs
	RootFSProvided bool
}

Container wraps container's info. there have two kind of containers now: One is created by pouch: first using image to create snapshot, then create container by specifying the snapshot; The other is create container by specify container rootfs, we use `RootFSProvided` flag to mark it,

type ContainerAPIClient

type ContainerAPIClient interface {
	// CreateContainer creates a containerd container and start process.
	CreateContainer(ctx context.Context, container *Container, checkpointDir string) error
	// DestroyContainer kill container and delete it.
	DestroyContainer(ctx context.Context, id string, timeout int64) (*Message, error)
	// ProbeContainer probe the container's status, if timeout <= 0, will block to receive message.
	ProbeContainer(ctx context.Context, id string, timeout time.Duration) *Message
	// ContainerPIDs returns the all processes's ids inside the container.
	ContainerPIDs(ctx context.Context, id string) ([]int, error)
	// ContainerPID returns the container's init process id.
	ContainerPID(ctx context.Context, id string) (int, error)
	// ContainerStats returns stats of the container.
	ContainerStats(ctx context.Context, id string) (*containerdtypes.Metric, error)
	// ExecContainer executes a process in container.
	ExecContainer(ctx context.Context, process *Process) error
	// ResizeContainer changes the size of the TTY of the exec process running
	// in the container to the given height and width.
	ResizeExec(ctx context.Context, id string, execid string, opts types.ResizeOptions) error
	// RecoverContainer reload the container from metadata and watch it, if program be restarted.
	RecoverContainer(ctx context.Context, id string, io *containerio.IO) error
	// PauseContainer pause container.
	PauseContainer(ctx context.Context, id string) error
	// UnpauseContainer unpauses a container.
	UnpauseContainer(ctx context.Context, id string) error
	// ResizeContainer changes the size of the TTY of the init process running
	// in the container to the given height and width.
	ResizeContainer(ctx context.Context, id string, opts types.ResizeOptions) error
	// WaitContainer waits until container's status is stopped.
	WaitContainer(ctx context.Context, id string) (types.ContainerWaitOKBody, error)
	// UpdateResources updates the configurations of a container.
	UpdateResources(ctx context.Context, id string, resources types.Resources) error
	// SetExitHooks specified the handlers of container exit.
	SetExitHooks(hooks ...func(string, *Message) error)
	// SetExecExitHooks specified the handlers of exec process exit.
	SetExecExitHooks(hooks ...func(string, *Message) error)
	// Events subscribe containerd events through an event subscribe client.
	Events(ctx context.Context, ef ...string) (eventsapi.Events_SubscribeClient, error)
	// SetEventsHooks specified the methods to handle the containerd events.
	SetEventsHooks(hooks ...func(context.Context, string, string, map[string]string) error)
}

ContainerAPIClient provides access to containerd container features.

type ImageAPIClient

type ImageAPIClient interface {
	// CreateImageReference creates the image data into meta data in the containerd.
	CreateImageReference(ctx context.Context, img ctrdmetaimages.Image) (ctrdmetaimages.Image, error)
	// GetImage returns containerd.Image by the given reference.
	GetImage(ctx context.Context, ref string) (containerd.Image, error)
	// ListImages returns the list of containerd.Image filtered by the given conditions.
	ListImages(ctx context.Context, filter ...string) ([]containerd.Image, error)
	// PullImage pulls image by the given reference.
	PullImage(ctx context.Context, ref string, authConfig *types.AuthConfig, stream *jsonstream.JSONStream) (containerd.Image, error)
	// RemoveImage removes the image by the given reference.
	RemoveImage(ctx context.Context, ref string) error
	// ImportImage creates a set of images by tarstream.
	ImportImage(ctx context.Context, importer ctrdmetaimages.Importer, reader io.Reader) ([]containerd.Image, error)
	// SaveImage saves image to tarstream
	SaveImage(ctx context.Context, exporter ctrdmetaimages.Exporter, ref string) (io.ReadCloser, error)
}

ImageAPIClient provides access to containerd image features.

type Message

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

Message is used to watch containerd.

func (*Message) ExitCode

func (m *Message) ExitCode() uint32

ExitCode returns the exit code in Message.

func (*Message) ExitTime

func (m *Message) ExitTime() time.Time

ExitTime returns the exit time in Message.

func (*Message) RawError

func (m *Message) RawError() error

RawError returns the error contained in Message.

type Process

type Process struct {
	ContainerID string
	ExecID      string
	IO          *containerio.IO
	P           *specs.Process
}

Process wraps exec process's info.

type SnapshotAPIClient

type SnapshotAPIClient interface {
	// CreateSnapshot creates a active snapshot with image's name and id.
	CreateSnapshot(ctx context.Context, id, ref string) error
	// GetSnapshot returns the snapshot's info by id.
	GetSnapshot(ctx context.Context, id string) (snapshots.Info, error)
	// RemoveSnapshot removes the snapshot by id.
	RemoveSnapshot(ctx context.Context, id string) error
	// GetMounts returns the mounts for the active snapshot transaction identified
	// by key.
	GetMounts(ctx context.Context, id string) ([]mount.Mount, error)
	// GetSnapshotUsage returns the resource usage of an active or committed snapshot
	// excluding the usage of parent snapshots.
	GetSnapshotUsage(ctx context.Context, id string) (snapshots.Usage, error)
	// WalkSnapshot walk all snapshots. For each snapshot, the function will be called.
	WalkSnapshot(ctx context.Context, fn func(context.Context, snapshots.Info) error) error
	// CreateCheckpoint creates a checkpoint from a running container
	CreateCheckpoint(ctx context.Context, id string, checkpointDir string, exit bool) error
}

SnapshotAPIClient provides access to containerd snapshot features

type WrapperClient

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

WrapperClient wrappers containerd grpc client, so that pouch daemon can holds a grpc client pool to improve grpc client performance.

func (*WrapperClient) Consume

func (w *WrapperClient) Consume(v int) error

Consume is to acquire specified numbers of grpc stream client

func (*WrapperClient) Produce

func (w *WrapperClient) Produce(v int)

Produce is to release specified numbers of grpc stream client FIXME(ziren): if streamQuota greater than defaultMaxStreamsClient what to do ???

func (*WrapperClient) Value

func (w *WrapperClient) Value() int

Value is to get the quota

Jump to

Keyboard shortcuts

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