| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package service |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/containerd/containerd/reference" |
| 24 |
"github.com/containerd/stargz-snapshotter/fs/source" |
| 25 |
digest "github.com/opencontainers/go-digest" |
| 26 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 27 |
) |
| 28 |
|
| 29 |
// TODO: switch to "github.com/containerd/containerd/pkg/snapshotters" once all tools using |
| 30 |
// |
| 31 |
// stargz-snapshotter (e.g. k3s) move to containerd version where that pkg is available. |
| 32 |
const ( |
| 33 |
// targetRefLabel is a label which contains image reference and will be passed |
| 34 |
// to snapshotters. |
| 35 |
targetRefLabel = "containerd.io/snapshot/cri.image-ref" |
| 36 |
// targetLayerDigestLabel is a label which contains layer digest and will be passed |
| 37 |
// to snapshotters. |
| 38 |
targetLayerDigestLabel = "containerd.io/snapshot/cri.layer-digest" |
| 39 |
// targetImageLayersLabel is a label which contains layer digests contained in |
| 40 |
// the target image and will be passed to snapshotters for preparing layers in |
| 41 |
// parallel. Skipping some layers is allowed and only affects performance. |
| 42 |
targetImageLayersLabel = "containerd.io/snapshot/cri.image-layers" |
| 43 |
) |
| 44 |
|
| 45 |
const ( |
| 46 |
// targetImageURLsLabelPrefix is a label prefix which constructs a map from the layer index to |
| 47 |
// urls of the layer descriptor. This isn't contained in the set of the labels passed from CRI plugin but |
| 48 |
// some clients (e.g. nerdctl) passes this for preserving url field in the OCI descriptor. |
| 49 |
targetImageURLsLabelPrefix = "containerd.io/snapshot/remote/urls." |
| 50 |
|
| 51 |
// targetURsLLabel is a label which contains layer URL. This is only used to pass URL from containerd |
| 52 |
// to snapshotter. This isn't contained in the set of the labels passed from CRI plugin but |
| 53 |
// some clients (e.g. nerdctl) passes this for preserving url field in the OCI descriptor. |
| 54 |
targetURLsLabel = "containerd.io/snapshot/remote/urls" |
| 55 |
) |
| 56 |
|
| 57 |
func sourceFromCRILabels(hosts source.RegistryHosts) source.GetSources { |
| 58 |
return func(labels map[string]string) ([]source.Source, error) { |
| 59 |
refStr, ok := labels[targetRefLabel] |
| 60 |
if !ok { |
| 61 |
return nil, fmt.Errorf("reference hasn't been passed") |
| 62 |
} |
| 63 |
refspec, err := reference.Parse(refStr) |
| 64 |
if err != nil { |
| 65 |
return nil, err |
| 66 |
} |
| 67 |
|
| 68 |
digestStr, ok := labels[targetLayerDigestLabel] |
| 69 |
if !ok { |
| 70 |
return nil, fmt.Errorf("digest hasn't been passed") |
| 71 |
} |
| 72 |
target, err := digest.Parse(digestStr) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
|
| 77 |
var neighboringLayers []ocispec.Descriptor |
| 78 |
if l, ok := labels[targetImageLayersLabel]; ok { |
| 79 |
layersStr := strings.Split(l, ",") |
| 80 |
for i, l := range layersStr { |
| 81 |
d, err := digest.Parse(l) |
| 82 |
if err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
if d.String() != target.String() { |
| 86 |
desc := ocispec.Descriptor{Digest: d} |
| 87 |
if urls, ok := labels[targetImageURLsLabelPrefix+fmt.Sprintf("%d", i)]; ok { |
| 88 |
desc.URLs = strings.Split(urls, ",") |
| 89 |
} |
| 90 |
neighboringLayers = append(neighboringLayers, desc) |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
targetDesc := ocispec.Descriptor{ |
| 96 |
Digest: target, |
| 97 |
Annotations: labels, |
| 98 |
} |
| 99 |
if targetURLs, ok := labels[targetURLsLabel]; ok { |
| 100 |
targetDesc.URLs = append(targetDesc.URLs, strings.Split(targetURLs, ",")...) |
| 101 |
} |
| 102 |
|
| 103 |
return []source.Source{ |
| 104 |
{ |
| 105 |
Hosts: hosts, |
| 106 |
Name: refspec, |
| 107 |
Target: targetDesc, |
| 108 |
Manifest: ocispec.Manifest{Layers: append([]ocispec.Descriptor{targetDesc}, neighboringLayers...)}, |
| 109 |
}, |
| 110 |
}, nil |
| 111 |
} |
| 112 |
} |