diff --git a/client.go b/client.go index 8b02c73ee..815a9e40f 100644 --- a/client.go +++ b/client.go @@ -458,7 +458,7 @@ func (c *Client) ImageService() images.Store { } // DiffService returns the underlying Differ -func (c *Client) DiffService() diff.Differ { +func (c *Client) DiffService() diff.DiffApplier { return NewDiffServiceFromClient(diffapi.NewDiffClient(c.conn)) } diff --git a/diff.go b/diff.go index 4e47efafc..1cfa14ab6 100644 --- a/diff.go +++ b/diff.go @@ -11,7 +11,7 @@ import ( // NewDiffServiceFromClient returns a new diff service which communicates // over a GRPC connection. -func NewDiffServiceFromClient(client diffapi.DiffClient) diff.Differ { +func NewDiffServiceFromClient(client diffapi.DiffClient) diff.DiffApplier { return &diffRemote{ client: client, } @@ -33,7 +33,7 @@ func (r *diffRemote) Apply(ctx context.Context, diff ocispec.Descriptor, mounts return toDescriptor(resp.Applied), nil } -func (r *diffRemote) DiffMounts(ctx context.Context, a, b []mount.Mount, opts ...diff.Opt) (ocispec.Descriptor, error) { +func (r *diffRemote) Diff(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 { diff --git a/diff/diff.go b/diff/diff.go index 85cef3583..d51a3468e 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -24,21 +24,33 @@ type Config struct { // Opt is used to configure a diff operation type Opt func(*Config) error -// Differ allows the apply and creation of filesystem diffs between mounts +// Differ allows creation of filesystem diffs between mounts type Differ interface { + // Diff computes the difference between two mounts and returns a + // descriptor for the computed diff. The options can provide + // a ref which can be used to track the content creation of the diff. + // The media type which is used to determine the format of the created + // content can also be provided as an option. + Diff(ctx context.Context, lower, upper []mount.Mount, opts ...Opt) (ocispec.Descriptor, error) +} + +// Applier allows applying diffs between mounts +type Applier interface { // Apply applies the content referred to by the given descriptor to // the provided mount. The method of applying is based on the // implementation and content descriptor. For example, in the common // case the descriptor is a file system difference in tar format, // that tar would be applied on top of the mounts. Apply(ctx context.Context, desc ocispec.Descriptor, mount []mount.Mount) (ocispec.Descriptor, error) +} - // DiffMounts computes the difference between two mounts and returns a - // descriptor for the computed diff. The options can provide - // a ref which can be used to track the content creation of the diff. - // The media type which is used to determine the format of the created - // content can also be provided as an option. - DiffMounts(ctx context.Context, lower, upper []mount.Mount, opts ...Opt) (ocispec.Descriptor, error) +// DiffApplier is the interface that groups the basic Apply and Diff methods. +// +// golint says `type name will be used as diff.DiffApplier by other packages, and that stutters`, +// but that can be ignored now. +type DiffApplier interface { // nolint: golint + Applier + Differ } // WithMediaType sets the media type to use for creating the diff, without diff --git a/diff/walking/differ.go b/diff/walking/differ.go index f1dbf43f5..baa787339 100644 --- a/diff/walking/differ.go +++ b/diff/walking/differ.go @@ -51,9 +51,9 @@ type walkingDiff struct { var emptyDesc = ocispec.Descriptor{} -// NewWalkingDiff is a generic implementation of diff.Differ. +// NewWalkingDiff is a generic implementation of diff.DiffApplier. // NewWalkingDiff is expected to work with any filesystem. -func NewWalkingDiff(store content.Store) (diff.Differ, error) { +func NewWalkingDiff(store content.Store) (diff.DiffApplier, error) { return &walkingDiff{ store: store, }, nil @@ -125,9 +125,9 @@ func (s *walkingDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts return ocidesc, nil } -// DiffMounts creates a diff between the given mounts and uploads the result +// Diff creates a diff between the given mounts and uploads the result // to the content store. -func (s *walkingDiff) DiffMounts(ctx context.Context, lower, upper []mount.Mount, opts ...diff.Opt) (d ocispec.Descriptor, err error) { +func (s *walkingDiff) Diff(ctx context.Context, lower, upper []mount.Mount, opts ...diff.Opt) (d ocispec.Descriptor, err error) { var config diff.Config for _, opt := range opts { if err := opt(&config); err != nil { diff --git a/rootfs/apply.go b/rootfs/apply.go index 405129564..21db6782c 100644 --- a/rootfs/apply.go +++ b/rootfs/apply.go @@ -30,7 +30,7 @@ type Layer struct { // The returned result is a chain id digest representing all the applied layers. // Layers are applied in order they are given, making the first layer the // bottom-most layer in the layer chain. -func ApplyLayers(ctx context.Context, layers []Layer, sn snapshots.Snapshotter, a diff.Differ) (digest.Digest, error) { +func ApplyLayers(ctx context.Context, layers []Layer, sn snapshots.Snapshotter, a diff.Applier) (digest.Digest, error) { var chain []digest.Digest for _, layer := range layers { if _, err := ApplyLayer(ctx, layer, chain, sn, a); err != nil { @@ -46,7 +46,7 @@ func ApplyLayers(ctx context.Context, layers []Layer, sn snapshots.Snapshotter, // ApplyLayer applies a single layer on top of the given provided layer chain, // using the provided snapshotter and applier. If the layer was unpacked true // is returned, if the layer already exists false is returned. -func ApplyLayer(ctx context.Context, layer Layer, chain []digest.Digest, sn snapshots.Snapshotter, a diff.Differ, opts ...snapshots.Opt) (bool, error) { +func ApplyLayer(ctx context.Context, layer Layer, chain []digest.Digest, sn snapshots.Snapshotter, a diff.Applier, opts ...snapshots.Opt) (bool, error) { var ( parent = identity.ChainID(chain) chainID = identity.ChainID(append(chain, layer.Diff.Digest)) diff --git a/rootfs/diff.go b/rootfs/diff.go index 89ed71d03..d99393c34 100644 --- a/rootfs/diff.go +++ b/rootfs/diff.go @@ -42,5 +42,5 @@ func Diff(ctx context.Context, snapshotID string, sn snapshots.Snapshotter, d di defer sn.Remove(ctx, upperKey) } - return d.DiffMounts(ctx, lower, upper, opts...) + return d.Diff(ctx, lower, upper, opts...) } diff --git a/services/diff/service.go b/services/diff/service.go index 4e1c9fcc6..37ec242ff 100644 --- a/services/diff/service.go +++ b/services/diff/service.go @@ -38,7 +38,7 @@ func init() { } orderedNames := ic.Config.(*config).Order - ordered := make([]diff.Differ, len(orderedNames)) + ordered := make([]diff.DiffApplier, len(orderedNames)) for i, n := range orderedNames { differp, ok := differs[n] if !ok { @@ -49,7 +49,10 @@ func init() { return nil, errors.Wrapf(err, "could not load required differ due plugin init error: %s", n) } - ordered[i] = differ.(diff.Differ) + ordered[i], ok = differ.(diff.DiffApplier) + if !ok { + return nil, errors.Errorf("differ does not implement diff.DiffApplier interface: %s", n) + } } return &service{ @@ -60,7 +63,7 @@ func init() { } type service struct { - differs []diff.Differ + differs []diff.DiffApplier } func (s *service) Register(gs *grpc.Server) error { @@ -113,7 +116,7 @@ func (s *service) Diff(ctx context.Context, dr *diffapi.DiffRequest) (*diffapi.D } for _, differ := range s.differs { - ocidesc, err = differ.DiffMounts(ctx, aMounts, bMounts, opts...) + ocidesc, err = differ.Diff(ctx, aMounts, bMounts, opts...) if !errdefs.IsNotImplemented(err) { break }