Skip to content

Commit 64a1893

Browse files
author
zhouhao
committed
Modify the ref option
Signed-off-by: zhouhao <[email protected]>
1 parent 2b3754c commit 64a1893

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

cmd/oci-image-tool/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ var createCommand = cli.Command{
8989
},
9090
cli.StringFlag{
9191
Name: "ref",
92-
Value: "v1.0",
93-
Usage: "The ref pointing to the manifest of the OCI image. This must be present in the 'refs' subdirectory of the image.",
92+
Value: "org.opencontainers.ref.name==v1.0",
93+
Usage: "Specify the search criteria, format is A==B. Only support `org.opencontainers.ref.name`, `platform.os` and `digest` three cases.",
9494
},
9595
cli.StringFlag{
9696
Name: "rootfs",

cmd/oci-image-tool/unpack.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ var unpackCommand = cli.Command{
8585
},
8686
cli.StringFlag{
8787
Name: "ref",
88-
Value: "v1.0",
89-
Usage: "The ref pointing to the manifest of the OCI image. This must be present in the 'refs' subdirectory of the image.",
88+
Value: "org.opencontainers.ref.name==v1.0",
89+
Usage: "Specify the search criteria, format is A==B. Only support `org.opencontainers.ref.name`, `platform.os` and `digest` three cases.",
9090
},
9191
cli.StringFlag{
9292
Name: "platform",

image/descriptor.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io"
2121
"os"
2222
"path/filepath"
23+
"strings"
2324

2425
"github.com/opencontainers/image-spec/specs-go/v1"
2526
"github.com/pkg/errors"
@@ -44,20 +45,32 @@ func listReferences(w walker) (map[string]*v1.Descriptor, error) {
4445
if index.Manifests[i].Annotations["org.opencontainers.ref.name"] != "" {
4546
refs[index.Manifests[i].Annotations["org.opencontainers.ref.name"]] = &index.Manifests[i]
4647
}
48+
49+
if index.Manifests[i].Platform != nil && index.Manifests[i].Platform.OS != "" {
50+
refs[index.Manifests[i].Platform.OS] = &index.Manifests[i]
51+
}
52+
53+
refs[string(index.Manifests[i].Digest)] = &index.Manifests[i]
4754
}
4855

4956
return nil
5057
}); err != nil {
5158
return nil, err
5259
}
60+
5361
return refs, nil
5462
}
5563

5664
func findDescriptor(w walker, name string) (*v1.Descriptor, error) {
57-
var d v1.Descriptor
65+
var refs []v1.Descriptor
5866
var index v1.Index
5967

60-
switch err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
68+
argsParts := strings.Split(name, "==")
69+
if len(argsParts) != 2 {
70+
return nil, fmt.Errorf("ref must contain two parts")
71+
}
72+
73+
if err := w.walk(func(path string, info os.FileInfo, r io.Reader) error {
6174
if info.IsDir() || filepath.Clean(path) != indexPath {
6275
return nil
6376
}
@@ -66,22 +79,41 @@ func findDescriptor(w walker, name string) (*v1.Descriptor, error) {
6679
return err
6780
}
6881

69-
for i := 0; i < len(index.Manifests); i++ {
70-
if index.Manifests[i].Annotations["org.opencontainers.ref.name"] == name {
71-
d = index.Manifests[i]
72-
return errEOW
82+
switch argsParts[0] {
83+
case "org.opencontainers.ref.name":
84+
for i := 0; i < len(index.Manifests); i++ {
85+
if index.Manifests[i].Annotations["org.opencontainers.ref.name"] == argsParts[1] {
86+
refs = append(refs, index.Manifests[i])
87+
}
88+
}
89+
case "platform.os":
90+
for i := 0; i < len(index.Manifests); i++ {
91+
if index.Manifests[i].Platform != nil && index.Manifests[i].Platform.OS == argsParts[1] {
92+
refs = append(refs, index.Manifests[i])
93+
}
7394
}
95+
case "digest":
96+
for i := 0; i < len(index.Manifests); i++ {
97+
if string(index.Manifests[i].Digest) == argsParts[1] {
98+
refs = append(refs, index.Manifests[i])
99+
}
100+
}
101+
default:
102+
return fmt.Errorf("criteria %q unimplemented", argsParts[0])
74103
}
75104

76105
return nil
77-
}); err {
78-
case nil:
79-
return nil, fmt.Errorf("index.json: descriptor not found")
80-
case errEOW:
81-
return &d, nil
82-
default:
106+
}); err != nil {
83107
return nil, err
84108
}
109+
110+
if len(refs) == 0 {
111+
return nil, fmt.Errorf("index.json: descriptor retrieved by %q is not unique", name)
112+
} else if len(refs) > 1 {
113+
return nil, fmt.Errorf("index.json: descriptor retrieved by %q is not unique", name)
114+
}
115+
116+
return &refs[0], nil
85117
}
86118

87119
func validateDescriptor(d *v1.Descriptor, w walker, mts []string) error {

man/oci-image-tool-create.1.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ runtime-spec-compatible `dest/config.json`.
1818
Print usage statement
1919

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

2325
**--rootfs**=""
2426
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")

man/oci-image-tool-unpack.1.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ oci-image-tool unpack \- Unpack an image or image source layout
1515
Print usage statement
1616

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

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

0 commit comments

Comments
 (0)