diff.go 2.8 KB

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