Refactor differ into separate package
Add differ options and package with interface. Update optional values on diff interface to use options. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
@@ -3,20 +3,15 @@ package diff
|
||||
import (
|
||||
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
||||
"github.com/containerd/containerd/api/types"
|
||||
"github.com/containerd/containerd/diff"
|
||||
"github.com/containerd/containerd/mount"
|
||||
"github.com/containerd/containerd/rootfs"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type DiffService interface {
|
||||
rootfs.Applier
|
||||
rootfs.MountDiffer
|
||||
}
|
||||
|
||||
// NewApplierFromClient returns a new Applier which communicates
|
||||
// over a GRPC connection.
|
||||
func NewDiffServiceFromClient(client diffapi.DiffClient) DiffService {
|
||||
func NewDiffServiceFromClient(client diffapi.DiffClient) diff.Differ {
|
||||
return &remote{
|
||||
client: client,
|
||||
}
|
||||
@@ -38,12 +33,19 @@ func (r *remote) Apply(ctx context.Context, diff ocispec.Descriptor, mounts []mo
|
||||
return toDescriptor(resp.Applied), nil
|
||||
}
|
||||
|
||||
func (r *remote) DiffMounts(ctx context.Context, a, b []mount.Mount, media, ref string) (ocispec.Descriptor, error) {
|
||||
func (r *remote) DiffMounts(ctx context.Context, a, b []mount.Mount, opts ...diff.Opt) (ocispec.Descriptor, error) {
|
||||
var config diff.Config
|
||||
for _, opt := range opts {
|
||||
if err := opt(&config); err != nil {
|
||||
return ocispec.Descriptor{}, err
|
||||
}
|
||||
}
|
||||
req := &diffapi.DiffRequest{
|
||||
Left: fromMounts(a),
|
||||
Right: fromMounts(b),
|
||||
MediaType: media,
|
||||
Ref: ref,
|
||||
MediaType: config.MediaType,
|
||||
Ref: config.Reference,
|
||||
Labels: config.Labels,
|
||||
}
|
||||
resp, err := r.client.Diff(ctx, req)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package diff
|
||||
import (
|
||||
diffapi "github.com/containerd/containerd/api/services/diff/v1"
|
||||
"github.com/containerd/containerd/api/types"
|
||||
"github.com/containerd/containerd/diff"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/containerd/containerd/mount"
|
||||
"github.com/containerd/containerd/plugin"
|
||||
@@ -39,7 +40,7 @@ func init() {
|
||||
}
|
||||
|
||||
orderedNames := ic.Config.(*config).Order
|
||||
ordered := make([]plugin.Differ, len(orderedNames))
|
||||
ordered := make([]diff.Differ, len(orderedNames))
|
||||
for i, n := range orderedNames {
|
||||
differp, ok := differs[n]
|
||||
if !ok {
|
||||
@@ -50,7 +51,7 @@ func init() {
|
||||
return nil, errors.Wrapf(err, "could not load required differ due plugin init error: %s", n)
|
||||
}
|
||||
|
||||
ordered[i] = differ.(plugin.Differ)
|
||||
ordered[i] = differ.(diff.Differ)
|
||||
}
|
||||
|
||||
return &service{
|
||||
@@ -61,7 +62,7 @@ func init() {
|
||||
}
|
||||
|
||||
type service struct {
|
||||
differs []plugin.Differ
|
||||
differs []diff.Differ
|
||||
}
|
||||
|
||||
func (s *service) Register(gs *grpc.Server) error {
|
||||
@@ -102,8 +103,19 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D
|
||||
bMounts = toMounts(dr.Right)
|
||||
)
|
||||
|
||||
var opts []diff.Opt
|
||||
if dr.MediaType != "" {
|
||||
opts = append(opts, diff.WithMediaType(dr.MediaType))
|
||||
}
|
||||
if dr.Ref != "" {
|
||||
opts = append(opts, diff.WithReference(dr.Ref))
|
||||
}
|
||||
if dr.Labels != nil {
|
||||
opts = append(opts, diff.WithLabels(dr.Labels))
|
||||
}
|
||||
|
||||
for _, differ := range s.differs {
|
||||
ocidesc, err = differ.DiffMounts(ctx, aMounts, bMounts, dr.MediaType, dr.Ref)
|
||||
ocidesc, err = differ.DiffMounts(ctx, aMounts, bMounts, opts...)
|
||||
if !errdefs.IsNotImplemented(err) {
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user