Add diff service implementation
Add snapshot subcommand to ctr for creating diffs of RW layers. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
52
rootfs/diff.go
Normal file
52
rootfs/diff.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package rootfs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/content"
|
||||
"github.com/containerd/containerd/snapshot"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
type MountDiffer interface {
|
||||
DiffMounts(ctx context.Context, lower, upper []containerd.Mount, media, ref string) (ocispec.Descriptor, error)
|
||||
}
|
||||
|
||||
type DiffOptions struct {
|
||||
MountDiffer
|
||||
content.Store
|
||||
snapshot.Snapshotter
|
||||
}
|
||||
|
||||
func Diff(ctx context.Context, snapshotID, contentRef string, sn snapshot.Snapshotter, md MountDiffer) (ocispec.Descriptor, error) {
|
||||
info, err := sn.Stat(ctx, snapshotID)
|
||||
if err != nil {
|
||||
return ocispec.Descriptor{}, err
|
||||
}
|
||||
|
||||
lowerKey := fmt.Sprintf("%s-parent-view", info.Parent)
|
||||
lower, err := sn.View(ctx, lowerKey, info.Parent)
|
||||
if err != nil {
|
||||
return ocispec.Descriptor{}, err
|
||||
}
|
||||
defer sn.Remove(ctx, lowerKey)
|
||||
|
||||
var upper []containerd.Mount
|
||||
if info.Kind == snapshot.KindActive {
|
||||
upper, err = sn.Mounts(ctx, snapshotID)
|
||||
if err != nil {
|
||||
return ocispec.Descriptor{}, err
|
||||
}
|
||||
} else {
|
||||
upperKey := fmt.Sprintf("%s-view", snapshotID)
|
||||
upper, err = sn.View(ctx, upperKey, snapshotID)
|
||||
if err != nil {
|
||||
return ocispec.Descriptor{}, err
|
||||
}
|
||||
defer sn.Remove(ctx, lowerKey)
|
||||
}
|
||||
|
||||
return md.DiffMounts(ctx, lower, upper, ocispec.MediaTypeImageLayer, contentRef)
|
||||
}
|
Reference in New Issue
Block a user