differ.go 3.7 KB

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