|
| 1 | +package apply |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/containerd/containerd/archive" |
| 10 | + "github.com/containerd/containerd/archive/compression" |
| 11 | + "github.com/containerd/containerd/content" |
| 12 | + "github.com/containerd/containerd/diff" |
| 13 | + "github.com/containerd/containerd/errdefs" |
| 14 | + "github.com/containerd/containerd/images" |
| 15 | + "github.com/containerd/containerd/log" |
| 16 | + "github.com/containerd/containerd/mount" |
| 17 | + digest "github.com/opencontainers/go-digest" |
| 18 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 19 | + "github.com/pkg/errors" |
| 20 | + "github.com/sirupsen/logrus" |
| 21 | +) |
| 22 | + |
| 23 | +// NewFileSystemApplier returns an applier which simply mounts |
| 24 | +// and applies diff onto the mounted filesystem. |
| 25 | +func NewFileSystemApplier(cs content.Store) diff.Applier { |
| 26 | + return &fsApplier{ |
| 27 | + store: cs, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +type fsApplier struct { |
| 32 | + store content.Store |
| 33 | +} |
| 34 | + |
| 35 | +var emptyDesc = ocispec.Descriptor{} |
| 36 | + |
| 37 | +// Apply applies the content associated with the provided digests onto the |
| 38 | +// provided mounts. Archive content will be extracted and decompressed if |
| 39 | +// necessary. |
| 40 | +func (s *fsApplier) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []mount.Mount) (d ocispec.Descriptor, err error) { |
| 41 | + t1 := time.Now() |
| 42 | + defer func() { |
| 43 | + if err == nil { |
| 44 | + log.G(ctx).WithFields(logrus.Fields{ |
| 45 | + "d": time.Now().Sub(t1), |
| 46 | + "dgst": desc.Digest, |
| 47 | + "size": desc.Size, |
| 48 | + "media": desc.MediaType, |
| 49 | + }).Debugf("diff applied") |
| 50 | + } |
| 51 | + }() |
| 52 | + |
| 53 | + isCompressed, err := images.IsCompressedDiff(ctx, desc.MediaType) |
| 54 | + if err != nil { |
| 55 | + return emptyDesc, errors.Wrapf(errdefs.ErrNotImplemented, "unsupported diff media type: %v", desc.MediaType) |
| 56 | + } |
| 57 | + |
| 58 | + var ocidesc ocispec.Descriptor |
| 59 | + if err := mount.WithTempMount(ctx, mounts, func(root string) error { |
| 60 | + ra, err := s.store.ReaderAt(ctx, desc.Digest) |
| 61 | + if err != nil { |
| 62 | + return errors.Wrap(err, "failed to get reader from content store") |
| 63 | + } |
| 64 | + defer ra.Close() |
| 65 | + |
| 66 | + r := content.NewReader(ra) |
| 67 | + if isCompressed { |
| 68 | + ds, err := compression.DecompressStream(r) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + defer ds.Close() |
| 73 | + r = ds |
| 74 | + } |
| 75 | + |
| 76 | + digester := digest.Canonical.Digester() |
| 77 | + rc := &readCounter{ |
| 78 | + r: io.TeeReader(r, digester.Hash()), |
| 79 | + } |
| 80 | + |
| 81 | + if _, err := archive.Apply(ctx, root, rc); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + // Read any trailing data |
| 86 | + if _, err := io.Copy(ioutil.Discard, rc); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + ocidesc = ocispec.Descriptor{ |
| 91 | + MediaType: ocispec.MediaTypeImageLayer, |
| 92 | + Size: rc.c, |
| 93 | + Digest: digester.Digest(), |
| 94 | + } |
| 95 | + return nil |
| 96 | + |
| 97 | + }); err != nil { |
| 98 | + return emptyDesc, err |
| 99 | + } |
| 100 | + return ocidesc, nil |
| 101 | +} |
| 102 | + |
| 103 | +type readCounter struct { |
| 104 | + r io.Reader |
| 105 | + c int64 |
| 106 | +} |
| 107 | + |
| 108 | +func (rc *readCounter) Read(p []byte) (n int, err error) { |
| 109 | + n, err = rc.r.Read(p) |
| 110 | + rc.c += int64(n) |
| 111 | + return |
| 112 | +} |
0 commit comments