Skip to content

Commit

Permalink
Modify the ref optiont
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
  • Loading branch information
zhouhao committed Sep 18, 2017
1 parent da84dc9 commit f758639
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
4 changes: 2 additions & 2 deletions cmd/oci-image-tool/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ var createCommand = cli.Command{
},
cli.StringFlag{
Name: "ref",
Value: "v1.0",
Usage: "The ref pointing to the manifest of the OCI image. This must be present in the 'refs' subdirectory of the image.",
Value: "name=v1.0",
Usage: "Specify the search criteria, format is A=B. Only support 'name', 'platform.os' and 'digest' three cases.",
},
cli.StringFlag{
Name: "rootfs",
Expand Down
4 changes: 2 additions & 2 deletions cmd/oci-image-tool/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ var unpackCommand = cli.Command{
},
cli.StringFlag{
Name: "ref",
Value: "v1.0",
Usage: "The ref pointing to the manifest of the OCI image. This must be present in the 'refs' subdirectory of the image.",
Value: "name=v1.0",
Usage: "Specify the search criteria, format is A=B. Only support 'name', 'platform.os' and 'digest' three cases.",
},
cli.StringFlag{
Name: "platform",
Expand Down
56 changes: 44 additions & 12 deletions image/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand All @@ -44,20 +45,32 @@ func listReferences(w walker) (map[string]*v1.Descriptor, error) {
if index.Manifests[i].Annotations[v1.AnnotationRefName] != "" {
refs[index.Manifests[i].Annotations[v1.AnnotationRefName]] = &index.Manifests[i]
}

if index.Manifests[i].Platform != nil && index.Manifests[i].Platform.OS != "" {
refs[index.Manifests[i].Platform.OS] = &index.Manifests[i]
}

refs[string(index.Manifests[i].Digest)] = &index.Manifests[i]
}

return nil
}); err != nil {
return nil, err
}

return refs, nil
}

func findDescriptor(w walker, name string) (*v1.Descriptor, error) {
var d v1.Descriptor
var refs []v1.Descriptor
var index v1.Index

switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
argsParts := strings.Split(name, "=")
if len(argsParts) != 2 {
return nil, fmt.Errorf("ref must contain two parts")
}

if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
if info.IsDir() || filepath.Clean(path) != indexPath {
return nil
}
Expand All @@ -66,22 +79,41 @@ func findDescriptor(w walker, name string) (*v1.Descriptor, error) {
return err
}

for i := 0; i < len(index.Manifests); i++ {
if index.Manifests[i].Annotations[v1.AnnotationRefName] == name {
d = index.Manifests[i]
return errEOW
switch argsParts[0] {
case "name":
for i := 0; i < len(index.Manifests); i++ {
if index.Manifests[i].Annotations[v1.AnnotationRefName] == argsParts[1] {
refs = append(refs, index.Manifests[i])
}
}
case "platform.os":
for i := 0; i < len(index.Manifests); i++ {
if index.Manifests[i].Platform != nil && index.Manifests[i].Platform.OS == argsParts[1] {
refs = append(refs, index.Manifests[i])
}
}
case "digest":
for i := 0; i < len(index.Manifests); i++ {
if string(index.Manifests[i].Digest) == argsParts[1] {
refs = append(refs, index.Manifests[i])
}
}
default:
return fmt.Errorf("criteria %q unimplemented", argsParts[0])
}

return nil
}); err {
case nil:
return nil, fmt.Errorf("index.json: descriptor %q not found", name)
case errEOW:
return &d, nil
default:
}); err != nil {
return nil, err
}

if len(refs) == 0 {
return nil, fmt.Errorf("index.json: descriptor retrieved by %q is not match", name)
} else if len(refs) > 1 {
return nil, fmt.Errorf("index.json: descriptor retrieved by %q is not unique", name)
}

return &refs[0], nil
}

func validateDescriptor(d *v1.Descriptor, w walker, mts []string) error {
Expand Down
12 changes: 8 additions & 4 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ func unpack(w walker, dest, refName string, platform string) error {
return err
}

if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
return err
if !strings.HasPrefix(refName, "digest==") {
if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
return err
}
}

if ref.MediaType == validRefMediaTypes[0] {
Expand Down Expand Up @@ -250,8 +252,10 @@ func createRuntimeBundle(w walker, dest, refName, rootfs string, platform string
return err
}

if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
return err
if !strings.HasPrefix(refName, "digest==") {
if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
return err
}
}

if ref.MediaType == validRefMediaTypes[0] {
Expand Down
8 changes: 7 additions & 1 deletion image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ const (

var (
refTag = []string{
"name=latest",
"name=v1.0",
}

refs = []string{
"latest",
"v1.0",
"linux",
}

indexJSON = `{
Expand Down Expand Up @@ -237,7 +243,7 @@ func TestImageLayout(t *testing.T) {
t.Fatal(err)
}

err = ValidateLayout(root, refTag, nil)
err = ValidateLayout(root, refs, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion man/oci-image-tool-create.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ runtime-spec-compatible `dest/config.json`.
Print usage statement

**--ref**=""
The ref pointing to the manifest of the OCI image. This must be present in the "refs" subdirectory of the image. (default "v1.0")
Specify the search criteria, format is A=B.
e.g. --ref name=v1.0
Only support `name`, `platform.os` and `digest` three cases.(default org.opencontainers.image.ref.name=v1.0)

**--rootfs**=""
A directory representing the root filesystem of the container in the OCI runtime bundle. It is strongly recommended to keep the default value. (default "rootfs")
Expand Down
4 changes: 3 additions & 1 deletion man/oci-image-tool-unpack.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ oci-image-tool unpack \- Unpack an image or image source layout
Print usage statement

**--ref**=""
The ref pointing to the manifest to be unpacked. This must be present in the "refs" subdirectory of the image. (default "v1.0")
Specify the search criteria, format is A=B.
e.g. --name=v1.0
Only support `name`, `platform.os` and `digest` three cases.(default org.opencontainers.image.ref.name=v1.0)

**--type**=""
Type of the file to unpack. If unset, oci-image-tool will try to auto-detect the type. One of "imageLayout,image"
Expand Down

0 comments on commit f758639

Please sign in to comment.