|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +// Copied from https://github.com/containerd/stargz-snapshotter/blob/v0.11.4/fs/source/source.go |
| 15 | +// |
| 16 | +// Taking Stargz as a code dependency required dropping support for Go 1.16. |
| 17 | +// Used to add Stargz annotations to pull requests. |
| 18 | + |
| 19 | +/* |
| 20 | + Copyright The containerd Authors. |
| 21 | +
|
| 22 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 23 | + you may not use this file except in compliance with the License. |
| 24 | + You may obtain a copy of the License at |
| 25 | +
|
| 26 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | +
|
| 28 | + Unless required by applicable law or agreed to in writing, software |
| 29 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 30 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | + See the License for the specific language governing permissions and |
| 32 | + limitations under the License. |
| 33 | +*/ |
| 34 | + |
| 35 | +package source |
| 36 | + |
| 37 | +import ( |
| 38 | + "context" |
| 39 | + "fmt" |
| 40 | + "strings" |
| 41 | + |
| 42 | + "github.com/containerd/containerd/images" |
| 43 | + "github.com/containerd/containerd/labels" |
| 44 | + "github.com/firecracker-microvm/firecracker-containerd/snapshotter/internal/integtest/stargz/fs/config" |
| 45 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 46 | +) |
| 47 | + |
| 48 | +const ( |
| 49 | + // targetRefLabel is a label which contains image reference. |
| 50 | + targetRefLabel = "containerd.io/snapshot/remote/stargz.reference" |
| 51 | + |
| 52 | + // targetDigestLabel is a label which contains layer digest. |
| 53 | + targetDigestLabel = "containerd.io/snapshot/remote/stargz.digest" |
| 54 | + |
| 55 | + // targetImageLayersLabel is a label which contains layer digests contained in |
| 56 | + // the target image. |
| 57 | + targetImageLayersLabel = "containerd.io/snapshot/remote/stargz.layers" |
| 58 | + |
| 59 | + // targetImageURLsLabelPrefix is a label prefix which constructs a map from the layer index to |
| 60 | + // urls of the layer descriptor. |
| 61 | + targetImageURLsLabelPrefix = "containerd.io/snapshot/remote/urls." |
| 62 | + |
| 63 | + // targetURsLLabel is a label which contains layer URL. This is only used to pass URL from containerd |
| 64 | + // to snapshotter. |
| 65 | + targetURLsLabel = "containerd.io/snapshot/remote/urls" |
| 66 | +) |
| 67 | + |
| 68 | +// AppendDefaultLabelsHandlerWrapper makes a handler which appends image's basic |
| 69 | +// information to each layer descriptor as annotations during unpack. These |
| 70 | +// annotations will be passed to this remote snapshotter as labels and used to |
| 71 | +// construct source information. |
| 72 | +func AppendDefaultLabelsHandlerWrapper(ref string, prefetchSize int64) func(f images.Handler) images.Handler { |
| 73 | + return func(f images.Handler) images.Handler { |
| 74 | + return images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { |
| 75 | + children, err := f.Handle(ctx, desc) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + switch desc.MediaType { |
| 80 | + case ocispec.MediaTypeImageManifest, images.MediaTypeDockerSchema2Manifest: |
| 81 | + for i := range children { |
| 82 | + c := &children[i] |
| 83 | + if images.IsLayerType(c.MediaType) { |
| 84 | + if c.Annotations == nil { |
| 85 | + c.Annotations = make(map[string]string) |
| 86 | + } |
| 87 | + c.Annotations[targetRefLabel] = ref |
| 88 | + c.Annotations[targetDigestLabel] = c.Digest.String() |
| 89 | + var layers string |
| 90 | + for i, l := range children[i:] { |
| 91 | + if images.IsLayerType(l.MediaType) { |
| 92 | + ls := fmt.Sprintf("%s,", l.Digest.String()) |
| 93 | + // This avoids the label hits the size limitation. |
| 94 | + // Skipping layers is allowed here and only affects performance. |
| 95 | + if err := labels.Validate(targetImageLayersLabel, layers+ls); err != nil { |
| 96 | + break |
| 97 | + } |
| 98 | + layers += ls |
| 99 | + |
| 100 | + // Store URLs of the neighbouring layer as well. |
| 101 | + urlsKey := targetImageURLsLabelPrefix + fmt.Sprintf("%d", i) |
| 102 | + c.Annotations[urlsKey] = appendWithValidation(urlsKey, l.URLs) |
| 103 | + } |
| 104 | + } |
| 105 | + c.Annotations[targetImageLayersLabel] = strings.TrimSuffix(layers, ",") |
| 106 | + c.Annotations[config.TargetPrefetchSizeLabel] = fmt.Sprintf("%d", prefetchSize) |
| 107 | + |
| 108 | + // store URL in annotation to let containerd to pass it to the snapshotter |
| 109 | + c.Annotations[targetURLsLabel] = appendWithValidation(targetURLsLabel, c.URLs) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return children, nil |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func appendWithValidation(key string, values []string) string { |
| 119 | + var v string |
| 120 | + for _, u := range values { |
| 121 | + s := fmt.Sprintf("%s,", u) |
| 122 | + if err := labels.Validate(key, v+s); err != nil { |
| 123 | + break |
| 124 | + } |
| 125 | + v += s |
| 126 | + } |
| 127 | + return strings.TrimSuffix(v, ",") |
| 128 | +} |
0 commit comments