1/* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/ 16 17package containerd 18 19import ( 20 "context" 21 22 diffapi "github.com/containerd/containerd/api/services/diff/v1" 23 "github.com/containerd/containerd/api/types" 24 "github.com/containerd/containerd/diff" 25 "github.com/containerd/containerd/errdefs" 26 "github.com/containerd/containerd/mount" 27 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 28) 29 30// DiffService handles the computation and application of diffs 31type DiffService interface { 32 diff.Comparer 33 diff.Applier 34} 35 36// NewDiffServiceFromClient returns a new diff service which communicates 37// over a GRPC connection. 38func NewDiffServiceFromClient(client diffapi.DiffClient) DiffService { 39 return &diffRemote{ 40 client: client, 41 } 42} 43 44type diffRemote struct { 45 client diffapi.DiffClient 46} 47 48func (r *diffRemote) Apply(ctx context.Context, diff ocispec.Descriptor, mounts []mount.Mount) (ocispec.Descriptor, error) { 49 req := &diffapi.ApplyRequest{ 50 Diff: fromDescriptor(diff), 51 Mounts: fromMounts(mounts), 52 } 53 resp, err := r.client.Apply(ctx, req) 54 if err != nil { 55 return ocispec.Descriptor{}, errdefs.FromGRPC(err) 56 } 57 return toDescriptor(resp.Applied), nil 58} 59 60func (r *diffRemote) Compare(ctx context.Context, a, b []mount.Mount, opts ...diff.Opt) (ocispec.Descriptor, error) { 61 var config diff.Config 62 for _, opt := range opts { 63 if err := opt(&config); err != nil { 64 return ocispec.Descriptor{}, err 65 } 66 } 67 req := &diffapi.DiffRequest{ 68 Left: fromMounts(a), 69 Right: fromMounts(b), 70 MediaType: config.MediaType, 71 Ref: config.Reference, 72 Labels: config.Labels, 73 } 74 resp, err := r.client.Diff(ctx, req) 75 if err != nil { 76 return ocispec.Descriptor{}, errdefs.FromGRPC(err) 77 } 78 return toDescriptor(resp.Diff), nil 79} 80 81func toDescriptor(d *types.Descriptor) ocispec.Descriptor { 82 return ocispec.Descriptor{ 83 MediaType: d.MediaType, 84 Digest: d.Digest, 85 Size: d.Size_, 86 } 87} 88 89func fromDescriptor(d ocispec.Descriptor) *types.Descriptor { 90 return &types.Descriptor{ 91 MediaType: d.MediaType, 92 Digest: d.Digest, 93 Size_: d.Size, 94 } 95} 96 97func fromMounts(mounts []mount.Mount) []*types.Mount { 98 apiMounts := make([]*types.Mount, len(mounts)) 99 for i, m := range mounts { 100 apiMounts[i] = &types.Mount{ 101 Type: m.Type, 102 Source: m.Source, 103 Options: m.Options, 104 } 105 } 106 return apiMounts 107} 108