Skip to content

Commit

Permalink
refactor: ctrd layer
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <zirenwan@gmail.com>
  • Loading branch information
HusterWan committed Mar 27, 2018
1 parent 107e1d9 commit 5c1a944
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
7 changes: 3 additions & 4 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/containerd/containerd"
"github.com/containerd/containerd/version"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
76 changes: 76 additions & 0 deletions ctrd/interface.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions daemon/mgr/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion docs/features/pouch_with_rich_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c1a944

Please sign in to comment.