Skip to content

Commit

Permalink
Modify the ref option
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhao <zhouhao@cn.fujitsu.com>
  • Loading branch information
zhouhao committed Aug 1, 2017
1 parent 5854592 commit 3cdbaa7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 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 @@ -89,8 +89,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: "org.opencontainers.image.ref.name==v1.0",
Usage: "Specify the search criteria, format is A==B. Only support `org.opencontainers.image.ref.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 @@ -85,8 +85,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: "org.opencontainers.image.ref.name==v1.0",
Usage: "Specify the search criteria, format is A==B. Only support `org.opencontainers.image.ref.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 v1.AnnotationRefName:
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 not found")
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
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 org.opencontainers.image.ref.name==v1.0
Only support `org.opencontainers.image.ref.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. --ref org.opencontainers.image.ref.name==v1.0
Only support `org.opencontainers.image.ref.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 3cdbaa7

Please sign in to comment.