2017-09-05 18:26:03 +00:00
|
|
|
package imageutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
"github.com/containerd/containerd/images"
|
2017-09-14 05:47:37 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
|
|
|
"github.com/containerd/containerd/reference"
|
2017-09-05 18:26:03 +00:00
|
|
|
"github.com/containerd/containerd/remotes"
|
2017-09-22 22:49:37 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-06-24 05:52:19 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-09-05 18:26:03 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IngesterProvider interface {
|
|
|
|
content.Ingester
|
|
|
|
content.Provider
|
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
func Config(ctx context.Context, str string, resolver remotes.Resolver, ingester IngesterProvider, platform *specs.Platform) (digest.Digest, []byte, error) {
|
|
|
|
// TODO: fix containerd to take struct instead of string
|
|
|
|
platformStr := platforms.Default()
|
|
|
|
if platform != nil {
|
|
|
|
platformStr = platforms.Format(*platform)
|
2018-05-22 16:18:36 +00:00
|
|
|
}
|
2017-09-14 05:47:37 +00:00
|
|
|
ref, err := reference.Parse(str)
|
2017-09-05 18:26:03 +00:00
|
|
|
if err != nil {
|
2017-09-22 22:49:37 +00:00
|
|
|
return "", nil, errors.WithStack(err)
|
2017-09-14 05:47:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
desc := specs.Descriptor{
|
2018-06-06 08:02:01 +00:00
|
|
|
Digest: ref.Digest(),
|
|
|
|
}
|
|
|
|
if desc.Digest != "" {
|
|
|
|
ra, err := ingester.ReaderAt(ctx, desc)
|
2017-09-14 05:47:37 +00:00
|
|
|
if err == nil {
|
2018-06-06 08:02:01 +00:00
|
|
|
desc.Size = ra.Size()
|
2017-09-22 22:49:37 +00:00
|
|
|
mt, err := DetectManifestMediaType(ra)
|
2017-09-16 00:58:53 +00:00
|
|
|
if err == nil {
|
2018-06-06 08:02:01 +00:00
|
|
|
desc.MediaType = mt
|
2017-09-14 05:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
2018-06-06 08:02:01 +00:00
|
|
|
// use resolver if desc is incomplete
|
|
|
|
if desc.MediaType == "" {
|
|
|
|
_, desc, err = resolver.Resolve(ctx, ref.String())
|
2017-09-14 05:47:37 +00:00
|
|
|
if err != nil {
|
2017-09-22 22:49:37 +00:00
|
|
|
return "", nil, err
|
2017-09-14 05:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher, err := resolver.Fetcher(ctx, ref.String())
|
2017-09-05 18:26:03 +00:00
|
|
|
if err != nil {
|
2017-09-22 22:49:37 +00:00
|
|
|
return "", nil, err
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlers := []images.Handler{
|
|
|
|
remotes.FetchHandler(ingester, fetcher),
|
2018-06-24 05:52:19 +00:00
|
|
|
childrenConfigHandler(ingester, platformStr),
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
2018-06-06 08:02:01 +00:00
|
|
|
if err := images.Dispatch(ctx, images.Handlers(handlers...), desc); err != nil {
|
2017-09-22 22:49:37 +00:00
|
|
|
return "", nil, err
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
2018-06-24 05:52:19 +00:00
|
|
|
config, err := images.Config(ctx, ingester, desc, platformStr)
|
2017-09-11 01:11:42 +00:00
|
|
|
if err != nil {
|
2017-09-22 22:49:37 +00:00
|
|
|
return "", nil, err
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:02:01 +00:00
|
|
|
dt, err := content.ReadBlob(ctx, ingester, config)
|
2017-09-22 22:49:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc.Digest, dt, nil
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:42:27 +00:00
|
|
|
func childrenConfigHandler(provider content.Provider, platform string) images.HandlerFunc {
|
2018-06-24 05:52:19 +00:00
|
|
|
return func(ctx context.Context, desc specs.Descriptor) ([]specs.Descriptor, error) {
|
|
|
|
var descs []specs.Descriptor
|
2017-09-05 18:26:03 +00:00
|
|
|
switch desc.MediaType {
|
2018-06-24 05:52:19 +00:00
|
|
|
case images.MediaTypeDockerSchema2Manifest, specs.MediaTypeImageManifest:
|
2018-06-06 08:02:01 +00:00
|
|
|
p, err := content.ReadBlob(ctx, provider, desc)
|
2017-09-05 18:26:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): We just assume oci manifest, for now. There may be
|
|
|
|
// subtle differences from the docker version.
|
2018-06-24 05:52:19 +00:00
|
|
|
var manifest specs.Manifest
|
2017-09-05 18:26:03 +00:00
|
|
|
if err := json.Unmarshal(p, &manifest); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
descs = append(descs, manifest.Config)
|
2018-06-24 05:52:19 +00:00
|
|
|
case images.MediaTypeDockerSchema2ManifestList, specs.MediaTypeImageIndex:
|
2018-06-06 08:02:01 +00:00
|
|
|
p, err := content.ReadBlob(ctx, provider, desc)
|
2017-09-05 18:26:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
var index specs.Index
|
2017-09-05 18:26:03 +00:00
|
|
|
if err := json.Unmarshal(p, &index); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:42:27 +00:00
|
|
|
if platform != "" {
|
2018-04-03 09:37:16 +00:00
|
|
|
pf, err := platforms.Parse(platform)
|
2017-12-12 21:42:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-03 09:37:16 +00:00
|
|
|
matcher := platforms.NewMatcher(pf)
|
2017-12-12 21:42:27 +00:00
|
|
|
|
|
|
|
for _, d := range index.Manifests {
|
|
|
|
if d.Platform == nil || matcher.Match(*d.Platform) {
|
|
|
|
descs = append(descs, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
descs = append(descs, index.Manifests...)
|
|
|
|
}
|
2018-06-24 05:52:19 +00:00
|
|
|
case images.MediaTypeDockerSchema2Config, specs.MediaTypeImageConfig:
|
2017-09-05 18:26:03 +00:00
|
|
|
// childless data types.
|
|
|
|
return nil, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("encountered unknown type %v; children may not be fetched", desc.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return descs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
// specs.MediaTypeImageManifest, // TODO: detect schema1/manifest-list
|
2017-09-22 22:49:37 +00:00
|
|
|
func DetectManifestMediaType(ra content.ReaderAt) (string, error) {
|
2017-09-16 00:58:53 +00:00
|
|
|
// TODO: schema1
|
2017-09-05 18:26:03 +00:00
|
|
|
|
2017-09-16 00:58:53 +00:00
|
|
|
p := make([]byte, ra.Size())
|
|
|
|
if _, err := ra.ReadAt(p, 0); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var mfst struct {
|
2018-06-26 22:24:33 +00:00
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Config json.RawMessage `json:"config"`
|
2017-09-16 00:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(p, &mfst); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-05 18:26:03 +00:00
|
|
|
|
2018-06-26 22:24:33 +00:00
|
|
|
if mfst.MediaType != "" {
|
|
|
|
return mfst.MediaType, nil
|
|
|
|
}
|
2017-09-16 00:58:53 +00:00
|
|
|
if mfst.Config != nil {
|
2017-10-13 17:08:47 +00:00
|
|
|
return images.MediaTypeDockerSchema2Manifest, nil
|
2017-09-16 00:58:53 +00:00
|
|
|
}
|
2017-10-13 18:54:26 +00:00
|
|
|
return images.MediaTypeDockerSchema2ManifestList, nil
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|