Skip to content

Commit 74f5081

Browse files
author
zhouhao
committed
Modify the ref option
Signed-off-by: zhouhao <[email protected]>
1 parent da84dc9 commit 74f5081

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

cmd/oci-image-tool/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ var createCommand = 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.image.ref.name==v1.0",
89+
Usage: "Specify the search criteria, format is A==B. Only support `org.opencontainers.image.ref.name`, `platform.os` and `digest` three cases.",
9090
},
9191
cli.StringFlag{
9292
Name: "rootfs",

cmd/oci-image-tool/unpack.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ var unpackCommand = cli.Command{
8282
},
8383
cli.StringFlag{
8484
Name: "ref",
85-
Value: "v1.0",
86-
Usage: "The ref pointing to the manifest of the OCI image. This must be present in the 'refs' subdirectory of the image.",
85+
Value: "org.opencontainers.image.ref.name==v1.0",
86+
Usage: "Specify the search criteria, format is A==B. Only support `org.opencontainers.image.ref.name`, `platform.os` and `digest` three cases.",
8787
},
8888
cli.StringFlag{
8989
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[v1.AnnotationRefName] != "" {
4546
refs[index.Manifests[i].Annotations[v1.AnnotationRefName]] = &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[v1.AnnotationRefName] == name {
71-
d = index.Manifests[i]
72-
return errEOW
82+
switch argsParts[0] {
83+
case v1.AnnotationRefName:
84+
for i := 0; i < len(index.Manifests); i++ {
85+
if index.Manifests[i].Annotations[v1.AnnotationRefName] == 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 %q not found", name)
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 match", 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 {

image/image.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ func unpack(w walker, dest, refName string, platform string) error {
174174
return err
175175
}
176176

177-
if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
178-
return err
177+
if !strings.HasPrefix(refName, "digest==") {
178+
if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
179+
return err
180+
}
179181
}
180182

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

253-
if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
254-
return err
255+
if !strings.HasPrefix(refName, "digest==") {
256+
if err = validateDescriptor(ref, w, validRefMediaTypes); err != nil {
257+
return err
258+
}
255259
}
256260

257261
if ref.MediaType == validRefMediaTypes[0] {

image/image_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ const (
8989

9090
var (
9191
refTag = []string{
92+
"org.opencontainers.image.ref.name==latest",
93+
"org.opencontainers.image.ref.name==v1.0",
94+
}
95+
96+
refs = []string{
9297
"latest",
9398
"v1.0",
99+
"linux",
94100
}
95101

96102
indexJSON = `{
@@ -237,7 +243,7 @@ func TestImageLayout(t *testing.T) {
237243
t.Fatal(err)
238244
}
239245

240-
err = ValidateLayout(root, refTag, nil)
246+
err = ValidateLayout(root, refs, nil)
241247
if err != nil {
242248
t.Fatal(err)
243249
}

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.image.ref.name==v1.0
23+
Only support `org.opencontainers.image.ref.name`, `platform.os` and `digest` three cases.(default org.opencontainers.image.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.image.ref.name==v1.0
20+
Only support `org.opencontainers.image.ref.name`, `platform.os` and `digest` three cases.(default org.opencontainers.image.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)