From ca9d2bbedba65b26331f61c13d3ca9b6875453dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Thu, 4 Aug 2022 11:06:45 +0200 Subject: [PATCH] c8d/pull: Add options for stargz/nydus snapshotters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- daemon/containerd/image_pull.go | 21 ++ vendor.mod | 5 +- vendor.sum | 5 +- .../containerd/nydus-snapshotter/LICENSE | 202 ++++++++++++++++++ .../nydus-snapshotter/pkg/label/handler.go | 59 +++++ .../nydus-snapshotter/pkg/label/label.go | 41 ++++ .../stargz-snapshotter/estargz/build.go | 44 +++- .../stargz-snapshotter/estargz/estargz.go | 5 +- .../stargz-snapshotter/estargz/testutil.go | 5 +- .../stargz-snapshotter/fs/config/config.go | 94 ++++++++ .../stargz-snapshotter/fs/source/source.go | 202 ++++++++++++++++++ vendor/modules.txt | 7 +- 12 files changed, 675 insertions(+), 15 deletions(-) create mode 100644 vendor/github.com/containerd/nydus-snapshotter/LICENSE create mode 100644 vendor/github.com/containerd/nydus-snapshotter/pkg/label/handler.go create mode 100644 vendor/github.com/containerd/nydus-snapshotter/pkg/label/label.go create mode 100644 vendor/github.com/containerd/stargz-snapshotter/fs/config/config.go create mode 100644 vendor/github.com/containerd/stargz-snapshotter/fs/source/source.go diff --git a/daemon/containerd/image_pull.go b/daemon/containerd/image_pull.go index dff6bd0a29b0b..d754b862bf874 100644 --- a/daemon/containerd/image_pull.go +++ b/daemon/containerd/image_pull.go @@ -7,6 +7,8 @@ import ( "github.com/containerd/containerd" "github.com/containerd/containerd/images" "github.com/containerd/containerd/platforms" + nyduslabel "github.com/containerd/nydus-snapshotter/pkg/label" + stargzsource "github.com/containerd/stargz-snapshotter/fs/source" "github.com/docker/distribution" "github.com/docker/distribution/reference" "github.com/docker/docker/api/types/registry" @@ -53,6 +55,7 @@ func (i *ImageService) PullImage(ctx context.Context, image, tagOrDigest string, return nil, nil }) opts = append(opts, containerd.WithImageHandler(h)) + opts = i.applySnapshotterOpts(opts, ref) finishProgress := showProgress(ctx, jobs, outStream, pullProgress(i.client.ContentStore())) defer finishProgress() @@ -79,3 +82,21 @@ func (i *ImageService) PullImage(ctx context.Context, image, tagOrDigest string, func (i *ImageService) GetRepository(ctx context.Context, ref reference.Named, authConfig *registry.AuthConfig) (distribution.Repository, error) { panic("not implemented") } + +func (i *ImageService) applySnapshotterOpts(opts []containerd.RemoteOpt, ref reference.Named) []containerd.RemoteOpt { + opts = append(opts, containerd.WithPullUnpack) + opts = append(opts, containerd.WithPullSnapshotter(i.snapshotter)) + + var wrapper func(images.Handler) images.Handler + switch i.snapshotter { + case "stargz": + const prefetch int64 = 10 * 1024 * 1024 // 10MiB + wrapper = stargzsource.AppendDefaultLabelsHandlerWrapper(ref.String(), prefetch) + case "nydus": + wrapper = nyduslabel.AppendLabelsHandlerWrapper(ref.String()) + } + if wrapper != nil { + opts = append(opts, containerd.WithImageHandlerWrapper(wrapper)) + } + return opts +} diff --git a/vendor.mod b/vendor.mod index 8dd540ada5701..8e4b4fd0465f2 100644 --- a/vendor.mod +++ b/vendor.mod @@ -23,6 +23,8 @@ require ( github.com/containerd/containerd v1.6.6 github.com/containerd/continuity v0.3.0 github.com/containerd/fifo v1.0.0 + github.com/containerd/nydus-snapshotter v0.3.0-alpha.5 + github.com/containerd/stargz-snapshotter v0.11.3 github.com/containerd/typeurl v1.0.2 github.com/coreos/go-systemd/v22 v22.3.2 github.com/creack/pty v1.1.11 @@ -100,8 +102,7 @@ require ( github.com/containerd/console v1.0.3 // indirect github.com/containerd/go-cni v1.1.6 // indirect github.com/containerd/go-runc v1.0.0 // indirect - github.com/containerd/stargz-snapshotter v0.11.3 // indirect - github.com/containerd/stargz-snapshotter/estargz v0.11.3 // indirect + github.com/containerd/stargz-snapshotter/estargz v0.11.4 // indirect github.com/containerd/ttrpc v1.1.0 // indirect github.com/containernetworking/cni v1.1.1 // indirect github.com/cyphar/filepath-securejoin v0.2.3 // indirect diff --git a/vendor.sum b/vendor.sum index a0e38ba6ba9ca..1bee329ccd320 100644 --- a/vendor.sum +++ b/vendor.sum @@ -280,11 +280,14 @@ github.com/containerd/imgcrypt v1.1.3/go.mod h1:/TPA1GIDXMzbj01yd8pIbQiLdQxed5ue github.com/containerd/nri v0.0.0-20201007170849-eb1350a75164/go.mod h1:+2wGSDGFYfE5+So4M5syatU0N0f0LbWpuqyMi4/BE8c= github.com/containerd/nri v0.0.0-20210316161719-dbaa18c31c14/go.mod h1:lmxnXF6oMkbqs39FiCt1s0R2HSMhcLel9vNL3m4AaeY= github.com/containerd/nri v0.1.0/go.mod h1:lmxnXF6oMkbqs39FiCt1s0R2HSMhcLel9vNL3m4AaeY= +github.com/containerd/nydus-snapshotter v0.3.0-alpha.5 h1:r6x2KjHqlcTiLUEZi8C5S3irTGpqj+1Y/9wdOHsLaVM= +github.com/containerd/nydus-snapshotter v0.3.0-alpha.5/go.mod h1:zBuYaCIt5l9DBvX89EQBrbp7K8wd2tvpR51KTxodtF8= github.com/containerd/stargz-snapshotter v0.11.3 h1:D3PoF563XmOBdtfx2G6AkhbHueqwIVPBFn2mrsWLa3w= github.com/containerd/stargz-snapshotter v0.11.3/go.mod h1:2j2EAUyvrLU4D9unYlTIwGhDKQIk74KJ9E71lJsQCVM= github.com/containerd/stargz-snapshotter/estargz v0.4.1/go.mod h1:x7Q9dg9QYb4+ELgxmo4gBUeJB0tl5dqH1Sdz0nJU1QM= -github.com/containerd/stargz-snapshotter/estargz v0.11.3 h1:k2kN16Px6LYuv++qFqK+JTcYqc8bEVxzGpf8/gFBL5M= github.com/containerd/stargz-snapshotter/estargz v0.11.3/go.mod h1:7vRJIcImfY8bpifnMjt+HTJoQxASq7T28MYbP15/Nf0= +github.com/containerd/stargz-snapshotter/estargz v0.11.4 h1:LjrYUZpyOhiSaU7hHrdR82/RBoxfGWSaC0VeSSMXqnk= +github.com/containerd/stargz-snapshotter/estargz v0.11.4/go.mod h1:7vRJIcImfY8bpifnMjt+HTJoQxASq7T28MYbP15/Nf0= github.com/containerd/ttrpc v0.0.0-20190828154514-0e0f228740de/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o= github.com/containerd/ttrpc v0.0.0-20190828172938-92c8520ef9f8/go.mod h1:PvCDdDGpgqzQIzDW1TphrGLssLDZp2GuS+X5DkEJB8o= github.com/containerd/ttrpc v0.0.0-20191028202541-4f1b8fe65a5c/go.mod h1:LPm1u0xBw8r8NOKoOdNMeVHSawSsltak+Ihv+etqsE8= diff --git a/vendor/github.com/containerd/nydus-snapshotter/LICENSE b/vendor/github.com/containerd/nydus-snapshotter/LICENSE new file mode 100644 index 0000000000000..d645695673349 --- /dev/null +++ b/vendor/github.com/containerd/nydus-snapshotter/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/containerd/nydus-snapshotter/pkg/label/handler.go b/vendor/github.com/containerd/nydus-snapshotter/pkg/label/handler.go new file mode 100644 index 0000000000000..6e407658be505 --- /dev/null +++ b/vendor/github.com/containerd/nydus-snapshotter/pkg/label/handler.go @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022. Ant Group. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package label + +import ( + "context" + "fmt" + "strings" + + "github.com/containerd/containerd/images" + "github.com/containerd/containerd/labels" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" +) + +// AppendLabelsHandlerWrapper returns a function which can wrap a handler by appending +// image's basic information to each layer descriptor as annotations during unpack. +// These annotations will be passed to this nydus snapshotter as labels. +func AppendLabelsHandlerWrapper(ref string) func(f images.Handler) images.Handler { + return func(f images.Handler) images.Handler { + return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { + children, err := f.Handle(ctx, desc) + if err != nil { + return nil, err + } + switch desc.MediaType { + case ocispec.MediaTypeImageManifest, images.MediaTypeDockerSchema2Manifest: + for i := range children { + c := &children[i] + if images.IsLayerType(c.MediaType) { + if c.Annotations == nil { + c.Annotations = make(map[string]string) + } + var layers string + for _, l := range children[i:] { + if images.IsLayerType(l.MediaType) { + ls := fmt.Sprintf("%s,", l.Digest.String()) + // This avoids the label hits the size limitation. + // Skipping layers is allowed here and only affects performance. + if err := labels.Validate(CRIImageLayers, layers+ls); err != nil { + break + } + layers += ls + } + } + c.Annotations[CRIImageLayers] = strings.TrimSuffix(layers, ",") + c.Annotations[CRIImageRef] = ref + c.Annotations[CRILayerDigest] = c.Digest.String() + c.Annotations[CRIManifestDigest] = desc.Digest.String() + } + } + } + return children, nil + }) + } +} diff --git a/vendor/github.com/containerd/nydus-snapshotter/pkg/label/label.go b/vendor/github.com/containerd/nydus-snapshotter/pkg/label/label.go new file mode 100644 index 0000000000000..b1dcdf5c5c1fe --- /dev/null +++ b/vendor/github.com/containerd/nydus-snapshotter/pkg/label/label.go @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020. Ant Group. All rights reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package label + +const ( + CRIImageRef = "containerd.io/snapshot/cri.image-ref" + CRIImageLayers = "containerd.io/snapshot/cri.image-layers" + CRILayerDigest = "containerd.io/snapshot/cri.layer-digest" + CRIManifestDigest = "containerd.io/snapshot/cri.manifest-digest" + + // Marker for remote snapshotter to handle the pull request. + // During image pull, the containerd client calls Prepare API with the label containerd.io/snapshot.ref. + // This is a containerd-defined label which contains ChainID that targets a committed snapshot that the + // client is trying to prepare. + TargetSnapshotRef = "containerd.io/snapshot.ref" + + // Annotation containing ids of data blobs referenced by the image, set by image builders. + NydusDataBlobIDs = "containerd.io/snapshot/nydus-blob-ids" + // A bool flag to mark the blob as a Nydus data blob, set by image builders. + NydusDataLayer = "containerd.io/snapshot/nydus-blob" + // A bool flag to mark the blob as a nydus bootstrap, set by image builders. + NydusMetaLayer = "containerd.io/snapshot/nydus-bootstrap" + // Annotation containing secret to pull images from registry, set by the snapshotter. + NydusImagePullSecret = "containerd.io/snapshot/pullsecret" + // Annotation containing username to pull images from registry, set by the snapshotter. + NydusImagePullUsername = "containerd.io/snapshot/pullusername" + // A bool flag to enable integrity verification of meta data blob + NydusSignature = "containerd.io/snapshot/nydus-signature" + + // A bool flag to mark the blob as a estargz data blob, set by the snapshotter. + StargzLayer = "containerd.io/snapshot/stargz" + + // volatileOpt is a key of an optional lablel to each snapshot. + // If this optional label of a snapshot is specified, when mounted to rootdir + // this snapshot will include volatile option + OverlayfsVolatileOpt = "containerd.io/snapshot/overlay.volatile" +) diff --git a/vendor/github.com/containerd/stargz-snapshotter/estargz/build.go b/vendor/github.com/containerd/stargz-snapshotter/estargz/build.go index 9ee97fc911053..0da3efe4c21e5 100644 --- a/vendor/github.com/containerd/stargz-snapshotter/estargz/build.go +++ b/vendor/github.com/containerd/stargz-snapshotter/estargz/build.go @@ -26,10 +26,10 @@ import ( "archive/tar" "bytes" "compress/gzip" + "context" "errors" "fmt" "io" - "io/ioutil" "os" "path" "runtime" @@ -48,6 +48,7 @@ type options struct { prioritizedFiles []string missedPrioritizedFiles *[]string compression Compression + ctx context.Context } type Option func(o *options) error @@ -104,6 +105,14 @@ func WithCompression(compression Compression) Option { } } +// WithContext specifies a context that can be used for clean canceleration. +func WithContext(ctx context.Context) Option { + return func(o *options) error { + o.ctx = ctx + return nil + } +} + // Blob is an eStargz blob. type Blob struct { io.ReadCloser @@ -139,12 +148,29 @@ func Build(tarBlob *io.SectionReader, opt ...Option) (_ *Blob, rErr error) { opts.compression = newGzipCompressionWithLevel(opts.compressionLevel) } layerFiles := newTempFiles() + ctx := opts.ctx + if ctx == nil { + ctx = context.Background() + } + done := make(chan struct{}) + defer close(done) + go func() { + select { + case <-done: + // nop + case <-ctx.Done(): + layerFiles.CleanupAll() + } + }() defer func() { if rErr != nil { if err := layerFiles.CleanupAll(); err != nil { rErr = fmt.Errorf("failed to cleanup tmp files: %v: %w", err, rErr) } } + if cErr := ctx.Err(); cErr != nil { + rErr = fmt.Errorf("error from context %q: %w", cErr, rErr) + } }() tarBlob, err := decompressBlob(tarBlob, layerFiles) if err != nil { @@ -506,12 +532,13 @@ func newTempFiles() *tempFiles { } type tempFiles struct { - files []*os.File - filesMu sync.Mutex + files []*os.File + filesMu sync.Mutex + cleanupOnce sync.Once } func (tf *tempFiles) TempFile(dir, pattern string) (*os.File, error) { - f, err := ioutil.TempFile(dir, pattern) + f, err := os.CreateTemp(dir, pattern) if err != nil { return nil, err } @@ -521,7 +548,14 @@ func (tf *tempFiles) TempFile(dir, pattern string) (*os.File, error) { return f, nil } -func (tf *tempFiles) CleanupAll() error { +func (tf *tempFiles) CleanupAll() (err error) { + tf.cleanupOnce.Do(func() { + err = tf.cleanupAll() + }) + return +} + +func (tf *tempFiles) cleanupAll() error { tf.filesMu.Lock() defer tf.filesMu.Unlock() var allErr []error diff --git a/vendor/github.com/containerd/stargz-snapshotter/estargz/estargz.go b/vendor/github.com/containerd/stargz-snapshotter/estargz/estargz.go index 4b655c14532fc..921e59ec6efc8 100644 --- a/vendor/github.com/containerd/stargz-snapshotter/estargz/estargz.go +++ b/vendor/github.com/containerd/stargz-snapshotter/estargz/estargz.go @@ -31,7 +31,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "os" "path" "sort" @@ -579,7 +578,7 @@ func (fr *fileReader) ReadAt(p []byte, off int64) (n int, err error) { return 0, fmt.Errorf("fileReader.ReadAt.decompressor.Reader: %v", err) } defer dr.Close() - if n, err := io.CopyN(ioutil.Discard, dr, off); n != off || err != nil { + if n, err := io.CopyN(io.Discard, dr, off); n != off || err != nil { return 0, fmt.Errorf("discard of %d bytes = %v, %v", off, n, err) } return io.ReadFull(dr, p) @@ -933,7 +932,7 @@ func (w *Writer) appendTar(r io.Reader, lossless bool) error { } } } - remainDest := ioutil.Discard + remainDest := io.Discard if lossless { remainDest = dst // Preserve the remaining bytes in lossless mode } diff --git a/vendor/github.com/containerd/stargz-snapshotter/estargz/testutil.go b/vendor/github.com/containerd/stargz-snapshotter/estargz/testutil.go index 1de13a4705be7..8f27dfb3ea27e 100644 --- a/vendor/github.com/containerd/stargz-snapshotter/estargz/testutil.go +++ b/vendor/github.com/containerd/stargz-snapshotter/estargz/testutil.go @@ -31,7 +31,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "reflect" "sort" @@ -287,11 +286,11 @@ func isSameTarGz(t *testing.T, controller TestingController, a, b []byte) bool { return false } - aFile, err := ioutil.ReadAll(aTar) + aFile, err := io.ReadAll(aTar) if err != nil { t.Fatal("failed to read tar payload of A") } - bFile, err := ioutil.ReadAll(bTar) + bFile, err := io.ReadAll(bTar) if err != nil { t.Fatal("failed to read tar payload of B") } diff --git a/vendor/github.com/containerd/stargz-snapshotter/fs/config/config.go b/vendor/github.com/containerd/stargz-snapshotter/fs/config/config.go new file mode 100644 index 0000000000000..5550c2fd5e2d5 --- /dev/null +++ b/vendor/github.com/containerd/stargz-snapshotter/fs/config/config.go @@ -0,0 +1,94 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/* + Copyright 2019 The Go Authors. All rights reserved. + Use of this source code is governed by a BSD-style + license that can be found in the NOTICE.md file. +*/ + +package config + +const ( + // TargetSkipVerifyLabel is a snapshot label key that indicates to skip content + // verification for the layer. + TargetSkipVerifyLabel = "containerd.io/snapshot/remote/stargz.skipverify" + + // TargetPrefetchSizeLabel is a snapshot label key that indicates size to prefetch + // the layer. If the layer is eStargz and contains prefetch landmarks, these config + // will be respeced. + TargetPrefetchSizeLabel = "containerd.io/snapshot/remote/stargz.prefetch" +) + +type Config struct { + HTTPCacheType string `toml:"http_cache_type"` + FSCacheType string `toml:"filesystem_cache_type"` + // ResolveResultEntryTTLSec is TTL (in sec) to cache resolved layers for + // future use. (default 120s) + ResolveResultEntryTTLSec int `toml:"resolve_result_entry_ttl_sec"` + ResolveResultEntry int `toml:"resolve_result_entry"` // deprecated + PrefetchSize int64 `toml:"prefetch_size"` + PrefetchTimeoutSec int64 `toml:"prefetch_timeout_sec"` + NoPrefetch bool `toml:"noprefetch"` + NoBackgroundFetch bool `toml:"no_background_fetch"` + Debug bool `toml:"debug"` + AllowNoVerification bool `toml:"allow_no_verification"` + DisableVerification bool `toml:"disable_verification"` + MaxConcurrency int64 `toml:"max_concurrency"` + NoPrometheus bool `toml:"no_prometheus"` + + // BlobConfig is config for layer blob management. + BlobConfig `toml:"blob"` + + // DirectoryCacheConfig is config for directory-based cache. + DirectoryCacheConfig `toml:"directory_cache"` + + FuseConfig `toml:"fuse"` +} + +type BlobConfig struct { + ValidInterval int64 `toml:"valid_interval"` + CheckAlways bool `toml:"check_always"` + // ChunkSize is the granularity at which background fetch and on-demand reads + // are fetched from the remote registry. + ChunkSize int64 `toml:"chunk_size"` + FetchTimeoutSec int64 `toml:"fetching_timeout_sec"` + ForceSingleRangeMode bool `toml:"force_single_range_mode"` + // PrefetchChunkSize is the maximum bytes transferred per http GET from remote registry + // during prefetch. It is recommended to have PrefetchChunkSize > ChunkSize. + // If PrefetchChunkSize < ChunkSize prefetch bytes will be fetched as a single http GET, + // else total GET requests for prefetch = ceil(PrefetchSize / PrefetchChunkSize). + PrefetchChunkSize int64 `toml:"prefetch_chunk_size"` + + MaxRetries int `toml:"max_retries"` + MinWaitMSec int `toml:"min_wait_msec"` + MaxWaitMSec int `toml:"max_wait_msec"` +} + +type DirectoryCacheConfig struct { + MaxLRUCacheEntry int `toml:"max_lru_cache_entry"` + MaxCacheFds int `toml:"max_cache_fds"` + SyncAdd bool `toml:"sync_add"` + Direct bool `toml:"direct" default:"true"` +} + +type FuseConfig struct { + // AttrTimeout defines overall timeout attribute for a file system in seconds. + AttrTimeout int64 `toml:"attr_timeout"` + + // EntryTimeout defines TTL for directory, name lookup in seconds. + EntryTimeout int64 `toml:"entry_timeout"` +} diff --git a/vendor/github.com/containerd/stargz-snapshotter/fs/source/source.go b/vendor/github.com/containerd/stargz-snapshotter/fs/source/source.go new file mode 100644 index 0000000000000..17b20ecde0205 --- /dev/null +++ b/vendor/github.com/containerd/stargz-snapshotter/fs/source/source.go @@ -0,0 +1,202 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package source + +import ( + "context" + "fmt" + "strings" + + "github.com/containerd/containerd/images" + "github.com/containerd/containerd/labels" + "github.com/containerd/containerd/reference" + "github.com/containerd/containerd/remotes/docker" + "github.com/containerd/stargz-snapshotter/fs/config" + digest "github.com/opencontainers/go-digest" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" +) + +// GetSources is a function for converting snapshot labels into typed blob sources +// information. This package defines a default converter which provides source +// information based on some labels but implementations aren't required to use labels. +// Implementations are allowed to return several sources (registry config + image refs) +// about the blob. +type GetSources func(labels map[string]string) (source []Source, err error) + +// RegistryHosts returns a list of registries that provides the specified image. +type RegistryHosts func(reference.Spec) ([]docker.RegistryHost, error) + +// Source is a typed blob source information. This contains information about +// a blob stored in registries and some contexts of the blob. +type Source struct { + + // Hosts is a registry configuration where this blob is stored. + Hosts RegistryHosts + + // Name is an image reference which contains this blob. + Name reference.Spec + + // Target is a descriptor of this blob. + Target ocispec.Descriptor + + // Manifest is an image manifest which contains the blob. This will + // be used by the filesystem to pre-resolve some layers contained in + // the manifest. + // Currently, only layer digests (Manifest.Layers.Digest) will be used. + Manifest ocispec.Manifest +} + +const ( + // targetRefLabel is a label which contains image reference. + targetRefLabel = "containerd.io/snapshot/remote/stargz.reference" + + // targetDigestLabel is a label which contains layer digest. + targetDigestLabel = "containerd.io/snapshot/remote/stargz.digest" + + // targetImageLayersLabel is a label which contains layer digests contained in + // the target image. + targetImageLayersLabel = "containerd.io/snapshot/remote/stargz.layers" + + // targetImageURLsLabelPrefix is a label prefix which constructs a map from the layer index to + // urls of the layer descriptor. + targetImageURLsLabelPrefix = "containerd.io/snapshot/remote/urls." + + // targetURsLLabel is a label which contains layer URL. This is only used to pass URL from containerd + // to snapshotter. + targetURLsLabel = "containerd.io/snapshot/remote/urls" +) + +// FromDefaultLabels returns a function for converting snapshot labels to +// source information based on labels. +func FromDefaultLabels(hosts RegistryHosts) GetSources { + return func(labels map[string]string) ([]Source, error) { + refStr, ok := labels[targetRefLabel] + if !ok { + return nil, fmt.Errorf("reference hasn't been passed") + } + refspec, err := reference.Parse(refStr) + if err != nil { + return nil, err + } + + digestStr, ok := labels[targetDigestLabel] + if !ok { + return nil, fmt.Errorf("digest hasn't been passed") + } + target, err := digest.Parse(digestStr) + if err != nil { + return nil, err + } + + var neighboringLayers []ocispec.Descriptor + if l, ok := labels[targetImageLayersLabel]; ok { + layersStr := strings.Split(l, ",") + for i, l := range layersStr { + d, err := digest.Parse(l) + if err != nil { + return nil, err + } + if d.String() != target.String() { + desc := ocispec.Descriptor{Digest: d} + if urls, ok := labels[targetImageURLsLabelPrefix+fmt.Sprintf("%d", i)]; ok { + desc.URLs = strings.Split(urls, ",") + } + neighboringLayers = append(neighboringLayers, desc) + } + } + } + + targetDesc := ocispec.Descriptor{ + Digest: target, + Annotations: labels, + } + if targetURLs, ok := labels[targetURLsLabel]; ok { + targetDesc.URLs = append(targetDesc.URLs, strings.Split(targetURLs, ",")...) + } + + return []Source{ + { + Hosts: hosts, + Name: refspec, + Target: targetDesc, + Manifest: ocispec.Manifest{Layers: append([]ocispec.Descriptor{targetDesc}, neighboringLayers...)}, + }, + }, nil + } +} + +// AppendDefaultLabelsHandlerWrapper makes a handler which appends image's basic +// information to each layer descriptor as annotations during unpack. These +// annotations will be passed to this remote snapshotter as labels and used to +// construct source information. +func AppendDefaultLabelsHandlerWrapper(ref string, prefetchSize int64) func(f images.Handler) images.Handler { + return func(f images.Handler) images.Handler { + return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { + children, err := f.Handle(ctx, desc) + if err != nil { + return nil, err + } + switch desc.MediaType { + case ocispec.MediaTypeImageManifest, images.MediaTypeDockerSchema2Manifest: + for i := range children { + c := &children[i] + if images.IsLayerType(c.MediaType) { + if c.Annotations == nil { + c.Annotations = make(map[string]string) + } + c.Annotations[targetRefLabel] = ref + c.Annotations[targetDigestLabel] = c.Digest.String() + var layers string + for i, l := range children[i:] { + if images.IsLayerType(l.MediaType) { + ls := fmt.Sprintf("%s,", l.Digest.String()) + // This avoids the label hits the size limitation. + // Skipping layers is allowed here and only affects performance. + if err := labels.Validate(targetImageLayersLabel, layers+ls); err != nil { + break + } + layers += ls + + // Store URLs of the neighbouring layer as well. + urlsKey := targetImageURLsLabelPrefix + fmt.Sprintf("%d", i) + c.Annotations[urlsKey] = appendWithValidation(urlsKey, l.URLs) + } + } + c.Annotations[targetImageLayersLabel] = strings.TrimSuffix(layers, ",") + c.Annotations[config.TargetPrefetchSizeLabel] = fmt.Sprintf("%d", prefetchSize) + + // store URL in annotation to let containerd to pass it to the snapshotter + c.Annotations[targetURLsLabel] = appendWithValidation(targetURLsLabel, c.URLs) + } + } + } + return children, nil + }) + } +} + +func appendWithValidation(key string, values []string) string { + var v string + for _, u := range values { + s := fmt.Sprintf("%s,", u) + if err := labels.Validate(key, v+s); err != nil { + break + } + v += s + } + return strings.TrimSuffix(v, ",") +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 542dbc249c834..ed51088e0f837 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -251,10 +251,15 @@ github.com/containerd/go-cni # github.com/containerd/go-runc v1.0.0 ## explicit; go 1.13 github.com/containerd/go-runc +# github.com/containerd/nydus-snapshotter v0.3.0-alpha.5 +## explicit; go 1.17 +github.com/containerd/nydus-snapshotter/pkg/label # github.com/containerd/stargz-snapshotter v0.11.3 ## explicit; go 1.16 +github.com/containerd/stargz-snapshotter/fs/config +github.com/containerd/stargz-snapshotter/fs/source github.com/containerd/stargz-snapshotter/snapshot/overlayutils -# github.com/containerd/stargz-snapshotter/estargz v0.11.3 +# github.com/containerd/stargz-snapshotter/estargz v0.11.4 ## explicit; go 1.16 github.com/containerd/stargz-snapshotter/estargz github.com/containerd/stargz-snapshotter/estargz/errorutil