Add snapshot subcommand to ctr for creating diffs of RW layers. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
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)
 | 
						|
}
 |