Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this type definition? Just use the type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the type def please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated,I used to define this type because of this function,

func (d *descriptor) validate(w walker, mts []string) error {

and now I changed it to

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

PTAL


func (d *descriptor) algo() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lost this code: this just go-digest to do this properly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to parse a digest that is already a digest. Just call d.Digest.Validate().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, PTAL, thanks.

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

desc.Digest.Validate()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, PTAL.
ping @opencontainers/image-tools-maintainers

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