Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add v1.Manifest 'artifactType' field from OCI spec #1931

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/v1/manifest.go
Expand Up @@ -25,6 +25,7 @@ import (
type Manifest struct {
SchemaVersion int64 `json:"schemaVersion"`
MediaType types.MediaType `json:"mediaType,omitempty"`
ArtifactType string `json:"artifactType,omitempty"`
Config Descriptor `json:"config"`
Layers []Descriptor `json:"layers"`
Annotations map[string]string `json:"annotations,omitempty"`
Expand Down
16 changes: 16 additions & 0 deletions pkg/v1/mutate/image.go
Expand Up @@ -35,6 +35,7 @@ type image struct {
manifest *v1.Manifest
annotations map[string]string
mediaType *types.MediaType
artifactType *string
configMediaType *types.MediaType
diffIDMap map[v1.Hash]v1.Layer
digestMap map[v1.Hash]v1.Layer
Expand All @@ -52,6 +53,13 @@ func (i *image) MediaType() (types.MediaType, error) {
return i.base.MediaType()
}

func (i *image) ArtifactType() (string, error) {
if i.artifactType != nil {
return *i.artifactType, nil
}
return partial.ArtifactType(i.base)
}

func (i *image) compute() error {
i.Lock()
defer i.Unlock()
Expand Down Expand Up @@ -117,6 +125,10 @@ func (i *image) compute() error {
desc.MediaType = add.MediaType
}

if add.ArtifactType != "" {
desc.ArtifactType = add.ArtifactType
}

manifestLayers = append(manifestLayers, *desc)
digestMap[desc.Digest] = add.Layer
}
Expand Down Expand Up @@ -151,6 +163,10 @@ func (i *image) compute() error {
manifest.MediaType = *i.mediaType
}

if i.artifactType != nil {
manifest.ArtifactType = *i.artifactType
}

if i.annotations != nil {
if manifest.Annotations == nil {
manifest.Annotations = map[string]string{}
Expand Down
19 changes: 14 additions & 5 deletions pkg/v1/mutate/mutate.go
Expand Up @@ -39,11 +39,12 @@ const whiteoutPrefix = ".wh."
// Addendum contains layers and history to be appended
// to a base image
type Addendum struct {
Layer v1.Layer
History v1.History
URLs []string
Annotations map[string]string
MediaType types.MediaType
Layer v1.Layer
History v1.History
URLs []string
Annotations map[string]string
MediaType types.MediaType
ArtifactType string
}

// AppendLayers applies layers to a base image.
Expand Down Expand Up @@ -536,6 +537,14 @@ func MediaType(img v1.Image, mt types.MediaType) v1.Image {
}
}

// ArtifactType modifies the ArtifactType() of the given image.
func ArtifactType(img v1.Image, at string) v1.Image {
return &image{
base: img,
artifactType: &at,
}
}

// ConfigMediaType modifies the MediaType() of the given image's Config.
//
// If !mt.IsConfig(), this will be the image's artifactType in any indexes it's a part of.
Expand Down
6 changes: 5 additions & 1 deletion pkg/v1/mutate/mutate_test.go
Expand Up @@ -148,7 +148,8 @@ func TestAppendWithAddendum(t *testing.T) {
Annotations: map[string]string{
"foo": "bar",
},
MediaType: types.MediaType("foo"),
MediaType: types.MediaType("foo"),
ArtifactType: "bar",
}

result, err := mutate.Append(source, addendum)
Expand Down Expand Up @@ -187,6 +188,9 @@ func TestAppendWithAddendum(t *testing.T) {
if diff := cmp.Diff(m.Layers[1].MediaType, addendum.MediaType); diff != "" {
t.Fatalf("the appended MediaType is not the same (-got, +want) %s", diff)
}
if diff := cmp.Diff(m.Layers[1].ArtifactType, addendum.ArtifactType); diff != "" {
t.Fatalf("the appended ArtifactType is not the same (-got, +want) %s", diff)
}
}

func TestAppendLayers(t *testing.T) {
Expand Down
17 changes: 13 additions & 4 deletions pkg/v1/partial/with.go
Expand Up @@ -337,8 +337,12 @@ func Descriptor(d Describable) (*v1.Descriptor, error) {
mf, _ := Manifest(wrm)
// Failing to parse as a manifest should just be ignored.
// The manifest might not be valid, and that's okay.
if mf != nil && !mf.Config.MediaType.IsConfig() {
desc.ArtifactType = string(mf.Config.MediaType)
if mf != nil {
if mf.ArtifactType != "" {
desc.ArtifactType = mf.ArtifactType
} else if !mf.Config.MediaType.IsConfig() {
desc.ArtifactType = string(mf.Config.MediaType)
}
}
}
}
Expand Down Expand Up @@ -429,8 +433,13 @@ func ArtifactType(w WithManifest) (string, error) {
mf, _ := w.Manifest()
// Failing to parse as a manifest should just be ignored.
// The manifest might not be valid, and that's okay.
if mf != nil && !mf.Config.MediaType.IsConfig() {
return string(mf.Config.MediaType), nil
if mf != nil {
if mf.ArtifactType != "" {
return mf.ArtifactType, nil
}
if !mf.Config.MediaType.IsConfig() {
return string(mf.Config.MediaType), nil
}
}
return "", nil
}