| 1 |
package registry |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"maps" |
| 6 |
"strconv" |
| 7 |
|
| 8 |
"github.com/containerd/containerd/v2/core/content" |
| 9 |
"github.com/containerd/containerd/v2/core/remotes/docker" |
| 10 |
"github.com/containerd/containerd/v2/core/snapshots" |
| 11 |
"github.com/containerd/containerd/v2/pkg/snapshotters" |
| 12 |
|
| 13 |
"github.com/distribution/reference" |
| 14 |
"github.com/moby/buildkit/cache/remotecache" |
| 15 |
"github.com/moby/buildkit/session" |
| 16 |
"github.com/moby/buildkit/util/compression" |
| 17 |
"github.com/moby/buildkit/util/contentutil" |
| 18 |
"github.com/moby/buildkit/util/estargz" |
| 19 |
"github.com/moby/buildkit/util/push" |
| 20 |
"github.com/moby/buildkit/util/resolver" |
| 21 |
resolverconfig "github.com/moby/buildkit/util/resolver/config" |
| 22 |
"github.com/moby/buildkit/util/resolver/limited" |
| 23 |
digest "github.com/opencontainers/go-digest" |
| 24 |
ocispecs "github.com/opencontainers/image-spec/specs-go/v1" |
| 25 |
"github.com/pkg/errors" |
| 26 |
) |
| 27 |
|
| 28 |
func canonicalizeRef(rawRef string) (reference.Named, error) { |
| 29 |
if rawRef == "" { |
| 30 |
return nil, errors.New("missing ref") |
| 31 |
} |
| 32 |
parsed, err := reference.ParseNormalizedNamed(rawRef) |
| 33 |
if err != nil { |
| 34 |
return nil, err |
| 35 |
} |
| 36 |
parsed = reference.TagNameOnly(parsed) |
| 37 |
return parsed, nil |
| 38 |
} |
| 39 |
|
| 40 |
const ( |
| 41 |
attrRef = "ref" |
| 42 |
attrImageManifest = "image-manifest" |
| 43 |
attrOCIMediatypes = "oci-mediatypes" |
| 44 |
attrInsecure = "registry.insecure" |
| 45 |
) |
| 46 |
|
| 47 |
type exporter struct { |
| 48 |
remotecache.Exporter |
| 49 |
} |
| 50 |
|
| 51 |
func (*exporter) Name() string { |
| 52 |
return "exporting cache to registry" |
| 53 |
} |
| 54 |
|
| 55 |
func ResolveCacheExporterFunc(sm *session.Manager, hosts docker.RegistryHosts) remotecache.ResolveCacheExporterFunc { |
| 56 |
return func(ctx context.Context, g session.Group, attrs map[string]string) (remotecache.Exporter, error) { |
| 57 |
compressionConfig, err := compression.ParseAttributes(attrs) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
ref, err := canonicalizeRef(attrs[attrRef]) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
refString := ref.String() |
| 66 |
ociMediatypes := true |
| 67 |
if v, ok := attrs[attrOCIMediatypes]; ok { |
| 68 |
b, err := strconv.ParseBool(v) |
| 69 |
if err != nil { |
| 70 |
return nil, errors.Wrapf(err, "failed to parse %s", attrOCIMediatypes) |
| 71 |
} |
| 72 |
ociMediatypes = b |
| 73 |
} |
| 74 |
imageManifest := true |
| 75 |
if v, ok := attrs[attrImageManifest]; ok { |
| 76 |
b, err := strconv.ParseBool(v) |
| 77 |
if err != nil { |
| 78 |
return nil, errors.Wrapf(err, "failed to parse %s", attrImageManifest) |
| 79 |
} |
| 80 |
imageManifest = b |
| 81 |
} else if !ociMediatypes { |
| 82 |
imageManifest = false |
| 83 |
} |
| 84 |
insecure := false |
| 85 |
if v, ok := attrs[attrInsecure]; ok { |
| 86 |
b, err := strconv.ParseBool(v) |
| 87 |
if err != nil { |
| 88 |
return nil, errors.Wrapf(err, "failed to parse %s", attrInsecure) |
| 89 |
} |
| 90 |
insecure = b |
| 91 |
} |
| 92 |
|
| 93 |
scope, hosts := registryConfig(hosts, ref, "push", insecure) |
| 94 |
remote := resolver.DefaultPool.GetResolver(hosts, refString, scope, sm, g) |
| 95 |
pusher, err := push.Pusher(ctx, remote, refString) |
| 96 |
if err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
return &exporter{remotecache.NewExporter(contentutil.FromPusher(pusher), refString, ociMediatypes, imageManifest, compressionConfig)}, nil |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func ResolveCacheImporterFunc(sm *session.Manager, cs content.Store, hosts docker.RegistryHosts) remotecache.ResolveCacheImporterFunc { |
| 104 |
return func(ctx context.Context, g session.Group, attrs map[string]string) (remotecache.Importer, ocispecs.Descriptor, error) { |
| 105 |
ref, err := canonicalizeRef(attrs[attrRef]) |
| 106 |
if err != nil { |
| 107 |
return nil, ocispecs.Descriptor{}, err |
| 108 |
} |
| 109 |
refString := ref.String() |
| 110 |
insecure := false |
| 111 |
if v, ok := attrs[attrInsecure]; ok { |
| 112 |
b, err := strconv.ParseBool(v) |
| 113 |
if err != nil { |
| 114 |
return nil, ocispecs.Descriptor{}, errors.Wrapf(err, "failed to parse %s", attrInsecure) |
| 115 |
} |
| 116 |
insecure = b |
| 117 |
} |
| 118 |
|
| 119 |
scope, hosts := registryConfig(hosts, ref, "pull", insecure) |
| 120 |
remote := resolver.DefaultPool.GetResolver(hosts, refString, scope, sm, g) |
| 121 |
xref, desc, err := remote.Resolve(ctx, refString) |
| 122 |
if err != nil { |
| 123 |
return nil, ocispecs.Descriptor{}, err |
| 124 |
} |
| 125 |
fetcher, err := remote.Fetcher(ctx, xref) |
| 126 |
if err != nil { |
| 127 |
return nil, ocispecs.Descriptor{}, err |
| 128 |
} |
| 129 |
src := &withDistributionSourceLabel{ |
| 130 |
Provider: contentutil.FromFetcher(limited.Default.WrapFetcher(fetcher, refString)), |
| 131 |
ref: refString, |
| 132 |
source: cs, |
| 133 |
} |
| 134 |
return remotecache.NewImporter(src), desc, nil |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
type withDistributionSourceLabel struct { |
| 139 |
content.Provider |
| 140 |
ref string |
| 141 |
source content.Manager |
| 142 |
} |
| 143 |
|
| 144 |
var _ remotecache.DistributionSourceLabelSetter = &withDistributionSourceLabel{} |
| 145 |
|
| 146 |
func (dsl *withDistributionSourceLabel) SetDistributionSourceLabel(ctx context.Context, dgst digest.Digest) error { |
| 147 |
hf, err := docker.AppendDistributionSourceLabel(dsl.source, dsl.ref) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
_, err = hf(ctx, ocispecs.Descriptor{Digest: dgst}) |
| 152 |
return err |
| 153 |
} |
| 154 |
|
| 155 |
func (dsl *withDistributionSourceLabel) SetDistributionSourceAnnotation(desc ocispecs.Descriptor) ocispecs.Descriptor { |
| 156 |
if desc.Annotations == nil { |
| 157 |
desc.Annotations = map[string]string{} |
| 158 |
} |
| 159 |
desc.Annotations["containerd.io/distribution.source.ref"] = dsl.ref |
| 160 |
return desc |
| 161 |
} |
| 162 |
|
| 163 |
func (dsl *withDistributionSourceLabel) SnapshotLabels(descs []ocispecs.Descriptor, index int) map[string]string { |
| 164 |
if len(descs) < index { |
| 165 |
return nil |
| 166 |
} |
| 167 |
labels := snapshots.FilterInheritedLabels(descs[index].Annotations) |
| 168 |
if labels == nil { |
| 169 |
labels = make(map[string]string) |
| 170 |
} |
| 171 |
maps.Copy(labels, estargz.SnapshotLabels(dsl.ref, descs, index)) |
| 172 |
labels[snapshotters.TargetRefLabel] = dsl.ref |
| 173 |
return labels |
| 174 |
} |
| 175 |
|
| 176 |
func registryConfig(hosts docker.RegistryHosts, ref reference.Named, scope string, insecure bool) (string, docker.RegistryHosts) { |
| 177 |
if insecure { |
| 178 |
insecureTrue := true |
| 179 |
httpTrue := true |
| 180 |
hosts = resolver.NewRegistryConfig(map[string]resolverconfig.RegistryConfig{ |
| 181 |
reference.Domain(ref): { |
| 182 |
Insecure: &insecureTrue, |
| 183 |
PlainHTTP: &httpTrue, |
| 184 |
}, |
| 185 |
}) |
| 186 |
scope += ":insecure" |
| 187 |
} |
| 188 |
return scope, hosts |
| 189 |
} |