Skip to content

Commit

Permalink
Merge pull request #1130 from HusterWan/zr/overlay-dir-dev
Browse files Browse the repository at this point in the history
feature: add snapshotter info into container json
  • Loading branch information
allencloud authored Apr 16, 2018
2 parents dd50f1a + 3048ff7 commit 65b9dc2
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 12 deletions.
21 changes: 10 additions & 11 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,17 @@ func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *
}

container := types.ContainerJSON{
ID: meta.ID,
Name: meta.Name,
Image: meta.Config.Image,
Created: meta.Created,
State: meta.State,
Config: meta.Config,
HostConfig: meta.HostConfig,
ID: meta.ID,
Name: meta.Name,
Image: meta.Config.Image,
Created: meta.Created,
State: meta.State,
Config: meta.Config,
HostConfig: meta.HostConfig,
Snapshotter: meta.Snapshotter,
GraphDriver: &types.GraphDriverData{
Name: "overlay2",
Data: map[string]string{
"BaseFS": meta.BaseFS,
},
Name: meta.Snapshotter.Name,
Data: meta.Snapshotter.Data,
},
}

Expand Down
16 changes: 16 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,8 @@ definitions:
x-nullable: true
Config:
$ref: "#/definitions/ContainerConfig"
Snapshotter:
$ref: "#/definitions/SnapshotterData"
GraphDriver:
$ref: "#/definitions/GraphDriverData"
Mounts:
Expand Down Expand Up @@ -2902,6 +2904,20 @@ definitions:
type: "string"
enum: ["created", "running", "stopped", "paused", "restarting", "removing", "exited", "dead"]

SnapshotterData:
description: "Information about a container's snapshotter."
type: "object"
required: [Name, Data]
properties:
Name:
type: "string"
x-nullable: false
Data:
type: "object"
x-nullable: false
additionalProperties:
type: "string"

GraphDriverData:
description: "Information about a container's graph driver."
type: "object"
Expand Down
29 changes: 29 additions & 0 deletions apis/types/container_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/types/graph_driver_data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions apis/types/snapshotter_data.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ctrd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alibaba/pouch/pkg/jsonstream"

"github.com/containerd/containerd"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/snapshots"
"github.com/opencontainers/image-spec/specs-go/v1"
)
Expand Down Expand Up @@ -73,4 +74,7 @@ type SnapshotAPIClient interface {
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)
}
15 changes: 15 additions & 0 deletions ctrd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/snapshots"
"github.com/opencontainers/image-spec/identity"
Expand Down Expand Up @@ -62,3 +63,17 @@ func (c *Client) RemoveSnapshot(ctx context.Context, id string) error {

return service.Remove(ctx, id)
}

// GetMounts returns the mounts for the active snapshot transaction identified
// by key.
func (c *Client) GetMounts(ctx context.Context, id string) ([]mount.Mount, error) {
wrapperCli, err := c.Get(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
}

service := wrapperCli.client.SnapshotService(defaultSnapshotterName)
defer service.Close()

return service.Mounts(ctx, id)
}
34 changes: 34 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
return nil, err
}

// Get snapshot UpperDir
var upperDir string
mounts, err := mgr.Client.GetMounts(ctx, id)
if err != nil {
return nil, err
} else if len(mounts) != 1 {
return nil, fmt.Errorf("failed to get snapshot %s mounts: not equals one", id)
}
for _, opt := range mounts[0].Options {
if strings.HasPrefix(opt, "upperdir=") {
upperDir = strings.TrimPrefix(opt, "upperdir=")
}
}

// set lxcfs binds
if config.HostConfig.EnableLxcfs && lxcfs.IsLxcfsEnabled {
config.HostConfig.Binds = append(config.HostConfig.Binds, lxcfs.LxcfsParentDir+":/var/lib/lxc:shared")
Expand Down Expand Up @@ -470,6 +484,17 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
return nil, err
}

// set snapshotter for container
// TODO(ziren): now we only support overlayfs
meta.Snapshotter = &types.SnapshotterData{
Name: "overlayfs",
Data: map[string]string{},
}

if upperDir != "" {
meta.Snapshotter.Data["UpperDir"] = upperDir
}

container := &Container{
meta: meta,
}
Expand Down Expand Up @@ -627,6 +652,9 @@ func (mgr *ContainerManager) createContainerdContainer(ctx context.Context, c *C
}
c.meta.State.Pid = int64(pid)
c.meta.State.ExitCode = 0

// set Snapshot MergedDir
c.meta.Snapshotter.Data["MergedDir"] = c.meta.BaseFS
} else {
c.meta.State.FinishedAt = time.Now().UTC().Format(utils.TimeLayout)
c.meta.State.Error = err.Error()
Expand Down Expand Up @@ -1261,6 +1289,12 @@ func (mgr *ContainerManager) markStoppedAndRelease(c *Container, m *ctrd.Message
}
}

// unset Snapshot MergedDir. Stop a container will
// delete the containerd container, the merged dir
// will also be deleted, so we should unset the
// container's MergedDir.
c.meta.Snapshotter.Data["MergedDir"] = ""

// update meta
if err := c.Write(mgr.Store); err != nil {
logrus.Errorf("failed to update meta: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ type ContainerMeta struct {
// exec ids
ExecIds string `json:"ExecIDs,omitempty"`

// Snapshotter, GraphDriver is same, keep both
// just for compatibility
// snapshotter informations of container
Snapshotter *types.SnapshotterData `json:"Snapshotter,omitempty"`

// graph driver
GraphDriver *types.GraphDriverData `json:"GraphDriver,omitempty"`

Expand Down

0 comments on commit 65b9dc2

Please sign in to comment.