Skip to content

Commit 3048ff7

Browse files
committed
feature: add snapshotter info in container json
Signed-off-by: Michael Wan <[email protected]>
1 parent 453e2ed commit 3048ff7

File tree

9 files changed

+202
-12
lines changed

9 files changed

+202
-12
lines changed

apis/server/container_bridge.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,17 @@ func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *
315315
}
316316

317317
container := types.ContainerJSON{
318-
ID: meta.ID,
319-
Name: meta.Name,
320-
Image: meta.Config.Image,
321-
Created: meta.Created,
322-
State: meta.State,
323-
Config: meta.Config,
324-
HostConfig: meta.HostConfig,
318+
ID: meta.ID,
319+
Name: meta.Name,
320+
Image: meta.Config.Image,
321+
Created: meta.Created,
322+
State: meta.State,
323+
Config: meta.Config,
324+
HostConfig: meta.HostConfig,
325+
Snapshotter: meta.Snapshotter,
325326
GraphDriver: &types.GraphDriverData{
326-
Name: "overlay2",
327-
Data: map[string]string{
328-
"BaseFS": meta.BaseFS,
329-
},
327+
Name: meta.Snapshotter.Name,
328+
Data: meta.Snapshotter.Data,
330329
},
331330
}
332331

apis/swagger.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,8 @@ definitions:
28082808
x-nullable: true
28092809
Config:
28102810
$ref: "#/definitions/ContainerConfig"
2811+
Snapshotter:
2812+
$ref: "#/definitions/SnapshotterData"
28112813
GraphDriver:
28122814
$ref: "#/definitions/GraphDriverData"
28132815
Mounts:
@@ -2902,6 +2904,20 @@ definitions:
29022904
type: "string"
29032905
enum: ["created", "running", "stopped", "paused", "restarting", "removing", "exited", "dead"]
29042906

2907+
SnapshotterData:
2908+
description: "Information about a container's snapshotter."
2909+
type: "object"
2910+
required: [Name, Data]
2911+
properties:
2912+
Name:
2913+
type: "string"
2914+
x-nullable: false
2915+
Data:
2916+
type: "object"
2917+
x-nullable: false
2918+
additionalProperties:
2919+
type: "string"
2920+
29052921
GraphDriverData:
29062922
description: "Information about a container's graph driver."
29072923
type: "object"

apis/types/container_json.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/types/graph_driver_data.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/types/snapshotter_data.go

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ctrd/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/alibaba/pouch/pkg/jsonstream"
1010

1111
"github.com/containerd/containerd"
12+
"github.com/containerd/containerd/mount"
1213
"github.com/containerd/containerd/snapshots"
1314
"github.com/opencontainers/image-spec/specs-go/v1"
1415
)
@@ -73,4 +74,7 @@ type SnapshotAPIClient interface {
7374
GetSnapshot(ctx context.Context, id string) (snapshots.Info, error)
7475
// RemoveSnapshot removes the snapshot by id.
7576
RemoveSnapshot(ctx context.Context, id string) error
77+
// GetMounts returns the mounts for the active snapshot transaction identified
78+
// by key.
79+
GetMounts(ctx context.Context, id string) ([]mount.Mount, error)
7680
}

ctrd/snapshot.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66

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

6364
return service.Remove(ctx, id)
6465
}
66+
67+
// GetMounts returns the mounts for the active snapshot transaction identified
68+
// by key.
69+
func (c *Client) GetMounts(ctx context.Context, id string) ([]mount.Mount, error) {
70+
wrapperCli, err := c.Get(ctx)
71+
if err != nil {
72+
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
73+
}
74+
75+
service := wrapperCli.client.SnapshotService(defaultSnapshotterName)
76+
defer service.Close()
77+
78+
return service.Mounts(ctx, id)
79+
}

daemon/mgr/container.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
409409
return nil, err
410410
}
411411

412+
// Get snapshot UpperDir
413+
var upperDir string
414+
mounts, err := mgr.Client.GetMounts(ctx, id)
415+
if err != nil {
416+
return nil, err
417+
} else if len(mounts) != 1 {
418+
return nil, fmt.Errorf("failed to get snapshot %s mounts: not equals one", id)
419+
}
420+
for _, opt := range mounts[0].Options {
421+
if strings.HasPrefix(opt, "upperdir=") {
422+
upperDir = strings.TrimPrefix(opt, "upperdir=")
423+
}
424+
}
425+
412426
// set lxcfs binds
413427
if config.HostConfig.EnableLxcfs && lxcfs.IsLxcfsEnabled {
414428
config.HostConfig.Binds = append(config.HostConfig.Binds, lxcfs.LxcfsParentDir+":/var/lib/lxc:shared")
@@ -470,6 +484,17 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty
470484
return nil, err
471485
}
472486

487+
// set snapshotter for container
488+
// TODO(ziren): now we only support overlayfs
489+
meta.Snapshotter = &types.SnapshotterData{
490+
Name: "overlayfs",
491+
Data: map[string]string{},
492+
}
493+
494+
if upperDir != "" {
495+
meta.Snapshotter.Data["UpperDir"] = upperDir
496+
}
497+
473498
container := &Container{
474499
meta: meta,
475500
}
@@ -627,6 +652,9 @@ func (mgr *ContainerManager) createContainerdContainer(ctx context.Context, c *C
627652
}
628653
c.meta.State.Pid = int64(pid)
629654
c.meta.State.ExitCode = 0
655+
656+
// set Snapshot MergedDir
657+
c.meta.Snapshotter.Data["MergedDir"] = c.meta.BaseFS
630658
} else {
631659
c.meta.State.FinishedAt = time.Now().UTC().Format(utils.TimeLayout)
632660
c.meta.State.Error = err.Error()
@@ -1261,6 +1289,12 @@ func (mgr *ContainerManager) markStoppedAndRelease(c *Container, m *ctrd.Message
12611289
}
12621290
}
12631291

1292+
// unset Snapshot MergedDir. Stop a container will
1293+
// delete the containerd container, the merged dir
1294+
// will also be deleted, so we should unset the
1295+
// container's MergedDir.
1296+
c.meta.Snapshotter.Data["MergedDir"] = ""
1297+
12641298
// update meta
12651299
if err := c.Write(mgr.Store); err != nil {
12661300
logrus.Errorf("failed to update meta: %v", err)

daemon/mgr/container_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ type ContainerMeta struct {
104104
// exec ids
105105
ExecIds string `json:"ExecIDs,omitempty"`
106106

107+
// Snapshotter, GraphDriver is same, keep both
108+
// just for compatibility
109+
// snapshotter informations of container
110+
Snapshotter *types.SnapshotterData `json:"Snapshotter,omitempty"`
111+
107112
// graph driver
108113
GraphDriver *types.GraphDriverData `json:"GraphDriver,omitempty"`
109114

0 commit comments

Comments
 (0)