|
| 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 | + |
| 17 | +package proxy |
| 18 | + |
| 19 | +import ( |
| 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 | + "github.com/containerd/containerd/pkg/epoch" |
| 28 | + "github.com/containerd/containerd/protobuf" |
| 29 | + ptypes "github.com/containerd/containerd/protobuf/types" |
| 30 | + "github.com/opencontainers/go-digest" |
| 31 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 32 | + |
| 33 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 34 | +) |
| 35 | + |
| 36 | +// NewDiffApplier returns a new comparer and applier which communicates |
| 37 | +// over a GRPC connection. |
| 38 | +func NewDiffApplier(client diffapi.DiffClient) interface{} { |
| 39 | + return &diffRemote{ |
| 40 | + client: client, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +type diffRemote struct { |
| 45 | + client diffapi.DiffClient |
| 46 | +} |
| 47 | + |
| 48 | +func (r *diffRemote) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []mount.Mount, opts ...diff.ApplyOpt) (ocispec.Descriptor, error) { |
| 49 | + var config diff.ApplyConfig |
| 50 | + for _, opt := range opts { |
| 51 | + if err := opt(ctx, desc, &config); err != nil { |
| 52 | + return ocispec.Descriptor{}, err |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + payloads := make(map[string]*ptypes.Any) |
| 57 | + for k, v := range config.ProcessorPayloads { |
| 58 | + payloads[k] = protobuf.FromAny(v) |
| 59 | + } |
| 60 | + |
| 61 | + req := &diffapi.ApplyRequest{ |
| 62 | + Diff: fromDescriptor(desc), |
| 63 | + Mounts: fromMounts(mounts), |
| 64 | + Payloads: payloads, |
| 65 | + } |
| 66 | + resp, err := r.client.Apply(ctx, req) |
| 67 | + if err != nil { |
| 68 | + return ocispec.Descriptor{}, errdefs.FromGRPC(err) |
| 69 | + } |
| 70 | + return toDescriptor(resp.Applied), nil |
| 71 | +} |
| 72 | + |
| 73 | +func (r *diffRemote) Compare(ctx context.Context, a, b []mount.Mount, opts ...diff.Opt) (ocispec.Descriptor, error) { |
| 74 | + var config diff.Config |
| 75 | + for _, opt := range opts { |
| 76 | + if err := opt(&config); err != nil { |
| 77 | + return ocispec.Descriptor{}, err |
| 78 | + } |
| 79 | + } |
| 80 | + if tm := epoch.FromContext(ctx); tm != nil && config.SourceDateEpoch == nil { |
| 81 | + config.SourceDateEpoch = tm |
| 82 | + } |
| 83 | + var sourceDateEpoch *timestamppb.Timestamp |
| 84 | + if config.SourceDateEpoch != nil { |
| 85 | + sourceDateEpoch = timestamppb.New(*config.SourceDateEpoch) |
| 86 | + } |
| 87 | + req := &diffapi.DiffRequest{ |
| 88 | + Left: fromMounts(a), |
| 89 | + Right: fromMounts(b), |
| 90 | + MediaType: config.MediaType, |
| 91 | + Ref: config.Reference, |
| 92 | + Labels: config.Labels, |
| 93 | + SourceDateEpoch: sourceDateEpoch, |
| 94 | + } |
| 95 | + resp, err := r.client.Diff(ctx, req) |
| 96 | + if err != nil { |
| 97 | + return ocispec.Descriptor{}, errdefs.FromGRPC(err) |
| 98 | + } |
| 99 | + return toDescriptor(resp.Diff), nil |
| 100 | +} |
| 101 | + |
| 102 | +func toDescriptor(d *types.Descriptor) ocispec.Descriptor { |
| 103 | + return ocispec.Descriptor{ |
| 104 | + MediaType: d.MediaType, |
| 105 | + Digest: digest.Digest(d.Digest), |
| 106 | + Size: d.Size, |
| 107 | + Annotations: d.Annotations, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func fromDescriptor(d ocispec.Descriptor) *types.Descriptor { |
| 112 | + return &types.Descriptor{ |
| 113 | + MediaType: d.MediaType, |
| 114 | + Digest: d.Digest.String(), |
| 115 | + Size: d.Size, |
| 116 | + Annotations: d.Annotations, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func fromMounts(mounts []mount.Mount) []*types.Mount { |
| 121 | + apiMounts := make([]*types.Mount, len(mounts)) |
| 122 | + for i, m := range mounts { |
| 123 | + apiMounts[i] = &types.Mount{ |
| 124 | + Type: m.Type, |
| 125 | + Source: m.Source, |
| 126 | + Target: m.Target, |
| 127 | + Options: m.Options, |
| 128 | + } |
| 129 | + } |
| 130 | + return apiMounts |
| 131 | +} |
0 commit comments