forked from openshift/origin
/
manifesthandler.go
71 lines (57 loc) · 2.57 KB
/
manifesthandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package server
import (
"fmt"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
)
// A ManifestHandler defines a common set of operations on all versions of manifest schema.
type ManifestHandler interface {
// Config returns a blob with image configuration associated with the manifest. This applies only to
// manifet schema 2.
Config(ctx context.Context) ([]byte, error)
// Digest returns manifest's digest.
Digest() (manifestDigest digest.Digest, err error)
// Manifest returns a deserialized manifest object.
Manifest() distribution.Manifest
// Layers returns image layers and a value for the dockerLayersOrder annotation.
Layers(ctx context.Context) (order string, layers []imageapiv1.ImageLayer, err error)
// Payload returns manifest's media type, complete payload with signatures and canonical payload without
// signatures or an error if the information could not be fetched.
Payload() (mediaType string, payload []byte, canonical []byte, err error)
// Verify returns an error if the contained manifest is not valid or has missing dependencies.
Verify(ctx context.Context, skipDependencyVerification bool) error
}
// NewManifestHandler creates a manifest handler for the given manifest.
func NewManifestHandler(repo *repository, manifest distribution.Manifest) (ManifestHandler, error) {
switch t := manifest.(type) {
case *schema1.SignedManifest:
return &manifestSchema1Handler{repo: repo, manifest: t}, nil
case *schema2.DeserializedManifest:
return &manifestSchema2Handler{repo: repo, manifest: t}, nil
default:
return nil, fmt.Errorf("unsupported manifest type %T", manifest)
}
}
// NewManifestHandlerFromImage creates a new manifest handler for a manifest stored in the given image.
func NewManifestHandlerFromImage(repo *repository, image *imageapiv1.Image) (ManifestHandler, error) {
var (
manifest distribution.Manifest
err error
)
switch image.DockerImageManifestMediaType {
case "", schema1.MediaTypeManifest:
manifest, err = unmarshalManifestSchema1([]byte(image.DockerImageManifest), image.DockerImageSignatures)
case schema2.MediaTypeManifest:
manifest, err = unmarshalManifestSchema2([]byte(image.DockerImageManifest))
default:
return nil, fmt.Errorf("unsupported manifest media type %s", image.DockerImageManifestMediaType)
}
if err != nil {
return nil, err
}
return NewManifestHandler(repo, manifest)
}