Skip to content

Commit

Permalink
make the image-tool work with -rc5
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <[email protected]>
  • Loading branch information
zhouhao committed Jun 6, 2017
1 parent 38db2e4 commit 6ff2eb1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 39 deletions.
43 changes: 26 additions & 17 deletions image/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,22 @@ import (
"strings"

"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)

type descriptor struct {
MediaType string `json:"mediaType"`
Digest string `json:"digest"`
Size int64 `json:"size"`
}
type descriptor v1.Descriptor

func (d *descriptor) algo() string {
pts := strings.SplitN(d.Digest, ":", 2)
pts := strings.SplitN(string(d.Digest), ":", 2)
if len(pts) != 2 {
return ""
}
return pts[0]
}

func (d *descriptor) hash() string {
pts := strings.SplitN(d.Digest, ":", 2)
pts := strings.SplitN(string(d.Digest), ":", 2)
if len(pts) != 2 {
return ""
}
Expand All @@ -50,17 +47,22 @@ func (d *descriptor) hash() string {

func listReferences(w walker) (map[string]*descriptor, error) {
refs := make(map[string]*descriptor)
var index v1.ImageIndex

if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
if info.IsDir() || !strings.HasPrefix(path, "refs") {
if info.IsDir() || filepath.Clean(path) != "index.json" {
return nil
}

var d descriptor
if err := json.NewDecoder(r).Decode(&d); err != nil {
if err := json.NewDecoder(r).Decode(&index); err != nil {
return err
}
refs[info.Name()] = &d

for i := 0; i < len(index.Manifests); i++ {
if index.Manifests[i].Descriptor.Annotations["org.opencontainers.ref.name"] != "" {
refs[index.Manifests[i].Descriptor.Annotations["org.opencontainers.ref.name"]] = (*descriptor)(&index.Manifests[i].Descriptor)
}
}

return nil
}); err != nil {
Expand All @@ -71,21 +73,28 @@ func listReferences(w walker) (map[string]*descriptor, error) {

func findDescriptor(w walker, name string) (*descriptor, error) {
var d descriptor
dpath := filepath.Join("refs", name)
var index v1.ImageIndex

switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
if info.IsDir() || filepath.Clean(path) != dpath {
if info.IsDir() || filepath.Clean(path) != "index.json" {
return nil
}

if err := json.NewDecoder(r).Decode(&d); err != nil {
if err := json.NewDecoder(r).Decode(&index); err != nil {
return err
}

return errEOW
for i := 0; i < len(index.Manifests); i++ {
if index.Manifests[i].Descriptor.Annotations["org.opencontainers.ref.name"] == name {
d = (descriptor)(index.Manifests[i].Descriptor)
return errEOW
}
}

return nil
}); err {
case nil:
return nil, fmt.Errorf("%s: descriptor not found", dpath)
return nil, fmt.Errorf("index.json: descriptor not found")
case errEOW:
return &d, nil
default:
Expand All @@ -105,7 +114,7 @@ func (d *descriptor) validate(w walker, mts []string) error {
return fmt.Errorf("invalid descriptor MediaType %q", d.MediaType)
}

parsed, err := digest.Parse(d.Digest)
parsed, err := digest.Parse(string(d.Digest))
if err != nil {
return err
}
Expand Down
70 changes: 50 additions & 20 deletions image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import (
)

const (
refTag = "latest"

refTag = "latest"
layoutStr = `{"imageLayoutVersion": "1.0.0"}`

configStr = `{
Expand Down Expand Up @@ -91,8 +90,44 @@ const (
)

var (
refStr = `{"digest":"<manifest_digest>","mediaType":"application/vnd.oci.image.manifest.v1+json","size":<manifest_size>}`

indexStr = `{
"schemaVersion": 2,
"manifests": [
{
"mediaType": "application/vnd.oci.image.index.v1+json",
"size": <manifest_size>,
"digest": "<manifest_digest>",
"annotations": {
"org.opencontainers.ref.name": "v1.0"
}
},
{
"mediaType": "application/vnd.oci.image.manifest.v1+json",
"size": <manifest_size>,
"digest": "<manifest_digest>",
"platform": {
"architecture": "ppc64le",
"os": "linux"
},
"annotations": {
"org.opencontainers.ref.name": "latest"
}
},
{
"mediaType": "application/xml",
"size": <manifest_size>,
"digest": "<manifest_digest>",
"annotations": {
"org.freedesktop.specifications.metainfo.version": "1.0",
"org.freedesktop.specifications.metainfo.type": "AppStream"
}
}
],
"annotations": {
"com.example.index.revision": "r124356"
}
}
`
manifestStr = `{
"annotations": null,
"config": {
Expand Down Expand Up @@ -162,11 +197,6 @@ func createImageLayoutBundle(il imageLayout) error {
return err
}

err = os.MkdirAll(filepath.Join(il.rootDir, "refs"), 0700)
if err != nil {
return err
}

// create image layout file
err = createLayoutFile(il.rootDir)
if err != nil {
Expand All @@ -178,14 +208,14 @@ func createImageLayoutBundle(il imageLayout) error {
if err != nil {
return err
}
il.manifest = strings.Replace(il.manifest, "<layer_digest>", desc.Digest, 1)
il.manifest = strings.Replace(il.manifest, "<layer_digest>", string(desc.Digest), 1)
il.manifest = strings.Replace(il.manifest, "<layer_size>", strconv.FormatInt(desc.Size, 10), 1)

desc, err = createConfigFile(il.rootDir, il.config)
if err != nil {
return err
}
il.manifest = strings.Replace(il.manifest, "<config_digest>", desc.Digest, 1)
il.manifest = strings.Replace(il.manifest, "<config_digest>", string(desc.Digest), 1)
il.manifest = strings.Replace(il.manifest, "<config_size>", strconv.FormatInt(desc.Size, 10), 1)

// create manifest blob file
Expand All @@ -194,7 +224,7 @@ func createImageLayoutBundle(il imageLayout) error {
return err
}

return createRefFile(il.rootDir, il.ref, desc)
return createIndexFile(il.rootDir, desc)
}

func createLayoutFile(root string) error {
Expand All @@ -208,16 +238,16 @@ func createLayoutFile(root string) error {
return err
}

func createRefFile(root, ref string, mft descriptor) error {
refpath := filepath.Join(root, "refs", ref)
f, err := os.Create(refpath)
func createIndexFile(root string, mft descriptor) error {
indexpath := filepath.Join(root, "index.json")
f, err := os.Create(indexpath)
if err != nil {
return err
}
defer f.Close()
refStr = strings.Replace(refStr, "<manifest_digest>", mft.Digest, -1)
refStr = strings.Replace(refStr, "<manifest_size>", strconv.FormatInt(mft.Size, 10), -1)
_, err = io.Copy(f, bytes.NewBuffer([]byte(refStr)))
indexStr = strings.Replace(indexStr, "<manifest_digest>", string(mft.Digest), -1)
indexStr = strings.Replace(indexStr, "<manifest_size>", strconv.FormatInt(mft.Size, 10), -1)
_, err = io.Copy(f, bytes.NewBuffer([]byte(indexStr)))
return err
}

Expand Down Expand Up @@ -297,7 +327,7 @@ func createHashedBlob(name string) (descriptor, error) {
return descriptor{}, err
}

parsed, err := digest.Parse(desc.Digest)
parsed, err := digest.Parse(string(desc.Digest))
if err != nil {
return descriptor{}, err
}
Expand Down Expand Up @@ -325,7 +355,7 @@ func newDescriptor(name string) (descriptor, error) {
}

return descriptor{
Digest: digester.Digest().String(),
Digest: digester.Digest(),
Size: size,
}, nil
}
4 changes: 2 additions & 2 deletions image/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func testUnpackLayer(t *testing.T, compression string, invalid bool) {
testManifest := manifest{
Layers: []descriptor{descriptor{
MediaType: mediatype,
Digest: digester.Digest().String(),
Digest: digester.Digest(),
}},
}
err = testManifest.unpack(newPathWalker(tmp1), filepath.Join(tmp1, "rootfs"))
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestUnpackLayerRemovePartialyUnpackedFile(t *testing.T) {
testManifest := manifest{
Layers: []descriptor{descriptor{
MediaType: "application/vnd.oci.image.layer.v1.tar+gzip",
Digest: digester.Digest().String(),
Digest: digester.Digest(),
}},
}
err = testManifest.unpack(newPathWalker(tmp1), filepath.Join(tmp1, "rootfs"))
Expand Down

0 comments on commit 6ff2eb1

Please sign in to comment.