forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
106 lines (84 loc) · 3.11 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ctrd
import (
"context"
"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"
)
const defaultSnapshotterName = "overlayfs"
// CreateSnapshot creates a active snapshot with image's name and id.
func (c *Client) CreateSnapshot(ctx context.Context, id, ref string) error {
wrapperCli, err := c.Get(ctx)
if err != nil {
return fmt.Errorf("failed to get a containerd grpc client: %v", err)
}
ctx = leases.WithLease(ctx, wrapperCli.lease.ID())
image, err := wrapperCli.client.ImageService().Get(ctx, ref)
if err != nil {
return err
}
diffIDs, err := image.RootFS(ctx, wrapperCli.client.ContentStore(), platforms.Default())
if err != nil {
return err
}
parent := identity.ChainID(diffIDs).String()
if _, err := wrapperCli.client.SnapshotService(defaultSnapshotterName).Prepare(ctx, id, parent); err != nil {
return err
}
return nil
}
// GetSnapshot returns the snapshot's info by id.
func (c *Client) GetSnapshot(ctx context.Context, id string) (snapshots.Info, error) {
wrapperCli, err := c.Get(ctx)
if err != nil {
return snapshots.Info{}, fmt.Errorf("failed to get a containerd grpc client: %v", err)
}
service := wrapperCli.client.SnapshotService(defaultSnapshotterName)
defer service.Close()
return service.Stat(ctx, id)
}
// RemoveSnapshot removes the snapshot by id.
func (c *Client) RemoveSnapshot(ctx context.Context, id string) error {
wrapperCli, err := c.Get(ctx)
if err != nil {
return fmt.Errorf("failed to get a containerd grpc client: %v", err)
}
service := wrapperCli.client.SnapshotService(defaultSnapshotterName)
defer service.Close()
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)
}
// GetSnapshotUsage returns the resource usage of an active or committed snapshot
// excluding the usage of parent snapshots.
func (c *Client) GetSnapshotUsage(ctx context.Context, id string) (snapshots.Usage, error) {
wrapperCli, err := c.Get(ctx)
if err != nil {
return snapshots.Usage{}, fmt.Errorf("failed to get a containerd grpc client: %v", err)
}
service := wrapperCli.client.SnapshotService(defaultSnapshotterName)
defer service.Close()
return service.Usage(ctx, id)
}
// WalkSnapshot walk all snapshots. For each snapshot, the function will be called.
func (c *Client) WalkSnapshot(ctx context.Context, fn func(context.Context, snapshots.Info) error) error {
wrapperCli, err := c.Get(ctx)
if err != nil {
return fmt.Errorf("failed to get a containerd grpc client: %v", err)
}
service := wrapperCli.client.SnapshotService(defaultSnapshotterName)
defer service.Close()
return service.Walk(ctx, fn)
}