diff --git a/ctrd/client.go b/ctrd/client.go index 19e54ac05..4fc227e24 100644 --- a/ctrd/client.go +++ b/ctrd/client.go @@ -4,7 +4,6 @@ import ( "context" "github.com/containerd/containerd" - "github.com/containerd/containerd/version" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -33,7 +32,7 @@ type Client struct { } // NewClient connect to containerd. -func NewClient(cfg Config) (*Client, error) { +func NewClient(cfg Config) (APIClient, error) { if cfg.Address == "" { cfg.Address = unixSocketPath } @@ -97,6 +96,6 @@ func (c *Client) Close() error { } // Version returns the version of containerd. -func (c *Client) Version() (string, error) { - return version.Version, nil +func (c *Client) Version(ctx context.Context) (containerd.Version, error) { + return c.client.Version(ctx) } diff --git a/ctrd/interface.go b/ctrd/interface.go new file mode 100644 index 000000000..0e296527d --- /dev/null +++ b/ctrd/interface.go @@ -0,0 +1,76 @@ +package ctrd + +import ( + "context" + "time" + + "github.com/alibaba/pouch/apis/types" + "github.com/alibaba/pouch/daemon/containerio" + "github.com/alibaba/pouch/pkg/jsonstream" + + "github.com/containerd/containerd" + "github.com/containerd/containerd/snapshots" + "github.com/opencontainers/image-spec/specs-go/v1" +) + +// APIClient defines common methods of containerd api client +type APIClient interface { + ContainerAPIClient + ImageAPIClient + SnapshotAPIClient + + Version(ctx context.Context) (containerd.Version, error) +} + +// ContainerAPIClient provides access to containerd container features. +type ContainerAPIClient interface { + // CreateContainer creates a containerd container and start process. + CreateContainer(ctx context.Context, container *Container) 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) + // ExecContainer executes a process in container. + ExecContainer(ctx context.Context, process *Process) 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 + // 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) +} + +// ImageAPIClient provides access to containerd image features. +type ImageAPIClient interface { + // GetOciImage returns the OCI Image. + GetOciImage(ctx context.Context, ref string) (v1.Image, error) + // RemoveImage deletes an image. + RemoveImage(ctx context.Context, ref string) error + // ListImages lists all images. + ListImages(ctx context.Context, filter ...string) ([]types.ImageInfo, error) + // PullImage downloads an image from the remote repository. + PullImage(ctx context.Context, ref string, authConfig *types.AuthConfig, stream *jsonstream.JSONStream) (types.ImageInfo, error) +} + +// SnapshotAPIClient provides access to containerd snapshot features +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 +} diff --git a/daemon/daemon.go b/daemon/daemon.go index 91d6f3130..751c6d1d4 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -26,7 +26,7 @@ import ( type Daemon struct { config config.Config containerStore *meta.Store - containerd *ctrd.Client + containerd ctrd.APIClient containerMgr mgr.ContainerMgr systemMgr mgr.SystemMgr imageMgr mgr.ImageMgr @@ -255,7 +255,7 @@ func (d *Daemon) NetMgr() mgr.NetworkMgr { } // Containerd gets containerd client. -func (d *Daemon) Containerd() *ctrd.Client { +func (d *Daemon) Containerd() ctrd.APIClient { return d.containerd } diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index b3d89b49e..c69644c79 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -98,7 +98,7 @@ type ContainerManager struct { Store *meta.Store // Client is used to interact with containerd. - Client *ctrd.Client + Client ctrd.APIClient // NameToID stores relations between container's name and ID. // It is used to get container ID via container name. @@ -123,7 +123,7 @@ type ContainerManager struct { } // NewContainerManager creates a brand new container manager. -func NewContainerManager(ctx context.Context, store *meta.Store, cli *ctrd.Client, imgMgr ImageMgr, volMgr VolumeMgr, netMgr NetworkMgr, cfg *config.Config, contPlugin plugins.ContainerPlugin) (*ContainerManager, error) { +func NewContainerManager(ctx context.Context, store *meta.Store, cli ctrd.APIClient, imgMgr ImageMgr, volMgr VolumeMgr, netMgr NetworkMgr, cfg *config.Config, contPlugin plugins.ContainerPlugin) (*ContainerManager, error) { mgr := &ContainerManager{ Store: store, NameToID: collect.NewSafeMap(), diff --git a/daemon/mgr/image.go b/daemon/mgr/image.go index 263aa0c0b..ebe4eb121 100644 --- a/daemon/mgr/image.go +++ b/daemon/mgr/image.go @@ -49,16 +49,16 @@ type ImageManager struct { // DefaultNamespace is the default namespace used in DefaultRegistry. DefaultNamespace string - // client is a pointer to the containerd client. + // client is a interface to the containerd client. // It is used to interact with containerd. - client *ctrd.Client + client ctrd.APIClient registry *registry.Client cache *imageCache } // NewImageManager initializes a brand new image manager. -func NewImageManager(cfg *config.Config, client *ctrd.Client) (*ImageManager, error) { +func NewImageManager(cfg *config.Config, client ctrd.APIClient) (*ImageManager, error) { mgr := &ImageManager{ DefaultRegistry: cfg.DefaultRegistry, DefaultNamespace: cfg.DefaultRegistryNS, diff --git a/docs/features/pouch_with_rich_container.md b/docs/features/pouch_with_rich_container.md index 77867c9f5..ffa4978f5 100644 --- a/docs/features/pouch_with_rich_container.md +++ b/docs/features/pouch_with_rich_container.md @@ -30,7 +30,7 @@ If a container is running with `--rich` flag, then every start or restart of thi Here is a simple example for rich container mode using dumb-init to init contaienr: -1. Insatll dumb-init as following: +1. Install dumb-init as following: ```shell # wget -O /usr/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.1/dumb-init_1.2.1_amd64 diff --git a/internal/generator.go b/internal/generator.go index 1798aa105..4aeb7abb6 100644 --- a/internal/generator.go +++ b/internal/generator.go @@ -14,7 +14,7 @@ import ( // DaemonProvider provides resources which are needed by container manager and are from daemon. type DaemonProvider interface { Config() *config.Config - Containerd() *ctrd.Client + Containerd() ctrd.APIClient CtrMgr() mgr.ContainerMgr ImgMgr() mgr.ImageMgr VolMgr() mgr.VolumeMgr